summaryrefslogtreecommitdiff
path: root/rotord/rendercontext.cpp
blob: e31a161e02e28fd5ffbe26a3e50e8b07924a5d0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include "rotor.h"


using namespace Rotor;
void Render_context::runTask() {
	while (!isCancelled()) {
		int cmd=0;
		mutex.lock();
		if (work_queue.size()){
			cmd=work_queue[0];
			work_queue.pop_front();
		}
		mutex.unlock();
		if(cmd==ANALYSE_AUDIO) {
			state=ANALYSING_AUDIO;
			vector<Base_audio_processor*> processors;
			processors.push_back(audio_thumb);
			vector<Node*> analysers=graph.find_nodes("audio_analysis");
			for (auto a: analysers) {
				processors.push_back(dynamic_cast<Base_audio_processor*>(a));
			}
			if (load_audio(audio_filename,processors)) {
				audio_loaded=true;
				state=IDLE;
			}
			else {
				//an error occurred: TODO have to clean up allocated data. autoptr?
				audio_loaded=false;
				state=IDLE;
			}
		}
		if(cmd==RENDER) {
			state=RENDERING;
			if(graph.video_render(output_filename,audio_filename,output_framerate,progress)){
				state=IDLE;
			}
			else {
				//an error occurred: TODO have to clean up allocated data. autoptr?
				state=IDLE;
			}
		}
		sleep(100);
	}
	printf("Rotor: stopping thread\n");
}
void Render_context::add_queue(int item) {
	mutex.lock();
	work_queue.push_back(item);
	mutex.unlock();
}
void Render_context::session_command(const std::vector<std::string>& command,xmlIO& XML,HTTPResponse::HTTPStatus& status){
	status=HTTPResponse::HTTP_BAD_REQUEST; //error by default
	if (command[2]=="resolution") {
		if (command[0]=="PUT") {				
			if (command.size()>2) {
				if (state==IDLE) {
					Poco::StringTokenizer t1(command[3],",");
					if (t1.count()>1){
						int w=ofToInt(t1[0]);
						int h=ofToInt(t1[1]);
						if (graph.set_resolution(w,h)){
							XML.addValue("status","resolution set to "+t1[0]+"x"+t1[1]);
							status=HTTPResponse::HTTP_OK;
						}
						else {
							XML.addValue("error","invalid resolution request: "+t1[0]+"x"+t1[1]);
						}
					}
				}
				else {
					XML.addValue("error","session busy");			
				}
			}
		}
	}
	if (command[2]=="audio") {
	    if (command[0]=="PUT") {          			//get audio file location and initiate analysis
			if (command.size()>2) {
			    if (state==IDLE) {
					audio_filename=media_dir+command[3]; 			//for now, store session variables in memory									//check file exists
					Poco::File f=Poco::File(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(ANALYSE_AUDIO);
					    status=HTTPResponse::HTTP_OK;
					    XML.addValue("status","Starting audio analysis: "+command[3]);
					}
					else {
					    status=HTTPResponse::HTTP_NOT_FOUND;
					    XML.addValue("error",command[3]+" not found");
					}

			    }
			    else {
					XML.addValue("error","session busy");
				}
			}
	    }
	     if (command[0]=="GET") {
		     if (state==ANALYSING_AUDIO) {
			     status=HTTPResponse::HTTP_OK;
			     XML.addValue("status","analysing audio");
			     char c[20];
			     sprintf(c,"%02f",progress);
			     XML.addValue("progress",string(c));
		     }
			else if (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");
				XML.addValue("audio",audio_thumb->print());
			}
			else {
				XML.addValue("error","no audio loaded");
			}
	     }
	     if (command[0]=="DELETE") {
		     //for now
			audio_filename="";
			XML.addValue("status","audio deleted");
			status=HTTPResponse::HTTP_OK;
	     }
	}
	if (command[2]=="graph") {
		if (command[0]=="GET") {
			if (graph.loaded) {
				status=HTTPResponse::HTTP_OK;
				//XML.addValue("patchbay",graph.toString());
				XML.loadFromBuffer(graph.toString());
			}
			else {
				XML.addValue("error","graph not loaded: check XML");
			}
		}
		if (command[0]=="PUT") {     				//get new graph from file
			if (command.size()>2) {
													//should interrupt whatever is happening?
													//before begining to load from xml
				if (state==IDLE) {					//eventually not like this
					string graph_filename=graph_dir+command[3];
					Poco::File f=Poco::File(graph_filename);
					if (f.exists()) {
						if (graph.load(graph_filename)) {
							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
						}
						else {
							status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR; //~/sources/poco-1.4.6-all/Net/include/Poco/Net/HTTPResponse.h
							XML.addValue("error","graph not loaded: check XML");
						}
					}
					else {
					    status=HTTPResponse::HTTP_NOT_FOUND;
					    XML.addValue("error",command[3]+" not found");
					}
				}
			}
		}
		if (command[0]=="DELETE") {
			 //for now
			graph=Graph();
			XML.addValue("status","graph deleted");
			status=HTTPResponse::HTTP_OK;
		}
	}
	if (command[2]=="signal") {
		if (command[0]=="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[3]);
					}
					string signal_xml;
					if (graph.signal_render(signal_xml,framerate)){
						status=HTTPResponse::HTTP_OK;
						XML.addValue("signal",signal_xml);
					}
					else {
						status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR;
						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;
				XML.addValue("error","Context busy");
			}
		}
	if (command[2]=="video") {
		if (command[0]=="PUT") {                                    //get vide file location and initiate analysis
			if (command.size()>4) { 								//there should be a filename + a destination node
			    if (state==IDLE) {
			    	string video_filename=media_dir+command[4];
																	//check file exists
					Poco::File f=Poco::File(video_filename);
					if (f.exists()) {
						if (load_video(command[3],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;
							XML.addValue("status","succesfully loaded "+command[4]+" into video node "+command[3]);
						}
						else {
							status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR;
							XML.addValue("error","could not load "+command[4]+" into video node "+command[3]);
						}
					}
					else {
					    status=HTTPResponse::HTTP_NOT_FOUND;
					    XML.addValue("error",command[4]+" not found");
					}
			    }
			    else {
					status=HTTPResponse::HTTP_BAD_REQUEST;
					XML.addValue("error","Session busy");
			    }
			}
			else {
				status=HTTPResponse::HTTP_BAD_REQUEST;
				XML.addValue("error","Bad request");
			}
		}
	}
	if (command[2]=="render") {
		if (command[0]=="GET") {
			status=HTTPResponse::HTTP_OK;
			XML.addValue("status","Rendering video");
			XML.addValue("progress",ofToString(progress));
		}
		if (command[0]=="PUT") {
			if (command.size()>2) {
			    if (state==IDLE) {
						output_filename=output_dir+command[3];
						if (command.size()>3) {
//							output_framerate=ofToFloat(command[4]);
						}
					    add_queue(RENDER);
					    status=HTTPResponse::HTTP_OK;
					    XML.addValue("status","Starting render: "+command[3]);
			    }
			    else {
					status=HTTPResponse::HTTP_BAD_REQUEST;
					XML.addValue("error","Session busy");
		    	}
			}
			else {
				status=HTTPResponse::HTTP_BAD_REQUEST;
				XML.addValue("error","No output file specified");
		    }
		}
		if (command[0]=="DELETE") {
			status=HTTPResponse::HTTP_OK;
			XML.addValue("status","DUMMY RESPONSE: cancelling render");
		}
	}
}
Command_response Render_context::session_command(const std::vector<std::string>& command){
													//method,id,command1,{command2,}{body}
													//here we allow the controlling server to communicate with running tasks
	string ems="";
	for (auto i:command) ems=ems+i+":";

	cerr<<"Rotor: session command with "<<command.size()<<" arguments- "<<ems<<endl;

	Command_response response;
	response.status=HTTPResponse::HTTP_BAD_REQUEST;
	if (command[2]=="resolution") {
			if (command[0]=="PUT") {				
				if (command.size()>2) {
					if (state==IDLE) {
						Poco::StringTokenizer t1(command[3],",");
						if (t1.count()>1){
							int w=ofToInt(t1[0]);
							int h=ofToInt(t1[1]);
							if (graph.set_resolution(w,h)){
								response.description="<status context='"+command[1]+"'>Rotor: resolution set to "+t1[0]+"x"+t1[1]+"</status>\n";
								response.status=HTTPResponse::HTTP_OK;
							}
							else {
								response.status=HTTPResponse::HTTP_BAD_REQUEST;
								response.description="<status context='"+command[1]+"'>Rotor: invalid resolution request: "+t1[0]+"x"+t1[1]+"</status>\n";
							}
						}
					}
					else {
					response.status=HTTPResponse::HTTP_BAD_REQUEST;
					response.description="<status context='"+command[1]+"'>Rotor: session busy</status>\n";
					}
				}
			}
		}
	if (command[2]=="audio") {
	    if (command[0]=="PUT") {          			//get audio file location and initiate analysis
			if (command.size()>2) {
			    if (state==IDLE) {
					audio_filename=media_dir+command[3]; 			//for now, store session variables in memory									//check file exists
					Poco::File f=Poco::File(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(ANALYSE_AUDIO);
					    response.status=HTTPResponse::HTTP_OK;
					    response.description="<status context='"+command[1]+"'>Starting audio analysis: "+command[3]+"</status>\n";
					}
					else {
					    response.status=HTTPResponse::HTTP_NOT_FOUND;
					    response.description="<status context='"+command[1]+"'>File "+command[3]+" not found</status>\n";
					}

			    }
			    else {
					response.status=HTTPResponse::HTTP_BAD_REQUEST;
					response.description="<status context='"+command[1]+"'>Rotor: session busy</status>\n";
			    }
			}
	    }
	     if (command[0]=="GET") {
		     if (state==ANALYSING_AUDIO) {
			     response.status=HTTPResponse::HTTP_OK;
			     response.description="<status context='"+command[1]+"'>Rotor: analysing audio</status>\n";
			     char c[20];
			     sprintf(c,"%02f",progress);
			     response.description+="<progress>"+string(c)+"</progress>\n";
		     }
			else if (audio_loaded) {
				//not sure about this-- should this state be retained?
				//can the data  only be read once?
				//for now
				response.status=HTTPResponse::HTTP_OK;
				response.description="<status context='"+command[1]+"'>Rotor: audio ready</status>\n";
				response.description+="<audio>\n";
				response.description+=audio_thumb->print();
				response.description+="</audio>";
			}
			else {
				response.status=HTTPResponse::HTTP_BAD_REQUEST;
				response.description="<status context='"+command[1]+"'>Rotor: no audio loaded</status>\n";
			}
	     }
	     if (command[0]=="DELETE") {
		     //for now
			audio_filename="";
			response.description="<status>1</status>\n";
			response.status=HTTPResponse::HTTP_OK;
	     }
	}
	if (command[2]=="graph") {
		if (command[0]=="GET") {
			if (graph.loaded) {
				response.status=HTTPResponse::HTTP_OK;
				response.description=graph.toString();
			}
			else {
				response.description="<status>Rotor: graph not loaded</status>\n";
			}
		}
		if (command[0]=="PUT") {     				//get new graph from file
			if (command.size()>2) {
													//should interrupt whatever is happening?
													//before begining to load from xml
				if (state==IDLE) {					//eventually not like this
					string graph_filename=graph_dir+command[3];
					Poco::File f=Poco::File(graph_filename);
					if (f.exists()) {
						if (graph.load(graph_filename)) {
							response.status=HTTPResponse::HTTP_OK;
							//response.description="<status context='"+command[1]+"'>Rotor: loaded graph "+command[3]+"</status>\n";
							response.description=graph.toString();
							//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
						}
						else {
							response.status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR; //~/sources/poco-1.4.6-all/Net/include/Poco/Net/HTTPResponse.h
							response.description="<status context='"+command[1]+"'>Rotor: could not load graph "+command[3]+"</status>\n";
						}
					}
					else {
					    response.status=HTTPResponse::HTTP_NOT_FOUND;
					    response.description="<status context='"+command[1]+"'>File "+command[3]+" not found</status>\n";
					}
				}
			}
		}
		if (command[0]=="DELETE") {
			 //for now
			graph=Graph();
			response.description="<status>1</status>\n";
			response.status=HTTPResponse::HTTP_OK;
		}
	}
	if (command[2]=="signal") {
		if (command[0]=="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[3]);
					}
					string signal_xml;
					if (graph.signal_render(signal_xml,framerate)){
						response.status=HTTPResponse::HTTP_OK;
						response.description=signal_xml;
					}
					else {
						response.status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR;
						response.description="<status context='"+command[1]+"'>Rotor: could not render output signal</status>\n";
					}
				}
				else {
					response.status=HTTPResponse::HTTP_NOT_FOUND;
					response.description="<status context='"+command[1]+"'>Signal output not found</status>\n";
				}
			}
			else {
				response.status=HTTPResponse::HTTP_SERVICE_UNAVAILABLE;
				response.description="<status context='"+command[1]+"'>Rotor: context busy</status>\n";
			}
		}
	if (command[2]=="video") {
		if (command[0]=="GET") {
													//DUMMY RESPONSE
			response.status=HTTPResponse::HTTP_OK;
			response.description="<status context='"+command[1]+"'>DUMMY RESPONSE Rotor: analysing video</status>\n";
			response.description+="<progress>45.2</progress>\n";
		}
		if (command[0]=="PUT") {                                    		//get vide file location and initiate analysis
			if (command.size()>4) { //there should be a filename + a destination node
			    if (state==IDLE) {
			    	string video_filename=media_dir+command[4];
													//check file exists
					Poco::File f=Poco::File(video_filename);
					if (f.exists()) {
						if (load_video(command[3],video_filename)) {
														//pass to worker thread ??if engine is ready?? ??what if engine has finished but results aren't read??
														//DUMMY RESPONSE
						response.description="<status context='"+command[1]+"'>Rotor: succesfully loaded "+command[4]+" into video node "+command[3]+"</status>\n";
						}
						else {
							response.status=HTTPResponse::HTTP_INTERNAL_SERVER_ERROR;
							response.description="<status context='"+command[1]+"'>Rotor: could not load "+command[4]+" into video node "+command[3]+"</status>\n";
						}
					}
					else {
					    response.status=HTTPResponse::HTTP_NOT_FOUND;
					    response.description="<status context='"+command[1]+"'>File '"+command[4]+"' not found</status>\n";
					}
			    }
			    else {
					response.status=HTTPResponse::HTTP_BAD_REQUEST;
					response.description="<status context='"+command[1]+"'>Rotor: session busy</status>\n";
			    }
			}
			else {
				response.status=HTTPResponse::HTTP_BAD_REQUEST;
				response.description="<status context='"+command[1]+"''>Rotor: bad request</status>\n";
			}
		}
		if (command[0]=="DELETE") {
		     //DUMMY RESPONSE
			response.description="<status context='"+command[1]+"''>DUMMY RESPONSE 1</status>\n";
			response.status=HTTPResponse::HTTP_OK;
	     }

	}
	if (command[2]=="render") {
		if (command[0]=="GET") {
													//DUMMY RESPONSE
			response.status=HTTPResponse::HTTP_OK;
			response.description="<status context='"+command[1]+"'>Rotor: rendering video</status>\n";
			response.description+="<progress>"+ofToString(progress)+"</progress>\n";
		}
		if (command[0]=="PUT") {
			if (command.size()>2) {
			    if (state==IDLE) {
						output_filename=output_dir+command[3];
						if (command.size()>3) {
//							output_framerate=ofToFloat(command[4]);
						}
					    add_queue(RENDER);
					    response.status=HTTPResponse::HTTP_OK;
					    response.description="<status context='"+command[1]+"'>Starting render: "+command[3]+"</status>\n";
			    }
			    else {
					response.status=HTTPResponse::HTTP_BAD_REQUEST;
					response.description="<status context='"+command[1]+"'>Rotor: session busy</status>\n";
		    	}
			}
			else {
				response.status=HTTPResponse::HTTP_BAD_REQUEST;
				response.description="<status context='"+command[1]+"'>Rotor: no output file specified</status>\n";
		    }
		}
		if (command[0]=="DELETE") {
													//DUMMY RESPONSE
													//SHOULD CHECK REQUIREMENTS
			response.status=HTTPResponse::HTTP_OK;
			response.description="<status context='"+command[1]+"'>DUMMY RESPONSE Rotor: cancelling render</status>\n";
		}
	}
	return response;
}


bool Render_context::load_audio(const string &filename,vector<Base_audio_processor*> processors){
	
	cerr<<"Rotor: starting audio analysis"<<endl;
	
	libav::audioloader loader;
	loader.setup(filename);

	graph.duration=((float)loader.formatContext->duration)/AV_TIME_BASE;

    int rate = loader.codecContext->sample_rate;
	int samples = ((loader.formatContext->duration + 5000)*rate)/AV_TIME_BASE; //why 5000 more?
	int channels= loader.codecContext->channels;
	int bits = loader.codecContext->bits_per_raw_sample;

	for (auto p: processors) {
		if(!p->init(channels,bits,samples,rate) ){
			cerr<<"Plugin failed to initialse"<<endl;
			return false;
		}
	}

	AVFrame* frame=loader.get_frame();
	int sample_processed=0;

	while (frame)
	{
		//now we can pass the data to the processor(s)
		for (auto p: processors) {
			p->process_frame(frame->data[0],frame->nb_samples);
		}
		sample_processed+=frame->nb_samples;
		//mutex.lock();
		progress=((float)sample_processed)/samples;
		//mutex.unlock();

		frame=loader.get_frame();
	}

	loader.close();

	for (auto p: processors) {
		p->cleanup();
		p->print_summary();
	}

	cerr<<"Rotor: finished audio analysis"<<endl;
}
bool Render_context::load_video(const string &nodeID,const string &filename){
	//this is a good standard example of how to find
	//a node of a specific type by ID and do something
	if (graph.nodes.find(nodeID)!=graph.nodes.end()){
		if (graph.nodes[nodeID]->type=="video_loader") {
			if (((Video_loader*)graph.nodes[nodeID])->load(filename)) {
				return true;
			}
		}
	}
	return false;
}