/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
*\r
-* This file is part of CasparCG.\r
+* This file is part of CasparCG (www.casparcg.com).\r
*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-* GNU General Public License for more details.\r
-\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
*\r
+* Author: Robert Nagy, ronag89@gmail.com\r
*/\r
- \r
+\r
#include "../stdafx.h"\r
\r
#include "decklink_producer.h"\r
#include "../util/util.h"\r
\r
#include "../../ffmpeg/producer/filter/filter.h"\r
-#include "../../ffmpeg/producer/util.h"\r
-#include "../../ffmpeg/producer/frame_muxer.h"\r
+#include "../../ffmpeg/producer/util/util.h"\r
+#include "../../ffmpeg/producer/muxer/frame_muxer.h"\r
+#include "../../ffmpeg/producer/muxer/display_mode.h"\r
\r
-#include <common/log/log.h>\r
+#include <common/concurrency/executor.h>\r
#include <common/diagnostics/graph.h>\r
-#include <common/exception/exceptions.h>\r
-#include <common/memory/memclr.h>\r
+#include <common/except.h>\r
+#include <common/log.h>\r
+#include <common/param.h>\r
\r
-#include <core/mixer/write_frame.h>\r
-#include <core/producer/frame/frame_transform.h>\r
-#include <core/producer/frame/frame_factory.h>\r
+#include <core/frame/write_frame.h>\r
+#include <core/frame/frame_transform.h>\r
+#include <core/frame/frame_factory.h>\r
\r
-#include <agents.h>\r
-#include <agents_extras.h>\r
-#include <ppl.h>\r
+#include <tbb/concurrent_queue.h>\r
\r
#include <boost/algorithm/string.hpp>\r
#include <boost/foreach.hpp>\r
+#include <boost/property_tree/ptree.hpp>\r
#include <boost/timer.hpp>\r
\r
#if defined(_MSC_VER)\r
\r
namespace caspar { namespace decklink {\r
\r
-typedef std::pair<CComPtr<IDeckLinkVideoInputFrame>, CComPtr<IDeckLinkAudioInputPacket>> frame_packet;\r
-\r
class decklink_producer : boost::noncopyable, public IDeckLinkInputCallback\r
{ \r
- Concurrency::ITarget<frame_packet>& target_;\r
+ spl::shared_ptr<diagnostics::graph> graph_;\r
+ boost::timer tick_timer_;\r
+ boost::timer frame_timer_;\r
\r
- CComPtr<IDeckLink> decklink_;\r
- CComQIPtr<IDeckLinkInput> input_;\r
+ CComPtr<IDeckLink> decklink_;\r
+ CComQIPtr<IDeckLinkInput> input_;\r
+ CComQIPtr<IDeckLinkAttributes > attributes_;\r
\r
- const std::wstring model_name_;\r
- const core::video_format_desc format_desc_;\r
- const size_t device_index_;\r
+ const std::wstring model_name_;\r
+ const size_t device_index_;\r
+ const std::wstring filter_;\r
+ \r
+ core::video_format_desc format_desc_;\r
+ std::vector<int> audio_cadence_;\r
+ boost::circular_buffer<size_t> sync_buffer_;\r
+ ffmpeg::frame_muxer muxer_;\r
+ \r
+ tbb::atomic<int> flags_;\r
+ spl::shared_ptr<core::frame_factory> frame_factory_;\r
+\r
+ tbb::concurrent_bounded_queue<\r
+ spl::shared_ptr<core::draw_frame>> frame_buffer_;\r
\r
- safe_ptr<diagnostics::graph> graph_;\r
- boost::timer tick_timer_;\r
- boost::timer frame_timer_;\r
+ std::exception_ptr exception_; \r
\r
public:\r
- decklink_producer(Concurrency::ITarget<frame_packet>& target, const core::video_format_desc& format_desc, size_t device_index)\r
- : target_(target)\r
- , decklink_(get_device(device_index))\r
+ decklink_producer(const core::video_format_desc& format_desc, size_t device_index, const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filter)\r
+ : decklink_(get_device(device_index))\r
, input_(decklink_)\r
+ , attributes_(decklink_)\r
, model_name_(get_model_name(decklink_))\r
- , format_desc_(format_desc)\r
, device_index_(device_index)\r
- , graph_ (diagnostics::create_graph("", false))\r
- { \r
- graph_->add_guide("tick-time", 0.5);\r
+ , filter_(filter)\r
+ , format_desc_(format_desc)\r
+ , audio_cadence_(format_desc.audio_cadence)\r
+ , muxer_(format_desc.fps, frame_factory, filter)\r
+ , sync_buffer_(format_desc.audio_cadence.size())\r
+ , frame_factory_(frame_factory)\r
+ { \r
+ flags_ = 0;\r
+ frame_buffer_.set_capacity(2);\r
+ \r
graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f)); \r
graph_->set_color("late-frame", diagnostics::color(0.6f, 0.3f, 0.3f));\r
graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
graph_->set_color("output-buffer", diagnostics::color(0.0f, 1.0f, 0.0f));\r
- graph_->update_text(narrow(print()));\r
-\r
+ graph_->set_text(print());\r
+ diagnostics::register_graph(graph_);\r
+ \r
auto display_mode = get_display_mode(input_, format_desc_.format, bmdFormat8BitYUV, bmdVideoInputFlagDefault);\r
\r
// NOTE: bmdFormat8BitARGB is currently not supported by any decklink card. (2011-05-08)\r
if(FAILED(input_->EnableVideoInput(display_mode, bmdFormat8BitYUV, 0))) \r
BOOST_THROW_EXCEPTION(caspar_exception() \r
- << msg_info(narrow(print()) + " Could not enable video input.")\r
+ << msg_info(print() + L" Could not enable video input.")\r
<< boost::errinfo_api_function("EnableVideoInput"));\r
\r
if(FAILED(input_->EnableAudioInput(bmdAudioSampleRate48kHz, bmdAudioSampleType32bitInteger, format_desc_.audio_channels))) \r
BOOST_THROW_EXCEPTION(caspar_exception() \r
- << msg_info(narrow(print()) + " Could not enable audio input.")\r
+ << msg_info(print() + L" Could not enable audio input.")\r
<< boost::errinfo_api_function("EnableAudioInput"));\r
\r
if (FAILED(input_->SetCallback(this)) != S_OK)\r
BOOST_THROW_EXCEPTION(caspar_exception() \r
- << msg_info(narrow(print()) + " Failed to set input callback.")\r
+ << msg_info(print() + L" Failed to set input callback.")\r
<< boost::errinfo_api_function("SetCallback"));\r
\r
if(FAILED(input_->StartStreams()))\r
BOOST_THROW_EXCEPTION(caspar_exception() \r
- << msg_info(narrow(print()) + " Failed to start input stream.")\r
+ << msg_info(print() + L" Failed to start input stream.")\r
<< boost::errinfo_api_function("StartStreams"));\r
-\r
- CASPAR_LOG(info) << print() << L" Successfully Initialized.";\r
-\r
- graph_->start();\r
}\r
\r
~decklink_producer()\r
{\r
- Concurrency::scoped_oversubcription_token oversubscribe;\r
if(input_ != nullptr) \r
{\r
input_->StopStreams();\r
\r
virtual HRESULT STDMETHODCALLTYPE VideoInputFrameArrived(IDeckLinkVideoInputFrame* video, IDeckLinkAudioInputPacket* audio)\r
{ \r
- if(!Concurrency::asend(target_, frame_packet(CComPtr<IDeckLinkVideoInputFrame>(video), CComPtr<IDeckLinkAudioInputPacket>(audio))))\r
- graph_->add_tag("dropped-frame");\r
+ if(!video)\r
+ return S_OK;\r
+\r
+ try\r
+ {\r
+ graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);\r
+ tick_timer_.restart();\r
+\r
+ frame_timer_.restart();\r
+\r
+ // PUSH\r
+\r
+ void* bytes = nullptr;\r
+ if(FAILED(video->GetBytes(&bytes)) || !bytes)\r
+ return S_OK;\r
+ \r
+ spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free); \r
+ avcodec_get_frame_defaults(av_frame.get());\r
+ \r
+ av_frame->data[0] = reinterpret_cast<uint8_t*>(bytes);\r
+ av_frame->linesize[0] = video->GetRowBytes(); \r
+ av_frame->format = PIX_FMT_UYVY422;\r
+ av_frame->width = video->GetWidth();\r
+ av_frame->height = video->GetHeight();\r
+ av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;\r
+ av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper ? 1 : 0;\r
+ \r
+ std::shared_ptr<core::audio_buffer> audio_buffer;\r
+\r
+ // It is assumed that audio is always equal or ahead of video.\r
+ if(audio && SUCCEEDED(audio->GetBytes(&bytes)) && bytes)\r
+ {\r
+ auto sample_frame_count = audio->GetSampleFrameCount();\r
+ auto audio_data = reinterpret_cast<int32_t*>(bytes);\r
+ audio_buffer = std::make_shared<core::audio_buffer>(audio_data, audio_data + sample_frame_count*format_desc_.audio_channels);\r
+ }\r
+ else \r
+ audio_buffer = std::make_shared<core::audio_buffer>(audio_cadence_.front(), 0);\r
+ \r
+ // Note: Uses 1 step rotated cadence for 1001 modes (1602, 1602, 1601, 1602, 1601)\r
+ // This cadence fills the audio mixer most optimally.\r
+\r
+ sync_buffer_.push_back(audio_buffer->size()); \r
+ if(!boost::range::equal(sync_buffer_, audio_cadence_))\r
+ {\r
+ CASPAR_LOG(trace) << print() << L" Syncing audio.";\r
+ return S_OK;\r
+ }\r
+\r
+ muxer_.push(audio_buffer);\r
+ muxer_.push(av_frame, flags_); \r
+ \r
+ boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);\r
+ \r
+ // POLL\r
+ \r
+ for(auto frame = muxer_.poll(); frame; frame = muxer_.poll())\r
+ {\r
+ if(!frame_buffer_.try_push(spl::make_shared_ptr(frame)))\r
+ graph_->set_tag("dropped-frame");\r
+ }\r
+\r
+ graph_->set_value("frame-time", frame_timer_.elapsed()*format_desc_.fps*0.5);\r
+\r
+ graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity())); \r
+ }\r
+ catch(...)\r
+ {\r
+ exception_ = std::current_exception();\r
+ return E_FAIL;\r
+ }\r
+\r
return S_OK;\r
}\r
- \r
+ \r
+ spl::shared_ptr<core::draw_frame> get_frame(int flags)\r
+ {\r
+ if(exception_ != nullptr)\r
+ std::rethrow_exception(exception_);\r
+\r
+ flags_ = flags;\r
+\r
+ spl::shared_ptr<core::draw_frame> frame = core::draw_frame::late();\r
+ if(!frame_buffer_.try_pop(frame))\r
+ graph_->set_tag("late-frame");\r
+ graph_->set_value("output-buffer", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity())); \r
+ return frame;\r
+ }\r
+ \r
std::wstring print() const\r
{\r
- return model_name_ + L" [" + boost::lexical_cast<std::wstring>(device_index_) + L"]";\r
+ return model_name_ + L" [" + boost::lexical_cast<std::wstring>(device_index_) + L"|" + format_desc_.name + L"]";\r
}\r
};\r
\r
-class decklink_producer_proxy : public Concurrency::agent, public core::frame_producer\r
+class decklink_producer_proxy : public core::frame_producer\r
{ \r
- Concurrency::bounded_buffer<safe_ptr<AVFrame>> video_frames_;\r
- Concurrency::bounded_buffer<safe_ptr<core::audio_buffer>> audio_buffers_;\r
- Concurrency::bounded_buffer<safe_ptr<core::basic_frame>> muxed_frames_;\r
-\r
- const core::video_format_desc format_desc_;\r
- const size_t device_index_;\r
-\r
- safe_ptr<core::basic_frame> last_frame_;\r
- const int64_t length_;\r
-\r
- ffmpeg::filter filter_;\r
- \r
- ffmpeg::frame_muxer2 muxer_;\r
-\r
- mutable Concurrency::single_assignment<std::wstring> print_;\r
-\r
- safe_ptr<Concurrency::semaphore> semaphore_;\r
-\r
- volatile bool is_running_;\r
+ std::unique_ptr<decklink_producer> producer_;\r
+ const uint32_t length_;\r
+ spl::shared_ptr<core::draw_frame> last_frame_;\r
+ executor executor_;\r
public:\r
-\r
- explicit decklink_producer_proxy(const safe_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, size_t device_index, const std::wstring& filter_str, int64_t length)\r
- : video_frames_(1)\r
- , audio_buffers_(1)\r
- , muxed_frames_(1)\r
- , format_desc_(format_desc)\r
- , device_index_(device_index)\r
- , last_frame_(core::basic_frame::empty())\r
+ explicit decklink_producer_proxy(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, size_t device_index, const std::wstring& filter_str, uint32_t length)\r
+ : executor_(L"decklink_producer[" + boost::lexical_cast<std::wstring>(device_index) + L"]")\r
, length_(length)\r
- , filter_(filter_str)\r
- , muxer_(&video_frames_, &audio_buffers_, muxed_frames_, ffmpeg::double_rate(filter_str) ? format_desc.fps * 2.0 : format_desc.fps, frame_factory)\r
- , is_running_(true)\r
- , semaphore_(make_safe<Concurrency::semaphore>(3))\r
+ , last_frame_(core::draw_frame::empty())\r
{\r
- agent::start();\r
+ executor_.invoke([=]\r
+ {\r
+ CoInitialize(nullptr);\r
+ producer_.reset(new decklink_producer(format_desc, device_index, frame_factory, filter_str));\r
+ });\r
}\r
\r
~decklink_producer_proxy()\r
- {\r
- is_running_ = false;\r
- agent::wait(this);\r
+ { \r
+ executor_.invoke([=]\r
+ {\r
+ producer_.reset();\r
+ CoUninitialize();\r
+ });\r
}\r
+ \r
+ // frame_producer\r
\r
- virtual safe_ptr<core::basic_frame> receive(int)\r
+ virtual spl::shared_ptr<core::draw_frame> receive(int flags) override\r
{\r
- auto frame = core::basic_frame::late();\r
+ auto frame = producer_->get_frame(flags);\r
\r
- try\r
- {\r
- last_frame_ = frame = Concurrency::receive(muxed_frames_);\r
- }\r
- catch(Concurrency::operation_timed_out&)\r
- { \r
- //graph_->add_tag("underflow"); \r
- }\r
+ if(frame != core::draw_frame::late())\r
+ last_frame_ = frame;\r
\r
return frame;\r
}\r
\r
- virtual safe_ptr<core::basic_frame> last_frame() const\r
+ virtual spl::shared_ptr<core::draw_frame> last_frame() const override\r
{\r
- return disable_audio(last_frame_);\r
+ return core::draw_frame::still(last_frame_);\r
}\r
- \r
- virtual int64_t nb_frames() const \r
+ \r
+ virtual uint32_t nb_frames() const override\r
{\r
return length_;\r
}\r
\r
- std::wstring print() const\r
+ virtual std::wstring print() const override\r
{\r
- return print_.value();\r
+ return producer_->print();\r
}\r
-\r
- virtual void run()\r
+ \r
+ virtual std::wstring name() const override\r
{\r
- try\r
- {\r
- struct co_init\r
- {\r
- co_init() {CoInitialize(NULL);}\r
- ~co_init() {CoUninitialize();}\r
- } init;\r
- \r
- Concurrency::bounded_buffer<frame_packet> input_buffer(2);\r
-\r
- std::unique_ptr<decklink_producer> producer;\r
- { \r
- Concurrency::scoped_oversubcription_token oversubscribe;\r
- producer.reset(new decklink_producer(input_buffer, format_desc_, device_index_));\r
- }\r
-\r
- Concurrency::send(print_, producer->print());\r
-\r
- while(is_running_)\r
- {\r
- auto packet = Concurrency::receive(input_buffer);\r
- auto video = packet.first;\r
- auto audio = packet.second;\r
- \r
- void* bytes = nullptr;\r
- if(FAILED(video->GetBytes(&bytes)) || !bytes)\r
- continue;\r
- \r
- safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free); \r
- avcodec_get_frame_defaults(av_frame.get());\r
- \r
- av_frame->data[0] = reinterpret_cast<uint8_t*>(bytes);\r
- av_frame->linesize[0] = video->GetRowBytes(); \r
- av_frame->format = PIX_FMT_UYVY422;\r
- av_frame->width = video->GetWidth();\r
- av_frame->height = video->GetHeight();\r
- av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;\r
- av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper ? 1 : 0;\r
- \r
- filter_.push(av_frame);\r
-\r
- Concurrency::parallel_invoke(\r
- [&]\r
- {\r
- while(true)\r
- {\r
- auto frame = filter_.poll();\r
- if(!frame)\r
- break;\r
- Concurrency::send(video_frames_, make_safe_ptr(frame));\r
- }\r
- },\r
- [&]\r
- { \r
- // It is assumed that audio is always equal or ahead of video.\r
- if(audio && SUCCEEDED(audio->GetBytes(&bytes)))\r
- {\r
- auto sample_frame_count = audio->GetSampleFrameCount();\r
- auto audio_data = reinterpret_cast<int32_t*>(bytes);\r
- Concurrency::send(audio_buffers_, make_safe<core::audio_buffer>(audio_data, audio_data + sample_frame_count*format_desc_.audio_channels));\r
- }\r
- else\r
- Concurrency::send(audio_buffers_, ffmpeg::empty_audio()); \r
- });\r
- }\r
-\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- }\r
- \r
- CASPAR_LOG(info) << print() << L" Successfully Uninitialized."; \r
+ return L"decklink";\r
+ }\r
\r
- done();\r
+ virtual boost::property_tree::wptree info() const override\r
+ {\r
+ boost::property_tree::wptree info;\r
+ info.add(L"type", L"decklink");\r
+ return info;\r
}\r
};\r
\r
-safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
+spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::vector<std::wstring>& params)\r
{\r
if(params.empty() || !boost::iequals(params[0], "decklink"))\r
return core::frame_producer::empty();\r
\r
- auto device_index = core::get_param(L"DEVICE", params, 1);\r
- auto filter_str = core::get_param<std::wstring>(L"FILTER", params, L""); \r
- auto length = core::get_param(L"LENGTH", params, std::numeric_limits<int64_t>::max()); \r
+ auto device_index = get_param(L"DEVICE", params, -1);\r
+ if(device_index == -1)\r
+ device_index = boost::lexical_cast<int>(params.at(1));\r
+\r
+ auto filter_str = get_param(L"FILTER", params); \r
+ auto length = get_param(L"LENGTH", params, std::numeric_limits<uint32_t>::max()); \r
+ auto format_desc = core::video_format_desc(get_param(L"FORMAT", params, L"INVALID"));\r
\r
boost::replace_all(filter_str, L"DEINTERLACE", L"YADIF=0:-1");\r
boost::replace_all(filter_str, L"DEINTERLACE_BOB", L"YADIF=1:-1");\r
-\r
- auto format_desc = core::video_format_desc::get(core::get_param<std::wstring>(L"FORMAT", params, L"INVALID"));\r
-\r
+ \r
if(format_desc.format == core::video_format::invalid)\r
- format_desc = frame_factory->get_video_format_desc();\r
+ format_desc = frame_factory->video_format_desc();\r
\r
- return make_safe<decklink_producer_proxy>(frame_factory, format_desc, device_index, filter_str, length);\r
+ return core::wrap_producer(spl::make_shared<decklink_producer_proxy>(frame_factory, format_desc, device_index, filter_str, length));\r
}\r
\r
}}
\ No newline at end of file