2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG.
\r
6 * CasparCG is free software: you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation, either version 3 of the License, or
\r
9 * (at your option) any later version.
\r
11 * CasparCG is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
\r
21 #include "../StdAfx.h"
\r
23 #include "decklink_consumer.h"
\r
25 #include "../util/util.h"
\r
27 #include "../interop/DeckLinkAPI_h.h"
\r
29 #include <core/video_format.h>
\r
31 #include <core/consumer/frame/read_frame.h>
\r
33 #include <common/concurrency/com_context.h>
\r
34 #include <common/diagnostics/graph.h>
\r
35 #include <common/exception/exceptions.h>
\r
36 #include <common/memory/memcpy.h>
\r
37 #include <common/memory/memclr.h>
\r
38 #include <common/utility/timer.h>
\r
40 #include <tbb/concurrent_queue.h>
\r
42 #include <boost/circular_buffer.hpp>
\r
43 #include <boost/timer.hpp>
\r
47 #pragma warning(push)
\r
48 #pragma warning(disable : 4996)
\r
50 #include <atlbase.h>
\r
53 #include <atlhost.h>
\r
55 #pragma warning(push)
\r
73 struct configuration
\r
75 size_t device_index;
\r
76 bool embedded_audio;
\r
82 , embedded_audio(false)
\r
83 , keyer(default_key)
\r
84 , latency(default_latency){}
\r
87 struct decklink_consumer : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback, boost::noncopyable
\r
89 static const size_t BUFFER_SIZE = 4;
\r
91 const configuration config_;
\r
93 CComPtr<IDeckLink> decklink_;
\r
94 CComQIPtr<IDeckLinkOutput> output_;
\r
95 CComQIPtr<IDeckLinkConfiguration> configuration_;
\r
96 CComQIPtr<IDeckLinkKeyer> keyer_;
\r
98 std::exception_ptr exception_;
\r
100 tbb::atomic<bool> is_running_;
\r
102 const std::wstring model_name_;
\r
103 const core::video_format_desc format_desc_;
\r
105 BMDTimeScale frame_time_scale_;
\r
106 BMDTimeValue frame_duration_;
\r
107 unsigned long frames_scheduled_;
\r
108 unsigned long audio_scheduled_;
\r
110 std::array<std::pair<void*, CComPtr<IDeckLinkMutableVideoFrame>>, BUFFER_SIZE+1> reserved_frames_;
\r
111 boost::circular_buffer<std::vector<short>> audio_container_;
\r
113 tbb::concurrent_bounded_queue<std::shared_ptr<const core::read_frame>> video_frame_buffer_;
\r
114 tbb::concurrent_bounded_queue<std::shared_ptr<const core::read_frame>> audio_frame_buffer_;
\r
116 std::shared_ptr<diagnostics::graph> graph_;
\r
117 boost::timer tick_timer_;
\r
120 decklink_consumer(const configuration& config, const core::video_format_desc& format_desc)
\r
122 , decklink_(get_device(config.device_index))
\r
123 , output_(decklink_)
\r
124 , configuration_(decklink_)
\r
125 , keyer_(decklink_)
\r
126 , model_name_(get_model_name(decklink_))
\r
127 , format_desc_(format_desc)
\r
128 , audio_container_(5)
\r
129 , frames_scheduled_(0)
\r
130 , audio_scheduled_(0)
\r
132 is_running_ = true;
\r
134 graph_ = diagnostics::create_graph(narrow(print()));
\r
135 graph_->add_guide("tick-time", 0.5);
\r
136 graph_->set_color("tick-time", diagnostics::color(0.1f, 0.7f, 0.8f));
\r
137 graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));
\r
138 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
\r
139 graph_->set_color("flushed-frame", diagnostics::color(0.3f, 0.3f, 0.6f));
\r
141 auto display_mode = get_display_mode(output_, format_desc_.format);
\r
142 if(display_mode == nullptr)
\r
143 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat."));
\r
145 display_mode->GetFrameRate(&frame_duration_, &frame_time_scale_);
\r
147 BMDDisplayModeSupport displayModeSupport;
\r
148 if(FAILED(output_->DoesSupportVideoMode(display_mode->GetDisplayMode(), bmdFormat8BitBGRA, bmdVideoOutputFlagDefault, &displayModeSupport, nullptr)) || displayModeSupport == bmdDisplayModeNotSupported)
\r
149 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Card does not support requested videoformat."));
\r
150 else if(displayModeSupport == bmdDisplayModeSupportedWithConversion)
\r
151 CASPAR_LOG(warning) << print() << " Display mode is supported with conversion.";
\r
153 if(config.embedded_audio)
\r
155 if(FAILED(output_->EnableAudioOutput(bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger, 2, bmdAudioOutputStreamTimestamped)))
\r
156 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable audio output."));
\r
158 if(FAILED(output_->SetAudioCallback(this)))
\r
159 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not set audio callback."));
\r
161 CASPAR_LOG(info) << print() << L" Enabled embedded-audio.";
\r
164 if(config.latency == normal_latency)
\r
166 configuration_->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, false);
\r
167 CASPAR_LOG(info) << print() << L" Enabled normal-latency mode";
\r
169 else if(config.latency == low_latency)
\r
171 configuration_->SetFlag(bmdDeckLinkConfigLowLatencyVideoOutput, true);
\r
172 CASPAR_LOG(info) << print() << L" Enabled low-latency mode";
\r
175 CASPAR_LOG(info) << print() << L" Uses driver latency settings.";
\r
177 if(config.keyer == internal_key)
\r
179 if(FAILED(keyer_->Enable(FALSE)))
\r
180 CASPAR_LOG(error) << print() << L" Failed to enable internal keyer.";
\r
181 else if(FAILED(keyer_->SetLevel(255)))
\r
182 CASPAR_LOG(error) << print() << L" Failed to set key-level to max.";
\r
184 CASPAR_LOG(info) << print() << L" Enabled internal keyer.";
\r
186 else if(config.keyer == external_key)
\r
188 if(FAILED(keyer_->Enable(TRUE)))
\r
189 CASPAR_LOG(error) << print() << L" Failed to enable external keyer.";
\r
190 else if(FAILED(keyer_->SetLevel(255)))
\r
191 CASPAR_LOG(error) << print() << L" Failed to set key-level to max.";
\r
193 CASPAR_LOG(info) << print() << L" Enabled external keyer.";
\r
196 CASPAR_LOG(info) << print() << L" Uses driver keyer settings.";
\r
198 if(FAILED(output_->EnableVideoOutput(display_mode->GetDisplayMode(), bmdVideoOutputFlagDefault)))
\r
199 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Could not enable video output."));
\r
201 if(FAILED(output_->SetScheduledFrameCompletionCallback(this)))
\r
202 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to set playback completion callback."));
\r
204 BOOST_FOREACH(auto& frame, reserved_frames_)
\r
206 if(FAILED(output_->CreateVideoFrame(format_desc_.width, format_desc_.height, format_desc_.size/format_desc_.height, bmdFormat8BitBGRA, bmdFrameFlagDefault, &frame.second)))
\r
207 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create frame."));
\r
209 if(FAILED(frame.second->GetBytes(&frame.first)))
\r
210 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to get frame bytes."));
\r
213 CASPAR_LOG(info) << print() << L" Buffer-depth: " << BUFFER_SIZE;
\r
215 for(size_t n = 0; n < BUFFER_SIZE; ++n)
\r
216 schedule_next_video(core::read_frame::empty());
\r
218 video_frame_buffer_.set_capacity(2);
\r
219 audio_frame_buffer_.set_capacity(2);
\r
221 if(config.embedded_audio)
\r
222 output_->BeginAudioPreroll();
\r
225 if(FAILED(output_->StartScheduledPlayback(0, frame_time_scale_, 1.0)))
\r
226 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to schedule playback."));
\r
229 CASPAR_LOG(info) << print() << L" Successfully initialized for " << format_desc_.name;
\r
232 ~decklink_consumer()
\r
234 is_running_ = false;
\r
235 video_frame_buffer_.clear();
\r
236 audio_frame_buffer_.clear();
\r
237 video_frame_buffer_.try_push(core::read_frame::empty());
\r
238 audio_frame_buffer_.try_push(core::read_frame::empty());
\r
240 if(output_ != nullptr)
\r
242 output_->StopScheduledPlayback(0, nullptr, 0);
\r
243 if(config_.embedded_audio)
\r
244 output_->DisableAudioOutput();
\r
245 output_->DisableVideoOutput();
\r
249 virtual HRESULT STDMETHODCALLTYPE QueryInterface (REFIID, LPVOID*) {return E_NOINTERFACE;}
\r
250 virtual ULONG STDMETHODCALLTYPE AddRef () {return 1;}
\r
251 virtual ULONG STDMETHODCALLTYPE Release () {return 1;}
\r
253 virtual HRESULT STDMETHODCALLTYPE ScheduledFrameCompleted(IDeckLinkVideoFrame* /*completedFrame*/, BMDOutputFrameCompletionResult result)
\r
258 if(result == bmdOutputFrameDisplayedLate)
\r
259 graph_->add_tag("late-frame");
\r
260 else if(result == bmdOutputFrameDropped)
\r
261 graph_->add_tag("dropped-frame");
\r
262 else if(result == bmdOutputFrameFlushed)
\r
263 graph_->add_tag("flushed-frame");
\r
265 std::shared_ptr<const core::read_frame> frame;
\r
266 video_frame_buffer_.pop(frame);
\r
267 schedule_next_video(safe_ptr<const core::read_frame>(frame));
\r
272 virtual HRESULT STDMETHODCALLTYPE ScheduledPlaybackHasStopped (void)
\r
274 is_running_ = false;
\r
275 CASPAR_LOG(info) << print() << L" Scheduled playback has stopped.";
\r
279 virtual HRESULT STDMETHODCALLTYPE RenderAudioSamples(BOOL preroll)
\r
288 if(FAILED(output_->StartScheduledPlayback(0, frame_time_scale_, 1.0)))
\r
289 BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to schedule playback."));
\r
292 std::shared_ptr<const core::read_frame> frame;
\r
293 audio_frame_buffer_.pop(frame);
\r
294 schedule_next_audio(safe_ptr<const core::read_frame>(frame));
\r
299 exception_ = std::current_exception();
\r
306 void schedule_next_audio(const safe_ptr<const core::read_frame>& frame)
\r
308 static std::vector<short> silence(48000, 0);
\r
310 int audio_samples = static_cast<size_t>(48000.0 / format_desc_.fps)*2; // Audio samples per channel
\r
312 const short* frame_audio_data = frame->audio_data().size() == audio_samples ? frame->audio_data().begin() : silence.data();
\r
313 audio_container_.push_back(std::vector<short>(frame_audio_data, frame_audio_data+audio_samples));
\r
315 if(FAILED(output_->ScheduleAudioSamples(audio_container_.back().data(), audio_samples/2, (audio_scheduled_++) * audio_samples/2, 48000, nullptr)))
\r
316 CASPAR_LOG(error) << print() << L" Failed to schedule audio.";
\r
319 void schedule_next_video(const safe_ptr<const core::read_frame>& frame)
\r
321 if(static_cast<size_t>(frame->image_data().size()) == format_desc_.size)
\r
322 fast_memcpy(reserved_frames_.front().first, frame->image_data().begin(), frame->image_data().size());
\r
324 fast_memclr(reserved_frames_.front().first, format_desc_.size);
\r
326 if(FAILED(output_->ScheduleVideoFrame(reserved_frames_.front().second, (frames_scheduled_++) * frame_duration_, frame_duration_, frame_time_scale_)))
\r
327 CASPAR_LOG(error) << print() << L" Failed to schedule video.";
\r
329 std::rotate(reserved_frames_.begin(), reserved_frames_.begin() + 1, reserved_frames_.end());
\r
330 graph_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()/format_desc_.interval)*0.5f);
\r
331 tick_timer_.restart();
\r
334 void send(const safe_ptr<const core::read_frame>& frame)
\r
339 if(exception_ != nullptr)
\r
340 std::rethrow_exception(exception_);
\r
342 video_frame_buffer_.push(frame);
\r
343 if(config_.embedded_audio)
\r
344 audio_frame_buffer_.push(frame);
\r
347 std::wstring print() const
\r
349 return model_name_ + L" [" + boost::lexical_cast<std::wstring>(config_.device_index) + L"]";
\r
353 struct decklink_consumer_proxy : public core::frame_consumer
\r
355 const configuration config_;
\r
357 com_context<decklink_consumer> context_;
\r
360 decklink_consumer_proxy(const configuration& config)
\r
362 , context_(L"decklink_consumer[" + boost::lexical_cast<std::wstring>(config.device_index) + L"]"){}
\r
364 void initialize(const core::video_format_desc& format_desc)
\r
366 context_.reset([&]{return new decklink_consumer(config_, format_desc);});
\r
369 void send(const safe_ptr<const core::read_frame>& frame)
\r
371 context_->send(frame);
\r
374 std::wstring print() const
\r
376 return context_->print();
\r
380 safe_ptr<core::frame_consumer> create_decklink_consumer(const std::vector<std::wstring>& params)
\r
382 if(params.size() < 1 || params[0] != L"DECKLINK")
\r
383 return core::frame_consumer::empty();
\r
385 configuration config;
\r
387 if(params.size() > 1)
\r
388 config.device_index = lexical_cast_or_default<int>(params[1], config.device_index);
\r
390 if(std::find(params.begin(), params.end(), L"INTERNAL_KEY") != params.end())
\r
391 config.keyer = internal_key;
\r
392 else if(std::find(params.begin(), params.end(), L"EXTERNAL_KEY") != params.end())
\r
393 config.keyer = external_key;
\r
395 if(std::find(params.begin(), params.end(), L"LOW_LATENCY") != params.end())
\r
396 config.latency = low_latency;
\r
397 else if(std::find(params.begin(), params.end(), L"NORMAL_LATENCY") != params.end())
\r
398 config.latency = normal_latency;
\r
400 config.embedded_audio = std::find(params.begin(), params.end(), L"EMBEDDED_AUDIO") != params.end();
\r
402 return make_safe<decklink_consumer_proxy>(config);
\r
405 safe_ptr<core::frame_consumer> create_decklink_consumer(const boost::property_tree::ptree& ptree)
\r
407 configuration config;
\r
409 auto key_str = ptree.get("key", "default");
\r
410 if(key_str == "internal")
\r
411 config.keyer = internal_key;
\r
412 else if(key_str == "external")
\r
413 config.keyer = external_key;
\r
415 auto latency_str = ptree.get("latency", "default");
\r
416 if(latency_str == "normal")
\r
417 config.latency = normal_latency;
\r
418 else if(latency_str == "low")
\r
419 config.latency = low_latency;
\r
421 config.device_index = ptree.get("device", 0);
\r
422 config.embedded_audio = ptree.get("embedded-audio", false);
\r
424 return make_safe<decklink_consumer_proxy>(config);
\r