]> git.sesse.net Git - casparcg/blob - core/consumer/frame_consumer.cpp
git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches...
[casparcg] / core / consumer / frame_consumer.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\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 */\r
20 #include "../StdAfx.h"\r
21 \r
22 #include "frame_consumer.h"\r
23 \r
24 #include <common/env.h>\r
25 #include <common/memory/safe_ptr.h>\r
26 #include <common/exception/exceptions.h>\r
27 \r
28 #include <concurrent_vector.h>\r
29 \r
30 namespace caspar { namespace core {\r
31         \r
32 size_t consumer_buffer_depth()\r
33 {\r
34         return env::properties().get("configuration.consumers.buffer-depth", 5);\r
35 }\r
36 \r
37 struct destruction_context\r
38 {\r
39         std::shared_ptr<frame_consumer> consumer;\r
40         Concurrency::event                              event;\r
41 \r
42         destruction_context(std::shared_ptr<frame_consumer>&& consumer) \r
43                 : consumer(consumer)\r
44         {\r
45         }\r
46 };\r
47 \r
48 void __cdecl destroy_consumer(LPVOID lpParam)\r
49 {\r
50         auto destruction = std::unique_ptr<destruction_context>(static_cast<destruction_context*>(lpParam));\r
51         \r
52         try\r
53         {               \r
54                 if(destruction->consumer.unique())\r
55                 {\r
56                         Concurrency::scoped_oversubcription_token oversubscribe;\r
57                         destruction->consumer.reset();\r
58                 }\r
59                 else\r
60                         CASPAR_LOG(warning) << destruction->consumer->print() << " Not destroyed asynchronously.";              \r
61         }\r
62         catch(...)\r
63         {\r
64                 CASPAR_LOG_CURRENT_EXCEPTION();\r
65         }\r
66         \r
67         destruction->event.set();\r
68 }\r
69 \r
70 void __cdecl destroy_and_wait_consumer(LPVOID lpParam)\r
71 {\r
72         try\r
73         {\r
74                 auto destruction = static_cast<destruction_context*>(lpParam);\r
75                 Concurrency::CurrentScheduler::ScheduleTask(destroy_consumer, lpParam);\r
76                 if(destruction->event.wait(1000) == Concurrency::COOPERATIVE_WAIT_TIMEOUT)\r
77                         CASPAR_LOG(warning) << " Potential destruction deadlock detected. Might leak resources.";\r
78         }\r
79         catch(...)\r
80         {\r
81                 CASPAR_LOG_CURRENT_EXCEPTION();\r
82         }\r
83 }\r
84         \r
85 class destroy_consumer_proxy : public frame_consumer\r
86 {\r
87         std::shared_ptr<frame_consumer> consumer_;\r
88 public:\r
89         destroy_consumer_proxy(const std::shared_ptr<frame_consumer>& consumer) \r
90                 : consumer_(consumer)\r
91         {\r
92         }\r
93 \r
94         ~destroy_consumer_proxy()\r
95         {               \r
96                 Concurrency::CurrentScheduler::ScheduleTask(destroy_consumer, new destruction_context(std::move(consumer_)));\r
97         }       \r
98         \r
99         virtual bool send(const safe_ptr<read_frame>& frame)                                    {return consumer_->send(frame);}\r
100         virtual void initialize(const video_format_desc& format_desc)                   {return consumer_->initialize(format_desc);}\r
101         virtual std::wstring print() const                                                                              {return consumer_->print();}\r
102         virtual bool has_synchronization_clock() const                                                  {return consumer_->has_synchronization_clock();}\r
103         virtual const core::video_format_desc& get_video_format_desc() const    {return consumer_->get_video_format_desc();}\r
104         virtual size_t buffer_depth() const                                                                             {return consumer_->buffer_depth();}\r
105 };\r
106 \r
107 Concurrency::concurrent_vector<std::shared_ptr<consumer_factory_t>> g_factories;\r
108 \r
109 void register_consumer_factory(const consumer_factory_t& factory)\r
110 {\r
111         g_factories.push_back(std::make_shared<consumer_factory_t>(factory));\r
112 }\r
113 \r
114 safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
115 {\r
116         if(params.empty())\r
117                 BOOST_THROW_EXCEPTION(invalid_argument() << arg_name_info("params") << arg_value_info(""));\r
118         \r
119         auto consumer = frame_consumer::empty();\r
120         std::any_of(g_factories.begin(), g_factories.end(), [&](const std::shared_ptr<consumer_factory_t>& factory) -> bool\r
121                 {\r
122                         try\r
123                         {\r
124                                 consumer = (*factory)(params);\r
125                         }\r
126                         catch(...)\r
127                         {\r
128                                 CASPAR_LOG_CURRENT_EXCEPTION();\r
129                         }\r
130                         return consumer != frame_consumer::empty();\r
131                 });\r
132 \r
133         if(consumer == frame_consumer::empty())\r
134                 BOOST_THROW_EXCEPTION(file_not_found() << msg_info("No match found for supplied commands. Check syntax."));\r
135 \r
136         return make_safe<destroy_consumer_proxy>(consumer);\r
137 }\r
138 \r
139 }}