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"
37 #include "playbackengine.h"
38 #include "playtransport.h"
39 #include "preferences.h"
40 #include "renderengine.h"
41 #include "mainsession.h"
42 #include "trackcanvas.h"
43 #include "transportque.h"
44 #include "videodevice.h"
45 #include "vdevicex11.h"
49 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
52 this->mwindow = mwindow;
53 this->output = output;
55 tracking_position = 0;
59 command = new TransportCommand();
60 command->command = STOP;
61 next_command = new TransportCommand();
62 next_command->change_type = CHANGE_ALL;
63 stop_command = new TransportCommand();
64 stop_command->command = STOP;
65 stop_command->realtime = 1;
66 sent_command = new TransportCommand();
67 sent_command->command = -1;
69 tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
70 renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
71 tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
72 pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
73 start_lock = new Condition(0, "PlaybackEngine::start_lock");
74 input_lock = new Condition(1, "PlaybackEngine::input_lock");
75 output_lock = new Condition(0, "PlaybackEngine::output_lock", 1);
81 PlaybackEngine::~PlaybackEngine()
84 output_lock->unlock();
87 delete_render_engine();
94 delete renderengine_lock;
103 void PlaybackEngine::create_objects()
105 preferences = new Preferences;
106 preferences->copy_from(mwindow->preferences);
110 start_lock->lock("PlaybackEngine::create_objects");
113 ChannelDB* PlaybackEngine::get_channeldb()
115 PlaybackConfig *config = command->get_edl()->session->playback_config;
116 switch(config->vconfig->driver)
118 case VIDEO4LINUX2JPEG:
119 return mwindow->channeldb_v4l2jpeg;
124 int PlaybackEngine::create_render_engine()
126 // Fix playback configurations
127 delete_render_engine();
128 render_engine = new RenderEngine(this, preferences, output, 0);
129 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
133 void PlaybackEngine::delete_render_engine()
135 renderengine_lock->lock("PlaybackEngine::delete_render_engine");
136 if( render_engine ) {
137 render_engine->interrupt_playback();
138 render_engine->wait_done();
139 delete render_engine; render_engine = 0;
141 renderengine_lock->unlock();
144 void PlaybackEngine::arm_render_engine()
147 render_engine->arm_command(command);
150 void PlaybackEngine::start_render_engine()
153 render_engine->start_command();
156 void PlaybackEngine::wait_render_engine()
158 if( command->realtime && render_engine ) {
159 render_engine->join();
163 void PlaybackEngine::create_cache()
165 if(audio_cache) { delete audio_cache; audio_cache = 0; }
166 if(video_cache) { delete video_cache; video_cache = 0; }
167 if(!audio_cache) audio_cache = new CICache(preferences);
168 if(!video_cache) video_cache = new CICache(preferences);
172 void PlaybackEngine::perform_change()
174 switch( command->change_type ) {
178 create_render_engine();
181 render_engine->get_edl()->synchronize_params(command->get_edl());
187 void PlaybackEngine::sync_parameters(EDL *edl)
189 // TODO: lock out render engine from keyframe deletions
190 command->get_edl()->synchronize_params(edl);
192 render_engine->get_edl()->synchronize_params(edl);
195 void PlaybackEngine::interrupt_playback(int wait_tracking)
197 renderengine_lock->lock("PlaybackEngine::interrupt_playback");
199 render_engine->interrupt_playback();
200 renderengine_lock->unlock();
203 pause_lock->unlock();
205 // Wait for tracking to finish if it is running
206 if( wait_tracking ) {
207 tracking_done->lock("PlaybackEngine::interrupt_playback");
208 tracking_done->unlock();
212 // Return 1 if levels exist
213 int PlaybackEngine::get_output_levels(double *levels, long position)
216 if( render_engine && render_engine->do_audio ) {
217 render_engine->get_output_levels(levels, position);
224 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
227 if( render_engine && render_engine->do_audio ) {
228 render_engine->get_module_levels(module_levels, position);
234 int PlaybackEngine::brender_available(long position)
239 void PlaybackEngine::init_cursor(int active)
243 void PlaybackEngine::init_meters()
247 void PlaybackEngine::stop_cursor()
252 void PlaybackEngine::init_tracking()
254 tracking_active = !command->single_frame() ? 1 : 0;
255 tracking_position = command->playbackstart;
256 tracking_done->lock("PlaybackEngine::init_tracking");
257 init_cursor(tracking_active);
261 void PlaybackEngine::stop_tracking(double position)
263 tracking_position = position;
266 tracking_done->unlock();
269 void PlaybackEngine::update_tracking(double position)
271 tracking_lock->lock("PlaybackEngine::update_tracking");
272 tracking_position = position;
273 // Signal that the timer is accurate.
274 if(tracking_active) tracking_active = 2;
275 tracking_timer.update();
276 tracking_lock->unlock();
279 double PlaybackEngine::get_tracking_position()
283 tracking_lock->lock("PlaybackEngine::get_tracking_position");
286 // Adjust for elapsed time since last update_tracking.
287 // But tracking timer isn't accurate until the first update_tracking
289 if(tracking_active == 2)
291 //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));
294 // Don't interpolate when every frame is played.
295 if( command->get_edl()->session->video_every_frame &&
296 render_engine && render_engine->do_video ) {
297 result = tracking_position;
302 double loop_start, loop_end;
303 int play_loop = command->loop_play ? 1 : 0;
304 EDL *edl = command->get_edl();
305 int loop_playback = edl->local_session->loop_playback ? 1 : 0;
306 if( play_loop || !loop_playback ) {
307 loop_start = command->start_position;
308 loop_end = command->end_position;
311 loop_start = edl->local_session->loop_start;
312 loop_end = edl->local_session->loop_end;
315 double loop_size = loop_end - loop_start;
317 if( command->get_direction() == PLAY_FORWARD ) {
319 result = tracking_position +
320 command->get_speed() *
321 tracking_timer.get_difference() /
324 // Compensate for loop
325 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
326 if( play_loop && loop_size > 0 ) {
327 while( result > loop_end ) result -= loop_size;
332 result = tracking_position -
333 command->get_speed() *
334 tracking_timer.get_difference() /
337 // Compensate for loop
338 if( play_loop && loop_size > 0 ) {
339 while( result < loop_start ) result += loop_size;
346 result = tracking_position;
348 tracking_lock->unlock();
349 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
356 void PlaybackEngine::update_transport(int command, int paused)
358 // mwindow->gui->lock_window();
359 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
360 // mwindow->gui->unlock_window();
363 void PlaybackEngine::run()
365 start_lock->unlock();
368 // Wait for current command to finish
369 output_lock->lock("PlaybackEngine::run");
371 // Read the new command
372 input_lock->lock("PlaybackEngine::run");
373 command->copy_from(sent_command);
374 //printf("sent command=%d\n", sent_command->command);
375 int active = this->send_active;
376 this->send_active = 0;
377 input_lock->unlock();
378 if( !active ) continue;
380 interrupt_playback(0);
381 wait_render_engine();
383 switch( command->command ) {
384 // Parameter change only
391 pause_lock->lock("PlaybackEngine::run");
403 // Dispatch the command
404 start_render_engine();
414 // Start tracking after arming so the tracking position doesn't change.
415 // The tracking for a single frame command occurs during PAUSE
418 // Dispatch the command
419 start_render_engine();
422 //printf("PlaybackEngine::run 100\n");
426 void PlaybackEngine::clear_borders()
428 EDL *edl = command->get_edl();
429 PlaybackConfig *config = edl->session->playback_config;
430 if( config->vconfig->driver == PLAYBACK_X11_GL ) {
431 if( render_engine && render_engine->video ) {
432 VDeviceBase *vdriver = render_engine->video->get_output_base();
433 ((VDeviceX11*)vdriver)->clear_output();
437 BC_WindowBase *window = output->get_canvas();
438 if( !window ) return;
439 window->lock_window("PlaybackEngine::clear_output");
440 output->clear_borders(edl);
441 window->unlock_window();
444 void PlaybackEngine::stop_playback(int wait_tracking)
446 transport_stop(wait_tracking);
447 renderengine_lock->lock("PlaybackEngine::stop_playback");
448 if( render_engine ) {
449 render_engine->interrupt_playback();
450 render_engine->wait_done();
452 renderengine_lock->unlock();
455 int PlaybackEngine::get_direction()
457 int curr_command = is_playing_back ? this->command->command : STOP;
458 return TransportCommand::get_direction(curr_command);
461 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
463 //printf("PlaybackEngine::send_command 1 %d\n", command);
464 // Stop requires transferring the output buffer to a refresh buffer.
465 int curr_command = is_playing_back ? this->command->command : STOP;
466 int curr_single_frame = TransportCommand::single_frame(curr_command);
467 int curr_audio = this->command->toggle_audio ?
468 !curr_single_frame : curr_single_frame;
469 int single_frame = TransportCommand::single_frame(command);
470 int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
471 float next_speed = next_command->speed;
475 transport_stop(wait_tracking);
477 case FAST_REWIND: // Commands that play back
480 case SINGLE_FRAME_REWIND:
481 case SINGLE_FRAME_FWD:
487 // run shuttle as no prev command
488 if( next_speed ) curr_command = COMMAND_NONE;
489 // Same direction pressed twice, not shuttle, and no change in audio state, Stop
490 if( curr_command == command && !curr_single_frame &&
491 curr_audio == next_audio ) {
492 transport_stop(wait_tracking);
495 // Resume or change direction
496 switch( curr_command ) {
501 case SINGLE_FRAME_FWD:
502 case SINGLE_FRAME_REWIND:
509 next_command->resume = 1;
512 next_command->realtime = 1;
513 transport_command(command, CHANGE_NONE, edl, use_inout);
518 next_command->realtime = 1;
519 transport_command(command, CHANGE_NONE, edl, use_inout);
520 stop_tracking(this->command->playbackstart);
525 int PlaybackEngine::put_command(TransportCommand *command, int reset)
527 input_lock->lock("PlaybackEngine::put_command");
528 int prev_change_type = sent_command->change_type;
529 sent_command->copy_from(command);
530 // run only last command, sum change type
532 sent_command->change_type |= prev_change_type;
534 if( reset ) command->reset();
535 output_lock->unlock();
536 input_lock->unlock();
540 int PlaybackEngine::transport_stop(int wait_tracking)
542 put_command(stop_command, 0);
543 if( wait_tracking ) {
544 tracking_done->lock("PlaybackEngine::transport_stop");
545 tracking_done->unlock();
547 //printf("send: %d (STOP) 0\n", STOP);
551 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
553 next_command->command = command;
554 next_command->change_type |= change_type;
556 // Just change the EDL if the change requires it because renderengine
557 // structures won't point to the new EDL otherwise and because copying the
558 // EDL for every cursor movement is slow.
559 if( change_type == CHANGE_EDL || change_type == CHANGE_ALL )
560 next_command->get_edl()->copy_all(new_edl);
561 else if( change_type == CHANGE_PARAMS )
562 next_command->get_edl()->synchronize_params(new_edl);
563 next_command->set_playback_range(new_edl, use_inout,
564 preferences->forward_render_displacement);
566 put_command(next_command, 1);
567 //static const char *types[] = { "NONE",
568 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
569 // "STOP", "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
571 //printf("send= %d (%s) %d\n", sent_command->command,
572 // types[sent_command->command], sent_command->locked);
576 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
578 int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
579 next_command->realtime = 1;
580 transport_command(command, change_type, edl);