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"
23 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
26 this->mwindow = mwindow;
27 this->output = output;
29 tracking_position = 0;
34 tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
35 tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
36 pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
37 start_lock = new Condition(0, "PlaybackEngine::start_lock");
42 PlaybackEngine::~PlaybackEngine()
45 que->send_command(STOP,
55 delete_render_engine();
64 int PlaybackEngine::create_objects()
67 preferences = new Preferences;
68 command = new TransportCommand;
69 que = new TransportQue;
70 // Set the first change to maximum
71 que->command.change_type = CHANGE_ALL;
73 preferences->copy_from(mwindow->preferences);
77 start_lock->lock("PlaybackEngine::create_objects");
81 ChannelDB* PlaybackEngine::get_channeldb()
83 PlaybackConfig *config = command->get_edl()->session->playback_config;
84 switch(config->vconfig->driver)
86 case VIDEO4LINUX2JPEG:
87 return mwindow->channeldb_v4l2jpeg;
90 return mwindow->channeldb_buz;
96 int PlaybackEngine::create_render_engine()
98 // Fix playback configurations
99 int current_vchannel = 0;
100 int current_achannel = 0;
102 delete_render_engine();
105 render_engine = new RenderEngine(this,
114 void PlaybackEngine::delete_render_engine()
116 delete render_engine;
120 void PlaybackEngine::arm_render_engine()
122 int current_achannel = 0, current_vchannel = 0;
124 render_engine->arm_command(command,
129 void PlaybackEngine::start_render_engine()
131 if(render_engine) render_engine->start_command();
134 void PlaybackEngine::wait_render_engine()
136 if(command->realtime && render_engine)
138 render_engine->join();
142 void PlaybackEngine::create_cache()
144 if(audio_cache) delete audio_cache;
146 if(video_cache) delete video_cache;
149 audio_cache = new CICache(preferences, mwindow->plugindb);
151 video_cache = new CICache(preferences, mwindow->plugindb);
155 void PlaybackEngine::perform_change()
157 switch(command->change_type)
162 create_render_engine();
164 if(command->change_type != CHANGE_EDL &&
165 command->change_type != CHANGE_ALL)
166 render_engine->edl->synchronize_params(command->get_edl());
172 void PlaybackEngine::sync_parameters(EDL *edl)
174 // TODO: lock out render engine from keyframe deletions
175 command->get_edl()->synchronize_params(edl);
176 if(render_engine) render_engine->edl->synchronize_params(edl);
180 void PlaybackEngine::interrupt_playback(int wait_tracking)
183 render_engine->interrupt_playback();
186 pause_lock->unlock();
188 // Wait for tracking to finish if it is running
191 tracking_done->lock("PlaybackEngine::interrupt_playback");
192 tracking_done->unlock();
197 // Return 1 if levels exist
198 int PlaybackEngine::get_output_levels(double *levels, long position)
201 if(render_engine && render_engine->do_audio)
204 render_engine->get_output_levels(levels, position);
210 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
213 if(render_engine && render_engine->do_audio)
216 render_engine->get_module_levels(module_levels, position);
221 int PlaybackEngine::brender_available(long position)
226 void PlaybackEngine::init_cursor()
230 void PlaybackEngine::stop_cursor()
235 void PlaybackEngine::init_tracking()
237 if(!command->single_frame())
242 tracking_position = command->playbackstart;
243 tracking_done->lock("PlaybackEngine::init_tracking");
247 void PlaybackEngine::stop_tracking()
251 tracking_done->unlock();
254 void PlaybackEngine::update_tracking(double position)
256 tracking_lock->lock("PlaybackEngine::update_tracking");
258 tracking_position = position;
260 // Signal that the timer is accurate.
261 if(tracking_active) tracking_active = 2;
262 tracking_timer.update();
263 tracking_lock->unlock();
266 double PlaybackEngine::get_tracking_position()
270 tracking_lock->lock("PlaybackEngine::get_tracking_position");
273 // Adjust for elapsed time since last update_tracking.
274 // But tracking timer isn't accurate until the first update_tracking
276 if(tracking_active == 2)
278 //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));
281 // Don't interpolate when every frame is played.
282 if(command->get_edl()->session->video_every_frame &&
284 render_engine->do_video)
286 result = tracking_position;
291 double loop_start = command->get_edl()->local_session->loop_start;
292 double loop_end = command->get_edl()->local_session->loop_end;
293 double loop_size = loop_end - loop_start;
295 if(command->get_direction() == PLAY_FORWARD)
298 result = tracking_position +
299 command->get_speed() *
300 tracking_timer.get_difference() /
303 // Compensate for loop
304 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
305 if(command->get_edl()->local_session->loop_playback)
307 while(result > loop_end) result -= loop_size;
313 result = tracking_position -
314 command->get_speed() *
315 tracking_timer.get_difference() /
318 // Compensate for loop
319 if(command->get_edl()->local_session->loop_playback)
321 while(result < loop_start) result += loop_size;
328 result = tracking_position;
330 tracking_lock->unlock();
331 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
338 void PlaybackEngine::update_transport(int command, int paused)
340 // mwindow->gui->lock_window();
341 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
342 // mwindow->gui->unlock_window();
345 void PlaybackEngine::run()
347 start_lock->unlock();
351 // Wait for current command to finish
352 que->output_lock->lock("PlaybackEngine::run");
354 wait_render_engine();
357 // Read the new command
358 que->input_lock->lock("PlaybackEngine::run");
361 command->copy_from(&que->command);
362 que->command.reset();
363 que->input_lock->unlock();
365 //printf("PlaybackEngine::run 1 %d\n", command->command);
368 switch(command->command)
370 // Parameter change only
372 // command->command = last_command;
378 pause_lock->lock("PlaybackEngine::run");
387 last_command = command->command;
390 // Dispatch the command
391 start_render_engine();
395 last_command = command->command;
397 if(command->command == SINGLE_FRAME_FWD ||
398 command->command == SINGLE_FRAME_REWIND)
400 command->playbackstart = get_tracking_position();
406 // Start tracking after arming so the tracking position doesn't change.
407 // The tracking for a single frame command occurs during PAUSE
410 // Dispatch the command
411 start_render_engine();
416 //printf("PlaybackEngine::run 100\n");