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;
55 command = new TransportCommand();
56 command->command = STOP;
57 next_command = new TransportCommand();
58 next_command->change_type = CHANGE_ALL;
59 stop_command = new TransportCommand();
60 stop_command->command = STOP;
61 stop_command->realtime = 1;
62 sent_command = new TransportCommand();
63 sent_command->command = -1;
64 tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
65 renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
66 tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
67 pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
68 start_lock = new Condition(0, "PlaybackEngine::start_lock");
69 input_lock = new Condition(1, "PlaybackEngine::input_lock");
70 output_lock = new Condition(0, "PlaybackEngine::output_lock", 1);
76 PlaybackEngine::~PlaybackEngine()
79 output_lock->unlock();
82 delete_render_engine();
89 delete renderengine_lock;
98 void PlaybackEngine::create_objects()
100 preferences = new Preferences;
101 preferences->copy_from(mwindow->preferences);
105 start_lock->lock("PlaybackEngine::create_objects");
108 ChannelDB* PlaybackEngine::get_channeldb()
110 PlaybackConfig *config = command->get_edl()->session->playback_config;
111 switch(config->vconfig->driver)
113 case VIDEO4LINUX2JPEG:
114 return mwindow->channeldb_v4l2jpeg;
119 int PlaybackEngine::create_render_engine()
121 // Fix playback configurations
122 delete_render_engine();
123 render_engine = new RenderEngine(this, preferences, output, 0);
124 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
128 void PlaybackEngine::delete_render_engine()
130 renderengine_lock->lock("PlaybackEngine::delete_render_engine");
131 delete render_engine;
133 renderengine_lock->unlock();
136 void PlaybackEngine::arm_render_engine()
139 render_engine->arm_command(command);
142 void PlaybackEngine::start_render_engine()
144 if(render_engine) render_engine->start_command();
147 void PlaybackEngine::wait_render_engine()
149 if(command->realtime && render_engine)
151 render_engine->join();
155 void PlaybackEngine::create_cache()
157 if(audio_cache) { delete audio_cache; audio_cache = 0; }
158 if(video_cache) { delete video_cache; video_cache = 0; }
159 if(!audio_cache) audio_cache = new CICache(preferences);
160 if(!video_cache) video_cache = new CICache(preferences);
164 void PlaybackEngine::perform_change()
166 switch( command->change_type ) {
170 create_render_engine();
173 render_engine->get_edl()->synchronize_params(command->get_edl());
179 void PlaybackEngine::sync_parameters(EDL *edl)
181 // TODO: lock out render engine from keyframe deletions
182 command->get_edl()->synchronize_params(edl);
183 if(render_engine) render_engine->get_edl()->synchronize_params(edl);
187 void PlaybackEngine::interrupt_playback(int wait_tracking)
189 renderengine_lock->lock("PlaybackEngine::interrupt_playback");
191 render_engine->interrupt_playback();
192 renderengine_lock->unlock();
195 pause_lock->unlock();
197 // Wait for tracking to finish if it is running
200 tracking_done->lock("PlaybackEngine::interrupt_playback");
201 tracking_done->unlock();
206 // Return 1 if levels exist
207 int PlaybackEngine::get_output_levels(double *levels, long position)
210 if(render_engine && render_engine->do_audio)
213 render_engine->get_output_levels(levels, position);
219 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
222 if(render_engine && render_engine->do_audio)
225 render_engine->get_module_levels(module_levels, position);
230 int PlaybackEngine::brender_available(long position)
235 void PlaybackEngine::init_cursor(int active)
239 void PlaybackEngine::init_meters()
243 void PlaybackEngine::stop_cursor()
248 void PlaybackEngine::init_tracking()
250 tracking_active = !command->single_frame() ? 1 : 0;
251 tracking_position = command->playbackstart;
252 tracking_done->lock("PlaybackEngine::init_tracking");
253 init_cursor(tracking_active);
257 void PlaybackEngine::stop_tracking()
261 tracking_done->unlock();
264 void PlaybackEngine::update_tracking(double position)
266 tracking_lock->lock("PlaybackEngine::update_tracking");
267 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, loop_end;
300 int play_loop = command->loop_play ? 1 : 0;
301 EDL *edl = command->get_edl();
302 int loop_playback = edl->local_session->loop_playback ? 1 : 0;
303 if( play_loop || !loop_playback ) {
304 loop_start = command->start_position;
305 loop_end = command->end_position;
308 loop_start = edl->local_session->loop_start;
309 loop_end = edl->local_session->loop_end;
312 double loop_size = loop_end - loop_start;
314 if( command->get_direction() == PLAY_FORWARD ) {
316 result = tracking_position +
317 command->get_speed() *
318 tracking_timer.get_difference() /
321 // Compensate for loop
322 //printf("PlaybackEngine::get_tracking_position 1 %d\n", command->get_edl()->local_session->loop_playback);
323 if( play_loop && loop_size > 0 ) {
324 while( result > loop_end ) result -= loop_size;
329 result = tracking_position -
330 command->get_speed() *
331 tracking_timer.get_difference() /
334 // Compensate for loop
335 if( play_loop && loop_size > 0 ) {
336 while( result < loop_start ) result += loop_size;
343 result = tracking_position;
345 tracking_lock->unlock();
346 //printf("PlaybackEngine::get_tracking_position %f %f %d\n", result, tracking_position, tracking_active);
353 void PlaybackEngine::update_transport(int command, int paused)
355 // mwindow->gui->lock_window();
356 // mwindow->gui->mbuttons->transport->update_gui_state(command, paused);
357 // mwindow->gui->unlock_window();
360 void PlaybackEngine::run()
362 start_lock->unlock();
365 // Wait for current command to finish
366 output_lock->lock("PlaybackEngine::run");
368 //printf("sent command=%d\n", sent_command->command);
369 // Read the new command
370 command->copy_from(sent_command);
371 input_lock->unlock();
373 interrupt_playback(0);
374 wait_render_engine();
376 switch( command->command ) {
377 // Parameter change only
384 pause_lock->lock("PlaybackEngine::run");
396 // Dispatch the command
397 start_render_engine();
400 case SINGLE_FRAME_FWD:
401 case SINGLE_FRAME_REWIND:
408 // Start tracking after arming so the tracking position doesn't change.
409 // The tracking for a single frame command occurs during PAUSE
412 // Dispatch the command
413 start_render_engine();
416 //printf("PlaybackEngine::run 100\n");
421 void PlaybackEngine::stop_playback(int wait_tracking)
423 transport_stop(wait_tracking);
424 renderengine_lock->lock("PlaybackEngine::stop_playback");
426 render_engine->wait_done();
427 renderengine_lock->unlock();
431 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
433 //printf("PlaybackEngine::send_command 1 %d\n", command);
434 // Stop requires transferring the output buffer to a refresh buffer.
435 int do_stop = 0, do_resume = 0;
436 int curr_command = this->command->command;
437 int curr_single_frame = TransportCommand::single_frame(curr_command);
438 int curr_audio = this->command->toggle_audio ?
439 !curr_single_frame : curr_single_frame;
440 int single_frame = TransportCommand::single_frame(command);
441 int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
445 case FAST_REWIND: // Commands that play back
448 case SINGLE_FRAME_REWIND:
449 case SINGLE_FRAME_FWD:
455 if( curr_command == command && !next_command->speed &&
456 !curr_single_frame && curr_audio == next_audio ) {
457 // Same direction pressed twice, not shuttle, and no change in audio state, Stop
461 // Resume or change direction
462 switch( curr_command ) {
469 case SINGLE_FRAME_FWD:
470 case SINGLE_FRAME_REWIND:
473 next_command->realtime = 1;
474 next_command->resume = do_resume;
475 // Start from scratch
476 transport_command(command, CHANGE_NONE, edl, use_inout);
481 // Commands that stop
490 transport_stop(wait_tracking);
494 int PlaybackEngine::transport_stop(int wait_tracking)
496 interrupt_playback(0);
497 input_lock->lock("PlaybackEngine::transport_stop");
498 sent_command->copy_from(stop_command);
499 output_lock->unlock();
500 if( wait_tracking ) {
501 tracking_done->lock("PlaybackEngine::transport_stop");
502 tracking_done->unlock();
504 //printf("send: %d (STOP) 0\n", STOP);
508 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
510 next_command->command = command;
511 next_command->change_type |= change_type;
513 // Just change the EDL if the change requires it because renderengine
514 // structures won't point to the new EDL otherwise and because copying the
515 // EDL for every cursor movement is slow.
516 switch( change_type ) {
519 next_command->get_edl()->copy_all(new_edl);
522 next_command->get_edl()->synchronize_params(new_edl);
525 next_command->set_playback_range(new_edl, use_inout,
526 preferences->forward_render_displacement);
529 interrupt_playback(0);
530 input_lock->lock("PlaybackEngine::transport_command");
531 sent_command->copy_from(next_command);
532 next_command->reset();
533 output_lock->unlock();
535 //static const char *types[] = { "NONE",
536 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
537 // "STOP", "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
539 //printf("send= %d (%s) %d\n", sent_command->command,
540 // types[sent_command->command], sent_command->locked);
544 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
546 int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
547 next_command->realtime = 1;
548 transport_command(command, change_type, edl);