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