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"
32 #include "playbackengine.h"
33 #include "preferences.h"
34 #include "preferencesthread.h"
35 #include "renderengine.h"
36 #include "mainsession.h"
38 #include "transportque.h"
39 #include "videodevice.h"
41 #include "workarounds.h"
45 RenderEngine::RenderEngine(PlaybackEngine *playback_engine,
46 Preferences *preferences,
51 this->playback_engine = playback_engine;
52 this->output = output;
53 this->is_nested = is_nested;
56 config = new PlaybackConfig;
62 this->preferences = new Preferences;
63 this->command = new TransportCommand;
64 this->preferences->copy_from(preferences);
69 if(playback_engine && playback_engine->mwindow)
70 mwindow = playback_engine->mwindow;
76 input_lock = new Condition(1, "RenderEngine::input_lock");
77 start_lock = new Condition(1, "RenderEngine::start_lock");
78 output_lock = new Condition(1, "RenderEngine::output_lock");
79 interrupt_lock = new Mutex("RenderEngine::interrupt_lock");
80 first_frame_lock = new Condition(1, "RenderEngine::first_frame_lock");
83 RenderEngine::~RenderEngine()
88 if(arender) delete arender;
89 if(vrender) delete vrender;
93 delete interrupt_lock;
94 delete first_frame_lock;
96 edl->Garbage::remove_user();
99 EDL* RenderEngine::get_edl()
101 // return command->get_edl();
105 int RenderEngine::arm_command(TransportCommand *command)
108 // Prevent this renderengine from accepting another command until finished.
109 // Since the renderengine is often deleted after the input_lock command it must
110 // be locked here as well as in the calling routine.
111 if(debug) printf("RenderEngine::arm_command %d\n", __LINE__);
114 input_lock->lock("RenderEngine::arm_command");
119 edl->create_objects();
120 edl->copy_all(command->get_edl());
122 this->command->copy_from(command);
124 // Fix background rendering asset to use current dimensions and ignore
126 preferences->brender_asset->frame_rate = command->get_edl()->session->frame_rate;
127 preferences->brender_asset->width = command->get_edl()->session->output_w;
128 preferences->brender_asset->height = command->get_edl()->session->output_h;
129 preferences->brender_asset->use_header = 0;
130 preferences->brender_asset->layers = 1;
131 preferences->brender_asset->video_data = 1;
136 // Retool configuration for this node
137 this->config->copy_from(command->get_edl()->session->playback_config);
138 VideoOutConfig *vconfig = this->config->vconfig;
139 AudioOutConfig *aconfig = this->config->aconfig;
140 if(command->realtime)
142 if(command->single_frame() && vconfig->driver != PLAYBACK_X11_GL)
144 vconfig->driver = PLAYBACK_X11;
149 vconfig->driver = PLAYBACK_X11;
158 fragment_len = aconfig->fragment_size;
159 // Larger of audio_module_fragment and fragment length adjusted for speed
160 // Extra memory must be allocated for rendering slow motion.
161 float speed = command->get_speed();
162 adjusted_fragment_len = speed >= 1.0 ?
163 (int64_t)(aconfig->fragment_size * speed + 0.5) :
164 (int64_t)(aconfig->fragment_size / speed + 0.5) ;
165 if(adjusted_fragment_len < aconfig->fragment_size)
166 adjusted_fragment_len = aconfig->fragment_size;
169 // Set lock so audio doesn't start until video has started.
172 while(first_frame_lock->get_value() > 0)
173 first_frame_lock->lock("RenderEngine::arm_command");
176 // Set lock so audio doesn't wait for video which is never to come.
178 while(first_frame_lock->get_value() <= 0)
179 first_frame_lock->unlock();
183 create_render_threads();
184 arm_render_threads();
185 if(debug) printf("RenderEngine::arm_command %d\n", __LINE__);
190 void RenderEngine::get_duty()
195 //printf("RenderEngine::get_duty %d\n", __LINE__);
196 if(!command->single_frame() &&
197 get_edl()->tracks->playable_audio_tracks() &&
198 get_edl()->session->audio_channels)
203 //printf("RenderEngine::get_duty %d\n", __LINE__);
204 if(get_edl()->tracks->playable_video_tracks())
206 //printf("RenderEngine::get_duty %d\n", __LINE__);
211 void RenderEngine::create_render_threads()
213 if(do_video && !vrender)
215 vrender = new VRender(this);
218 if(do_audio && !arender)
220 arender = new ARender(this);
225 int RenderEngine::get_output_w()
227 return get_edl()->session->output_w;
230 int RenderEngine::get_output_h()
232 return get_edl()->session->output_h;
235 int RenderEngine::brender_available(int position, int direction)
239 int64_t corrected_position = position;
240 if(direction == PLAY_REVERSE)
241 corrected_position--;
242 return playback_engine->brender_available(corrected_position);
249 CICache* RenderEngine::get_acache()
252 return playback_engine->audio_cache;
257 CICache* RenderEngine::get_vcache()
260 return playback_engine->video_cache;
265 void RenderEngine::set_acache(CICache *cache)
267 this->audio_cache = cache;
270 void RenderEngine::set_vcache(CICache *cache)
272 this->video_cache = cache;
276 double RenderEngine::get_tracking_position()
279 return playback_engine->get_tracking_position();
284 int RenderEngine::open_output()
286 if(command->realtime && !is_nested)
291 audio = new AudioDevice(mwindow);
296 video = new VideoDevice(mwindow);
299 // Initialize sharing
303 if(do_audio && do_video)
305 video->set_adevice(audio);
306 audio->set_vdevice(video);
311 // Retool playback configuration
314 if(audio->open_output(config->aconfig,
315 get_edl()->session->sample_rate,
316 adjusted_fragment_len,
317 get_edl()->session->audio_channels,
318 get_edl()->session->real_time_playback))
322 audio->set_software_positioning(
323 get_edl()->session->playback_software_position);
324 audio->start_playback();
330 video->open_output(config->vconfig,
331 get_edl()->session->frame_rate,
335 command->single_frame());
336 video->set_quality(80);
337 video->set_cpus(preferences->processors);
344 void RenderEngine::reset_sync_position()
349 int64_t RenderEngine::sync_position()
352 // No danger of race conditions because the output devices are closed after all
356 return audio->current_position();
361 int64_t result = timer.get_scaled_difference(
362 get_edl()->session->sample_rate);
369 int RenderEngine::start_command()
371 if(command->realtime && !is_nested)
373 interrupt_lock->lock("RenderEngine::start_command");
374 start_lock->lock("RenderEngine::start_command 1");
376 start_lock->lock("RenderEngine::start_command 2");
377 start_lock->unlock();
382 void RenderEngine::arm_render_threads()
386 arender->arm_command();
391 vrender->arm_command();
396 void RenderEngine::start_render_threads()
398 // Synchronization timer. Gets reset once again after the first video frame.
403 arender->start_command();
408 vrender->start_command();
412 void RenderEngine::update_framerate(float framerate)
414 playback_engine->mwindow->session->actual_frame_rate = framerate;
415 playback_engine->mwindow->preferences_thread->update_framerate();
418 void RenderEngine::wait_render_threads()
422 arender->Thread::join();
427 vrender->Thread::join();
431 void RenderEngine::interrupt_playback()
433 interrupt_lock->lock("RenderEngine::interrupt_playback");
435 if(do_audio && arender)
437 arender->interrupt_playback();
440 if(do_video && vrender)
442 vrender->interrupt_playback();
444 interrupt_lock->unlock();
447 int RenderEngine::close_output()
449 // Nested engines share devices
472 void RenderEngine::get_output_levels(double *levels, int64_t position)
476 int history_entry = arender->get_history_number(arender->level_samples,
478 for(int i = 0; i < MAXCHANNELS; i++)
480 if(arender->audio_out[i])
481 levels[i] = arender->level_history[i][history_entry];
486 void RenderEngine::get_module_levels(ArrayList<double> *module_levels, int64_t position)
490 for(int i = 0; i < arender->total_modules; i++)
492 //printf("RenderEngine::get_module_levels %p %p\n", ((AModule*)arender->modules[i]), ((AModule*)arender->modules[i])->level_samples);
493 int history_entry = arender->get_history_number(((AModule*)arender->modules[i])->level_samples, position);
495 module_levels->append(((AModule*)arender->modules[i])->level_history[history_entry]);
504 void RenderEngine::run()
506 start_render_threads();
507 start_lock->unlock();
508 interrupt_lock->unlock();
510 wait_render_threads();
512 interrupt_lock->lock("RenderEngine::run");
517 playback_engine->tracking_position = playback_engine->get_tracking_position();
522 // Fix the tracking position
525 if(command->command == CURRENT_FRAME)
527 //printf("RenderEngine::run 4.1 %d\n", playback_engine->tracking_position);
528 playback_engine->tracking_position = command->playbackstart;
532 // Make sure transport doesn't issue a pause command next
533 //printf("RenderEngine::run 4.1 %d\n", playback_engine->tracking_position);
537 playback_engine->tracking_position =
538 (double)arender->current_position /
539 command->get_edl()->session->sample_rate;
543 playback_engine->tracking_position =
544 (double)vrender->current_position /
545 command->get_edl()->session->frame_rate;
549 if(!interrupted) playback_engine->command->command = STOP;
550 playback_engine->stop_tracking();
553 playback_engine->is_playing_back = 0;
556 input_lock->unlock();
557 interrupt_lock->unlock();