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 cache_lock = new Mutex("PlaybackEngine::cache_lock");
75 input_lock = new Condition(1, "PlaybackEngine::input_lock");
76 output_lock = new Condition(0, "PlaybackEngine::output_lock", 1);
82 PlaybackEngine::~PlaybackEngine()
85 output_lock->unlock();
88 delete_render_engine();
90 audio_cache->remove_user();
92 video_cache->remove_user();
98 delete renderengine_lock;
107 void PlaybackEngine::create_objects()
109 preferences = new Preferences;
110 preferences->copy_from(mwindow->preferences);
114 start_lock->lock("PlaybackEngine::create_objects");
117 ChannelDB* PlaybackEngine::get_channeldb()
119 PlaybackConfig *config = command->get_edl()->session->playback_config;
120 switch(config->vconfig->driver)
122 case VIDEO4LINUX2JPEG:
123 return mwindow->channeldb_v4l2jpeg;
128 int PlaybackEngine::create_render_engine()
130 // Fix playback configurations
131 delete_render_engine();
132 render_engine = new RenderEngine(this, preferences, output, 0);
133 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
137 void PlaybackEngine::delete_render_engine()
139 renderengine_lock->lock("PlaybackEngine::delete_render_engine");
140 if( render_engine ) {
141 render_engine->interrupt_playback();
142 render_engine->wait_done();
143 delete render_engine; render_engine = 0;
145 renderengine_lock->unlock();
148 void PlaybackEngine::arm_render_engine()
151 render_engine->arm_command(command);
154 void PlaybackEngine::start_render_engine()
157 render_engine->start_command();
160 void PlaybackEngine::wait_render_engine()
162 if( command->realtime && render_engine ) {
163 render_engine->join();
167 void PlaybackEngine::create_cache()
169 cache_lock->lock("PlaybackEngine::create_cache");
171 audio_cache->remove_user();
173 video_cache->remove_user();
174 audio_cache = new CICache(preferences);
175 video_cache = new CICache(preferences);
176 cache_lock->unlock();
180 void PlaybackEngine::perform_change()
182 switch( command->change_type ) {
186 create_render_engine();
189 render_engine->get_edl()->synchronize_params(command->get_edl());
195 void PlaybackEngine::sync_parameters(EDL *edl)
197 // TODO: lock out render engine from keyframe deletions
198 command->get_edl()->synchronize_params(edl);
200 render_engine->get_edl()->synchronize_params(edl);
203 void PlaybackEngine::interrupt_playback(int wait_tracking)
205 renderengine_lock->lock("PlaybackEngine::interrupt_playback");
207 render_engine->interrupt_playback();
208 renderengine_lock->unlock();
211 pause_lock->unlock();
213 // Wait for tracking to finish if it is running
214 if( wait_tracking ) {
215 tracking_done->lock("PlaybackEngine::interrupt_playback");
216 tracking_done->unlock();
220 // Return 1 if levels exist
221 int PlaybackEngine::get_output_levels(double *levels, long position)
224 if( render_engine && render_engine->do_audio ) {
225 render_engine->get_output_levels(levels, position);
232 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
235 if( render_engine && render_engine->do_audio ) {
236 render_engine->get_module_levels(module_levels, position);
242 int PlaybackEngine::brender_available(long position)
247 void PlaybackEngine::init_cursor(int active)
251 void PlaybackEngine::init_meters()
255 void PlaybackEngine::stop_cursor()
260 void PlaybackEngine::init_tracking()
262 tracking_active = !command->single_frame() ? 1 : 0;
263 tracking_position = command->playbackstart;
264 tracking_done->lock("PlaybackEngine::init_tracking");
265 init_cursor(tracking_active);
269 void PlaybackEngine::stop_tracking(double position)
271 tracking_position = position;
274 tracking_done->unlock();
277 void PlaybackEngine::update_tracking(double position)
279 tracking_lock->lock("PlaybackEngine::update_tracking");
280 tracking_position = position;
281 // Signal that the timer is accurate.
282 if(tracking_active) tracking_active = 2;
283 tracking_timer.update();
284 tracking_lock->unlock();
287 double PlaybackEngine::get_tracking_position()
291 tracking_lock->lock("PlaybackEngine::get_tracking_position");
294 // Adjust for elapsed time since last update_tracking.
295 // But tracking timer isn't accurate until the first update_tracking
297 if(tracking_active == 2)
299 //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));
302 // Don't interpolate when every frame is played.
303 if( command->get_edl()->session->video_every_frame &&
304 render_engine && render_engine->do_video ) {
305 result = tracking_position;
310 double loop_start, loop_end;
311 int play_loop = command->loop_play ? 1 : 0;
312 EDL *edl = command->get_edl();
313 int loop_playback = edl->local_session->loop_playback ? 1 : 0;
314 if( play_loop || !loop_playback ) {
315 loop_start = command->start_position;
316 loop_end = command->end_position;
319 loop_start = edl->local_session->loop_start;
320 loop_end = edl->local_session->loop_end;
323 double loop_size = loop_end - loop_start;
325 if( command->get_direction() == PLAY_FORWARD ) {
327 result = tracking_position +
328 command->get_speed() *
329 tracking_timer.get_difference() /
332 // Compensate for loop
333 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
334 if( play_loop && loop_size > 0 ) {
335 while( result > loop_end ) result -= loop_size;
340 result = tracking_position -
341 command->get_speed() *
342 tracking_timer.get_difference() /
345 // Compensate for loop
346 if( play_loop && loop_size > 0 ) {
347 while( result < loop_start ) result += loop_size;
354 result = tracking_position;
356 tracking_lock->unlock();
357 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
364 void PlaybackEngine::update_transport(int command, int paused)
366 // mwindow->gui->lock_window();
367 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
368 // mwindow->gui->unlock_window();
371 void PlaybackEngine::run()
373 start_lock->unlock();
376 // Wait for current command to finish
377 output_lock->lock("PlaybackEngine::run");
379 // Read the new command
380 input_lock->lock("PlaybackEngine::run");
381 command->copy_from(sent_command);
382 //printf("sent command=%d\n", sent_command->command);
383 int active = this->send_active;
384 this->send_active = 0;
385 input_lock->unlock();
386 if( !active ) continue;
388 interrupt_playback(0);
389 wait_render_engine();
391 switch( command->command ) {
392 // Parameter change only
399 pause_lock->lock("PlaybackEngine::run");
411 // Dispatch the command
412 start_render_engine();
422 // Start tracking after arming so the tracking position doesn't change.
423 // The tracking for a single frame command occurs during PAUSE
426 // Dispatch the command
427 start_render_engine();
430 //printf("PlaybackEngine::run 100\n");
434 void PlaybackEngine::clear_borders()
436 EDL *edl = command->get_edl();
437 PlaybackConfig *config = edl->session->playback_config;
438 if( config->vconfig->driver == PLAYBACK_X11_GL ) {
439 if( render_engine && render_engine->video ) {
440 VDeviceBase *vdriver = render_engine->video->get_output_base();
441 ((VDeviceX11*)vdriver)->clear_output();
445 BC_WindowBase *window = output->get_canvas();
446 if( !window ) return;
447 window->lock_window("PlaybackEngine::clear_output");
448 output->clear_borders(edl);
449 window->unlock_window();
452 void PlaybackEngine::stop_playback(int wait_tracking)
454 transport_stop(wait_tracking);
455 renderengine_lock->lock("PlaybackEngine::stop_playback");
456 if( render_engine ) {
457 render_engine->interrupt_playback();
458 render_engine->wait_done();
460 renderengine_lock->unlock();
463 int PlaybackEngine::get_direction()
465 int curr_command = is_playing_back ? this->command->command : STOP;
466 return TransportCommand::get_direction(curr_command);
469 void PlaybackEngine::update_preferences(Preferences *prefs)
471 preferences->copy_from(prefs);
472 create_render_engine();
475 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
477 //printf("PlaybackEngine::send_command 1 %d\n", command);
478 // Stop requires transferring the output buffer to a refresh buffer.
479 int curr_command = is_playing_back ? this->command->command : STOP;
480 int curr_single_frame = TransportCommand::single_frame(curr_command);
481 int curr_audio = this->command->toggle_audio ?
482 !curr_single_frame : curr_single_frame;
483 int single_frame = TransportCommand::single_frame(command);
484 int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
485 float next_speed = next_command->speed;
489 transport_stop(wait_tracking);
491 case FAST_REWIND: // Commands that play back
494 case SINGLE_FRAME_REWIND:
495 case SINGLE_FRAME_FWD:
501 // run shuttle as no prev command
502 if( next_speed ) curr_command = COMMAND_NONE;
503 // Same direction pressed twice, not shuttle, and no change in audio state, Stop
504 if( curr_command == command && !curr_single_frame &&
505 curr_audio == next_audio ) {
506 transport_stop(wait_tracking);
509 // Resume or change direction
510 switch( curr_command ) {
515 case SINGLE_FRAME_FWD:
516 case SINGLE_FRAME_REWIND:
523 next_command->resume = 1;
526 next_command->realtime = 1;
527 transport_command(command, CHANGE_NONE, edl, use_inout);
532 next_command->realtime = 1;
533 transport_command(command, CHANGE_NONE, edl, use_inout);
534 stop_tracking(this->command->playbackstart);
539 int PlaybackEngine::put_command(TransportCommand *command, int reset)
541 input_lock->lock("PlaybackEngine::put_command");
542 int prev_change_type = sent_command->change_type;
543 sent_command->copy_from(command);
544 // run only last command, sum change type
546 sent_command->change_type |= prev_change_type;
548 if( reset ) command->reset();
549 output_lock->unlock();
550 input_lock->unlock();
554 int PlaybackEngine::transport_stop(int wait_tracking)
556 put_command(stop_command, 0);
557 if( wait_tracking ) {
558 tracking_done->lock("PlaybackEngine::transport_stop");
559 tracking_done->unlock();
561 //printf("send: %d (STOP) 0\n", STOP);
565 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
567 next_command->command = command;
568 next_command->change_type |= change_type;
570 // Just change the EDL if the change requires it because renderengine
571 // structures won't point to the new EDL otherwise and because copying the
572 // EDL for every cursor movement is slow.
573 if( change_type == CHANGE_EDL || change_type == CHANGE_ALL )
574 next_command->get_edl()->copy_all(new_edl);
575 else if( change_type == CHANGE_PARAMS )
576 next_command->get_edl()->synchronize_params(new_edl);
577 next_command->set_playback_range(new_edl, use_inout,
578 preferences->forward_render_displacement);
580 put_command(next_command, 1);
581 //static const char *types[] = { "NONE",
582 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
583 // "STOP", "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
585 //printf("send= %d (%s) %d\n", sent_command->command,
586 // types[sent_command->command], sent_command->locked);
590 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
592 int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
593 next_command->realtime = 1;
594 transport_command(command, change_type, edl);