shuttle and transportque reworks, new shdmp, titler font textbox tweak
[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         curr_command = new TransportCommand();
60         stop_command = new TransportCommand();
61         stop_command->command = STOP;
62         tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
63         renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
64         tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
65         pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
66         start_lock = new Condition(0, "PlaybackEngine::start_lock");
67         input_lock = new Condition(1, "PlaybackEngine::input_lock");
68         output_lock = new Condition(0, "PlaybackEngine::output_lock", 1);
69
70         render_engine = 0;
71         debug = 0;
72 }
73
74 PlaybackEngine::~PlaybackEngine()
75 {
76         done = 1;
77         transport_stop(0);
78         Thread::join();
79         delete preferences;
80         delete_render_engine();
81         delete audio_cache;
82         delete video_cache;
83         delete tracking_lock;
84         delete tracking_done;
85         delete pause_lock;
86         delete start_lock;
87         delete renderengine_lock;
88         delete command;
89         delete next_command;
90         delete stop_command;
91         delete curr_command;
92         delete input_lock;
93         delete output_lock;
94 }
95
96 void PlaybackEngine::create_objects()
97 {
98         preferences = new Preferences;
99         preferences->copy_from(mwindow->preferences);
100
101         done = 0;
102         Thread::start();
103         start_lock->lock("PlaybackEngine::create_objects");
104 }
105
106 ChannelDB* PlaybackEngine::get_channeldb()
107 {
108         PlaybackConfig *config = command->get_edl()->session->playback_config;
109         switch(config->vconfig->driver)
110         {
111                 case VIDEO4LINUX2JPEG:
112                         return mwindow->channeldb_v4l2jpeg;
113         }
114         return 0;
115 }
116
117 int PlaybackEngine::create_render_engine()
118 {
119 // Fix playback configurations
120         delete_render_engine();
121         render_engine = new RenderEngine(this, preferences, output, 0);
122 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
123         return 0;
124 }
125
126 void PlaybackEngine::delete_render_engine()
127 {
128         renderengine_lock->lock("PlaybackEngine::delete_render_engine");
129         delete render_engine;
130         render_engine = 0;
131         renderengine_lock->unlock();
132 }
133
134 void PlaybackEngine::arm_render_engine()
135 {
136         if(render_engine)
137                 render_engine->arm_command(command);
138 }
139
140 void PlaybackEngine::start_render_engine()
141 {
142         if(render_engine) render_engine->start_command();
143 }
144
145 void PlaybackEngine::wait_render_engine()
146 {
147         if(command->realtime && render_engine)
148         {
149                 render_engine->join();
150         }
151 }
152
153 void PlaybackEngine::create_cache()
154 {
155         if(audio_cache) { delete audio_cache;  audio_cache = 0; }
156         if(video_cache) { delete video_cache;  video_cache = 0; }
157         if(!audio_cache) audio_cache = new CICache(preferences);
158         if(!video_cache) video_cache = new CICache(preferences);
159 }
160
161
162 void PlaybackEngine::perform_change()
163 {
164         switch( command->change_type )
165         {
166                 case CHANGE_ALL:
167                         create_cache();
168                 case CHANGE_EDL:
169                         create_render_engine();
170                 case CHANGE_PARAMS:
171                         if(command->change_type != CHANGE_EDL &&
172                                 (uint32_t)command->change_type != CHANGE_ALL)
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 //printf("PlaybackEngine::run 0 %d\n", curr_command->command);
368                 if( curr_command->command < 0 ) continue;
369 // this covers a glitch that occurs when stop is asserted
370 // when the render_engine starting, but not initialized
371                 if( curr_command->command == STOP && render_engine )
372                         render_engine->interrupt_playback();
373                 wait_render_engine();
374
375 // Read the new command
376                 input_lock->lock("PlaybackEngine::run");
377                 command->copy_from(curr_command);
378                 curr_command->command = -1;
379                 input_lock->unlock();
380                 if( done ) break;
381 //printf("PlaybackEngine::run 1 %d\n", command->command);
382
383                 switch( command->command ) {
384 // Parameter change only
385                 case COMMAND_NONE:
386                         perform_change();
387                         break;
388
389                 case PAUSE:
390                         init_cursor(0);
391                         pause_lock->lock("PlaybackEngine::run");
392                         stop_cursor();
393                         break;
394
395                 case STOP:
396 // No changing
397                         break;
398
399                 case CURRENT_FRAME:
400                 case LAST_FRAME:
401                         perform_change();
402                         arm_render_engine();
403 // Dispatch the command
404                         start_render_engine();
405                         break;
406
407                 case SINGLE_FRAME_FWD:
408                 case SINGLE_FRAME_REWIND:
409 // fall through
410                 default:
411                         is_playing_back = 1;
412                         perform_change();
413                         arm_render_engine();
414
415 // Start tracking after arming so the tracking position doesn't change.
416 // The tracking for a single frame command occurs during PAUSE
417                         init_tracking();
418
419 // Dispatch the command
420                         start_render_engine();
421                         break;
422                 }
423 //printf("PlaybackEngine::run 100\n");
424         }
425 }
426
427
428 void PlaybackEngine::stop_playback(int wait_tracking)
429 {
430         transport_stop(wait_tracking);
431         renderengine_lock->lock("PlaybackEngine::stop_playback");
432         if(render_engine)
433                 render_engine->wait_done();
434         renderengine_lock->unlock();
435 }
436
437
438 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
439 {
440 //printf("PlaybackEngine::send_command 1 %d\n", command);
441 // Stop requires transferring the output buffer to a refresh buffer.
442         int do_stop = 0, do_resume = 0;
443         int curr_command = this->command->command;
444         int curr_single_frame = TransportCommand::single_frame(curr_command);
445         int curr_audio = this->command->toggle_audio ?
446                 !curr_single_frame : curr_single_frame;
447         int single_frame = TransportCommand::single_frame(command);
448         int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
449
450 // Dispatch command
451         switch( command ) {
452         case FAST_REWIND:       // Commands that play back
453         case NORMAL_REWIND:
454         case SLOW_REWIND:
455         case SINGLE_FRAME_REWIND:
456         case SINGLE_FRAME_FWD:
457         case SLOW_FWD:
458         case NORMAL_FWD:
459         case FAST_FWD:
460         case CURRENT_FRAME:
461         case LAST_FRAME:
462                 if( curr_command == command && !next_command->speed &&
463                     !curr_single_frame && curr_audio == next_audio ) {
464 // Same direction pressed twice, not shuttle, and no change in audio state,  Stop
465                         do_stop = 1;
466                         break;
467                 }
468 // Resume or change direction
469                 switch( curr_command ) {
470                 default:
471                         transport_stop(wait_tracking);
472                         do_resume = 1;
473 // fall through
474                 case STOP:
475                 case COMMAND_NONE:
476                 case SINGLE_FRAME_FWD:
477                 case SINGLE_FRAME_REWIND:
478                 case CURRENT_FRAME:
479                 case LAST_FRAME:
480                         next_command->realtime = 1;
481                         next_command->resume = do_resume;
482 // Start from scratch
483                         transport_command(command, CHANGE_NONE, edl, use_inout);
484                         break;
485                 }
486                 break;
487
488 // Commands that stop
489         case STOP:
490         case REWIND:
491         case GOTO_END:
492                 do_stop = 1;
493                 break;
494         }
495
496         if( do_stop ) {
497                 transport_stop(wait_tracking);
498         }
499 }
500
501 int PlaybackEngine::transport_stop(int wait_tracking)
502 {
503         input_lock->lock("PlaybackEngine::transport_stop 1");
504         curr_command->copy_from(stop_command);
505         interrupt_playback(wait_tracking);
506 //printf("send: %d (STOP)\n", STOP);
507         input_lock->unlock();
508         output_lock->unlock();
509         return 0;
510 }
511
512 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
513 {
514         input_lock->lock("PlaybackEngine::transport_command 1");
515         next_command->command = command;
516         next_command->change_type |= change_type;
517         if( new_edl ) {
518 // Just change the EDL if the change requires it because renderengine
519 // structures won't point to the new EDL otherwise and because copying the
520 // EDL for every cursor movement is slow.
521                 switch( change_type ) {
522                 case CHANGE_EDL:
523                 case CHANGE_ALL:
524                         next_command->get_edl()->copy_all(new_edl);
525                         break;
526                 case CHANGE_PARAMS:
527                         next_command->get_edl()->synchronize_params(new_edl);
528                         break;
529                 }
530                 next_command->set_playback_range(new_edl, use_inout,
531                                 preferences->forward_render_displacement);
532         }
533         curr_command->copy_from(next_command);
534         next_command->reset();
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)\n", command, types[command]);
540         input_lock->unlock();
541         output_lock->unlock();
542         return 0;
543 }
544
545 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
546 {
547         int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
548         next_command->realtime = 1;
549         transport_command(command, change_type, edl);
550 }
551