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"
25 #include "condition.h"
27 #include "edlsession.h"
28 #include "localsession.h"
32 #include "mwindowgui.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"
45 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
48 this->mwindow = mwindow;
49 this->output = output;
51 tracking_position = 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;
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);
77 PlaybackEngine::~PlaybackEngine()
80 output_lock->unlock();
83 delete_render_engine();
90 delete renderengine_lock;
99 void PlaybackEngine::create_objects()
101 preferences = new Preferences;
102 preferences->copy_from(mwindow->preferences);
106 start_lock->lock("PlaybackEngine::create_objects");
109 ChannelDB* PlaybackEngine::get_channeldb()
111 PlaybackConfig *config = command->get_edl()->session->playback_config;
112 switch(config->vconfig->driver)
114 case VIDEO4LINUX2JPEG:
115 return mwindow->channeldb_v4l2jpeg;
120 int PlaybackEngine::create_render_engine()
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__);
129 void PlaybackEngine::delete_render_engine()
131 renderengine_lock->lock("PlaybackEngine::delete_render_engine");
132 delete render_engine; render_engine = 0;
133 renderengine_lock->unlock();
136 void PlaybackEngine::arm_render_engine()
139 render_engine->arm_command(command);
142 void PlaybackEngine::start_render_engine()
145 render_engine->start_command();
148 void PlaybackEngine::wait_render_engine()
150 if( command->realtime && render_engine ) {
151 render_engine->join();
155 void PlaybackEngine::create_cache()
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);
164 void PlaybackEngine::perform_change()
166 switch( command->change_type ) {
170 create_render_engine();
173 render_engine->get_edl()->synchronize_params(command->get_edl());
179 void PlaybackEngine::sync_parameters(EDL *edl)
181 // TODO: lock out render engine from keyframe deletions
182 command->get_edl()->synchronize_params(edl);
184 render_engine->get_edl()->synchronize_params(edl);
187 void PlaybackEngine::interrupt_playback(int wait_tracking)
189 renderengine_lock->lock("PlaybackEngine::interrupt_playback");
191 render_engine->interrupt_playback();
192 renderengine_lock->unlock();
195 pause_lock->unlock();
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();
204 // Return 1 if levels exist
205 int PlaybackEngine::get_output_levels(double *levels, long position)
208 if( render_engine && render_engine->do_audio ) {
209 render_engine->get_output_levels(levels, position);
216 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
219 if( render_engine && render_engine->do_audio ) {
220 render_engine->get_module_levels(module_levels, position);
226 int PlaybackEngine::brender_available(long position)
231 void PlaybackEngine::init_cursor(int active)
235 void PlaybackEngine::init_meters()
239 void PlaybackEngine::stop_cursor()
244 void PlaybackEngine::init_tracking()
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);
253 void PlaybackEngine::stop_tracking()
257 tracking_done->unlock();
260 void PlaybackEngine::update_tracking(double position)
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();
270 double PlaybackEngine::get_tracking_position()
274 tracking_lock->lock("PlaybackEngine::get_tracking_position");
277 // Adjust for elapsed time since last update_tracking.
278 // But tracking timer isn't accurate until the first update_tracking
280 if(tracking_active == 2)
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));
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;
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;
302 loop_start = edl->local_session->loop_start;
303 loop_end = edl->local_session->loop_end;
306 double loop_size = loop_end - loop_start;
308 if( command->get_direction() == PLAY_FORWARD ) {
310 result = tracking_position +
311 command->get_speed() *
312 tracking_timer.get_difference() /
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;
323 result = tracking_position -
324 command->get_speed() *
325 tracking_timer.get_difference() /
328 // Compensate for loop
329 if( play_loop && loop_size > 0 ) {
330 while( result < loop_start ) result += loop_size;
337 result = tracking_position;
339 tracking_lock->unlock();
340 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
347 void PlaybackEngine::update_transport(int command, int paused)
349 // mwindow->gui->lock_window();
350 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
351 // mwindow->gui->unlock_window();
354 void PlaybackEngine::run()
356 start_lock->unlock();
359 // Wait for current command to finish
360 output_lock->lock("PlaybackEngine::run");
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;
371 interrupt_playback(0);
372 wait_render_engine();
374 switch( command->command ) {
375 // Parameter change only
382 pause_lock->lock("PlaybackEngine::run");
394 // Dispatch the command
395 start_render_engine();
398 case SINGLE_FRAME_FWD:
399 case SINGLE_FRAME_REWIND:
406 // Start tracking after arming so the tracking position doesn't change.
407 // The tracking for a single frame command occurs during PAUSE
410 // Dispatch the command
411 start_render_engine();
414 //printf("PlaybackEngine::run 100\n");
419 void PlaybackEngine::stop_playback(int wait_tracking)
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();
427 renderengine_lock->unlock();
431 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
433 //printf("PlaybackEngine::send_command 1 %d\n", command);
434 // Stop requires transferring the output buffer to a refresh buffer.
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;
446 case FAST_REWIND: // Commands that play back
449 case SINGLE_FRAME_REWIND:
450 case SINGLE_FRAME_FWD:
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; }
462 // Resume or change direction
463 switch( curr_command ) {
466 next_command->resume = 1;
470 case SINGLE_FRAME_FWD:
471 case SINGLE_FRAME_REWIND:
474 next_command->realtime = 1;
475 // Start from scratch
476 transport_command(command, CHANGE_NONE, edl, use_inout);
481 // Commands that stop
490 transport_stop(wait_tracking);
494 int PlaybackEngine::put_command(TransportCommand *command, int reset)
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
501 sent_command->change_type |= prev_change_type;
503 if( reset ) command->reset();
504 output_lock->unlock();
505 input_lock->unlock();
509 int PlaybackEngine::transport_stop(int wait_tracking)
511 put_command(stop_command, 0);
512 if( wait_tracking ) {
513 tracking_done->lock("PlaybackEngine::transport_stop");
514 tracking_done->unlock();
516 //printf("send: %d (STOP) 0\n", STOP);
520 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
522 next_command->command = command;
523 next_command->change_type |= change_type;
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 ) {
531 next_command->get_edl()->copy_all(new_edl);
534 next_command->get_edl()->synchronize_params(new_edl);
537 next_command->set_playback_range(new_edl, use_inout,
538 preferences->forward_render_displacement);
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",
545 //printf("send= %d (%s) %d\n", sent_command->command,
546 // types[sent_command->command], sent_command->locked);
550 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
552 int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
553 next_command->realtime = 1;
554 transport_command(command, change_type, edl);