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"
25 #include "condition.h"
27 #include "edlsession.h"
28 #include "localsession.h"
32 #include "mwindowgui.h"
35 #include "playbackengine.h"
36 #include "playtransport.h"
37 #include "preferences.h"
38 #include "renderengine.h"
39 #include "mainsession.h"
40 #include "trackcanvas.h"
41 #include "transportque.h"
45 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
48 this->mwindow = mwindow;
49 this->output = output;
51 tracking_position = 0;
56 tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
57 renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
58 tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
59 pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
60 start_lock = new Condition(0, "PlaybackEngine::start_lock");
65 PlaybackEngine::~PlaybackEngine()
68 que->send_command(STOP,
78 delete_render_engine();
85 delete renderengine_lock;
88 void PlaybackEngine::create_objects()
90 preferences = new Preferences;
91 command = new TransportCommand;
92 que = new TransportQue;
93 // Set the first change to maximum
94 que->command.change_type = CHANGE_ALL;
96 preferences->copy_from(mwindow->preferences);
100 start_lock->lock("PlaybackEngine::create_objects");
103 ChannelDB* PlaybackEngine::get_channeldb()
105 PlaybackConfig *config = command->get_edl()->session->playback_config;
106 switch(config->vconfig->driver)
108 case VIDEO4LINUX2JPEG:
109 return mwindow->channeldb_v4l2jpeg;
114 int PlaybackEngine::create_render_engine()
116 // Fix playback configurations
117 delete_render_engine();
118 render_engine = new RenderEngine(this, preferences, output, 0);
119 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
123 void PlaybackEngine::delete_render_engine()
125 renderengine_lock->lock("PlaybackEngine::delete_render_engine");
126 delete render_engine;
128 renderengine_lock->unlock();
131 void PlaybackEngine::arm_render_engine()
134 render_engine->arm_command(command);
137 void PlaybackEngine::start_render_engine()
139 if(render_engine) render_engine->start_command();
142 void PlaybackEngine::wait_render_engine()
144 if(command->realtime && render_engine)
146 render_engine->join();
150 void PlaybackEngine::create_cache()
152 if(audio_cache) { delete audio_cache; audio_cache = 0; }
153 if(video_cache) { delete video_cache; video_cache = 0; }
154 if(!audio_cache) audio_cache = new CICache(preferences);
155 if(!video_cache) video_cache = new CICache(preferences);
159 void PlaybackEngine::perform_change()
161 switch(command->change_type)
166 create_render_engine();
168 if(command->change_type != CHANGE_EDL &&
169 (uint32_t)command->change_type != CHANGE_ALL)
170 render_engine->get_edl()->synchronize_params(command->get_edl());
176 void PlaybackEngine::sync_parameters(EDL *edl)
178 // TODO: lock out render engine from keyframe deletions
179 command->get_edl()->synchronize_params(edl);
180 if(render_engine) render_engine->get_edl()->synchronize_params(edl);
184 void PlaybackEngine::interrupt_playback(int wait_tracking)
186 renderengine_lock->lock("PlaybackEngine::interrupt_playback");
188 render_engine->interrupt_playback();
189 renderengine_lock->unlock();
192 pause_lock->unlock();
194 // Wait for tracking to finish if it is running
197 tracking_done->lock("PlaybackEngine::interrupt_playback");
198 tracking_done->unlock();
203 // Return 1 if levels exist
204 int PlaybackEngine::get_output_levels(double *levels, long position)
207 if(render_engine && render_engine->do_audio)
210 render_engine->get_output_levels(levels, position);
216 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
219 if(render_engine && render_engine->do_audio)
222 render_engine->get_module_levels(module_levels, position);
227 int PlaybackEngine::brender_available(long position)
232 void PlaybackEngine::init_cursor(int active)
236 void PlaybackEngine::init_meters()
240 void PlaybackEngine::stop_cursor()
245 void PlaybackEngine::init_tracking()
247 tracking_active = !command->single_frame() ? 1 : 0;
248 tracking_position = command->playbackstart;
249 tracking_done->lock("PlaybackEngine::init_tracking");
250 init_cursor(tracking_active);
254 void PlaybackEngine::stop_tracking()
258 tracking_done->unlock();
261 void PlaybackEngine::update_tracking(double position)
263 tracking_lock->lock("PlaybackEngine::update_tracking");
264 tracking_position = position;
265 // Signal that the timer is accurate.
266 if(tracking_active) tracking_active = 2;
267 tracking_timer.update();
268 tracking_lock->unlock();
271 double PlaybackEngine::get_tracking_position()
275 tracking_lock->lock("PlaybackEngine::get_tracking_position");
278 // Adjust for elapsed time since last update_tracking.
279 // But tracking timer isn't accurate until the first update_tracking
281 if(tracking_active == 2)
283 //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));
286 // Don't interpolate when every frame is played.
287 if(command->get_edl()->session->video_every_frame &&
289 render_engine->do_video)
291 result = tracking_position;
296 double loop_start = command->get_edl()->local_session->loop_start;
297 double loop_end = command->get_edl()->local_session->loop_end;
298 double loop_size = loop_end - loop_start;
300 if(command->get_direction() == PLAY_FORWARD)
303 result = tracking_position +
304 command->get_speed() *
305 tracking_timer.get_difference() /
308 // Compensate for loop
309 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
310 if(command->get_edl()->local_session->loop_playback)
312 while(result > loop_end) result -= loop_size;
318 result = tracking_position -
319 command->get_speed() *
320 tracking_timer.get_difference() /
323 // Compensate for loop
324 if(command->get_edl()->local_session->loop_playback)
326 while(result < loop_start) result += loop_size;
333 result = tracking_position;
335 tracking_lock->unlock();
336 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
343 void PlaybackEngine::update_transport(int command, int paused)
345 // mwindow->gui->lock_window();
346 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
347 // mwindow->gui->unlock_window();
350 void PlaybackEngine::run()
352 start_lock->unlock();
355 // Wait for current command to finish
356 que->output_lock->lock("PlaybackEngine::run");
357 wait_render_engine();
359 // Read the new command
360 que->input_lock->lock("PlaybackEngine::run");
363 command->copy_from(&que->command);
364 que->command.reset();
365 que->input_lock->unlock();
366 //printf("PlaybackEngine::run 1 %d\n", command->command);
368 switch( command->command ) {
369 // Parameter change only
371 // command->command = last_command;
377 pause_lock->lock("PlaybackEngine::run");
386 last_command = command->command;
389 // Dispatch the command
390 start_render_engine();
393 case SINGLE_FRAME_FWD:
394 case SINGLE_FRAME_REWIND:
397 last_command = command->command;
403 // Start tracking after arming so the tracking position doesn't change.
404 // The tracking for a single frame command occurs during PAUSE
407 // Dispatch the command
408 start_render_engine();
413 //printf("PlaybackEngine::run 100\n");
418 void PlaybackEngine::stop_playback(int wait)
420 que->send_command(STOP, CHANGE_NONE, 0, 0);
421 interrupt_playback(wait);
422 renderengine_lock->lock("PlaybackEngine::stop_playback");
424 render_engine->wait_done();
425 renderengine_lock->unlock();
429 void PlaybackEngine::issue_command(EDL *edl, int command, int wait_tracking,
430 int use_inout, int update_refresh, int toggle_audio)
432 //printf("PlayTransport::handle_transport 1 %d\n", command);
433 // Stop requires transferring the output buffer to a refresh buffer.
434 int do_stop = 0, resume = 0;
435 int prev_command = this->command->command;
436 int prev_single_frame = this->command->single_frame();
437 int prev_audio = this->command->audio_toggle ?
438 !prev_single_frame : prev_single_frame;
439 int cur_single_frame = TransportCommand::single_frame(command);
440 int cur_audio = toggle_audio ?
441 !cur_single_frame : cur_single_frame;
445 case FAST_REWIND: // Commands that play back
448 case SINGLE_FRAME_REWIND:
449 case SINGLE_FRAME_FWD:
453 if( !prev_single_frame &&
454 prev_command == command &&
455 cur_audio == prev_audio ) {
456 // Same direction pressed twice and no change in audio state, Stop
460 // Resume or change direction
461 switch( prev_command ) {
463 que->send_command(STOP, CHANGE_NONE, 0, 0);
464 interrupt_playback(wait_tracking);
469 case SINGLE_FRAME_FWD:
470 case SINGLE_FRAME_REWIND:
471 // Start from scratch
472 que->send_command(command, CHANGE_NONE, edl,
473 1, resume, use_inout, toggle_audio,
474 mwindow->preferences->forward_render_displacement);
479 // Commands that stop
488 que->send_command(STOP, CHANGE_NONE, 0, 0);
489 interrupt_playback(wait_tracking);
493 void PlaybackEngine::refresh_frame(int change_type, EDL *edl)
495 que->send_command(CURRENT_FRAME, change_type, edl, 1);