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()
265 tracking_done->unlock();
268 void PlaybackEngine::update_tracking(double position)
270 tracking_lock->lock("PlaybackEngine::update_tracking");
271 tracking_position = position;
272 // Signal that the timer is accurate.
273 if(tracking_active) tracking_active = 2;
274 tracking_timer.update();
275 tracking_lock->unlock();
278 double PlaybackEngine::get_tracking_position()
282 tracking_lock->lock("PlaybackEngine::get_tracking_position");
285 // Adjust for elapsed time since last update_tracking.
286 // But tracking timer isn't accurate until the first update_tracking
288 if(tracking_active == 2)
290 //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));
293 // Don't interpolate when every frame is played.
294 if( command->get_edl()->session->video_every_frame &&
295 render_engine && render_engine->do_video ) {
296 result = tracking_position;
301 double loop_start, loop_end;
302 int play_loop = command->loop_play ? 1 : 0;
303 EDL *edl = command->get_edl();
304 int loop_playback = edl->local_session->loop_playback ? 1 : 0;
305 if( play_loop || !loop_playback ) {
306 loop_start = command->start_position;
307 loop_end = command->end_position;
310 loop_start = edl->local_session->loop_start;
311 loop_end = edl->local_session->loop_end;
314 double loop_size = loop_end - loop_start;
316 if( command->get_direction() == PLAY_FORWARD ) {
318 result = tracking_position +
319 command->get_speed() *
320 tracking_timer.get_difference() /
323 // Compensate for loop
324 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
325 if( play_loop && loop_size > 0 ) {
326 while( result > loop_end ) result -= loop_size;
331 result = tracking_position -
332 command->get_speed() *
333 tracking_timer.get_difference() /
336 // Compensate for loop
337 if( play_loop && loop_size > 0 ) {
338 while( result < loop_start ) result += loop_size;
345 result = tracking_position;
347 tracking_lock->unlock();
348 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
355 void PlaybackEngine::update_transport(int command, int paused)
357 // mwindow->gui->lock_window();
358 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
359 // mwindow->gui->unlock_window();
362 void PlaybackEngine::run()
364 start_lock->unlock();
367 // Wait for current command to finish
368 output_lock->lock("PlaybackEngine::run");
370 // Read the new command
371 input_lock->lock("PlaybackEngine::run");
372 command->copy_from(sent_command);
373 //printf("sent command=%d\n", sent_command->command);
374 int active = this->send_active;
375 this->send_active = 0;
376 input_lock->unlock();
377 if( !active ) continue;
379 interrupt_playback(0);
380 wait_render_engine();
382 switch( command->command ) {
383 // Parameter change only
390 pause_lock->lock("PlaybackEngine::run");
402 // Dispatch the command
403 start_render_engine();
406 case SINGLE_FRAME_FWD:
407 case SINGLE_FRAME_REWIND:
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.
466 int curr_command = is_playing_back ? this->command->command : STOP;
467 int curr_single_frame = TransportCommand::single_frame(curr_command);
468 int curr_audio = this->command->toggle_audio ?
469 !curr_single_frame : curr_single_frame;
470 int single_frame = TransportCommand::single_frame(command);
471 int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
472 float next_speed = next_command->speed;
476 case FAST_REWIND: // Commands that play back
479 case SINGLE_FRAME_REWIND:
480 case SINGLE_FRAME_FWD:
486 // run shuttle as no prev command
487 if( next_speed ) curr_command = COMMAND_NONE;
488 // Same direction pressed twice, not shuttle, and no change in audio state, Stop
489 if( curr_command == command && !curr_single_frame &&
490 curr_audio == next_audio ) { do_stop = 1; break; }
492 // Resume or change direction
493 switch( curr_command ) {
496 next_command->resume = 1;
500 case SINGLE_FRAME_FWD:
501 case SINGLE_FRAME_REWIND:
504 next_command->realtime = 1;
505 // Start from scratch
506 transport_command(command, CHANGE_NONE, edl, use_inout);
511 // Commands that stop
520 transport_stop(wait_tracking);
524 int PlaybackEngine::put_command(TransportCommand *command, int reset)
526 input_lock->lock("PlaybackEngine::put_command");
527 int prev_change_type = sent_command->change_type;
528 sent_command->copy_from(command);
529 // run only last command, sum change type
531 sent_command->change_type |= prev_change_type;
533 if( reset ) command->reset();
534 output_lock->unlock();
535 input_lock->unlock();
539 int PlaybackEngine::transport_stop(int wait_tracking)
541 put_command(stop_command, 0);
542 if( wait_tracking ) {
543 tracking_done->lock("PlaybackEngine::transport_stop");
544 tracking_done->unlock();
546 //printf("send: %d (STOP) 0\n", STOP);
550 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
552 next_command->command = command;
553 next_command->change_type |= change_type;
555 // Just change the EDL if the change requires it because renderengine
556 // structures won't point to the new EDL otherwise and because copying the
557 // EDL for every cursor movement is slow.
558 if( change_type == CHANGE_EDL || change_type == CHANGE_ALL )
559 next_command->get_edl()->copy_all(new_edl);
560 else if( change_type == CHANGE_PARAMS )
561 next_command->get_edl()->synchronize_params(new_edl);
562 next_command->set_playback_range(new_edl, use_inout,
563 preferences->forward_render_displacement);
565 put_command(next_command, 1);
566 //static const char *types[] = { "NONE",
567 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
568 // "STOP", "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
570 //printf("send= %d (%s) %d\n", sent_command->command,
571 // types[sent_command->command], sent_command->locked);
575 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
577 int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
578 next_command->realtime = 1;
579 transport_command(command, change_type, edl);