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