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"
26 #include "condition.h"
28 #include "edlsession.h"
29 #include "localsession.h"
33 #include "mwindowgui.h"
37 #include "playbackengine.h"
38 #include "playtransport.h"
39 #include "preferences.h"
40 #include "renderengine.h"
41 #include "mainsession.h"
42 #include "trackcanvas.h"
43 #include "transportque.h"
47 PlaybackEngine::PlaybackEngine(MWindow *mwindow, Canvas *output)
50 this->mwindow = mwindow;
51 this->output = output;
53 tracking_position = 0;
57 command = new TransportCommand();
58 command->command = STOP;
59 next_command = new TransportCommand();
60 next_command->change_type = CHANGE_ALL;
61 stop_command = new TransportCommand();
62 stop_command->command = STOP;
63 stop_command->realtime = 1;
64 sent_command = new TransportCommand();
65 sent_command->command = -1;
67 tracking_lock = new Mutex("PlaybackEngine::tracking_lock");
68 renderengine_lock = new Mutex("PlaybackEngine::renderengine_lock");
69 tracking_done = new Condition(1, "PlaybackEngine::tracking_done");
70 pause_lock = new Condition(0, "PlaybackEngine::pause_lock");
71 start_lock = new Condition(0, "PlaybackEngine::start_lock");
72 input_lock = new Condition(1, "PlaybackEngine::input_lock");
73 output_lock = new Condition(0, "PlaybackEngine::output_lock", 1);
79 PlaybackEngine::~PlaybackEngine()
82 output_lock->unlock();
85 delete_render_engine();
92 delete renderengine_lock;
101 void PlaybackEngine::create_objects()
103 preferences = new Preferences;
104 preferences->copy_from(mwindow->preferences);
108 start_lock->lock("PlaybackEngine::create_objects");
111 ChannelDB* PlaybackEngine::get_channeldb()
113 PlaybackConfig *config = command->get_edl()->session->playback_config;
114 switch(config->vconfig->driver)
116 case VIDEO4LINUX2JPEG:
117 return mwindow->channeldb_v4l2jpeg;
122 int PlaybackEngine::create_render_engine()
124 // Fix playback configurations
125 delete_render_engine();
126 render_engine = new RenderEngine(this, preferences, output, 0);
127 //printf("PlaybackEngine::create_render_engine %d\n", __LINE__);
131 void PlaybackEngine::delete_render_engine()
133 renderengine_lock->lock("PlaybackEngine::delete_render_engine");
134 if( render_engine ) {
135 render_engine->interrupt_playback();
136 render_engine->wait_done();
137 delete render_engine; render_engine = 0;
139 renderengine_lock->unlock();
142 void PlaybackEngine::arm_render_engine()
145 render_engine->arm_command(command);
148 void PlaybackEngine::start_render_engine()
151 render_engine->start_command();
154 void PlaybackEngine::wait_render_engine()
156 if( command->realtime && render_engine ) {
157 render_engine->join();
161 void PlaybackEngine::create_cache()
163 if(audio_cache) { delete audio_cache; audio_cache = 0; }
164 if(video_cache) { delete video_cache; video_cache = 0; }
165 if(!audio_cache) audio_cache = new CICache(preferences);
166 if(!video_cache) video_cache = new CICache(preferences);
170 void PlaybackEngine::perform_change()
172 switch( command->change_type ) {
176 create_render_engine();
179 render_engine->get_edl()->synchronize_params(command->get_edl());
185 void PlaybackEngine::sync_parameters(EDL *edl)
187 // TODO: lock out render engine from keyframe deletions
188 command->get_edl()->synchronize_params(edl);
190 render_engine->get_edl()->synchronize_params(edl);
193 void PlaybackEngine::interrupt_playback(int wait_tracking)
195 renderengine_lock->lock("PlaybackEngine::interrupt_playback");
197 render_engine->interrupt_playback();
198 renderengine_lock->unlock();
201 pause_lock->unlock();
203 // Wait for tracking to finish if it is running
204 if( wait_tracking ) {
205 tracking_done->lock("PlaybackEngine::interrupt_playback");
206 tracking_done->unlock();
210 // Return 1 if levels exist
211 int PlaybackEngine::get_output_levels(double *levels, long position)
214 if( render_engine && render_engine->do_audio ) {
215 render_engine->get_output_levels(levels, position);
222 int PlaybackEngine::get_module_levels(ArrayList<double> *module_levels, long position)
225 if( render_engine && render_engine->do_audio ) {
226 render_engine->get_module_levels(module_levels, position);
232 int PlaybackEngine::brender_available(long position)
237 void PlaybackEngine::init_cursor(int active)
241 void PlaybackEngine::init_meters()
245 void PlaybackEngine::stop_cursor()
250 void PlaybackEngine::init_tracking()
252 tracking_active = !command->single_frame() ? 1 : 0;
253 tracking_position = command->playbackstart;
254 tracking_done->lock("PlaybackEngine::init_tracking");
255 init_cursor(tracking_active);
259 void PlaybackEngine::stop_tracking()
263 tracking_done->unlock();
266 void PlaybackEngine::update_tracking(double position)
268 tracking_lock->lock("PlaybackEngine::update_tracking");
269 tracking_position = position;
270 // Signal that the timer is accurate.
271 if(tracking_active) tracking_active = 2;
272 tracking_timer.update();
273 tracking_lock->unlock();
276 double PlaybackEngine::get_tracking_position()
280 tracking_lock->lock("PlaybackEngine::get_tracking_position");
283 // Adjust for elapsed time since last update_tracking.
284 // But tracking timer isn't accurate until the first update_tracking
286 if(tracking_active == 2)
288 //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));
291 // Don't interpolate when every frame is played.
292 if( command->get_edl()->session->video_every_frame &&
293 render_engine && 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 // Read the new command
369 input_lock->lock("PlaybackEngine::run");
370 command->copy_from(sent_command);
371 //printf("sent command=%d\n", sent_command->command);
372 int active = this->send_active;
373 this->send_active = 0;
374 input_lock->unlock();
375 if( !active ) continue;
377 interrupt_playback(0);
378 wait_render_engine();
380 switch( command->command ) {
381 // Parameter change only
388 pause_lock->lock("PlaybackEngine::run");
400 // Dispatch the command
401 start_render_engine();
404 case SINGLE_FRAME_FWD:
405 case SINGLE_FRAME_REWIND:
412 // Start tracking after arming so the tracking position doesn't change.
413 // The tracking for a single frame command occurs during PAUSE
415 if( !command->single_frame() ) {
416 EDL *edl = command->get_edl();
417 if( edl && edl->tracks->playable_video_tracks() )
420 // Dispatch the command
421 start_render_engine();
424 //printf("PlaybackEngine::run 100\n");
428 void PlaybackEngine::clear_output()
430 BC_WindowBase *cwdw = output->get_canvas();
432 cwdw->lock_window("PlaybackEngine::clear_output");
434 cwdw->unlock_window();
437 void PlaybackEngine::stop_playback(int wait_tracking)
439 transport_stop(wait_tracking);
440 renderengine_lock->lock("PlaybackEngine::stop_playback");
441 if( render_engine ) {
442 render_engine->interrupt_playback();
443 render_engine->wait_done();
445 renderengine_lock->unlock();
449 void PlaybackEngine::send_command(int command, EDL *edl, int wait_tracking, int use_inout)
451 //printf("PlaybackEngine::send_command 1 %d\n", command);
452 // Stop requires transferring the output buffer to a refresh buffer.
454 int curr_command = is_playing_back ? this->command->command : STOP;
455 int curr_single_frame = TransportCommand::single_frame(curr_command);
456 int curr_audio = this->command->toggle_audio ?
457 !curr_single_frame : curr_single_frame;
458 int single_frame = TransportCommand::single_frame(command);
459 int next_audio = next_command->toggle_audio ? !single_frame : single_frame;
460 float next_speed = next_command->speed;
464 case FAST_REWIND: // Commands that play back
467 case SINGLE_FRAME_REWIND:
468 case SINGLE_FRAME_FWD:
474 // run shuttle as no prev command
475 if( next_speed ) curr_command = COMMAND_NONE;
476 // Same direction pressed twice, not shuttle, and no change in audio state, Stop
477 if( curr_command == command && !curr_single_frame &&
478 curr_audio == next_audio ) { do_stop = 1; break; }
480 // Resume or change direction
481 switch( curr_command ) {
484 next_command->resume = 1;
488 case SINGLE_FRAME_FWD:
489 case SINGLE_FRAME_REWIND:
492 next_command->realtime = 1;
493 // Start from scratch
494 transport_command(command, CHANGE_NONE, edl, use_inout);
499 // Commands that stop
508 transport_stop(wait_tracking);
512 int PlaybackEngine::put_command(TransportCommand *command, int reset)
514 input_lock->lock("PlaybackEngine::put_command");
515 int prev_change_type = sent_command->change_type;
516 sent_command->copy_from(command);
517 // run only last command, sum change type
519 sent_command->change_type |= prev_change_type;
521 if( reset ) command->reset();
522 output_lock->unlock();
523 input_lock->unlock();
527 int PlaybackEngine::transport_stop(int wait_tracking)
529 put_command(stop_command, 0);
530 if( wait_tracking ) {
531 tracking_done->lock("PlaybackEngine::transport_stop");
532 tracking_done->unlock();
534 //printf("send: %d (STOP) 0\n", STOP);
538 int PlaybackEngine::transport_command(int command, int change_type, EDL *new_edl, int use_inout)
540 next_command->command = command;
541 next_command->change_type |= change_type;
543 // Just change the EDL if the change requires it because renderengine
544 // structures won't point to the new EDL otherwise and because copying the
545 // EDL for every cursor movement is slow.
546 if( change_type == CHANGE_EDL || change_type == CHANGE_ALL )
547 next_command->get_edl()->copy_all(new_edl);
548 else if( change_type == CHANGE_PARAMS )
549 next_command->get_edl()->synchronize_params(new_edl);
550 next_command->set_playback_range(new_edl, use_inout,
551 preferences->forward_render_displacement);
553 put_command(next_command, 1);
554 //static const char *types[] = { "NONE",
555 // "FRAME_FWD", "NORMAL_FWD", "FAST_FWD", "FRAME_REV", "NORMAL_REV", "FAST_REV",
556 // "STOP", "PAUSE", "SLOW_FWD", "SLOW_REV", "REWIND", "GOTO_END", "CURRENT_FRAME",
558 //printf("send= %d (%s) %d\n", sent_command->command,
559 // types[sent_command->command], sent_command->locked);
563 void PlaybackEngine::refresh_frame(int change_type, EDL *edl, int dir)
565 int command = dir >= 0 ? CURRENT_FRAME : LAST_FRAME;
566 next_command->realtime = 1;
567 transport_command(command, change_type, edl);