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 "channeldb.h"
28 #include "condition.h"
30 #include "edlsession.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,
53 this->playback_engine = playback_engine;
54 this->output = output;
55 this->channeldb = channeldb;
56 this->is_nested = is_nested;
59 config = new PlaybackConfig;
65 this->preferences = new Preferences;
66 this->command = new TransportCommand;
67 this->preferences->copy_from(preferences);
72 if(playback_engine && playback_engine->mwindow)
73 mwindow = playback_engine->mwindow;
79 input_lock = new Condition(1, "RenderEngine::input_lock");
80 start_lock = new Condition(1, "RenderEngine::start_lock");
81 output_lock = new Condition(1, "RenderEngine::output_lock");
82 interrupt_lock = new Mutex("RenderEngine::interrupt_lock");
83 first_frame_lock = new Condition(1, "RenderEngine::first_frame_lock");
86 RenderEngine::~RenderEngine()
91 if(arender) delete arender;
92 if(vrender) delete vrender;
96 delete interrupt_lock;
97 delete first_frame_lock;
99 edl->Garbage::remove_user();
102 EDL* RenderEngine::get_edl()
104 // return command->get_edl();
108 int RenderEngine::arm_command(TransportCommand *command)
111 // Prevent this renderengine from accepting another command until finished.
112 // Since the renderengine is often deleted after the input_lock command it must
113 // be locked here as well as in the calling routine.
114 if(debug) printf("RenderEngine::arm_command %d\n", __LINE__);
117 input_lock->lock("RenderEngine::arm_command");
122 edl->create_objects();
123 edl->copy_all(command->get_edl());
125 this->command->copy_from(command);
127 // Fix background rendering asset to use current dimensions and ignore
129 preferences->brender_asset->frame_rate = command->get_edl()->session->frame_rate;
130 preferences->brender_asset->width = command->get_edl()->session->output_w;
131 preferences->brender_asset->height = command->get_edl()->session->output_h;
132 preferences->brender_asset->use_header = 0;
133 preferences->brender_asset->layers = 1;
134 preferences->brender_asset->video_data = 1;
139 // Retool configuration for this node
140 this->config->copy_from(command->get_edl()->session->playback_config);
141 VideoOutConfig *vconfig = this->config->vconfig;
142 AudioOutConfig *aconfig = this->config->aconfig;
143 if(command->realtime)
145 if(command->single_frame() && vconfig->driver != PLAYBACK_X11_GL)
147 vconfig->driver = PLAYBACK_X11;
152 vconfig->driver = PLAYBACK_X11;
161 fragment_len = aconfig->fragment_size;
162 // Larger of audio_module_fragment and fragment length adjusted for speed
163 // Extra memory must be allocated for rendering slow motion.
164 float speed = command->get_speed();
165 adjusted_fragment_len = speed >= 1.0 ?
166 (int64_t)(aconfig->fragment_size * speed + 0.5) :
167 (int64_t)(aconfig->fragment_size / speed + 0.5) ;
168 if(adjusted_fragment_len < aconfig->fragment_size)
169 adjusted_fragment_len = aconfig->fragment_size;
172 // Set lock so audio doesn't start until video has started.
175 while(first_frame_lock->get_value() > 0)
176 first_frame_lock->lock("RenderEngine::arm_command");
179 // Set lock so audio doesn't wait for video which is never to come.
181 while(first_frame_lock->get_value() <= 0)
182 first_frame_lock->unlock();
186 create_render_threads();
187 arm_render_threads();
188 if(debug) printf("RenderEngine::arm_command %d\n", __LINE__);
193 void RenderEngine::get_duty()
198 //printf("RenderEngine::get_duty %d\n", __LINE__);
199 if(!command->single_frame() &&
200 get_edl()->tracks->playable_audio_tracks() &&
201 get_edl()->session->audio_channels)
206 //printf("RenderEngine::get_duty %d\n", __LINE__);
207 if(get_edl()->tracks->playable_video_tracks())
209 //printf("RenderEngine::get_duty %d\n", __LINE__);
214 void RenderEngine::create_render_threads()
216 if(do_video && !vrender)
218 vrender = new VRender(this);
221 if(do_audio && !arender)
223 arender = new ARender(this);
228 int RenderEngine::get_output_w()
230 return get_edl()->session->output_w;
233 int RenderEngine::get_output_h()
235 return get_edl()->session->output_h;
238 int RenderEngine::brender_available(int position, int direction)
242 int64_t corrected_position = position;
243 if(direction == PLAY_REVERSE)
244 corrected_position--;
245 return playback_engine->brender_available(corrected_position);
251 Channel* RenderEngine::get_current_channel()
255 switch(config->vconfig->driver)
258 if(config->vconfig->buz_out_channel >= 0 &&
259 config->vconfig->buz_out_channel < channeldb->size())
261 return channeldb->get(config->vconfig->buz_out_channel);
264 case VIDEO4LINUX2JPEG:
265 case VIDEO4LINUX2MPEG:
272 CICache* RenderEngine::get_acache()
275 return playback_engine->audio_cache;
280 CICache* RenderEngine::get_vcache()
283 return playback_engine->video_cache;
288 void RenderEngine::set_acache(CICache *cache)
290 this->audio_cache = cache;
293 void RenderEngine::set_vcache(CICache *cache)
295 this->video_cache = cache;
299 double RenderEngine::get_tracking_position()
302 return playback_engine->get_tracking_position();
307 int RenderEngine::open_output()
309 if(command->realtime && !is_nested)
314 audio = new AudioDevice(mwindow);
319 video = new VideoDevice(mwindow);
322 // Initialize sharing
326 if(do_audio && do_video)
328 video->set_adevice(audio);
329 audio->set_vdevice(video);
334 // Retool playback configuration
337 if(audio->open_output(config->aconfig,
338 get_edl()->session->sample_rate,
339 adjusted_fragment_len,
340 get_edl()->session->audio_channels,
341 get_edl()->session->real_time_playback))
345 audio->set_software_positioning(
346 get_edl()->session->playback_software_position);
347 audio->start_playback();
353 video->open_output(config->vconfig,
354 get_edl()->session->frame_rate,
358 command->single_frame());
359 Channel *channel = get_current_channel();
360 if(channel) video->set_channel(channel);
361 video->set_quality(80);
362 video->set_cpus(preferences->processors);
369 void RenderEngine::reset_sync_position()
374 int64_t RenderEngine::sync_position()
377 // No danger of race conditions because the output devices are closed after all
381 return audio->current_position();
386 int64_t result = timer.get_scaled_difference(
387 get_edl()->session->sample_rate);
394 int RenderEngine::start_command()
396 if(command->realtime && !is_nested)
398 interrupt_lock->lock("RenderEngine::start_command");
399 start_lock->lock("RenderEngine::start_command 1");
401 start_lock->lock("RenderEngine::start_command 2");
402 start_lock->unlock();
407 void RenderEngine::arm_render_threads()
411 arender->arm_command();
416 vrender->arm_command();
421 void RenderEngine::start_render_threads()
423 // Synchronization timer. Gets reset once again after the first video frame.
428 arender->start_command();
433 vrender->start_command();
437 void RenderEngine::update_framerate(float framerate)
439 playback_engine->mwindow->session->actual_frame_rate = framerate;
440 playback_engine->mwindow->preferences_thread->update_framerate();
443 void RenderEngine::wait_render_threads()
447 arender->Thread::join();
452 vrender->Thread::join();
456 void RenderEngine::interrupt_playback()
458 interrupt_lock->lock("RenderEngine::interrupt_playback");
460 if(do_audio && arender)
462 arender->interrupt_playback();
465 if(do_video && vrender)
467 vrender->interrupt_playback();
469 interrupt_lock->unlock();
472 int RenderEngine::close_output()
474 // Nested engines share devices
497 void RenderEngine::get_output_levels(double *levels, int64_t position)
501 int history_entry = arender->get_history_number(arender->level_samples,
503 for(int i = 0; i < MAXCHANNELS; i++)
505 if(arender->audio_out[i])
506 levels[i] = arender->level_history[i][history_entry];
511 void RenderEngine::get_module_levels(ArrayList<double> *module_levels, int64_t position)
515 for(int i = 0; i < arender->total_modules; i++)
517 //printf("RenderEngine::get_module_levels %p %p\n", ((AModule*)arender->modules[i]), ((AModule*)arender->modules[i])->level_samples);
518 int history_entry = arender->get_history_number(((AModule*)arender->modules[i])->level_samples, position);
520 module_levels->append(((AModule*)arender->modules[i])->level_history[history_entry]);
529 void RenderEngine::run()
531 start_render_threads();
532 start_lock->unlock();
533 interrupt_lock->unlock();
535 wait_render_threads();
537 interrupt_lock->lock("RenderEngine::run");
542 playback_engine->tracking_position = playback_engine->get_tracking_position();
547 // Fix the tracking position
550 if(command->command == CURRENT_FRAME)
552 //printf("RenderEngine::run 4.1 %d\n", playback_engine->tracking_position);
553 playback_engine->tracking_position = command->playbackstart;
557 // Make sure transport doesn't issue a pause command next
558 //printf("RenderEngine::run 4.1 %d\n", playback_engine->tracking_position);
562 playback_engine->tracking_position =
563 (double)arender->current_position /
564 command->get_edl()->session->sample_rate;
568 playback_engine->tracking_position =
569 (double)vrender->current_position /
570 command->get_edl()->session->frame_rate;
574 if(!interrupted) playback_engine->command->command = STOP;
575 playback_engine->stop_tracking();
578 playback_engine->is_playing_back = 0;
581 input_lock->unlock();
582 interrupt_lock->unlock();