]> git.sesse.net Git - casparcg/blob - core/consumer/frame_consumer.cpp
49309380f1df92baa6e0bca9437461b0d281ade5
[casparcg] / core / consumer / frame_consumer.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\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
10 *\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
15 *\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
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "../StdAfx.h"\r
23 \r
24 #include "frame_consumer.h"\r
25 \r
26 #include <common/env.h>\r
27 #include <common/memory/safe_ptr.h>\r
28 #include <common/exception/exceptions.h>\r
29 #include <core/video_format.h>\r
30 #include <core/frame.h>\r
31 \r
32 #include <boost/circular_buffer.hpp>\r
33 \r
34 namespace caspar { namespace core {\r
35                 \r
36 std::vector<const consumer_factory_t> g_factories;\r
37 \r
38 void register_consumer_factory(const consumer_factory_t& factory)\r
39 {\r
40         g_factories.push_back(factory);\r
41 }\r
42 \r
43 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
44 {\r
45         if(params.empty())\r
46                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));\r
47         \r
48         auto consumer = frame_consumer::empty();\r
49         std::any_of(g_factories.begin(), g_factories.end(), [&](const consumer_factory_t& factory) -> bool\r
50                 {\r
51                         try\r
52                         {\r
53                                 consumer = factory(params);\r
54                         }\r
55                         catch(...)\r
56                         {\r
57                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
58                         }\r
59                         return consumer != frame_consumer::empty();\r
60                 });\r
61 \r
62         if(consumer == frame_consumer::empty())\r
63                 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax."));\r
64 \r
65         return consumer;\r
66 }\r
67 \r
68 // This class is used to guarantee that audio cadence is correct. This is important for NTSC audio.\r
69 class cadence_guard : public frame_consumer\r
70 {\r
71         safe_ptr<frame_consumer>                consumer_;\r
72         std::vector<int>                                audio_cadence_;\r
73         boost::circular_buffer<int>     sync_buffer_;\r
74 public:\r
75         cadence_guard(const safe_ptr<frame_consumer>& consumer)\r
76                 : consumer_(consumer)\r
77         {\r
78         }\r
79         \r
80         virtual void initialize(const video_format_desc& format_desc, int channel_index) override\r
81         {\r
82                 audio_cadence_  = format_desc.audio_cadence;\r
83                 sync_buffer_    = boost::circular_buffer<int>(format_desc.audio_cadence.size());\r
84                 consumer_->initialize(format_desc, channel_index);\r
85         }\r
86 \r
87         virtual bool send(const safe_ptr<const frame>& frame) override\r
88         {               \r
89                 if(audio_cadence_.size() == 1)\r
90                         return consumer_->send(frame);\r
91 \r
92                 bool result = true;\r
93                 \r
94                 if(boost::range::equal(sync_buffer_, audio_cadence_) && audio_cadence_.front() == static_cast<int>(frame->audio_data().size())) \r
95                 {       \r
96                         // Audio sent so far is in sync, now we can send the next chunk.\r
97                         result = consumer_->send(frame);\r
98                         boost::range::rotate(audio_cadence_, std::begin(audio_cadence_)+1);\r
99                 }\r
100                 else\r
101                         CASPAR_LOG(trace) << print() << L" Syncing audio.";\r
102 \r
103                 sync_buffer_.push_back(static_cast<int>(frame->audio_data().size()));\r
104                 \r
105                 return result;\r
106         }\r
107 \r
108         virtual std::wstring print() const override\r
109         {\r
110                 return consumer_->print();\r
111         }\r
112 \r
113         virtual boost::property_tree::wptree info() const override\r
114         {\r
115                 return consumer_->info();\r
116         }\r
117 \r
118         virtual bool has_synchronization_clock() const override\r
119         {\r
120                 return consumer_->has_synchronization_clock();\r
121         }\r
122 \r
123         virtual int buffer_depth() const override\r
124         {\r
125                 return consumer_->buffer_depth();\r
126         }\r
127 \r
128         virtual int index() const override\r
129         {\r
130                 return consumer_->index();\r
131         }\r
132 };\r
133 \r
134 safe_ptr<frame_consumer> create_consumer_cadence_guard(const safe_ptr<frame_consumer>& consumer)\r
135 {\r
136         return make_safe<cadence_guard>(std::move(consumer));\r
137 }\r
138 \r
139 const safe_ptr<frame_consumer>& frame_consumer::empty()\r
140 {\r
141         struct empty_frame_consumer : public frame_consumer\r
142         {\r
143                 virtual bool send(const safe_ptr<const frame>&) override {return false;}\r
144                 virtual void initialize(const video_format_desc&, int) override{}\r
145                 virtual std::wstring print() const override {return L"empty";}\r
146                 virtual bool has_synchronization_clock() const override {return false;}\r
147                 virtual int buffer_depth() const override {return 0;};\r
148                 virtual int index() const{return -1;}\r
149                 virtual boost::property_tree::wptree info() const override\r
150                 {\r
151                         boost::property_tree::wptree info;\r
152                         info.add(L"type", L"empty-consumer");\r
153                         return info;\r
154                 }\r
155         };\r
156         static safe_ptr<frame_consumer> consumer = make_safe<empty_frame_consumer>();\r
157         return consumer;\r
158 }\r
159 \r
160 }}