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 #include "../StdAfx.h"
\r
24 #include "read_frame.h"
\r
25 #include "write_frame.h"
\r
27 #include "audio/audio_mixer.h"
\r
28 #include "image/image_mixer.h"
\r
30 #include <common/exception/exceptions.h>
\r
31 #include <common/concurrency/executor.h>
\r
32 #include <common/utility/tweener.h>
\r
33 #include <common/env.h>
\r
34 #include <common/gl/gl_check.h>
\r
36 #include <core/mixer/read_frame.h>
\r
37 #include <core/mixer/write_frame.h>
\r
38 #include <core/producer/frame/basic_frame.h>
\r
39 #include <core/producer/frame/frame_factory.h>
\r
40 #include <core/producer/frame/pixel_format.h>
\r
41 #include <core/producer/frame/frame_transform.h>
\r
43 #include <core/video_format.h>
\r
45 #include <boost/fusion/container/map.hpp>
\r
46 #include <boost/fusion/include/at_key.hpp>
\r
47 #include <boost/foreach.hpp>
\r
49 #include <unordered_map>
\r
51 using namespace Concurrency;
\r
53 namespace caspar { namespace core {
\r
55 template<typename T>
\r
56 class tweened_transform
\r
67 , tweener_(get_tweener(L"linear")){}
\r
68 tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")
\r
71 , duration_(duration)
\r
73 , tweener_(get_tweener(tween)){}
\r
77 return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);
\r
80 T fetch_and_tick(int num)
\r
82 time_ = std::min(time_+num, duration_);
\r
87 struct mixer::implementation : boost::noncopyable
\r
89 const video_format_desc format_desc_;
\r
92 audio_mixer audio_mixer_;
\r
93 image_mixer image_mixer_;
\r
95 std::unordered_map<int, tweened_transform<core::frame_transform>> transforms_;
\r
96 std::unordered_map<int, blend_mode::type> blend_modes_;
\r
98 critical_section mutex_;
\r
99 Concurrency::transformer<safe_ptr<message<std::map<int, safe_ptr<basic_frame>>>>,
\r
100 safe_ptr<message<safe_ptr<core::read_frame>>>> mixer_;
\r
102 implementation(mixer::source_t& source, mixer::target_t& target, const video_format_desc& format_desc, ogl_device& ogl)
\r
103 : format_desc_(format_desc)
\r
105 , audio_mixer_(format_desc)
\r
106 , image_mixer_(ogl, format_desc)
\r
107 , mixer_(std::bind(&implementation::mix, this, std::placeholders::_1), &target)
\r
109 CASPAR_LOG(info) << print() << L" Successfully initialized.";
\r
110 source.link_target(&mixer_);
\r
113 safe_ptr<message<safe_ptr<core::read_frame>>> mix(const safe_ptr<message<std::map<int, safe_ptr<basic_frame>>>>& msg)
\r
115 auto frames = msg->value();
\r
117 auto frame = make_safe<read_frame>();
\r
122 critical_section::scoped_lock lock(mutex_);
\r
124 BOOST_FOREACH(auto& frame, frames)
\r
126 auto blend_it = blend_modes_.find(frame.first);
\r
127 image_mixer_.begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);
\r
129 auto frame1 = make_safe<core::basic_frame>(frame.second);
\r
130 frame1->get_frame_transform() = transforms_[frame.first].fetch_and_tick(1);
\r
132 if(format_desc_.field_mode != core::field_mode::progressive)
\r
134 auto frame2 = make_safe<core::basic_frame>(frame.second);
\r
135 frame2->get_frame_transform() = transforms_[frame.first].fetch_and_tick(1);
\r
136 frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);
\r
139 frame1->accept(audio_mixer_);
\r
140 frame1->accept(image_mixer_);
\r
142 image_mixer_.end_layer();
\r
146 auto image = image_mixer_.render();
\r
147 auto audio = audio_mixer_.mix();
\r
150 scoped_oversubcription_token oversubscribe;
\r
154 frame = make_safe<read_frame>(ogl_, format_desc_.size, std::move(image.get()), std::move(audio));
\r
158 CASPAR_LOG_CURRENT_EXCEPTION();
\r
159 Concurrency::wait(20);
\r
162 return msg->transfer<safe_ptr<core::read_frame>>(std::move(frame));
\r
165 boost::unique_future<safe_ptr<core::write_frame>> async_create_frame(const void* tag, const core::pixel_format_desc& desc)
\r
167 return image_mixer_.async_create_frame(tag, desc);
\r
170 void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)
\r
172 critical_section::scoped_lock lock(mutex_);
\r
174 auto src = transforms_[index].fetch();
\r
175 auto dst = transform;
\r
176 transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);
\r
179 void apply_transform(int index, const std::function<frame_transform(frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween)
\r
181 critical_section::scoped_lock lock(mutex_);
\r
183 auto src = transforms_[index].fetch();
\r
184 auto dst = transform(src);
\r
185 transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);
\r
188 void clear_transforms()
\r
190 critical_section::scoped_lock lock(mutex_);
\r
192 transforms_.clear();
\r
193 blend_modes_.clear();
\r
196 void set_blend_mode(int index, blend_mode::type value)
\r
198 critical_section::scoped_lock lock(mutex_);
\r
200 blend_modes_[index] = value;
\r
203 std::wstring print() const
\r
209 mixer::mixer(mixer::source_t& source, mixer::target_t& target, const video_format_desc& format_desc, ogl_device& ogl) : impl_(new implementation(source, target, format_desc, ogl)){}
\r
210 core::video_format_desc mixer::get_video_format_desc() const { return impl_->format_desc_; }
\r
211 boost::unique_future<safe_ptr<write_frame>> mixer::async_create_frame(const void* video_stream_tag, const pixel_format_desc& desc){ return impl_->async_create_frame(video_stream_tag, desc); }
\r
212 void mixer::set_frame_transform(int index, const core::frame_transform& transform, unsigned int mix_duration, const std::wstring& tween){impl_->set_transform(index, transform, mix_duration, tween);}
\r
213 void mixer::apply_frame_transform(int index, const std::function<core::frame_transform(core::frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween){impl_->apply_transform(index, transform, mix_duration, tween);}
\r
214 void mixer::clear_transforms(){impl_->clear_transforms();}
\r
215 void mixer::set_blend_mode(int index, blend_mode::type value){impl_->set_blend_mode(index, value);}
\r