#include "rendercontext.h" using namespace Rotor; using Poco::Net::HTTPResponse; using Poco::Logger; void Render_context::runTask() { while (!isCancelled()) { Session_task cmd; mutex.lock(); if (work_queue.size()){ cmd=work_queue[0]; work_queue.pop_front(); graph.cancelled=false; } mutex.unlock(); if(cmd.task==ANALYSE_AUDIO) { state=ANALYSING_AUDIO; vector processors; processors.push_back(audio_thumb); for (auto a: graph.nodes) { if (dynamic_cast(a.second)){ processors.push_back(dynamic_cast(a.second)); } } if (graph.load_audio(graph.audio_filename,processors)) { graph.audio_loaded=true; state=IDLE; } else { //an error occurred: TODO have to clean up allocated data. autoptr? graph.audio_loaded=false; state=IDLE; } } if(cmd.task==RENDER) { state=RENDERING; renders[cmd.uid]=Render_status(RENDERING); if(graph.video_render(output_filename,output_framerate)){ state=IDLE; if (graph.cancelled) renders[cmd.uid].status=CANCELLED; else renders[cmd.uid].status=RENDER_READY; } else { //an error occurred: TODO have to clean up allocated data. autoptr? cerr<<"Rotor: render failed"<& command void Render_context::session_command(const Session_command& command,xmlIO& XML,HTTPResponse::HTTPStatus& status){ string s; for (auto c:command.commands){ s+=c; s+=" "; } //cerr<<"uid:"<1){ int w=ofToInt(t1[0]); int h=ofToInt(t1[1]); if (graph.set_resolution(w,h)){ logger.information("resolution set to "+t1[0]+"x"+t1[1]); XML.addValue("status","resolution set to "+t1[0]+"x"+t1[1]); status=HTTPResponse::HTTP_OK; } else { logger.error("ERROR: invalid resolution request: "+t1[0]+"x"+t1[1]); XML.addValue("error","invalid resolution request: "+t1[0]+"x"+t1[1]); } } } else { XML.addValue("error","session busy"); } } } } if (command.commands[1]=="audio") { if (command.method=="PUT") { //get audio file location and initiate analysis if (command.body!="") { if (state==IDLE) { graph.audio_filename=media_dir+command.body; //for now, store session variables in memory //check file exists Poco::File f=Poco::File(graph.audio_filename); if (f.exists()) { //pass to worker thread ??if engine is ready?? ??what if engine has finished but results aren't read?? add_queue(Session_task(command.uid,ANALYSE_AUDIO)); status=HTTPResponse::HTTP_OK; logger.information("Audio analysis: "+command.body); XML.addValue("status","Starting audio analysis: "+command.body); } else { status=HTTPResponse::HTTP_NOT_FOUND; logger.error("ERROR: audio file "+command.body+" not found"); XML.addValue("error",command.body+" not found"); } } else { logger.error("ERROR: Session busy"); XML.addValue("error","Session busy"); } } } if (command.method=="GET") { if (state==ANALYSING_AUDIO) { status=HTTPResponse::HTTP_OK; XML.addValue("status","Analysing audio"); char c[20]; sprintf(c,"%02f",graph.progress); XML.addValue("progress",string(c)); } else if (graph.audio_loaded) { //not sure about this-- should this state be retained? //can the data only be read once? //for now status=HTTPResponse::HTTP_OK; XML.addValue("status","Audio ready"); audio_thumb->print_vector(XML); //XML.addValue("audio",audio_thumb->print()); } else { logger.error("ERROR: audio thumbnail requested but no audio loaded"); XML.addValue("error","No audio loaded"); } } if (command.method=="DELETE") { if (state==IDLE) { graph.audio_filename=""; graph.audio_loaded=false; logger.information("Audio deleted"); XML.addValue("status","Audio deleted"); status=HTTPResponse::HTTP_OK; } else { logger.error("ERROR: Session busy"); XML.addValue("error","Session busy"); } } } if (command.commands[1]=="graph") { if (command.method=="GET") { if (graph.loaded) { status=HTTPResponse::HTTP_OK; //XML.addValue("patchbay",graph.toString()); logger.information("Requested graph"); XML.loadFromBuffer(graph.toString()); } else { logger.error("ERROR: graph not loaded: check XML"); XML.addValue("error","graph not loaded: check XML"); } } if (command.method=="PUT") { //get new graph from file if (command.body!="") { //should interrupt whatever is happening? //before begining to load from xml if (state==IDLE) { //eventually not like this if (graph.load(command.body,media_dir)) { status=HTTPResponse::HTTP_OK; logger.information("Loaded graph from http PUT body"); XML.addValue("status","Loaded graph from PUT body"); if (graph.audio_loaded) { add_queue(Session_task(command.uid,ANALYSE_AUDIO)); status=HTTPResponse::HTTP_OK; logger.information("Starting audio analysis for graph: "+command.commands[0]); XML.addValue("status","Starting audio analysis for graph: "+command.commands[0]); } } else { string graph_filename=graph_dir+command.body; Poco::File f=Poco::File(graph_filename); if (f.exists()) { if (graph.loadFile(graph_filename,media_dir)) { status=HTTPResponse::HTTP_OK; //XML.addValue("patchbay",graph.toString()); //XML.loadFromBuffer(graph.toString()); XML=graph.xml; //the graph could actually contain an xml object and we could just print it here? //or could our nodes even be subclassed from xml nodes? //the graph or the audio could load first- have to analyse the audio with vamp after the graph is loaded //for now the graph must load 1st if (graph.audio_loaded) { add_queue(Session_task(command.uid,ANALYSE_AUDIO)); status=HTTPResponse::HTTP_OK; logger.information("Starting audio analysis for graph: "+command.commands[0]); XML.addValue("status","Starting audio analysis for graph: "+command.commands[0]); } } else { status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR; //~/sources/poco-1.4.6-all/Net/include/Poco/Net/HTTPResponse.h logger.error("ERROR: graph not loaded: check XML"); XML.addValue("error","graph not loaded: check XML"); } } else { status=HTTPResponse::HTTP_NOT_FOUND; logger.error("ERROR: "+command.body+" not found"); XML.addValue("error",command.body+" not found"); } } } else { logger.error("ERROR: Session busy"); XML.addValue("error","Session busy"); } } } if (command.method=="DELETE") { //for now graph=Graph(); logger.information("graph deleted"); XML.addValue("status","graph deleted"); status=HTTPResponse::HTTP_OK; } } if (command.commands[1]=="signal") { if (command.method=="GET") { //generate xml from 1st signal output if (state==IDLE) { //direct call for testing float framerate=25.0f; //if (command.size()>2) { // framerate=ofToFloat(command.id); //} string signal_xml; if (false) { //graph.signal_render(signal_xml,framerate)){ status=HTTPResponse::HTTP_OK; logger.information("rendering signal to xml"); XML.addValue("signal",signal_xml); //this doesn't work >> pseudo xml } else { status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR; logger.error("ERROR: could not render output signal"); XML.addValue("error","could not render output signal"); } //else { // status=HTTPResponse::HTTP_NOT_FOUND; // XML.addValue("error","Signal output not found in graph"); //} } else { status=HTTPResponse::HTTP_SERVICE_UNAVAILABLE; logger.error("ERROR: context busy"); XML.addValue("error","Context busy"); } } } if (command.commands[1]=="video") { if (command.method=="PUT") { //get vide file location and initiate analysis if (command.body!="") { //there should be a filename + a destination node if (state!=RENDERING) { string video_filename=media_dir+command.body; //check file exists Poco::File f=Poco::File(video_filename); if (f.exists()) { if (graph.load_video(command.commands[2],video_filename)) { //pass to worker thread ??if engine is ready?? ??what if engine has finished but results aren't read?? //DUMMY RESPONSE status=HTTPResponse::HTTP_OK; logger.information("Succesfully loaded "+command.body+" into video node "+command.commands[2]); XML.addValue("status","Succesfully loaded "+command.body+" into video node "+command.commands[2]); } else { status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR; logger.error("ERROR: could not load "+command.body+" into video node "+command.commands[2]); XML.addValue("error","could not load "+command.body+" into video node "+command.commands[2]); } } else { status=HTTPResponse::HTTP_NOT_FOUND; logger.error("ERROR: "+command.body+" not found"); XML.addValue("error",command.body+" not found"); } } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Session busy"); XML.addValue("error","Session busy"); } } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Bad request"); XML.addValue("error","Bad request"); } } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Bad request"); XML.addValue("error","Bad request"); } } if (command.commands[1]=="preview") { if (command.method=="GET") { if(state!=RENDERING){ //parse json to get preview spec, return XML? this is a mess string preview_node=command.commands[2]; Json::Value root; // will contains the root value after parsing. Json::Reader reader; if (reader.parse( command.body, root )) { int frame=root["frame"].asInt(); int width=root["width"].asInt(); int height=root["height"].asInt(); string format=root["format"].asString(); if (graph.preview(XML,preview_node,format,frame,width,height)) { status=HTTPResponse::HTTP_OK; } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Could not create preview"); XML.addValue("error","Could not preview node '"+preview_node+"', frame "+ofToString(frame)+" at "+ofToString(width)+"x"+ofToString(height)); } } else { std::cout << "Failed to parse preview request\n" << reader.getFormattedErrorMessages(); status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Bad preview request"); XML.addValue("error","Bad preview request"); } } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Session busy"); XML.addValue("error","Session busy"); } } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Bad request"); XML.addValue("error","Bad request"); } } if (command.commands[1]=="features") { if (command.method=="GET") { if(state!=RENDERING){ //parse json to get preview spec, return XML? this is a mess string features_node=command.commands[2]; if (graph.print_features(XML,features_node)) { status=HTTPResponse::HTTP_OK; } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Could not print features for node "+features_node); } } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Session busy"); XML.addValue("error","Session busy"); } } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Bad request"); XML.addValue("error","Bad request"); } } if (command.commands[1]=="render") { if (command.method=="GET") { if(state==RENDERING){ status=HTTPResponse::HTTP_OK; XML.addValue("status","Rendering video"); XML.addValue("progress",ofToString(graph.progress)); } else { logger.error("ERROR: Render progress requested but not rendering"); XML.addValue("error","Not rendering"); } } if (command.method=="PUT") { if (command.body!="") { if (state==IDLE) { output_filename=output_dir+command.body; if (command.body!="") { // output_framerate=ofToFloat(command.body); } add_queue(Session_task(command.uid,RENDER)); status=HTTPResponse::HTTP_OK; logger.information("Starting render: "+command.body); XML.addValue("status","Starting render: "+command.body); XML.addValue("render_id",command.uid); } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Session busy"); XML.addValue("error","Session busy"); } } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: No output file specified"); XML.addValue("error","No output file specified"); } } if (command.method=="DELETE") { status=HTTPResponse::HTTP_OK; logger.error("ERROR: Not implemented"); XML.addValue("status","DUMMY RESPONSE: cancelling render"); } } if (command.commands[1]=="cancel") { if (command.method=="PUT") { if (state==RENDERING){ graph.cancelled=true; logger.information("Render cancelled."); XML.addValue("status","render cancelled"); } else if (state==ANALYSING_AUDIO){ graph.cancelled=true; logger.information("Audio analysis cancelled."); XML.addValue("status","audio analysis cancelled"); } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: no process to cancel"); XML.addValue("error","No process to cancel"); } } else { status=HTTPResponse::HTTP_BAD_REQUEST; logger.error("ERROR: Bad request"); XML.addValue("error","Bad request"); } } }