rework set default transtion, nested proxy edl fixes, doubleclick proxy media fix...
[goodguy/history.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         last_command = STOP;
56         tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
57         renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
58         tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
59         pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
60         start_lock = new Condition(0, "PlaybackEngine::start_lock");
61         render_engine = 0;
62         debug = 0;
63 }
64
65 PlaybackEngine::~PlaybackEngine()
66 {
67         done = 1;
68         que->send_command(STOP,
69                 CHANGE_NONE,
70                 0,
71                 0);
72         interrupt_playback();
73
74         Thread::join();
75         delete preferences;
76         delete command;
77         delete que;
78         delete_render_engine();
79         delete audio_cache;
80         delete video_cache;
81         delete tracking_lock;
82         delete tracking_done;
83         delete pause_lock;
84         delete start_lock;
85         delete renderengine_lock;
86 }
87
88 void PlaybackEngine::create_objects()
89 {
90         preferences = new Preferences;
91         command = new TransportCommand;
92         que = new TransportQue;
93 // Set the first change to maximum
94         que->command.change_type = CHANGE_ALL;
95
96         preferences->copy_from(mwindow->preferences);
97
98         done = 0;
99         Thread::start();
100         start_lock->lock("PlaybackEngine::create_objects");
101 }
102
103 ChannelDB* PlaybackEngine::get_channeldb()
104 {
105         PlaybackConfig *config = command->get_edl()->session->playback_config;
106         switch(config->vconfig->driver)
107         {
108                 case VIDEO4LINUX2JPEG:
109                         return mwindow->channeldb_v4l2jpeg;
110         }
111         return 0;
112 }
113
114 int PlaybackEngine::create_render_engine()
115 {
116 // Fix playback configurations
117         delete_render_engine();
118         render_engine = new RenderEngine(this, preferences, output, 0);
119 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
120         return 0;
121 }
122
123 void PlaybackEngine::delete_render_engine()
124 {
125         renderengine_lock->lock("PlaybackEngine::delete_render_engine");
126         delete render_engine;
127         render_engine = 0;
128         renderengine_lock->unlock();
129 }
130
131 void PlaybackEngine::arm_render_engine()
132 {
133         if(render_engine)
134                 render_engine->arm_command(command);
135 }
136
137 void PlaybackEngine::start_render_engine()
138 {
139         if(render_engine) render_engine->start_command();
140 }
141
142 void PlaybackEngine::wait_render_engine()
143 {
144         if(command->realtime && render_engine)
145         {
146                 render_engine->join();
147         }
148 }
149
150 void PlaybackEngine::create_cache()
151 {
152         if(audio_cache) { delete audio_cache;  audio_cache = 0; }
153         if(video_cache) { delete video_cache;  video_cache = 0; }
154         if(!audio_cache) audio_cache = new CICache(preferences);
155         if(!video_cache) video_cache = new CICache(preferences);
156 }
157
158
159 void PlaybackEngine::perform_change()
160 {
161         switch(command->change_type)
162         {
163                 case CHANGE_ALL:
164                         create_cache();
165                 case CHANGE_EDL:
166                         create_render_engine();
167                 case CHANGE_PARAMS:
168                         if(command->change_type != CHANGE_EDL &&
169                                 (uint32_t)command->change_type != CHANGE_ALL)
170                                 render_engine->get_edl()->synchronize_params(command->get_edl());
171                 case CHANGE_NONE:
172                         break;
173         }
174 }
175
176 void PlaybackEngine::sync_parameters(EDL *edl)
177 {
178 // TODO: lock out render engine from keyframe deletions
179         command->get_edl()->synchronize_params(edl);
180         if(render_engine) render_engine->get_edl()->synchronize_params(edl);
181 }
182
183
184 void PlaybackEngine::interrupt_playback(int wait_tracking)
185 {
186         renderengine_lock->lock("PlaybackEngine::interrupt_playback");
187         if(render_engine)
188                 render_engine->interrupt_playback();
189         renderengine_lock->unlock();
190
191 // Stop pausing
192         pause_lock->unlock();
193
194 // Wait for tracking to finish if it is running
195         if(wait_tracking)
196         {
197                 tracking_done->lock("PlaybackEngine::interrupt_playback");
198                 tracking_done->unlock();
199         }
200 }
201
202
203 // Return 1 if levels exist
204 int PlaybackEngine::get_output_levels(double *levels, long position)
205 {
206         int result = 0;
207         if(render_engine && render_engine->do_audio)
208         {
209                 result = 1;
210                 render_engine->get_output_levels(levels, position);
211         }
212         return result;
213 }
214
215
216 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
217 {
218         int result = 0;
219         if(render_engine && render_engine->do_audio)
220         {
221                 result = 1;
222                 render_engine->get_module_levels(module_levels, position);
223         }
224         return result;
225 }
226
227 int PlaybackEngine::brender_available(long position)
228 {
229         return 0;
230 }
231
232 void PlaybackEngine::init_cursor(int active)
233 {
234 }
235
236 void PlaybackEngine::init_meters()
237 {
238 }
239
240 void PlaybackEngine::stop_cursor()
241 {
242 }
243
244
245 void PlaybackEngine::init_tracking()
246 {
247         tracking_active = !command->single_frame() ? 1 : 0;
248         tracking_position = command->playbackstart;
249         tracking_done->lock("PlaybackEngine::init_tracking");
250         init_cursor(tracking_active);
251         init_meters();
252 }
253
254 void PlaybackEngine::stop_tracking()
255 {
256         tracking_active = 0;
257         stop_cursor();
258         tracking_done->unlock();
259 }
260
261 void PlaybackEngine::update_tracking(double position)
262 {
263         tracking_lock->lock("PlaybackEngine::update_tracking");
264         tracking_position = position;
265 // Signal that the timer is accurate.
266         if(tracking_active) tracking_active = 2;
267         tracking_timer.update();
268         tracking_lock->unlock();
269 }
270
271 double PlaybackEngine::get_tracking_position()
272 {
273         double result = 0;
274
275         tracking_lock->lock("PlaybackEngine::get_tracking_position");
276
277
278 // Adjust for elapsed time since last update_tracking.
279 // But tracking timer isn't accurate until the first update_tracking
280 // so wait.
281         if(tracking_active == 2)
282         {
283 //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));
284
285
286 // Don't interpolate when every frame is played.
287                 if(command->get_edl()->session->video_every_frame &&
288                         render_engine &&
289                         render_engine->do_video)
290                 {
291                         result = tracking_position;
292                 }
293                 else
294 // Interpolate
295                 {
296                         double loop_start, loop_end;
297                         int play_loop = command->play_loop ? 1 : 0;
298                         EDL *edl = command->get_edl();
299                         int loop_playback = edl->local_session->loop_playback ? 1 : 0;
300                         if( play_loop || !loop_playback ) {
301                                 loop_start = command->start_position;
302                                 loop_end = command->end_position;
303                         }
304                         else {
305                                 loop_start = edl->local_session->loop_start;
306                                 loop_end = edl->local_session->loop_end;
307                                 play_loop = 1;
308                         }
309                         double loop_size = loop_end - loop_start;
310
311                         if( command->get_direction() == PLAY_FORWARD ) {
312 // Interpolate
313                                 result = tracking_position +
314                                         command->get_speed() *
315                                         tracking_timer.get_difference() /
316                                         1000.0;
317
318 // Compensate for loop
319 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
320                                 if( play_loop && loop_size > 0 ) {
321                                         while( result > loop_end ) result -= loop_size;
322                                 }
323                         }
324                         else {
325 // Interpolate
326                                 result = tracking_position -
327                                         command->get_speed() *
328                                         tracking_timer.get_difference() /
329                                         1000.0;
330
331 // Compensate for loop
332                                 if( play_loop && loop_size > 0 ) {
333                                         while( result < loop_start ) result += loop_size;
334                                 }
335                         }
336
337                 }
338         }
339         else
340                 result = tracking_position;
341
342         tracking_lock->unlock();
343 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
344
345 // Adjust for loop
346
347         return result;
348 }
349
350 void PlaybackEngine::update_transport(int command, int paused)
351 {
352 //      mwindow->gui->lock_window();
353 //      mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
354 //      mwindow->gui->unlock_window();
355 }
356
357 void PlaybackEngine::run()
358 {
359         start_lock->unlock();
360
361         while( !done ) {
362 // Wait for current command to finish
363                 que->output_lock->lock("PlaybackEngine::run");
364                 wait_render_engine();
365
366 // Read the new command
367                 que->input_lock->lock("PlaybackEngine::run");
368                 if(done) return;
369
370                 command->copy_from(&que->command);
371                 que->command.reset();
372                 que->input_lock->unlock();
373 //printf("PlaybackEngine::run 1 %d\n", command->command);
374
375                 switch( command->command ) {
376 // Parameter change only
377                 case COMMAND_NONE:
378 //                      command->command = last_command;
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                         last_command = command->command;
395                         perform_change();
396                         arm_render_engine();
397 // Dispatch the command
398                         start_render_engine();
399                         break;
400
401                 case SINGLE_FRAME_FWD:
402                 case SINGLE_FRAME_REWIND:
403 // fall through
404                 default:
405                         last_command = command->command;
406                         is_playing_back = 1;
407
408                         perform_change();
409                         arm_render_engine();
410
411 // Start tracking after arming so the tracking position doesn't change.
412 // The tracking for a single frame command occurs during PAUSE
413                         init_tracking();
414
415 // Dispatch the command
416                         start_render_engine();
417                         break;
418                 }
419
420
421 //printf("PlaybackEngine::run 100\n");
422         }
423 }
424
425
426 void PlaybackEngine::stop_playback(int wait)
427 {
428         que->send_command(STOP, CHANGE_NONE, 0, 0);
429         interrupt_playback(wait);
430         renderengine_lock->lock("PlaybackEngine::stop_playback");
431         if(render_engine)
432                 render_engine->wait_done();
433         renderengine_lock->unlock();
434 }
435
436
437 void PlaybackEngine::issue_command(EDL *edl, int command, int wait_tracking,
438                 int use_inout, int update_refresh, int toggle_audio, int loop_play)
439 {
440 //printf("PlaybackEngine::issue_command 1 %d\n", command);
441 // Stop requires transferring the output buffer to a refresh buffer.
442         int do_stop = 0, resume = 0;
443         int prev_command = this->command->command;
444         int prev_single_frame = this->command->single_frame();
445         int prev_audio = this->command->audio_toggle ?
446                  !prev_single_frame : prev_single_frame;
447         int cur_single_frame = TransportCommand::single_frame(command);
448         int cur_audio = toggle_audio ?
449                  !cur_single_frame : cur_single_frame;
450
451 // Dispatch command
452         switch(command) {
453         case FAST_REWIND:       // Commands that play back
454         case NORMAL_REWIND:
455         case SLOW_REWIND:
456         case SINGLE_FRAME_REWIND:
457         case SINGLE_FRAME_FWD:
458         case SLOW_FWD:
459         case NORMAL_FWD:
460         case FAST_FWD:
461         case CURRENT_FRAME:
462         case LAST_FRAME:
463                 if( !prev_single_frame &&
464                     prev_command == command &&
465                     cur_audio == prev_audio ) {
466 // Same direction pressed twice and no change in audio state,  Stop
467                         do_stop = 1;
468                         break;
469                 }
470 // Resume or change direction
471                 switch( prev_command ) {
472                 default:
473                         que->send_command(STOP, CHANGE_NONE, 0, 0);
474                         interrupt_playback(wait_tracking);
475                         resume = 1;
476 // fall through
477                 case STOP:
478                 case COMMAND_NONE:
479                 case SINGLE_FRAME_FWD:
480                 case SINGLE_FRAME_REWIND:
481                 case CURRENT_FRAME:
482                 case LAST_FRAME:
483 // Start from scratch
484                         que->send_command(command, CHANGE_NONE, edl,
485                                 1, resume, use_inout, toggle_audio, loop_play,
486                                 mwindow->preferences->forward_render_displacement);
487                         break;
488                 }
489                 break;
490
491 // Commands that stop
492         case STOP:
493         case REWIND:
494         case GOTO_END:
495                 do_stop = 1;
496                 break;
497         }
498
499         if( do_stop ) {
500                 que->send_command(STOP, CHANGE_NONE, 0, 0);
501                 interrupt_playback(wait_tracking);
502         }
503 }
504
505 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
506 {
507         que->send_command(dir >= 0 ? CURRENT_FRAME : LAST_FRAME,
508                 change_type, edl, 1);
509 }
510