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