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 "read_frame.h"
\r
27 #include "write_frame.h"
\r
29 #include "audio/audio_mixer.h"
\r
30 #include "image/image_mixer.h"
\r
32 #include <common/env.h>
\r
33 #include <common/concurrency/executor.h>
\r
34 #include <common/diagnostics/graph.h>
\r
35 #include <common/exception/exceptions.h>
\r
36 #include <common/gl/gl_check.h>
\r
37 #include <common/utility/tweener.h>
\r
39 #include <core/mixer/read_frame.h>
\r
40 #include <core/mixer/write_frame.h>
\r
41 #include <core/producer/frame/basic_frame.h>
\r
42 #include <core/producer/frame/frame_factory.h>
\r
43 #include <core/producer/frame/frame_transform.h>
\r
44 #include <core/producer/frame/pixel_format.h>
\r
45 #include <core/video_format.h>
\r
47 #include <boost/foreach.hpp>
\r
48 #include <boost/timer.hpp>
\r
49 #include <boost/property_tree/ptree.hpp>
\r
50 #include <boost/range/algorithm_ext.hpp>
\r
52 #include <tbb/concurrent_queue.h>
\r
53 #include <tbb/spin_mutex.h>
\r
55 #include <unordered_map>
\r
58 namespace caspar { namespace core {
\r
60 struct mixer::impl : boost::noncopyable
\r
62 safe_ptr<mixer::target_t> target_;
\r
64 safe_ptr<diagnostics::graph> graph_;
\r
65 boost::timer mix_timer_;
\r
67 video_format_desc format_desc_;
\r
68 safe_ptr<ogl_device> ogl_;
\r
70 audio_mixer audio_mixer_;
\r
71 image_mixer image_mixer_;
\r
73 std::unordered_map<int, blend_mode> blend_modes_;
\r
78 impl(const safe_ptr<mixer::target_t>& target, const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, const safe_ptr<ogl_device>& ogl)
\r
81 , format_desc_(format_desc)
\r
84 , audio_mixer_(graph_)
\r
85 , executor_(L"mixer")
\r
87 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));
\r
90 void send(const std::pair<std::map<int, safe_ptr<core::basic_frame>>, std::shared_ptr<void>>& packet)
\r
92 executor_.begin_invoke([=]
\r
96 mix_timer_.restart();
\r
98 auto frames = packet.first;
\r
100 BOOST_FOREACH(auto& frame, frames)
\r
102 auto blend_it = blend_modes_.find(frame.first);
\r
103 image_mixer_.begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);
\r
105 frame.second->accept(audio_mixer_);
\r
106 frame.second->accept(image_mixer_);
\r
108 image_mixer_.end_layer();
\r
111 auto image = image_mixer_(format_desc_);
\r
112 auto audio = audio_mixer_(format_desc_);
\r
115 graph_->set_value("mix-time", mix_timer_.elapsed()*format_desc_.fps*0.5);
\r
117 target_->send(std::make_pair(make_safe<read_frame>(ogl_, format_desc_.width, format_desc_.height, std::move(image.get()), std::move(audio)), packet.second));
\r
121 CASPAR_LOG_CURRENT_EXCEPTION();
\r
126 void set_blend_mode(int index, blend_mode value)
\r
128 executor_.begin_invoke([=]
\r
130 auto it = blend_modes_.find(index);
\r
131 if(it == blend_modes_.end())
\r
132 blend_modes_.insert(std::make_pair(index, value));
\r
134 it->second = value;
\r
138 void set_video_format_desc(const video_format_desc& format_desc)
\r
140 executor_.begin_invoke([=]
\r
142 format_desc_ = format_desc;
\r
146 boost::unique_future<boost::property_tree::wptree> info() const
\r
148 boost::promise<boost::property_tree::wptree> info;
\r
149 info.set_value(boost::property_tree::wptree());
\r
150 return info.get_future();
\r
154 mixer::mixer(const safe_ptr<target_t>& target, const safe_ptr<diagnostics::graph>& graph, const video_format_desc& format_desc, const safe_ptr<ogl_device>& ogl)
\r
155 : impl_(new impl(target, graph, format_desc, ogl)){}
\r
156 void mixer::send(const std::pair<std::map<int, safe_ptr<core::basic_frame>>, std::shared_ptr<void>>& frames){ impl_->send(frames);}
\r
157 void mixer::set_blend_mode(int index, blend_mode value){impl_->set_blend_mode(index, value);}
\r
158 void mixer::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}
\r
159 boost::unique_future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}
\r