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