4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bcsignals.h"
26 #include "condition.h"
28 #include "edlsession.h"
29 #include "localsession.h"
33 #include "mwindowgui.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"
46 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
49 this->mwindow = mwindow;
50 this->output = output;
52 tracking_position = 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;
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);
78 PlaybackEngine::~PlaybackEngine()
81 output_lock->unlock();
84 delete_render_engine();
91 delete renderengine_lock;
100 void PlaybackEngine::create_objects()
102 preferences = new Preferences;
103 preferences->copy_from(mwindow->preferences);
107 start_lock->lock("PlaybackEngine::create_objects");
110 ChannelDB* PlaybackEngine::get_channeldb()
112 PlaybackConfig *config = command->get_edl()->session->playback_config;
113 switch(config->vconfig->driver)
115 case VIDEO4LINUX2JPEG:
116 return mwindow->channeldb_v4l2jpeg;
121 int PlaybackEngine::create_render_engine()
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__);
130 void PlaybackEngine::delete_render_engine()
132 renderengine_lock->lock("PlaybackEngine::delete_render_engine");
133 delete render_engine; render_engine = 0;
134 renderengine_lock->unlock();
137 void PlaybackEngine::arm_render_engine()
140 render_engine->arm_command(command);
143 void PlaybackEngine::start_render_engine()
146 render_engine->start_command();
149 void PlaybackEngine::wait_render_engine()
151 if( command->realtime && render_engine ) {
152 render_engine->join();
156 void PlaybackEngine::create_cache()
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);
165 void PlaybackEngine::perform_change()
167 switch( command->change_type ) {
171 create_render_engine();
174 render_engine->get_edl()->synchronize_params(command->get_edl());
180 void PlaybackEngine::sync_parameters(EDL *edl)
182 // TODO: lock out render engine from keyframe deletions
183 command->get_edl()->synchronize_params(edl);
185 render_engine->get_edl()->synchronize_params(edl);
188 void PlaybackEngine::interrupt_playback(int wait_tracking)
190 renderengine_lock->lock("PlaybackEngine::interrupt_playback");
192 render_engine->interrupt_playback();
193 renderengine_lock->unlock();
196 pause_lock->unlock();
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();
205 // Return 1 if levels exist
206 int PlaybackEngine::get_output_levels(double *levels, long position)
209 if( render_engine && render_engine->do_audio ) {
210 render_engine->get_output_levels(levels, position);
217 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
220 if( render_engine && render_engine->do_audio ) {
221 render_engine->get_module_levels(module_levels, position);
227 int PlaybackEngine::brender_available(long position)
232 void PlaybackEngine::init_cursor(int active)
236 void PlaybackEngine::init_meters()
240 void PlaybackEngine::stop_cursor()
245 void PlaybackEngine::init_tracking()
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);
254 void PlaybackEngine::stop_tracking()
258 tracking_done->unlock();
261 void PlaybackEngine::update_tracking(double position)
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();
271 double PlaybackEngine::get_tracking_position()
275 tracking_lock->lock("PlaybackEngine::get_tracking_position");
278 // Adjust for elapsed time since last update_tracking.
279 // But tracking timer isn't accurate until the first update_tracking
281 if(tracking_active == 2)
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));
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;
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;
303 loop_start = edl->local_session->loop_start;
304 loop_end = edl->local_session->loop_end;
307 double loop_size = loop_end - loop_start;
309 if( command->get_direction() == PLAY_FORWARD ) {
311 result = tracking_position +
312 command->get_speed() *
313 tracking_timer.get_difference() /
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;
324 result = tracking_position -
325 command->get_speed() *
326 tracking_timer.get_difference() /
329 // Compensate for loop
330 if( play_loop && loop_size > 0 ) {
331 while( result < loop_start ) result += loop_size;
338 result = tracking_position;
340 tracking_lock->unlock();
341 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
348 void PlaybackEngine::update_transport(int command, int paused)
350 // mwindow->gui->lock_window();
351 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
352 // mwindow->gui->unlock_window();
355 void PlaybackEngine::run()
357 start_lock->unlock();
360 // Wait for current command to finish
361 output_lock->lock("PlaybackEngine::run");
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;
372 interrupt_playback(0);
373 wait_render_engine();
375 switch( command->command ) {
376 // Parameter change only
383 pause_lock->lock("PlaybackEngine::run");
395 // Dispatch the command
396 start_render_engine();
399 case SINGLE_FRAME_FWD:
400 case SINGLE_FRAME_REWIND:
407 // Start tracking after arming so the tracking position doesn't change.
408 // The tracking for a single frame command occurs during PAUSE
410 if( !command->single_frame() )
412 // Dispatch the command
413 start_render_engine();
416 //printf("PlaybackEngine::run 100\n");
420 void PlaybackEngine::clear_output()
422 BC_WindowBase *cwdw = output->get_canvas();
424 cwdw->lock_window("PlaybackEngine::clear_output");
426 cwdw->unlock_window();
429 void PlaybackEngine::stop_playback(int wait_tracking)
431 transport_stop(wait_tracking);
432 renderengine_lock->lock("PlaybackEngine::stop_playback");
433 if( render_engine ) {
434 render_engine->interrupt_playback();
435 render_engine->wait_done();
437 renderengine_lock->unlock();
441 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
443 //printf("PlaybackEngine::send_command 1 %d\n", command);
444 // Stop requires transferring the output buffer to a refresh buffer.
446 int curr_command = is_playing_back ? this->command->command : STOP;
447 int curr_single_frame = TransportCommand::single_frame(curr_command);
448 int curr_audio = this->command->toggle_audio ?
449 !curr_single_frame : curr_single_frame;
450 int single_frame = TransportCommand::single_frame(command);
451 int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
452 float next_speed = next_command->speed;
456 case FAST_REWIND: // Commands that play back
459 case SINGLE_FRAME_REWIND:
460 case SINGLE_FRAME_FWD:
466 // run shuttle as no prev command
467 if( next_speed ) curr_command = COMMAND_NONE;
468 // Same direction pressed twice, not shuttle, and no change in audio state, Stop
469 if( curr_command == command && !curr_single_frame &&
470 curr_audio == next_audio ) { do_stop = 1; break; }
472 // Resume or change direction
473 switch( curr_command ) {
476 next_command->resume = 1;
480 case SINGLE_FRAME_FWD:
481 case SINGLE_FRAME_REWIND:
484 next_command->realtime = 1;
485 // Start from scratch
486 transport_command(command, CHANGE_NONE, edl, use_inout);
491 // Commands that stop
500 transport_stop(wait_tracking);
504 int PlaybackEngine::put_command(TransportCommand *command, int reset)
506 input_lock->lock("PlaybackEngine::put_command");
507 int prev_change_type = sent_command->change_type;
508 sent_command->copy_from(command);
509 // run only last command, sum change type
511 sent_command->change_type |= prev_change_type;
513 if( reset ) command->reset();
514 output_lock->unlock();
515 input_lock->unlock();
519 int PlaybackEngine::transport_stop(int wait_tracking)
521 put_command(stop_command, 0);
522 if( wait_tracking ) {
523 tracking_done->lock("PlaybackEngine::transport_stop");
524 tracking_done->unlock();
526 //printf("send: %d (STOP) 0\n", STOP);
530 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
532 next_command->command = command;
533 next_command->change_type |= change_type;
535 // Just change the EDL if the change requires it because renderengine
536 // structures won't point to the new EDL otherwise and because copying the
537 // EDL for every cursor movement is slow.
538 if( change_type & CHANGE_EDL )
539 next_command->get_edl()->copy_all(new_edl);
540 else if( change_type & CHANGE_PARAMS )
541 next_command->get_edl()->synchronize_params(new_edl);
542 next_command->set_playback_range(new_edl, use_inout,
543 preferences->forward_render_displacement);
545 put_command(next_command, 1);
546 //static const char *types[] = { "NONE",
547 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
548 // "STOP", "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
550 //printf("send= %d (%s) %d\n", sent_command->command,
551 // types[sent_command->command], sent_command->locked);
555 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
557 int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
558 next_command->realtime = 1;
559 transport_command(command, change_type, edl);