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();
89 audio_cache->remove_user();
91 video_cache->remove_user();
96 delete renderengine_lock;
105 void PlaybackEngine::create_objects()
107 preferences = new Preferences;
108 preferences->copy_from(mwindow->preferences);
112 start_lock->lock("PlaybackEngine::create_objects");
115 ChannelDB* PlaybackEngine::get_channeldb()
117 PlaybackConfig *config = command->get_edl()->session->playback_config;
118 switch(config->vconfig->driver)
120 case VIDEO4LINUX2JPEG:
121 return mwindow->channeldb_v4l2jpeg;
126 int PlaybackEngine::create_render_engine()
128 // Fix playback configurations
129 delete_render_engine();
130 render_engine = new RenderEngine(this, preferences, output, 0);
131 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
135 void PlaybackEngine::delete_render_engine()
137 renderengine_lock->lock("PlaybackEngine::delete_render_engine");
138 if( render_engine ) {
139 render_engine->interrupt_playback();
140 render_engine->wait_done();
141 delete render_engine; render_engine = 0;
143 renderengine_lock->unlock();
146 void PlaybackEngine::arm_render_engine()
149 render_engine->arm_command(command);
152 void PlaybackEngine::start_render_engine()
155 render_engine->start_command();
158 void PlaybackEngine::wait_render_engine()
160 if( command->realtime && render_engine ) {
161 render_engine->join();
165 void PlaybackEngine::create_cache()
168 audio_cache->remove_user();
170 video_cache->remove_user();
171 audio_cache = new CICache(preferences);
172 video_cache = new CICache(preferences);
176 void PlaybackEngine::perform_change()
178 switch( command->change_type ) {
182 create_render_engine();
185 render_engine->get_edl()->synchronize_params(command->get_edl());
191 void PlaybackEngine::sync_parameters(EDL *edl)
193 // TODO: lock out render engine from keyframe deletions
194 command->get_edl()->synchronize_params(edl);
196 render_engine->get_edl()->synchronize_params(edl);
199 void PlaybackEngine::interrupt_playback(int wait_tracking)
201 renderengine_lock->lock("PlaybackEngine::interrupt_playback");
203 render_engine->interrupt_playback();
204 renderengine_lock->unlock();
207 pause_lock->unlock();
209 // Wait for tracking to finish if it is running
210 if( wait_tracking ) {
211 tracking_done->lock("PlaybackEngine::interrupt_playback");
212 tracking_done->unlock();
216 // Return 1 if levels exist
217 int PlaybackEngine::get_output_levels(double *levels, long position)
220 if( render_engine && render_engine->do_audio ) {
221 render_engine->get_output_levels(levels, position);
228 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
231 if( render_engine && render_engine->do_audio ) {
232 render_engine->get_module_levels(module_levels, position);
238 int PlaybackEngine::brender_available(long position)
243 void PlaybackEngine::init_cursor(int active)
247 void PlaybackEngine::init_meters()
251 void PlaybackEngine::stop_cursor()
256 void PlaybackEngine::init_tracking()
258 tracking_active = !command->single_frame() ? 1 : 0;
259 tracking_position = command->playbackstart;
260 tracking_done->lock("PlaybackEngine::init_tracking");
261 init_cursor(tracking_active);
265 void PlaybackEngine::stop_tracking(double position)
267 tracking_position = position;
270 tracking_done->unlock();
273 void PlaybackEngine::update_tracking(double position)
275 tracking_lock->lock("PlaybackEngine::update_tracking");
276 tracking_position = position;
277 // Signal that the timer is accurate.
278 if(tracking_active) tracking_active = 2;
279 tracking_timer.update();
280 tracking_lock->unlock();
283 double PlaybackEngine::get_tracking_position()
287 tracking_lock->lock("PlaybackEngine::get_tracking_position");
290 // Adjust for elapsed time since last update_tracking.
291 // But tracking timer isn't accurate until the first update_tracking
293 if(tracking_active == 2)
295 //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));
298 // Don't interpolate when every frame is played.
299 if( command->get_edl()->session->video_every_frame &&
300 render_engine && render_engine->do_video ) {
301 result = tracking_position;
306 double loop_start, loop_end;
307 int play_loop = command->loop_play ? 1 : 0;
308 EDL *edl = command->get_edl();
309 int loop_playback = edl->local_session->loop_playback ? 1 : 0;
310 if( play_loop || !loop_playback ) {
311 loop_start = command->start_position;
312 loop_end = command->end_position;
315 loop_start = edl->local_session->loop_start;
316 loop_end = edl->local_session->loop_end;
319 double loop_size = loop_end - loop_start;
321 if( command->get_direction() == PLAY_FORWARD ) {
323 result = tracking_position +
324 command->get_speed() *
325 tracking_timer.get_difference() /
328 // Compensate for loop
329 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
330 if( play_loop && loop_size > 0 ) {
331 while( result > loop_end ) result -= loop_size;
336 result = tracking_position -
337 command->get_speed() *
338 tracking_timer.get_difference() /
341 // Compensate for loop
342 if( play_loop && loop_size > 0 ) {
343 while( result < loop_start ) result += loop_size;
350 result = tracking_position;
352 tracking_lock->unlock();
353 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
360 void PlaybackEngine::update_transport(int command, int paused)
362 // mwindow->gui->lock_window();
363 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
364 // mwindow->gui->unlock_window();
367 void PlaybackEngine::run()
369 start_lock->unlock();
372 // Wait for current command to finish
373 output_lock->lock("PlaybackEngine::run");
375 // Read the new command
376 input_lock->lock("PlaybackEngine::run");
377 command->copy_from(sent_command);
378 //printf("sent command=%d\n", sent_command->command);
379 int active = this->send_active;
380 this->send_active = 0;
381 input_lock->unlock();
382 if( !active ) continue;
384 interrupt_playback(0);
385 wait_render_engine();
387 switch( command->command ) {
388 // Parameter change only
395 pause_lock->lock("PlaybackEngine::run");
407 // Dispatch the command
408 start_render_engine();
418 // Start tracking after arming so the tracking position doesn't change.
419 // The tracking for a single frame command occurs during PAUSE
422 // Dispatch the command
423 start_render_engine();
426 //printf("PlaybackEngine::run 100\n");
430 void PlaybackEngine::clear_borders()
432 EDL *edl = command->get_edl();
433 PlaybackConfig *config = edl->session->playback_config;
434 if( config->vconfig->driver == PLAYBACK_X11_GL ) {
435 if( render_engine && render_engine->video ) {
436 VDeviceBase *vdriver = render_engine->video->get_output_base();
437 ((VDeviceX11*)vdriver)->clear_output();
441 BC_WindowBase *window = output->get_canvas();
442 if( !window ) return;
443 window->lock_window("PlaybackEngine::clear_output");
444 output->clear_borders(edl);
445 window->unlock_window();
448 void PlaybackEngine::stop_playback(int wait_tracking)
450 transport_stop(wait_tracking);
451 renderengine_lock->lock("PlaybackEngine::stop_playback");
452 if( render_engine ) {
453 render_engine->interrupt_playback();
454 render_engine->wait_done();
456 renderengine_lock->unlock();
459 int PlaybackEngine::get_direction()
461 int curr_command = is_playing_back ? this->command->command : STOP;
462 return TransportCommand::get_direction(curr_command);
465 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
467 //printf("PlaybackEngine::send_command 1 %d\n", command);
468 // Stop requires transferring the output buffer to a refresh buffer.
469 int curr_command = is_playing_back ? this->command->command : STOP;
470 int curr_single_frame = TransportCommand::single_frame(curr_command);
471 int curr_audio = this->command->toggle_audio ?
472 !curr_single_frame : curr_single_frame;
473 int single_frame = TransportCommand::single_frame(command);
474 int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
475 float next_speed = next_command->speed;
479 transport_stop(wait_tracking);
481 case FAST_REWIND: // Commands that play back
484 case SINGLE_FRAME_REWIND:
485 case SINGLE_FRAME_FWD:
491 // run shuttle as no prev command
492 if( next_speed ) curr_command = COMMAND_NONE;
493 // Same direction pressed twice, not shuttle, and no change in audio state, Stop
494 if( curr_command == command && !curr_single_frame &&
495 curr_audio == next_audio ) {
496 transport_stop(wait_tracking);
499 // Resume or change direction
500 switch( curr_command ) {
505 case SINGLE_FRAME_FWD:
506 case SINGLE_FRAME_REWIND:
513 next_command->resume = 1;
516 next_command->realtime = 1;
517 transport_command(command, CHANGE_NONE, edl, use_inout);
522 next_command->realtime = 1;
523 transport_command(command, CHANGE_NONE, edl, use_inout);
524 stop_tracking(this->command->playbackstart);
529 int PlaybackEngine::put_command(TransportCommand *command, int reset)
531 input_lock->lock("PlaybackEngine::put_command");
532 int prev_change_type = sent_command->change_type;
533 sent_command->copy_from(command);
534 // run only last command, sum change type
536 sent_command->change_type |= prev_change_type;
538 if( reset ) command->reset();
539 output_lock->unlock();
540 input_lock->unlock();
544 int PlaybackEngine::transport_stop(int wait_tracking)
546 put_command(stop_command, 0);
547 if( wait_tracking ) {
548 tracking_done->lock("PlaybackEngine::transport_stop");
549 tracking_done->unlock();
551 //printf("send: %d (STOP) 0\n", STOP);
555 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
557 next_command->command = command;
558 next_command->change_type |= change_type;
560 // Just change the EDL if the change requires it because renderengine
561 // structures won't point to the new EDL otherwise and because copying the
562 // EDL for every cursor movement is slow.
563 if( change_type == CHANGE_EDL || change_type == CHANGE_ALL )
564 next_command->get_edl()->copy_all(new_edl);
565 else if( change_type == CHANGE_PARAMS )
566 next_command->get_edl()->synchronize_params(new_edl);
567 next_command->set_playback_range(new_edl, use_inout,
568 preferences->forward_render_displacement);
570 put_command(next_command, 1);
571 //static const char *types[] = { "NONE",
572 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
573 // "STOP", "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
575 //printf("send= %d (%s) %d\n", sent_command->command,
576 // types[sent_command->command], sent_command->locked);
580 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
582 int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
583 next_command->realtime = 1;
584 transport_command(command, change_type, edl);