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/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/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 mixer::impl : boost::noncopyable
\r
58 spl::shared_ptr<diagnostics::graph> graph_;
\r
59 audio_mixer audio_mixer_;
\r
60 spl::shared_ptr<image_mixer> image_mixer_;
\r
62 std::unordered_map<int, blend_mode> blend_modes_;
\r
67 impl(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer)
\r
68 : graph_(std::move(graph))
\r
70 , image_mixer_(std::move(image_mixer))
\r
71 , executor_(L"mixer")
\r
73 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8));
\r
76 const_frame operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc)
\r
78 return executor_.invoke([=]() mutable -> const_frame
\r
82 boost::timer frame_timer;
\r
84 BOOST_FOREACH(auto& frame, frames)
\r
86 auto blend_it = blend_modes_.find(frame.first);
\r
87 image_mixer_->begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);
\r
89 frame.second.accept(audio_mixer_);
\r
90 frame.second.accept(*image_mixer_);
\r
92 image_mixer_->end_layer();
\r
95 auto image = (*image_mixer_)(format_desc);
\r
96 auto audio = audio_mixer_(format_desc);
\r
98 graph_->set_value("mix-time", frame_timer.elapsed()*format_desc.fps*0.5);
\r
100 auto desc = core::pixel_format_desc(core::pixel_format::bgra);
\r
101 desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));
\r
102 return const_frame(std::move(image), std::move(audio), this, desc, format_desc.fps, format_desc.field_mode);
\r
106 CASPAR_LOG_CURRENT_EXCEPTION();
\r
107 return const_frame::empty();
\r
112 void set_blend_mode(int index, blend_mode value)
\r
114 executor_.begin_invoke([=]
\r
116 auto it = blend_modes_.find(index);
\r
117 if(it == blend_modes_.end())
\r
118 blend_modes_.insert(std::make_pair(index, value));
\r
120 it->second = value;
\r
121 }, task_priority::high_priority);
\r
124 boost::unique_future<boost::property_tree::wptree> info() const
\r
126 boost::promise<boost::property_tree::wptree> info;
\r
127 info.set_value(boost::property_tree::wptree());
\r
128 return info.get_future();
\r
132 mixer::mixer(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer)
\r
133 : impl_(new impl(std::move(graph), std::move(image_mixer))){}
\r
134 void mixer::set_blend_mode(int index, blend_mode value){impl_->set_blend_mode(index, value);}
\r
135 boost::unique_future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}
\r
136 const_frame mixer::operator()(std::map<int, draw_frame> frames, const struct video_format_desc& format_desc){return (*impl_)(std::move(frames), format_desc);}
\r
137 mutable_frame mixer::create_frame(const void* tag, const core::pixel_format_desc& desc, double frame_rate, core::field_mode field_mode) {return impl_->image_mixer_->create_frame(tag, desc, frame_rate, field_mode);}
\r