2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
\r
4 * This file is part of CasparCG (www.casparcg.com).
\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
19 * Author: Robert Nagy, ronag89@gmail.com
\r
22 #include "../StdAfx.h"
\r
26 #include "../frame/data_frame.h"
\r
28 #include "audio/audio_mixer.h"
\r
29 #include "image/image_mixer.h"
\r
31 #include <common/env.h>
\r
32 #include <common/concurrency/executor.h>
\r
33 #include <common/diagnostics/graph.h>
\r
34 #include <common/except.h>
\r
35 #include <common/gl/gl_check.h>
\r
37 #include <core/frame/draw_frame.h>
\r
38 #include <core/frame/frame_factory.h>
\r
39 #include <core/frame/frame_transform.h>
\r
40 #include <core/frame/pixel_format.h>
\r
41 #include <core/video_format.h>
\r
43 #include <boost/foreach.hpp>
\r
44 #include <boost/timer.hpp>
\r
45 #include <boost/property_tree/ptree.hpp>
\r
46 #include <boost/range/algorithm_ext.hpp>
\r
48 #include <tbb/concurrent_queue.h>
\r
49 #include <tbb/spin_mutex.h>
\r
51 #include <unordered_map>
\r
54 namespace caspar { namespace core {
\r
56 struct mixed_frame : public data_frame
\r
58 mutable boost::shared_future<boost::iterator_range<const uint8_t*>> image_data_;
\r
59 const audio_buffer audio_data_;
\r
60 const video_format_desc video_desc_;
\r
61 pixel_format_desc pixel_desc_;
\r
65 mixed_frame(const void* tag, boost::shared_future<boost::iterator_range<const uint8_t*>>&& image_data, audio_buffer&& audio_data, const video_format_desc& format_desc)
\r
67 , image_data_(std::move(image_data))
\r
68 , audio_data_(std::move(audio_data))
\r
69 , video_desc_(format_desc)
\r
70 , pixel_desc_(core::pixel_format::bgra)
\r
72 pixel_desc_.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));
\r
75 const boost::iterator_range<const uint8_t*> image_data(int index = 0) const override
\r
77 return image_data_.get();
\r
80 const boost::iterator_range<uint8_t*> image_data(int) override
\r
82 BOOST_THROW_EXCEPTION(invalid_operation());
\r
85 virtual const struct pixel_format_desc& get_pixel_format_desc() const override
\r
90 virtual const audio_buffer& audio_data() const override
\r
95 virtual audio_buffer& audio_data() override
\r
97 BOOST_THROW_EXCEPTION(invalid_operation());
\r
100 virtual double get_frame_rate() const override
\r
102 return video_desc_.fps;
\r
105 virtual int width() const override
\r
107 return video_desc_.width;
\r
110 virtual int height() const override
\r
112 return video_desc_.height;
\r
115 virtual const void* tag() const override
\r
121 struct mixer::impl : boost::noncopyable
\r
123 spl::shared_ptr<diagnostics::graph> graph_;
\r
124 audio_mixer audio_mixer_;
\r
125 spl::shared_ptr<image_mixer> image_mixer_;
\r
127 std::unordered_map<int, blend_mode> blend_modes_;
\r
129 executor executor_;
\r
132 impl(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer)
\r
133 : graph_(std::move(graph))
\r
135 , image_mixer_(std::move(image_mixer))
\r
136 , executor_(L"mixer")
\r
138 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));
\r
141 spl::shared_ptr<const data_frame> operator()(std::map<int, spl::shared_ptr<draw_frame>> frames, const video_format_desc& format_desc)
\r
143 return executor_.invoke([=]() mutable -> spl::shared_ptr<const struct data_frame>
\r
147 boost::timer frame_timer;
\r
149 BOOST_FOREACH(auto& frame, frames)
\r
151 auto blend_it = blend_modes_.find(frame.first);
\r
152 image_mixer_->begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);
\r
154 frame.second->accept(audio_mixer_);
\r
155 frame.second->accept(*image_mixer_);
\r
157 image_mixer_->end_layer();
\r
160 auto image = (*image_mixer_)(format_desc);
\r
161 auto audio = audio_mixer_(format_desc);
\r
163 graph_->set_value("mix-time", frame_timer.elapsed()*format_desc.fps*0.5);
\r
165 return spl::make_shared<mixed_frame>(this, std::move(image), std::move(audio), format_desc);
\r
169 CASPAR_LOG_CURRENT_EXCEPTION();
\r
170 return data_frame::empty();
\r
175 void set_blend_mode(int index, blend_mode value)
\r
177 executor_.begin_invoke([=]
\r
179 auto it = blend_modes_.find(index);
\r
180 if(it == blend_modes_.end())
\r
181 blend_modes_.insert(std::make_pair(index, value));
\r
183 it->second = value;
\r
184 }, task_priority::high_priority);
\r
187 boost::unique_future<boost::property_tree::wptree> info() const
\r
189 boost::promise<boost::property_tree::wptree> info;
\r
190 info.set_value(boost::property_tree::wptree());
\r
191 return info.get_future();
\r
195 mixer::mixer(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer)
\r
196 : impl_(new impl(std::move(graph), std::move(image_mixer))){}
\r
197 void mixer::set_blend_mode(int index, blend_mode value){impl_->set_blend_mode(index, value);}
\r
198 boost::unique_future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}
\r
199 spl::shared_ptr<const data_frame> mixer::operator()(std::map<int, spl::shared_ptr<draw_frame>> frames, const struct video_format_desc& format_desc){return (*impl_)(std::move(frames), format_desc);}
\r
200 spl::shared_ptr<write_frame> mixer::create_frame(const void* tag, const core::pixel_format_desc& desc) {return impl_->image_mixer_->create_frame(tag, desc);}
\r