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/env.h>
\r
31 #include <common/concurrency/executor.h>
\r
32 #include <common/exception/exceptions.h>
\r
33 #include <common/gl/gl_check.h>
\r
34 #include <common/utility/tweener.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/frame_transform.h>
\r
41 #include <core/producer/frame/pixel_format.h>
\r
43 #include <core/video_format.h>
\r
45 #include <boost/foreach.hpp>
\r
46 #include <boost/timer.hpp>
\r
48 #include <tbb/concurrent_queue.h>
\r
49 #include <tbb/spin_mutex.h>
\r
51 #include <unordered_map>
\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 safe_ptr<diagnostics::graph> graph_;
\r
90 boost::timer mix_timer_;
\r
92 safe_ptr<mixer::target_t> target_;
\r
93 mutable tbb::spin_mutex format_desc_mutex_;
\r
94 video_format_desc format_desc_;
\r
95 safe_ptr<ogl_device> ogl_;
\r
97 audio_mixer audio_mixer_;
\r
98 image_mixer image_mixer_;
\r
100 std::unordered_map<int, tweened_transform<core::frame_transform>> transforms_;
\r
101 std::unordered_map<int, blend_mode::type> blend_modes_;
\r
103 executor executor_;
\r
106 implementation(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<mixer::target_t>& target, const video_format_desc& format_desc, const safe_ptr<ogl_device>& ogl)
\r
109 , format_desc_(format_desc)
\r
111 , image_mixer_(ogl)
\r
112 , executor_(L"mixer")
\r
114 graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f));
\r
117 void send(const std::pair<std::map<int, safe_ptr<core::basic_frame>>, std::shared_ptr<void>>& packet)
\r
119 executor_.begin_invoke([=]
\r
123 mix_timer_.restart();
\r
125 auto frames = packet.first;
\r
127 BOOST_FOREACH(auto& frame, frames)
\r
129 auto blend_it = blend_modes_.find(frame.first);
\r
130 image_mixer_.begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);
\r
132 auto frame1 = make_safe<core::basic_frame>(frame.second);
\r
133 frame1->get_frame_transform() = transforms_[frame.first].fetch_and_tick(1);
\r
135 if(format_desc_.field_mode != core::field_mode::progressive)
\r
137 auto frame2 = make_safe<core::basic_frame>(frame.second);
\r
138 frame2->get_frame_transform() = transforms_[frame.first].fetch_and_tick(1);
\r
139 frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);
\r
142 frame1->accept(audio_mixer_);
\r
143 frame1->accept(image_mixer_);
\r
145 image_mixer_.end_layer();
\r
148 auto image = image_mixer_.render(format_desc_);
\r
149 auto audio = audio_mixer_.mix(format_desc_);
\r
152 graph_->update_value("mix-time", mix_timer_.elapsed()*format_desc_.fps*0.5);
\r
154 target_->send(std::make_pair(make_safe<read_frame>(ogl_, format_desc_.size, std::move(image.get()), std::move(audio)), packet.second));
\r
158 CASPAR_LOG_CURRENT_EXCEPTION();
\r
163 safe_ptr<core::write_frame> create_frame(const void* tag, const core::pixel_format_desc& desc)
\r
165 return image_mixer_.create_frame(tag, desc);
\r
168 void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)
\r
170 executor_.begin_invoke([=]
\r
172 auto src = transforms_[index].fetch();
\r
173 auto dst = transform;
\r
174 transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);
\r
178 void apply_transform(int index, const std::function<frame_transform(frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween)
\r
180 executor_.begin_invoke([=]
\r
182 auto src = transforms_[index].fetch();
\r
183 auto dst = transform(src);
\r
184 transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);
\r
188 void clear_transforms(int index)
\r
190 executor_.begin_invoke([=]
\r
192 transforms_.erase(index);
\r
193 blend_modes_.erase(index);
\r
197 void clear_transforms()
\r
199 executor_.begin_invoke([=]
\r
201 transforms_.clear();
\r
202 blend_modes_.clear();
\r
206 void set_blend_mode(int index, blend_mode::type value)
\r
208 executor_.begin_invoke([=]
\r
210 blend_modes_[index] = value;
\r
214 void set_video_format_desc(const video_format_desc& format_desc)
\r
216 executor_.begin_invoke([=]
\r
218 tbb::spin_mutex::scoped_lock lock(format_desc_mutex_);
\r
219 format_desc_ = format_desc;
\r
223 core::video_format_desc get_video_format_desc() const // nothrow
\r
225 tbb::spin_mutex::scoped_lock lock(format_desc_mutex_);
\r
226 return format_desc_;
\r
230 mixer::mixer(const safe_ptr<diagnostics::graph>& graph, const safe_ptr<target_t>& target, const video_format_desc& format_desc, const safe_ptr<ogl_device>& ogl)
\r
231 : impl_(new implementation(graph, target, format_desc, ogl)){}
\r
232 void mixer::send(const std::pair<std::map<int, safe_ptr<core::basic_frame>>, std::shared_ptr<void>>& frames){ impl_->send(frames);}
\r
233 core::video_format_desc mixer::get_video_format_desc() const { return impl_->get_video_format_desc(); }
\r
234 safe_ptr<core::write_frame> mixer::create_frame(const void* tag, const core::pixel_format_desc& desc){ return impl_->create_frame(tag, desc); }
\r
235 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
236 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)
\r
237 {impl_->apply_transform(index, transform, mix_duration, tween);}
\r
238 void mixer::clear_transforms(int index){impl_->clear_transforms(index);}
\r
239 void mixer::clear_transforms(){impl_->clear_transforms();}
\r
240 void mixer::set_blend_mode(int index, blend_mode::type value){impl_->set_blend_mode(index, value);}
\r
241 void mixer::set_video_format_desc(const video_format_desc& format_desc){impl_->set_video_format_desc(format_desc);}
\r