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