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(preferences, mwindow->plugindb);
152 video_cache = new CICache(preferences, mwindow->plugindb);
156 void PlaybackEngine::perform_change()
158 switch(command->change_type)
163 create_render_engine();
165 if(command->change_type != CHANGE_EDL &&
166 command->change_type != CHANGE_ALL)
167 render_engine->edl->synchronize_params(command->get_edl());
173 void PlaybackEngine::sync_parameters(EDL *edl)
175 // TODO: lock out render engine from keyframe deletions
176 command->get_edl()->synchronize_params(edl);
177 if(render_engine) render_engine->edl->synchronize_params(edl);
181 void PlaybackEngine::interrupt_playback(int wait_tracking)
184 render_engine->interrupt_playback();
187 pause_lock->unlock();
189 // Wait for tracking to finish if it is running
192 tracking_done->lock("PlaybackEngine::interrupt_playback");
193 tracking_done->unlock();
198 // Return 1 if levels exist
199 int PlaybackEngine::get_output_levels(double *levels, long position)
202 if(render_engine && render_engine->do_audio)
205 render_engine->get_output_levels(levels, position);
211 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
214 if(render_engine && render_engine->do_audio)
217 render_engine->get_module_levels(module_levels, position);
222 int PlaybackEngine::brender_available(long position)
227 void PlaybackEngine::init_cursor()
231 void PlaybackEngine::stop_cursor()
236 void PlaybackEngine::init_tracking()
238 if(!command->single_frame())
243 tracking_position = command->playbackstart;
244 tracking_done->lock("PlaybackEngine::init_tracking");
248 void PlaybackEngine::stop_tracking()
252 tracking_done->unlock();
255 void PlaybackEngine::update_tracking(double position)
257 tracking_lock->lock("PlaybackEngine::update_tracking");
259 tracking_position = position;
261 // Signal that the timer is accurate.
262 if(tracking_active) tracking_active = 2;
263 tracking_timer.update();
264 tracking_lock->unlock();
267 double PlaybackEngine::get_tracking_position()
271 tracking_lock->lock("PlaybackEngine::get_tracking_position");
274 // Adjust for elapsed time since last update_tracking.
275 // But tracking timer isn't accurate until the first update_tracking
277 if(tracking_active == 2)
279 //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));
282 // Don't interpolate when every frame is played.
283 if(command->get_edl()->session->video_every_frame &&
285 render_engine->do_video)
287 result = tracking_position;
292 double loop_start = command->get_edl()->local_session->loop_start;
293 double loop_end = command->get_edl()->local_session->loop_end;
294 double loop_size = loop_end - loop_start;
296 if(command->get_direction() == PLAY_FORWARD)
299 result = tracking_position +
300 command->get_speed() *
301 tracking_timer.get_difference() /
304 // Compensate for loop
305 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
306 if(command->get_edl()->local_session->loop_playback)
308 while(result > loop_end) result -= loop_size;
314 result = tracking_position -
315 command->get_speed() *
316 tracking_timer.get_difference() /
319 // Compensate for loop
320 if(command->get_edl()->local_session->loop_playback)
322 while(result < loop_start) result += loop_size;
329 result = tracking_position;
331 tracking_lock->unlock();
332 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
339 void PlaybackEngine::update_transport(int command, int paused)
341 // mwindow->gui->lock_window();
342 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
343 // mwindow->gui->unlock_window();
346 void PlaybackEngine::run()
348 start_lock->unlock();
352 // Wait for current command to finish
353 que->output_lock->lock("PlaybackEngine::run");
355 wait_render_engine();
358 // Read the new command
359 que->input_lock->lock("PlaybackEngine::run");
362 command->copy_from(&que->command);
363 que->command.reset();
364 que->input_lock->unlock();
366 //printf("PlaybackEngine::run 1 %d\n", command->command);
369 switch(command->command)
371 // Parameter change only
373 // command->command = last_command;
379 pause_lock->lock("PlaybackEngine::run");
388 last_command = command->command;
391 // Dispatch the command
392 start_render_engine();
396 last_command = command->command;
398 if(command->command == SINGLE_FRAME_FWD ||
399 command->command == SINGLE_FRAME_REWIND)
401 command->playbackstart = get_tracking_position();
407 // Start tracking after arming so the tracking position doesn't change.
408 // The tracking for a single frame command occurs during PAUSE
411 // Dispatch the command
412 start_render_engine();
417 //printf("PlaybackEngine::run 100\n");