4 * Copyright (C) 2009 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
25 #include "audiodevice.h"
26 #include "bcsignals.h"
27 #include "condition.h"
29 #include "edlsession.h"
30 #include "meterhistory.h"
33 #include "playbackengine.h"
34 #include "preferences.h"
35 #include "preferencesthread.h"
36 #include "renderengine.h"
37 #include "mainsession.h"
39 #include "transportque.h"
40 #include "videodevice.h"
42 #include "workarounds.h"
46 RenderEngine::RenderEngine(PlaybackEngine *playback_engine,
47 Preferences *preferences,
52 this->playback_engine = playback_engine;
53 this->output = output;
54 this->is_nested = is_nested;
57 config = new PlaybackConfig;
63 this->preferences = new Preferences;
64 this->command = new TransportCommand;
65 this->preferences->copy_from(preferences);
70 mwindow = !playback_engine ? 0 : playback_engine->mwindow;
72 input_lock = new Condition(1, "RenderEngine::input_lock");
73 start_lock = new Condition(1, "RenderEngine::start_lock");
74 output_lock = new Condition(1, "RenderEngine::output_lock");
75 render_active = new Condition(1,"RenderEngine::render_active");
76 interrupt_lock = new Condition(1, "RenderEngine::interrupt_lock");
77 first_frame_lock = new Condition(1, "RenderEngine::first_frame_lock");
80 RenderEngine::~RenderEngine()
85 if(arender) delete arender;
86 if(vrender) delete vrender;
91 delete interrupt_lock;
92 delete first_frame_lock;
94 if( edl ) edl->Garbage::remove_user();
97 EDL* RenderEngine::get_edl()
99 // return command->get_edl();
103 int RenderEngine::arm_command(TransportCommand *command)
106 // Prevent this renderengine from accepting another command until finished.
107 // Since the renderengine is often deleted after the input_lock command it must
108 // be locked here as well as in the calling routine.
109 if(debug) printf("RenderEngine::arm_command %d\n", __LINE__);
112 input_lock->lock("RenderEngine::arm_command");
117 edl->create_objects();
118 edl->copy_all(command->get_edl());
120 this->command->copy_from(command);
122 // Fix background rendering asset to use current dimensions and ignore
124 preferences->brender_asset->frame_rate = command->get_edl()->session->frame_rate;
125 preferences->brender_asset->width = command->get_edl()->session->output_w;
126 preferences->brender_asset->height = command->get_edl()->session->output_h;
127 preferences->brender_asset->use_header = 0;
128 preferences->brender_asset->layers = 1;
129 preferences->brender_asset->video_data = 1;
134 // Retool configuration for this node
135 this->config->copy_from(command->get_edl()->session->playback_config);
136 VideoOutConfig *vconfig = this->config->vconfig;
137 AudioOutConfig *aconfig = this->config->aconfig;
138 if(command->realtime)
140 if(command->single_frame() && vconfig->driver != PLAYBACK_X11_GL)
142 vconfig->driver = PLAYBACK_X11;
147 vconfig->driver = PLAYBACK_X11;
156 fragment_len = aconfig->fragment_size;
157 // Larger of audio_module_fragment and fragment length adjusted for speed
158 // Extra memory must be allocated for rendering slow motion.
159 float speed = command->get_speed();
160 adjusted_fragment_len = speed >= 1.0 ?
161 (int64_t)(aconfig->fragment_size * speed + 0.5) :
162 (int64_t)(aconfig->fragment_size / speed + 0.5) ;
163 if(adjusted_fragment_len < aconfig->fragment_size)
164 adjusted_fragment_len = aconfig->fragment_size;
167 // Set lock so audio doesn't start until video has started.
170 while(first_frame_lock->get_value() > 0)
171 first_frame_lock->lock("RenderEngine::arm_command");
174 // Set lock so audio doesn't wait for video which is never to come.
176 while(first_frame_lock->get_value() <= 0)
177 first_frame_lock->unlock();
181 create_render_threads();
182 arm_render_threads();
183 if(debug) printf("RenderEngine::arm_command %d\n", __LINE__);
188 void RenderEngine::get_duty()
193 //printf("RenderEngine::get_duty %d\n", __LINE__);
194 if( get_edl()->tracks->playable_audio_tracks() &&
195 get_edl()->session->audio_channels )
197 do_audio = !command->single_frame() ? 1 : 0;
198 if( command->audio_toggle ) do_audio = !do_audio;
201 //printf("RenderEngine::get_duty %d\n", __LINE__);
202 if(get_edl()->tracks->playable_video_tracks())
204 //printf("RenderEngine::get_duty %d\n", __LINE__);
209 void RenderEngine::create_render_threads()
211 if(do_video && !vrender)
213 vrender = new VRender(this);
216 if(do_audio && !arender)
218 arender = new ARender(this);
223 int RenderEngine::get_output_w()
225 return get_edl()->session->output_w;
228 int RenderEngine::get_output_h()
230 return get_edl()->session->output_h;
233 int RenderEngine::brender_available(int position, int direction)
237 int64_t corrected_position = position;
238 if(direction == PLAY_REVERSE)
239 corrected_position--;
240 return playback_engine->brender_available(corrected_position);
247 CICache* RenderEngine::get_acache()
250 return playback_engine->audio_cache;
255 CICache* RenderEngine::get_vcache()
258 return playback_engine->video_cache;
263 void RenderEngine::set_acache(CICache *cache)
265 this->audio_cache = cache;
268 void RenderEngine::set_vcache(CICache *cache)
270 this->video_cache = cache;
274 double RenderEngine::get_tracking_position()
277 return playback_engine->get_tracking_position();
282 int RenderEngine::open_output()
284 if(command->realtime && !is_nested)
289 audio = new AudioDevice(mwindow);
294 video = new VideoDevice(mwindow);
297 // Initialize sharing
301 if(do_audio && do_video)
303 video->set_adevice(audio);
304 audio->set_vdevice(video);
309 // Retool playback configuration
312 if(audio->open_output(config->aconfig,
313 get_edl()->session->sample_rate,
314 adjusted_fragment_len,
315 get_edl()->session->audio_channels,
316 get_edl()->session->real_time_playback))
320 audio->set_software_positioning(
321 get_edl()->session->playback_software_position);
322 audio->start_playback();
328 video->open_output(config->vconfig,
329 get_edl()->session->frame_rate,
333 command->single_frame());
334 video->set_quality(80);
335 video->set_cpus(preferences->processors);
342 void RenderEngine::reset_sync_position()
347 int64_t RenderEngine::sync_position()
350 // No danger of race conditions because the output devices are closed after all
354 return audio->current_position();
359 int64_t result = timer.get_scaled_difference(
360 get_edl()->session->sample_rate);
367 int RenderEngine::start_command()
369 if(command->realtime && !is_nested)
371 interrupt_lock->lock("RenderEngine::start_command");
372 start_lock->lock("RenderEngine::start_command 1");
374 start_lock->lock("RenderEngine::start_command 2");
375 start_lock->unlock();
380 void RenderEngine::arm_render_threads()
384 arender->arm_command();
389 vrender->arm_command();
394 void RenderEngine::start_render_threads()
396 // Synchronization timer. Gets reset once again after the first video frame.
401 arender->start_command();
406 vrender->start_command();
410 void RenderEngine::update_framerate(float framerate)
412 playback_engine->mwindow->session->actual_frame_rate = framerate;
413 playback_engine->mwindow->preferences_thread->update_framerate();
416 void RenderEngine::wait_render_threads()
420 arender->Thread::join();
425 vrender->Thread::join();
429 void RenderEngine::interrupt_playback()
431 interrupt_lock->lock("RenderEngine::interrupt_playback");
433 if(do_audio && arender)
435 arender->interrupt_playback();
438 if(do_video && vrender)
440 vrender->interrupt_playback();
442 interrupt_lock->unlock();
445 int RenderEngine::close_output()
447 // Nested engines share devices
470 void RenderEngine::get_output_levels(double *levels, int64_t position)
473 MeterHistory *meter_history = arender->meter_history;
474 int64_t tolerance = 4*arender->meter_render_fragment;
475 int pos = meter_history->get_nearest(position, tolerance);
476 for( int i=0; i<MAXCHANNELS; ++i ) {
477 if( !arender->audio_out[i] ) continue;
478 levels[i] = meter_history->get_peak(i, pos);
483 void RenderEngine::get_module_levels(ArrayList<double> *module_levels, int64_t position)
486 int64_t tolerance = 4*arender->meter_render_fragment;
487 for( int i=0; i<arender->total_modules; ++i ) {
488 AModule *amodule = (AModule *)arender->modules[i];
489 MeterHistory *meter_history = amodule->meter_history;
490 int pos = meter_history->get_nearest(position, tolerance);
491 module_levels->append(meter_history->get_peak(0, pos));
497 void RenderEngine::run()
499 render_active->lock("RenderEngine::run");
500 start_render_threads();
501 start_lock->unlock();
502 interrupt_lock->unlock();
504 wait_render_threads();
506 interrupt_lock->lock("RenderEngine::run");
511 playback_engine->tracking_position = playback_engine->get_tracking_position();
516 // Fix the tracking position
519 if(command->command == CURRENT_FRAME || command->command == LAST_FRAME)
521 //printf("RenderEngine::run 4.1 %d\n", playback_engine->tracking_position);
522 playback_engine->tracking_position = command->playbackstart;
526 // Make sure transport doesn't issue a pause command next
527 //printf("RenderEngine::run 4.1 %d\n", playback_engine->tracking_position);
530 playback_engine->tracking_position =
531 command->get_direction() == PLAY_FORWARD ?
532 command->end_position : command->start_position;
535 if( playback_engine->is_playing_back && command->displacement ) {
536 double position = playback_engine->tracking_position -
537 1./command->get_edl()->session->frame_rate;
538 playback_engine->tracking_position = position >= 0 ? position : 0;
541 if(!interrupted) playback_engine->command->command = STOP;
542 playback_engine->stop_tracking();
545 playback_engine->is_playing_back = 0;
548 input_lock->unlock();
549 interrupt_lock->unlock();
550 render_active->unlock();
553 void RenderEngine::wait_done()
555 render_active->lock("RenderEngine::wait_done");
556 render_active->unlock();