0dd9b37f1e943dfd5db3406cc84779fc61e8c83a
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / playbackengine.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "bchash.h"
23 #include "bcsignals.h"
24 #include "cache.h"
25 #include "condition.h"
26 #include "edl.h"
27 #include "edlsession.h"
28 #include "localsession.h"
29 #include "mbuttons.h"
30 #include "mutex.h"
31 #include "mwindow.h"
32 #include "mwindowgui.h"
33 #include "patchbay.h"
34 #include "tracking.h"
35 #include "playbackengine.h"
36 #include "playtransport.h"
37 #include "preferences.h"
38 #include "renderengine.h"
39 #include "mainsession.h"
40 #include "trackcanvas.h"
41 #include "transportque.h"
42 #include "vrender.h"
43
44
45 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
46  : Thread(1, 0, 0)
47 {
48         this->mwindow = mwindow;
49         this->output = output;
50         is_playing_back = 0;
51         tracking_position = 0;
52         tracking_active = 0;
53         audio_cache = 0;
54         video_cache = 0;
55         command = new TransportCommand();
56         command->command = STOP;
57         next_command = new TransportCommand();
58         next_command->change_type = CHANGE_ALL;
59         stop_command = new TransportCommand();
60         stop_command->command = STOP;
61         stop_command->realtime = 1;
62         sent_command = new TransportCommand();
63         sent_command->command = -1;
64         tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
65         renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
66         tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
67         pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
68         start_lock = new Condition(0, "PlaybackEngine::start_lock");
69         input_lock = new Condition(1, "PlaybackEngine::input_lock");
70         output_lock = new Condition(0, "PlaybackEngine::output_lock", 1);
71
72         render_engine = 0;
73         debug = 0;
74 }
75
76 PlaybackEngine::~PlaybackEngine()
77 {
78         done = 1;
79         output_lock->unlock();
80         Thread::join();
81         delete preferences;
82         delete_render_engine();
83         delete audio_cache;
84         delete video_cache;
85         delete tracking_lock;
86         delete tracking_done;
87         delete pause_lock;
88         delete start_lock;
89         delete renderengine_lock;
90         delete command;
91         delete next_command;
92         delete stop_command;
93         delete sent_command;
94         delete input_lock;
95         delete output_lock;
96 }
97
98 void PlaybackEngine::create_objects()
99 {
100         preferences = new Preferences;
101         preferences->copy_from(mwindow->preferences);
102
103         done = 0;
104         Thread::start();
105         start_lock->lock("PlaybackEngine::create_objects");
106 }
107
108 ChannelDB* PlaybackEngine::get_channeldb()
109 {
110         PlaybackConfig *config = command->get_edl()->session->playback_config;
111         switch(config->vconfig->driver)
112         {
113                 case VIDEO4LINUX2JPEG:
114                         return mwindow->channeldb_v4l2jpeg;
115         }
116         return 0;
117 }
118
119 int PlaybackEngine::create_render_engine()
120 {
121 // Fix playback configurations
122         delete_render_engine();
123         render_engine = new RenderEngine(this, preferences, output, 0);
124 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
125         return 0;
126 }
127
128 void PlaybackEngine::delete_render_engine()
129 {
130         renderengine_lock->lock("PlaybackEngine::delete_render_engine");
131         delete render_engine;
132         render_engine = 0;
133         renderengine_lock->unlock();
134 }
135
136 void PlaybackEngine::arm_render_engine()
137 {
138         if(render_engine)
139                 render_engine->arm_command(command);
140 }
141
142 void PlaybackEngine::start_render_engine()
143 {
144         if(render_engine) render_engine->start_command();
145 }
146
147 void PlaybackEngine::wait_render_engine()
148 {
149         if(command->realtime && render_engine)
150         {
151                 render_engine->join();
152         }
153 }
154
155 void PlaybackEngine::create_cache()
156 {
157         if(audio_cache) { delete audio_cache;  audio_cache = 0; }
158         if(video_cache) { delete video_cache;  video_cache = 0; }
159         if(!audio_cache) audio_cache = new CICache(preferences);
160         if(!video_cache) video_cache = new CICache(preferences);
161 }
162
163
164 void PlaybackEngine::perform_change()
165 {
166         switch( command->change_type ) {
167                 case CHANGE_ALL:
168                         create_cache();
169                 case CHANGE_EDL:
170                         create_render_engine();
171                         break;
172                 case CHANGE_PARAMS:
173                         render_engine->get_edl()->synchronize_params(command->get_edl());
174                 case CHANGE_NONE:
175                         break;
176         }
177 }
178
179 void PlaybackEngine::sync_parameters(EDL *edl)
180 {
181 // TODO: lock out render engine from keyframe deletions
182         command->get_edl()->synchronize_params(edl);
183         if(render_engine) render_engine->get_edl()->synchronize_params(edl);
184 }
185
186
187 void PlaybackEngine::interrupt_playback(int wait_tracking)
188 {
189         renderengine_lock->lock("PlaybackEngine::interrupt_playback");
190         if(render_engine)
191                 render_engine->interrupt_playback();
192         renderengine_lock->unlock();
193
194 // Stop pausing
195         pause_lock->unlock();
196
197 // Wait for tracking to finish if it is running
198         if(wait_tracking)
199         {
200                 tracking_done->lock("PlaybackEngine::interrupt_playback");
201                 tracking_done->unlock();
202         }
203 }
204
205
206 // Return 1 if levels exist
207 int PlaybackEngine::get_output_levels(double *levels, long position)
208 {
209         int result = 0;
210         if(render_engine && render_engine->do_audio)
211         {
212                 result = 1;
213                 render_engine->get_output_levels(levels, position);
214         }
215         return result;
216 }
217
218
219 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
220 {
221         int result = 0;
222         if(render_engine && render_engine->do_audio)
223         {
224                 result = 1;
225                 render_engine->get_module_levels(module_levels, position);
226         }
227         return result;
228 }
229
230 int PlaybackEngine::brender_available(long position)
231 {
232         return 0;
233 }
234
235 void PlaybackEngine::init_cursor(int active)
236 {
237 }
238
239 void PlaybackEngine::init_meters()
240 {
241 }
242
243 void PlaybackEngine::stop_cursor()
244 {
245 }
246
247
248 void PlaybackEngine::init_tracking()
249 {
250         tracking_active = !command->single_frame() ? 1 : 0;
251         tracking_position = command->playbackstart;
252         tracking_done->lock("PlaybackEngine::init_tracking");
253         init_cursor(tracking_active);
254         init_meters();
255 }
256
257 void PlaybackEngine::stop_tracking()
258 {
259         tracking_active = 0;
260         stop_cursor();
261         tracking_done->unlock();
262 }
263
264 void PlaybackEngine::update_tracking(double position)
265 {
266         tracking_lock->lock("PlaybackEngine::update_tracking");
267         tracking_position = position;
268 // Signal that the timer is accurate.
269         if(tracking_active) tracking_active = 2;
270         tracking_timer.update();
271         tracking_lock->unlock();
272 }
273
274 double PlaybackEngine::get_tracking_position()
275 {
276         double result = 0;
277
278         tracking_lock->lock("PlaybackEngine::get_tracking_position");
279
280
281 // Adjust for elapsed time since last update_tracking.
282 // But tracking timer isn't accurate until the first update_tracking
283 // so wait.
284         if(tracking_active == 2)
285         {
286 //printf("PlaybackEngine::get_tracking_position %d %d %d\n", command->get_direction(), tracking_position, tracking_timer.get_scaled_difference(command->get_edl()->session->sample_rate));
287
288
289 // Don't interpolate when every frame is played.
290                 if(command->get_edl()->session->video_every_frame &&
291                         render_engine &&
292                         render_engine->do_video)
293                 {
294                         result = tracking_position;
295                 }
296                 else
297 // Interpolate
298                 {
299                         double loop_start, loop_end;
300                         int play_loop = command->loop_play ? 1 : 0;
301                         EDL *edl = command->get_edl();
302                         int loop_playback = edl->local_session->loop_playback ? 1 : 0;
303                         if( play_loop || !loop_playback ) {
304                                 loop_start = command->start_position;
305                                 loop_end = command->end_position;
306                         }
307                         else {
308                                 loop_start = edl->local_session->loop_start;
309                                 loop_end = edl->local_session->loop_end;
310                                 play_loop = 1;
311                         }
312                         double loop_size = loop_end - loop_start;
313
314                         if( command->get_direction() == PLAY_FORWARD ) {
315 // Interpolate
316                                 result = tracking_position +
317                                         command->get_speed() *
318                                         tracking_timer.get_difference() /
319                                         1000.0;
320
321 // Compensate for loop
322 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
323                                 if( play_loop && loop_size > 0 ) {
324                                         while( result > loop_end ) result -= loop_size;
325                                 }
326                         }
327                         else {
328 // Interpolate
329                                 result = tracking_position -
330                                         command->get_speed() *
331                                         tracking_timer.get_difference() /
332                                         1000.0;
333
334 // Compensate for loop
335                                 if( play_loop && loop_size > 0 ) {
336                                         while( result < loop_start ) result += loop_size;
337                                 }
338                         }
339
340                 }
341         }
342         else
343                 result = tracking_position;
344
345         tracking_lock->unlock();
346 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
347
348 // Adjust for loop
349
350         return result;
351 }
352
353 void PlaybackEngine::update_transport(int command, int paused)
354 {
355 //      mwindow->gui->lock_window();
356 //      mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
357 //      mwindow->gui->unlock_window();
358 }
359
360 void PlaybackEngine::run()
361 {
362         start_lock->unlock();
363
364         while( !done ) {
365 // Wait for current command to finish
366                 output_lock->lock("PlaybackEngine::run");
367                 if( done ) break;
368 //printf("sent command=%d\n", sent_command->command);
369 // Read the new command
370                 command->copy_from(sent_command);
371                 input_lock->unlock();
372
373                 interrupt_playback(0);
374                 wait_render_engine();
375
376                 switch( command->command ) {
377 // Parameter change only
378                 case COMMAND_NONE:
379                         perform_change();
380                         break;
381
382                 case PAUSE:
383                         init_cursor(0);
384                         pause_lock->lock("PlaybackEngine::run");
385                         stop_cursor();
386                         break;
387
388                 case STOP:
389 // No changing
390                         break;
391
392                 case CURRENT_FRAME:
393                 case LAST_FRAME:
394                         perform_change();
395                         arm_render_engine();
396 // Dispatch the command
397                         start_render_engine();
398                         break;
399
400                 case SINGLE_FRAME_FWD:
401                 case SINGLE_FRAME_REWIND:
402 // fall through
403                 default:
404                         is_playing_back = 1;
405                         perform_change();
406                         arm_render_engine();
407
408 // Start tracking after arming so the tracking position doesn't change.
409 // The tracking for a single frame command occurs during PAUSE
410                         init_tracking();
411
412 // Dispatch the command
413                         start_render_engine();
414                         break;
415                 }
416 //printf("PlaybackEngine::run 100\n");
417         }
418 }
419
420
421 void PlaybackEngine::stop_playback(int wait_tracking)
422 {
423         transport_stop(wait_tracking);
424         renderengine_lock->lock("PlaybackEngine::stop_playback");
425         if(render_engine)
426                 render_engine->wait_done();
427         renderengine_lock->unlock();
428 }
429
430
431 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
432 {
433 //printf("PlaybackEngine::send_command 1 %d\n", command);
434 // Stop requires transferring the output buffer to a refresh buffer.
435         int do_stop = 0, do_resume = 0;
436         int curr_command = this->command->command;
437         int curr_single_frame = TransportCommand::single_frame(curr_command);
438         int curr_audio = this->command->toggle_audio ?
439                 !curr_single_frame : curr_single_frame;
440         int single_frame = TransportCommand::single_frame(command);
441         int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
442
443 // Dispatch command
444         switch( command ) {
445         case FAST_REWIND:       // Commands that play back
446         case NORMAL_REWIND:
447         case SLOW_REWIND:
448         case SINGLE_FRAME_REWIND:
449         case SINGLE_FRAME_FWD:
450         case SLOW_FWD:
451         case NORMAL_FWD:
452         case FAST_FWD:
453         case CURRENT_FRAME:
454         case LAST_FRAME:
455                 if( curr_command == command && !next_command->speed &&
456                     !curr_single_frame && curr_audio == next_audio ) {
457 // Same direction pressed twice, not shuttle, and no change in audio state,  Stop
458                         do_stop = 1;
459                         break;
460                 }
461 // Resume or change direction
462                 switch( curr_command ) {
463                 default:
464                         transport_stop(0);
465                         do_resume = 1;
466 // fall through
467                 case STOP:
468                 case COMMAND_NONE:
469                 case SINGLE_FRAME_FWD:
470                 case SINGLE_FRAME_REWIND:
471                 case CURRENT_FRAME:
472                 case LAST_FRAME:
473                         next_command->realtime = 1;
474                         next_command->resume = do_resume;
475 // Start from scratch
476                         transport_command(command, CHANGE_NONE, edl, use_inout);
477                         break;
478                 }
479                 break;
480
481 // Commands that stop
482         case STOP:
483         case REWIND:
484         case GOTO_END:
485                 do_stop = 1;
486                 break;
487         }
488
489         if( do_stop ) {
490                 transport_stop(wait_tracking);
491         }
492 }
493
494 int PlaybackEngine::transport_stop(int wait_tracking)
495 {
496         interrupt_playback(0);
497         input_lock->lock("PlaybackEngine::transport_stop");
498         sent_command->copy_from(stop_command);
499         output_lock->unlock();
500         if( wait_tracking ) {
501                 tracking_done->lock("PlaybackEngine::transport_stop");
502                 tracking_done->unlock();
503         }
504 //printf("send: %d (STOP) 0\n", STOP);
505         return 0;
506 }
507
508 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
509 {
510         next_command->command = command;
511         next_command->change_type |= change_type;
512         if( new_edl ) {
513 // Just change the EDL if the change requires it because renderengine
514 // structures won't point to the new EDL otherwise and because copying the
515 // EDL for every cursor movement is slow.
516                 switch( change_type ) {
517                 case CHANGE_EDL:
518                 case CHANGE_ALL:
519                         next_command->get_edl()->copy_all(new_edl);
520                         break;
521                 case CHANGE_PARAMS:
522                         next_command->get_edl()->synchronize_params(new_edl);
523                         break;
524                 }
525                 next_command->set_playback_range(new_edl, use_inout,
526                                 preferences->forward_render_displacement);
527         }
528
529         interrupt_playback(0);
530         input_lock->lock("PlaybackEngine::transport_command");
531         sent_command->copy_from(next_command);
532         next_command->reset();
533         output_lock->unlock();
534
535 //static const char *types[] = { "NONE",
536 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
537 // "STOP",  "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
538 // "LAST_FRAME" };
539 //printf("send= %d (%s) %d\n", sent_command->command,
540 // types[sent_command->command], sent_command->locked);
541         return 0;
542 }
543
544 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
545 {
546         int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
547         next_command->realtime = 1;
548         transport_command(command, change_type, edl);
549 }
550