no /. in ffmpeg init_decode segv, build index fix audio wave, stop playback state...
[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 "canvas.h"
26 #include "condition.h"
27 #include "edl.h"
28 #include "edlsession.h"
29 #include "localsession.h"
30 #include "mbuttons.h"
31 #include "mutex.h"
32 #include "mwindow.h"
33 #include "mwindowgui.h"
34 #include "patchbay.h"
35 #include "tracking.h"
36 #include "playbackengine.h"
37 #include "playtransport.h"
38 #include "preferences.h"
39 #include "renderengine.h"
40 #include "mainsession.h"
41 #include "trackcanvas.h"
42 #include "transportque.h"
43 #include "vrender.h"
44
45
46 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
47  : Thread(1, 0, 0)
48 {
49         this->mwindow = mwindow;
50         this->output = output;
51         is_playing_back = 0;
52         tracking_position = 0;
53         tracking_active = 0;
54         audio_cache = 0;
55         video_cache = 0;
56         command = new TransportCommand();
57         command->command = STOP;
58         next_command = new TransportCommand();
59         next_command->change_type = CHANGE_ALL;
60         stop_command = new TransportCommand();
61         stop_command->command = STOP;
62         stop_command->realtime = 1;
63         sent_command = new TransportCommand();
64         sent_command->command = -1;
65         send_active = 0;
66         tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
67         renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
68         tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
69         pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
70         start_lock = new Condition(0, "PlaybackEngine::start_lock");
71         input_lock = new Condition(1, "PlaybackEngine::input_lock");
72         output_lock = new Condition(0, "PlaybackEngine::output_lock", 1);
73
74         render_engine = 0;
75         debug = 0;
76 }
77
78 PlaybackEngine::~PlaybackEngine()
79 {
80         done = 1;
81         output_lock->unlock();
82         Thread::join();
83         delete preferences;
84         delete_render_engine();
85         delete audio_cache;
86         delete video_cache;
87         delete tracking_lock;
88         delete tracking_done;
89         delete pause_lock;
90         delete start_lock;
91         delete renderengine_lock;
92         delete command;
93         delete next_command;
94         delete stop_command;
95         delete sent_command;
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;  render_engine = 0;
134         renderengine_lock->unlock();
135 }
136
137 void PlaybackEngine::arm_render_engine()
138 {
139         if( render_engine )
140                 render_engine->arm_command(command);
141 }
142
143 void PlaybackEngine::start_render_engine()
144 {
145         if( render_engine )
146                 render_engine->start_command();
147 }
148
149 void PlaybackEngine::wait_render_engine()
150 {
151         if( command->realtime && render_engine ) {
152                 render_engine->join();
153         }
154 }
155
156 void PlaybackEngine::create_cache()
157 {
158         if(audio_cache) { delete audio_cache;  audio_cache = 0; }
159         if(video_cache) { delete video_cache;  video_cache = 0; }
160         if(!audio_cache) audio_cache = new CICache(preferences);
161         if(!video_cache) video_cache = new CICache(preferences);
162 }
163
164
165 void PlaybackEngine::perform_change()
166 {
167         switch( command->change_type ) {
168                 case CHANGE_ALL:
169                         create_cache();
170                 case CHANGE_EDL:
171                         create_render_engine();
172                         break;
173                 case CHANGE_PARAMS:
174                         render_engine->get_edl()->synchronize_params(command->get_edl());
175                 case CHANGE_NONE:
176                         break;
177         }
178 }
179
180 void PlaybackEngine::sync_parameters(EDL *edl)
181 {
182 // TODO: lock out render engine from keyframe deletions
183         command->get_edl()->synchronize_params(edl);
184         if( render_engine )
185                 render_engine->get_edl()->synchronize_params(edl);
186 }
187
188 void PlaybackEngine::interrupt_playback(int wait_tracking)
189 {
190         renderengine_lock->lock("PlaybackEngine::interrupt_playback");
191         if( render_engine )
192                 render_engine->interrupt_playback();
193         renderengine_lock->unlock();
194
195 // Stop pausing
196         pause_lock->unlock();
197
198 // Wait for tracking to finish if it is running
199         if( wait_tracking ) {
200                 tracking_done->lock("PlaybackEngine::interrupt_playback");
201                 tracking_done->unlock();
202         }
203 }
204
205 // Return 1 if levels exist
206 int PlaybackEngine::get_output_levels(double *levels, long position)
207 {
208         int result = 0;
209         if( render_engine && render_engine->do_audio ) {
210                 render_engine->get_output_levels(levels, position);
211                 result = 1;
212         }
213         return result;
214 }
215
216
217 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
218 {
219         int result = 0;
220         if( render_engine && render_engine->do_audio ) {
221                 render_engine->get_module_levels(module_levels, position);
222                 result = 1;
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 && render_engine->do_video ) {
289                         result = tracking_position;
290                 }
291                 else
292 // Interpolate
293                 {
294                         double loop_start, loop_end;
295                         int play_loop = command->loop_play ? 1 : 0;
296                         EDL *edl = command->get_edl();
297                         int loop_playback = edl->local_session->loop_playback ? 1 : 0;
298                         if( play_loop || !loop_playback ) {
299                                 loop_start = command->start_position;
300                                 loop_end = command->end_position;
301                         }
302                         else {
303                                 loop_start = edl->local_session->loop_start;
304                                 loop_end = edl->local_session->loop_end;
305                                 play_loop = 1;
306                         }
307                         double loop_size = loop_end - loop_start;
308
309                         if( command->get_direction() == PLAY_FORWARD ) {
310 // Interpolate
311                                 result = tracking_position +
312                                         command->get_speed() *
313                                         tracking_timer.get_difference() /
314                                         1000.0;
315
316 // Compensate for loop
317 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
318                                 if( play_loop && loop_size > 0 ) {
319                                         while( result > loop_end ) result -= loop_size;
320                                 }
321                         }
322                         else {
323 // Interpolate
324                                 result = tracking_position -
325                                         command->get_speed() *
326                                         tracking_timer.get_difference() /
327                                         1000.0;
328
329 // Compensate for loop
330                                 if( play_loop && loop_size > 0 ) {
331                                         while( result < loop_start ) result += loop_size;
332                                 }
333                         }
334
335                 }
336         }
337         else
338                 result = tracking_position;
339
340         tracking_lock->unlock();
341 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
342
343 // Adjust for loop
344
345         return result;
346 }
347
348 void PlaybackEngine::update_transport(int command, int paused)
349 {
350 //      mwindow->gui->lock_window();
351 //      mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
352 //      mwindow->gui->unlock_window();
353 }
354
355 void PlaybackEngine::run()
356 {
357         start_lock->unlock();
358
359         while( !done ) {
360 // Wait for current command to finish
361                 output_lock->lock("PlaybackEngine::run");
362                 if( done ) break;
363 // Read the new command
364                 input_lock->lock("PlaybackEngine::run");
365                 command->copy_from(sent_command);
366 //printf("sent command=%d\n", sent_command->command);
367                 int active = this->send_active;
368                 this->send_active = 0;
369                 input_lock->unlock();
370                 if( !active ) continue;
371
372                 interrupt_playback(0);
373                 wait_render_engine();
374
375                 switch( command->command ) {
376 // Parameter change only
377                 case COMMAND_NONE:
378                         perform_change();
379                         break;
380
381                 case PAUSE:
382                         init_cursor(0);
383                         pause_lock->lock("PlaybackEngine::run");
384                         stop_cursor();
385                         break;
386
387                 case STOP:
388 // No changing
389                         break;
390
391                 case CURRENT_FRAME:
392                 case LAST_FRAME:
393                         perform_change();
394                         arm_render_engine();
395 // Dispatch the command
396                         start_render_engine();
397                         break;
398
399                 case SINGLE_FRAME_FWD:
400                 case SINGLE_FRAME_REWIND:
401 // fall through
402                 default:
403                         is_playing_back = 1;
404                         perform_change();
405                         arm_render_engine();
406
407 // Start tracking after arming so the tracking position doesn't change.
408 // The tracking for a single frame command occurs during PAUSE
409                         init_tracking();
410                         clear_output();
411 // Dispatch the command
412                         start_render_engine();
413                         break;
414                 }
415 //printf("PlaybackEngine::run 100\n");
416         }
417 }
418
419 void PlaybackEngine::clear_output()
420 {
421         BC_WindowBase *cwdw = output->get_canvas();
422         if( !cwdw ) return;
423         cwdw->lock_window("PlaybackEngine::clear_output");
424         output->clear();
425         cwdw->unlock_window();
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->interrupt_playback();
434                 render_engine->wait_done();
435         }
436         renderengine_lock->unlock();
437 }
438
439
440 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
441 {
442 //printf("PlaybackEngine::send_command 1 %d\n", command);
443 // Stop requires transferring the output buffer to a refresh buffer.
444         int do_stop = 0;
445         int curr_command = is_playing_back ? this->command->command : STOP;
446         int curr_single_frame = TransportCommand::single_frame(curr_command);
447         int curr_audio = this->command->toggle_audio ?
448                 !curr_single_frame : curr_single_frame;
449         int single_frame = TransportCommand::single_frame(command);
450         int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
451         float next_speed = next_command->speed;
452
453 // Dispatch command
454         switch( command ) {
455         case FAST_REWIND:       // Commands that play back
456         case NORMAL_REWIND:
457         case SLOW_REWIND:
458         case SINGLE_FRAME_REWIND:
459         case SINGLE_FRAME_FWD:
460         case SLOW_FWD:
461         case NORMAL_FWD:
462         case FAST_FWD:
463         case CURRENT_FRAME:
464         case LAST_FRAME:
465 // run shuttle as no prev command
466                 if( next_speed ) curr_command = COMMAND_NONE;
467 // Same direction pressed twice, not shuttle, and no change in audio state,  Stop
468                 if( curr_command == command && !curr_single_frame &&
469                     curr_audio == next_audio ) { do_stop = 1;  break; }
470
471 // Resume or change direction
472                 switch( curr_command ) {
473                 default:
474                         transport_stop(0);
475                         next_command->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                         next_command->realtime = 1;
484 // Start from scratch
485                         transport_command(command, CHANGE_NONE, edl, use_inout);
486                         break;
487                 }
488                 break;
489
490 // Commands that stop
491         case STOP:
492         case REWIND:
493         case GOTO_END:
494                 do_stop = 1;
495                 break;
496         }
497
498         if( do_stop ) {
499                 transport_stop(wait_tracking);
500         }
501 }
502
503 int PlaybackEngine::put_command(TransportCommand *command, int reset)
504 {
505         input_lock->lock("PlaybackEngine::put_command");
506         int prev_change_type = sent_command->change_type;
507         sent_command->copy_from(command);
508 // run only last command, sum change type
509         if( send_active )
510                 sent_command->change_type |= prev_change_type;
511         send_active = 1;
512         if( reset ) command->reset();
513         output_lock->unlock();
514         input_lock->unlock();
515         return 0;
516 }
517
518 int PlaybackEngine::transport_stop(int wait_tracking)
519 {
520         put_command(stop_command, 0);
521         if( wait_tracking ) {
522                 tracking_done->lock("PlaybackEngine::transport_stop");
523                 tracking_done->unlock();
524         }
525 //printf("send: %d (STOP) 0\n", STOP);
526         return 0;
527 }
528
529 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
530 {
531         next_command->command = command;
532         next_command->change_type |= change_type;
533         if( new_edl ) {
534 // Just change the EDL if the change requires it because renderengine
535 // structures won't point to the new EDL otherwise and because copying the
536 // EDL for every cursor movement is slow.
537                 if( change_type & CHANGE_EDL )
538                         next_command->get_edl()->copy_all(new_edl);
539                 else if( change_type & CHANGE_PARAMS )
540                         next_command->get_edl()->synchronize_params(new_edl);
541                 next_command->set_playback_range(new_edl, use_inout,
542                                 preferences->forward_render_displacement);
543         }
544         put_command(next_command, 1);
545 //static const char *types[] = { "NONE",
546 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
547 // "STOP",  "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
548 // "LAST_FRAME" };
549 //printf("send= %d (%s) %d\n", sent_command->command,
550 // types[sent_command->command], sent_command->locked);
551         return 0;
552 }
553
554 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
555 {
556         int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
557         next_command->realtime = 1;
558         transport_command(command, change_type, edl);
559 }
560