5 #include "edlsession.h"
6 #include "localsession.h"
10 #include "mwindowgui.h"
13 #include "playbackengine.h"
14 #include "playtransport.h"
15 #include "preferences.h"
16 #include "renderengine.h"
17 #include "mainsession.h"
18 #include "trackcanvas.h"
19 #include "transportque.h"
20 #include "videodevice.h"
24 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
27 this->mwindow = mwindow;
28 this->output = output;
30 tracking_position = 0;
35 tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
36 tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
37 pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
38 start_lock = new Condition(0, "PlaybackEngine::start_lock");
43 PlaybackEngine::~PlaybackEngine()
46 que->send_command(STOP,
56 delete_render_engine();
65 int PlaybackEngine::create_objects()
68 preferences = new Preferences;
69 command = new TransportCommand;
70 que = new TransportQue;
71 // Set the first change to maximum
72 que->command.change_type = CHANGE_ALL;
74 preferences->copy_from(mwindow->preferences);
78 start_lock->lock("PlaybackEngine::create_objects");
82 ChannelDB* PlaybackEngine::get_channeldb()
84 PlaybackConfig *config = command->get_edl()->session->playback_config;
85 switch(config->vconfig->driver)
87 case VIDEO4LINUX2JPEG:
88 return mwindow->channeldb_v4l2jpeg;
91 return mwindow->channeldb_buz;
97 int PlaybackEngine::create_render_engine()
99 // Fix playback configurations
100 int current_vchannel = 0;
101 int current_achannel = 0;
103 delete_render_engine();
106 render_engine = new RenderEngine(this,
115 void PlaybackEngine::delete_render_engine()
117 delete render_engine;
121 void PlaybackEngine::arm_render_engine()
123 int current_achannel = 0, current_vchannel = 0;
125 render_engine->arm_command(command,
130 void PlaybackEngine::start_render_engine()
132 if(render_engine) render_engine->start_command();
135 void PlaybackEngine::wait_render_engine()
137 if(command->realtime && render_engine)
139 render_engine->join();
143 void PlaybackEngine::create_cache()
145 if(audio_cache) delete audio_cache;
147 if(video_cache) delete video_cache;
150 audio_cache = new CICache(command->get_edl(), preferences, mwindow->plugindb);
152 audio_cache->set_edl(command->get_edl());
155 video_cache = new CICache(command->get_edl(), preferences, mwindow->plugindb);
157 video_cache->set_edl(command->get_edl());
161 void PlaybackEngine::perform_change()
163 switch(command->change_type)
168 audio_cache->set_edl(command->get_edl());
169 video_cache->set_edl(command->get_edl());
170 create_render_engine();
172 if(command->change_type != CHANGE_EDL &&
173 command->change_type != CHANGE_ALL)
174 render_engine->edl->synchronize_params(command->get_edl());
180 void PlaybackEngine::sync_parameters(EDL *edl)
182 // TODO: lock out render engine from keyframe deletions
183 command->get_edl()->synchronize_params(edl);
184 if(render_engine) render_engine->edl->synchronize_params(edl);
188 void PlaybackEngine::interrupt_playback(int wait_tracking)
191 render_engine->interrupt_playback();
194 pause_lock->unlock();
196 // Wait for tracking to finish if it is running
199 tracking_done->lock("PlaybackEngine::interrupt_playback");
200 tracking_done->unlock();
205 // Return 1 if levels exist
206 int PlaybackEngine::get_output_levels(double *levels, long position)
209 if(render_engine && render_engine->do_audio)
212 render_engine->get_output_levels(levels, position);
218 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
221 if(render_engine && render_engine->do_audio)
224 render_engine->get_module_levels(module_levels, position);
229 int PlaybackEngine::brender_available(long position)
234 void PlaybackEngine::init_cursor()
238 void PlaybackEngine::stop_cursor()
243 void PlaybackEngine::init_tracking()
245 if(!command->single_frame())
250 tracking_position = command->playbackstart;
251 tracking_done->lock("PlaybackEngine::init_tracking");
255 void PlaybackEngine::stop_tracking()
259 tracking_done->unlock();
262 void PlaybackEngine::update_tracking(double position)
264 tracking_lock->lock("PlaybackEngine::update_tracking");
266 tracking_position = position;
268 // Signal that the timer is accurate.
269 if(tracking_active) tracking_active = 2;
270 tracking_timer.update();
271 tracking_lock->unlock();
274 double PlaybackEngine::get_tracking_position()
278 tracking_lock->lock("PlaybackEngine::get_tracking_position");
281 // Adjust for elapsed time since last update_tracking.
282 // But tracking timer isn't accurate until the first update_tracking
284 if(tracking_active == 2)
286 //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));
289 // Don't interpolate when every frame is played.
290 if(command->get_edl()->session->video_every_frame &&
292 render_engine->do_video)
294 result = tracking_position;
299 double loop_start = command->get_edl()->local_session->loop_start;
300 double loop_end = command->get_edl()->local_session->loop_end;
301 double loop_size = loop_end - loop_start;
303 if(command->get_direction() == PLAY_FORWARD)
306 result = tracking_position +
307 command->get_speed() *
308 tracking_timer.get_difference() /
311 // Compensate for loop
312 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
313 if(command->get_edl()->local_session->loop_playback)
315 while(result > loop_end) result -= loop_size;
321 result = tracking_position -
322 command->get_speed() *
323 tracking_timer.get_difference() /
326 // Compensate for loop
327 if(command->get_edl()->local_session->loop_playback)
329 while(result < loop_start) result += loop_size;
336 result = tracking_position;
338 tracking_lock->unlock();
339 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
346 void PlaybackEngine::update_transport(int command, int paused)
348 // mwindow->gui->lock_window();
349 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
350 // mwindow->gui->unlock_window();
353 void PlaybackEngine::run()
355 start_lock->unlock();
359 // Wait for current command to finish
360 que->output_lock->lock("PlaybackEngine::run");
362 //printf("PlaybackEngine::run 1\n");
363 wait_render_engine();
364 //printf("PlaybackEngine::run 2\n");
367 // Read the new command
368 que->input_lock->lock("PlaybackEngine::run");
371 command->copy_from(&que->command);
372 que->command.reset();
373 que->input_lock->unlock();
377 switch(command->command)
379 // Parameter change only
381 // command->command = last_command;
387 pause_lock->lock("PlaybackEngine::run");
396 last_command = command->command;
399 // Dispatch the command
400 start_render_engine();
404 last_command = command->command;
406 if(command->command == SINGLE_FRAME_FWD ||
407 command->command == SINGLE_FRAME_REWIND)
409 command->playbackstart = get_tracking_position();
415 // Start tracking after arming so the tracking position doesn't change.
416 // The tracking for a single frame command occurs during PAUSE
419 // Dispatch the command
420 start_render_engine();
425 //printf("PlaybackEngine::run 100\n");