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