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
20 // TODO: Try to recover consumer from bad_alloc...
\r
21 #include "../StdAfx.h"
\r
24 #pragma warning (disable : 4244)
\r
29 #include "../video_channel_context.h"
\r
31 #include "../video_format.h"
\r
32 #include "../mixer/gpu/ogl_device.h"
\r
33 #include "../mixer/read_frame.h"
\r
35 #include <common/concurrency/executor.h>
\r
36 #include <common/utility/assert.h>
\r
37 #include <common/utility/timer.h>
\r
38 #include <common/memory/memshfl.h>
\r
40 #include <tbb/mutex.h>
\r
42 namespace caspar { namespace core {
\r
44 struct output::implementation
\r
46 typedef std::pair<safe_ptr<read_frame>, safe_ptr<read_frame>> fill_and_key;
\r
48 video_channel_context& channel_;
\r
50 std::map<int, safe_ptr<frame_consumer>> consumers_;
\r
51 typedef std::map<int, safe_ptr<frame_consumer>>::value_type layer_t;
\r
53 high_prec_timer timer_;
\r
56 implementation(video_channel_context& video_channel)
\r
57 : channel_(video_channel){}
\r
59 void add(int index, safe_ptr<frame_consumer>&& consumer)
\r
61 channel_.execution().invoke([&]
\r
63 consumers_.erase(index);
\r
66 consumer->initialize(channel_.get_format_desc());
\r
68 channel_.execution().invoke([&]
\r
70 consumers_.insert(std::make_pair(index, consumer));
\r
72 CASPAR_LOG(info) << print() << L" " << consumer->print() << L" Added.";
\r
76 void remove(int index)
\r
78 channel_.execution().invoke([&]
\r
80 auto it = consumers_.find(index);
\r
81 if(it != consumers_.end())
\r
83 CASPAR_LOG(info) << print() << L" " << it->second->print() << L" Removed.";
\r
84 consumers_.erase(it);
\r
89 void execute(const safe_ptr<read_frame>& frame)
\r
91 if(!has_synchronization_clock())
\r
92 timer_.tick(1.0/channel_.get_format_desc().fps);
\r
94 if(frame->image_size() != channel_.get_format_desc().size)
\r
96 timer_.tick(1.0/channel_.get_format_desc().fps);
\r
100 auto it = consumers_.begin();
\r
101 while(it != consumers_.end())
\r
103 auto consumer = it->second;
\r
105 if(consumer->get_video_format_desc() != channel_.get_format_desc())
\r
106 consumer->initialize(channel_.get_format_desc());
\r
108 if(consumer->send(frame))
\r
111 consumers_.erase(it++);
\r
117 bool has_synchronization_clock()
\r
119 return std::any_of(consumers_.begin(), consumers_.end(), [](const decltype(*consumers_.begin())& p)
\r
121 return p.second->has_synchronization_clock();
\r
125 std::wstring print() const
\r
131 output::output(video_channel_context& video_channel) : impl_(new implementation(video_channel)){}
\r
132 void output::add(int index, safe_ptr<frame_consumer>&& consumer){impl_->add(index, std::move(consumer));}
\r
133 void output::remove(int index){impl_->remove(index);}
\r
134 void output::execute(const safe_ptr<read_frame>& frame) {impl_->execute(frame); }
\r