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(mwindow->preferences);
60 command->command = STOP;
61 next_command = new TransportCommand(mwindow->preferences);
62 next_command->change_type = CHANGE_ALL;
63 stop_command = new TransportCommand(mwindow->preferences);
64 stop_command->command = STOP;
65 stop_command->realtime = 1;
66 sent_command = new TransportCommand(mwindow->preferences);
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();
87 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()
150 renderengine_lock->lock("PlaybackEngine::arm_render_engine");
152 render_engine->arm_command(command);
153 renderengine_lock->unlock();
156 void PlaybackEngine::start_render_engine()
158 renderengine_lock->lock("PlaybackEngine::start_render_engine");
160 render_engine->start_command();
161 renderengine_lock->unlock();
164 void PlaybackEngine::wait_render_engine()
166 if( command->realtime && render_engine ) {
167 render_engine->join();
171 void PlaybackEngine::create_cache()
173 cache_lock->lock("PlaybackEngine::create_cache");
175 audio_cache->remove_user();
177 video_cache->remove_user();
178 audio_cache = new CICache(preferences);
179 video_cache = new CICache(preferences);
180 cache_lock->unlock();
184 void PlaybackEngine::perform_change()
186 switch( command->change_type ) {
190 create_render_engine();
192 case CHANGE_PARAMS: {
193 renderengine_lock->lock("PlaybackEngine::perform_change");
194 EDL *edl = render_engine ? render_engine->get_edl() : 0;
195 if( edl ) edl->add_user();
196 renderengine_lock->unlock();
198 edl->synchronize_params(command->get_edl());
206 void PlaybackEngine::sync_parameters(EDL *edl)
208 // TODO: lock out render engine from keyframe deletions
209 command->get_edl()->synchronize_params(edl);
211 render_engine->get_edl()->synchronize_params(edl);
214 void PlaybackEngine::interrupt_playback(int wait_tracking)
216 renderengine_lock->lock("PlaybackEngine::interrupt_playback");
218 render_engine->interrupt_playback();
219 renderengine_lock->unlock();
222 pause_lock->unlock();
224 // Wait for tracking to finish if it is running
225 if( wait_tracking ) {
226 tracking_done->lock("PlaybackEngine::interrupt_playback");
227 tracking_done->unlock();
231 // Return 1 if levels exist
232 int PlaybackEngine::get_output_levels(double *levels, long position)
235 if( render_engine && render_engine->do_audio ) {
236 render_engine->get_output_levels(levels, position);
243 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
246 if( render_engine && render_engine->do_audio ) {
247 render_engine->get_module_levels(module_levels, position);
253 int PlaybackEngine::brender_available(long position)
258 void PlaybackEngine::init_cursor(int active)
262 void PlaybackEngine::init_meters()
266 void PlaybackEngine::stop_cursor()
271 void PlaybackEngine::init_tracking()
273 tracking_active = !command->single_frame() ? 1 : 0;
274 tracking_position = command->playbackstart;
275 tracking_done->lock("PlaybackEngine::init_tracking");
276 init_cursor(tracking_active);
280 void PlaybackEngine::stop_tracking(double position)
282 tracking_position = position;
285 tracking_done->unlock();
288 void PlaybackEngine::update_tracking(double position)
290 tracking_lock->lock("PlaybackEngine::update_tracking");
291 tracking_position = position;
292 // Signal that the timer is accurate.
293 if(tracking_active) tracking_active = 2;
294 tracking_timer.update();
295 tracking_lock->unlock();
298 double PlaybackEngine::get_tracking_position()
302 tracking_lock->lock("PlaybackEngine::get_tracking_position");
305 // Adjust for elapsed time since last update_tracking.
306 // But tracking timer isn't accurate until the first update_tracking
308 if(tracking_active == 2)
310 //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));
313 // Don't interpolate when every frame is played.
314 if( command->get_edl()->session->video_every_frame &&
315 render_engine && render_engine->do_video ) {
316 result = tracking_position;
321 double loop_start, loop_end;
322 int play_loop = command->loop_play ? 1 : 0;
323 EDL *edl = command->get_edl();
324 int loop_playback = edl->local_session->loop_playback ? 1 : 0;
325 if( play_loop || !loop_playback ) {
326 loop_start = command->start_position;
327 loop_end = command->end_position;
330 loop_start = edl->local_session->loop_start;
331 loop_end = edl->local_session->loop_end;
334 double loop_size = loop_end - loop_start;
336 if( command->get_direction() == PLAY_FORWARD ) {
338 result = tracking_position +
339 command->get_speed() *
340 tracking_timer.get_difference() /
343 // Compensate for loop
344 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
345 if( play_loop && loop_size > 0 ) {
346 while( result > loop_end ) result -= loop_size;
351 result = tracking_position -
352 command->get_speed() *
353 tracking_timer.get_difference() /
356 // Compensate for loop
357 if( play_loop && loop_size > 0 ) {
358 while( result < loop_start ) result += loop_size;
365 result = tracking_position;
367 tracking_lock->unlock();
368 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
375 void PlaybackEngine::update_transport(int command, int paused)
377 // mwindow->gui->lock_window();
378 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
379 // mwindow->gui->unlock_window();
382 void PlaybackEngine::run()
384 start_lock->unlock();
387 // Wait for current command to finish
388 output_lock->lock("PlaybackEngine::run");
390 // Read the new command
391 input_lock->lock("PlaybackEngine::run");
392 command->copy_from(sent_command);
393 //printf("sent command=%d\n", sent_command->command);
394 int active = this->send_active;
395 this->send_active = 0;
396 input_lock->unlock();
397 if( !active ) continue;
399 interrupt_playback(0);
400 wait_render_engine();
402 switch( command->command ) {
403 // Parameter change only
410 pause_lock->lock("PlaybackEngine::run");
422 // Dispatch the command
423 start_render_engine();
433 // Start tracking after arming so the tracking position doesn't change.
434 // The tracking for a single frame command occurs during PAUSE
437 // Dispatch the command
438 start_render_engine();
441 //printf("PlaybackEngine::run 100\n");
445 void PlaybackEngine::clear_borders()
447 EDL *edl = command->get_edl();
448 PlaybackConfig *config = edl->session->playback_config;
449 if( config->vconfig->driver == PLAYBACK_X11_GL ) {
450 if( render_engine && render_engine->video ) {
451 VDeviceBase *vdriver = render_engine->video->get_output_base();
452 ((VDeviceX11*)vdriver)->clear_output();
456 BC_WindowBase *window = output->get_canvas();
457 if( !window ) return;
458 window->lock_window("PlaybackEngine::clear_output");
459 output->clear_borders(edl);
460 window->unlock_window();
463 void PlaybackEngine::stop_playback(int wait_tracking)
465 transport_stop(wait_tracking);
466 renderengine_lock->lock("PlaybackEngine::stop_playback");
467 if( render_engine ) {
468 render_engine->interrupt_playback();
469 render_engine->wait_done();
471 renderengine_lock->unlock();
474 int PlaybackEngine::get_direction()
476 int curr_command = is_playing_back ? this->command->command : STOP;
477 return TransportCommand::get_direction(curr_command);
480 void PlaybackEngine::update_preferences(Preferences *prefs)
482 preferences->copy_from(prefs);
483 create_render_engine();
486 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
488 //printf("PlaybackEngine::send_command 1 %d\n", command);
489 // Stop requires transferring the output buffer to a refresh buffer.
490 int curr_command = is_playing_back ? this->command->command : STOP;
491 int curr_single_frame = TransportCommand::single_frame(curr_command);
492 int curr_audio = this->command->toggle_audio ?
493 !curr_single_frame : curr_single_frame;
494 int single_frame = TransportCommand::single_frame(command);
495 int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
496 float next_speed = next_command->speed;
500 transport_stop(wait_tracking);
502 case FAST_REWIND: // Commands that play back
505 case SINGLE_FRAME_REWIND:
506 case SINGLE_FRAME_FWD:
512 // run shuttle as no prev command
513 if( next_speed ) curr_command = COMMAND_NONE;
514 // Same direction pressed twice, not shuttle, and no change in audio state, Stop
515 if( curr_command == command && !curr_single_frame &&
516 curr_audio == next_audio ) {
517 transport_stop(wait_tracking);
520 // Resume or change direction
521 switch( curr_command ) {
526 case SINGLE_FRAME_FWD:
527 case SINGLE_FRAME_REWIND:
534 next_command->resume = 1;
537 next_command->realtime = 1;
538 transport_command(command, CHANGE_NONE, edl, use_inout);
543 next_command->realtime = 1;
544 transport_command(command, CHANGE_NONE, edl, use_inout);
545 stop_tracking(this->command->playbackstart);
550 int PlaybackEngine::put_command(TransportCommand *command, int reset)
552 input_lock->lock("PlaybackEngine::put_command");
553 int prev_change_type = sent_command->change_type;
554 sent_command->copy_from(command);
555 // run only last command, sum change type
557 sent_command->change_type |= prev_change_type;
559 if( reset ) command->reset();
560 output_lock->unlock();
561 input_lock->unlock();
565 int PlaybackEngine::transport_stop(int wait_tracking)
567 put_command(stop_command, 0);
568 if( wait_tracking ) {
569 tracking_done->lock("PlaybackEngine::transport_stop");
570 tracking_done->unlock();
572 //printf("send: %d (STOP) 0\n", STOP);
576 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
578 next_command->command = command;
579 next_command->change_type |= change_type;
581 // Just change the EDL if the change requires it because renderengine
582 // structures won't point to the new EDL otherwise and because copying the
583 // EDL for every cursor movement is slow.
584 if( change_type == CHANGE_EDL || change_type == CHANGE_ALL )
585 next_command->get_edl()->copy_all(new_edl);
586 else if( change_type == CHANGE_PARAMS )
587 next_command->get_edl()->synchronize_params(new_edl);
588 next_command->set_playback_range(new_edl, use_inout,
589 preferences->forward_render_displacement);
591 put_command(next_command, 1);
592 //static const char *types[] = { "NONE",
593 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
594 // "STOP", "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
596 //printf("send= %d (%s) %d\n", sent_command->command,
597 // types[sent_command->command], sent_command->locked);
601 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
603 int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
604 next_command->realtime = 1;
605 transport_command(command, change_type, edl);