-/*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG.\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
-* GNU General Public License for more details.\r
-\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-*/\r
-#include "../StdAfx.h"\r
-\r
-#include "mixer.h"\r
-\r
-#include "read_frame.h"\r
-#include "write_frame.h"\r
-\r
-#include "audio/audio_mixer.h"\r
-#include "image/image_mixer.h"\r
-\r
-#include <common/exception/exceptions.h>\r
-#include <common/concurrency/executor.h>\r
-#include <common/utility/tweener.h>\r
-#include <common/env.h>\r
-#include <common/gl/gl_check.h>\r
-\r
-#include <core/mixer/read_frame.h>\r
-#include <core/mixer/write_frame.h>\r
-#include <core/producer/frame/basic_frame.h>\r
-#include <core/producer/frame/frame_factory.h>\r
-#include <core/producer/frame/pixel_format.h>\r
-#include <core/producer/frame/frame_transform.h>\r
-\r
-#include <core/video_format.h>\r
-\r
-#include <boost/fusion/container/map.hpp>\r
-#include <boost/fusion/include/at_key.hpp>\r
-#include <boost/foreach.hpp>\r
-\r
-#include <unordered_map>\r
-\r
-using namespace Concurrency;\r
-\r
-namespace caspar { namespace core {\r
- \r
-template<typename T>\r
-class tweened_transform\r
-{\r
- T source_;\r
- T dest_;\r
- int duration_;\r
- int time_;\r
- tweener_t tweener_;\r
-public: \r
- tweened_transform()\r
- : duration_(0)\r
- , time_(0)\r
- , tweener_(get_tweener(L"linear")){}\r
- tweened_transform(const T& source, const T& dest, int duration, const std::wstring& tween = L"linear")\r
- : source_(source)\r
- , dest_(dest)\r
- , duration_(duration)\r
- , time_(0)\r
- , tweener_(get_tweener(tween)){}\r
- \r
- T fetch()\r
- {\r
- return time_ == duration_ ? dest_ : tween(static_cast<double>(time_), source_, dest_, static_cast<double>(duration_), tweener_);\r
- }\r
-\r
- T fetch_and_tick(int num)\r
- { \r
- time_ = std::min(time_+num, duration_);\r
- return fetch();\r
- }\r
-};\r
-\r
-struct mixer::implementation : boost::noncopyable\r
-{ \r
- const video_format_desc format_desc_;\r
- ogl_device& ogl_;\r
- \r
- audio_mixer audio_mixer_;\r
- image_mixer image_mixer_;\r
- \r
- std::unordered_map<int, tweened_transform<core::frame_transform>> transforms_; \r
- std::unordered_map<int, blend_mode::type> blend_modes_;\r
- \r
- critical_section mutex_;\r
- Concurrency::transformer<mixer::source_element_t, mixer::target_element_t> mixer_;\r
-public:\r
- implementation(mixer::source_t& source, mixer::target_t& target, const video_format_desc& format_desc, ogl_device& ogl) \r
- : format_desc_(format_desc)\r
- , ogl_(ogl)\r
- , audio_mixer_(format_desc)\r
- , image_mixer_(ogl, format_desc)\r
- , mixer_(std::bind(&implementation::mix, this, std::placeholders::_1), &target)\r
- { \r
- CASPAR_LOG(info) << print() << L" Successfully initialized."; \r
- source.link_target(&mixer_);\r
- }\r
- \r
- mixer::target_element_t mix(const mixer::source_element_t& element)\r
- { \r
- auto frames = element.first;\r
-\r
- auto frame = make_safe<read_frame>();\r
-\r
- try\r
- {\r
- {\r
- critical_section::scoped_lock lock(mutex_);\r
-\r
- BOOST_FOREACH(auto& frame, frames)\r
- {\r
- auto blend_it = blend_modes_.find(frame.first);\r
- image_mixer_.begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);\r
- \r
- auto frame1 = make_safe<core::basic_frame>(frame.second);\r
- frame1->get_frame_transform() = transforms_[frame.first].fetch_and_tick(1);\r
-\r
- if(format_desc_.field_mode != core::field_mode::progressive)\r
- { \r
- auto frame2 = make_safe<core::basic_frame>(frame.second);\r
- frame2->get_frame_transform() = transforms_[frame.first].fetch_and_tick(1);\r
- frame1 = core::basic_frame::interlace(frame1, frame2, format_desc_.field_mode);\r
- }\r
- \r
- frame1->accept(audio_mixer_); \r
- frame1->accept(image_mixer_);\r
-\r
- image_mixer_.end_layer();\r
- }\r
- }\r
-\r
- auto image = image_mixer_.render();\r
- auto audio = audio_mixer_.mix();\r
- \r
- {\r
- scoped_oversubcription_token oversubscribe;\r
- image.wait();\r
- }\r
-\r
- frame = make_safe<read_frame>(ogl_, format_desc_.size, std::move(image.get()), std::move(audio));\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- Concurrency::wait(20);\r
- }\r
-\r
- return mixer::target_element_t(std::move(frame), element.second); \r
- }\r
- \r
- safe_ptr<core::write_frame> async_create_frame(const void* tag, const core::pixel_format_desc& desc)\r
- { \r
- auto frame = image_mixer_.async_create_frame(tag, desc);\r
-\r
- Concurrency::Context::Yield();\r
-\r
- if(!frame.has_value())\r
- {\r
- Concurrency::scoped_oversubcription_token oversubscribe;\r
- frame.wait();\r
- }\r
- return frame.get();\r
- }\r
- \r
- void set_transform(int index, const frame_transform& transform, unsigned int mix_duration, const std::wstring& tween)\r
- {\r
- critical_section::scoped_lock lock(mutex_);\r
-\r
- auto src = transforms_[index].fetch();\r
- auto dst = transform;\r
- transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
- }\r
- \r
- void apply_transform(int index, const std::function<frame_transform(frame_transform)>& transform, unsigned int mix_duration, const std::wstring& tween)\r
- {\r
- critical_section::scoped_lock lock(mutex_);\r
-\r
- auto src = transforms_[index].fetch();\r
- auto dst = transform(src);\r
- transforms_[index] = tweened_transform<frame_transform>(src, dst, mix_duration, tween);\r
- }\r
-\r
- void clear_transforms()\r
- {\r
- critical_section::scoped_lock lock(mutex_);\r
-\r
- transforms_.clear();\r
- blend_modes_.clear();\r
- }\r
- \r
- void set_blend_mode(int index, blend_mode::type value)\r
- {\r
- critical_section::scoped_lock lock(mutex_);\r
-\r
- blend_modes_[index] = value;\r
- }\r
-\r
- std::wstring print() const\r
- {\r
- return L"mixer";\r
- }\r
-};\r
- \r
-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
-core::video_format_desc mixer::get_video_format_desc() const { return impl_->format_desc_; }\r
-safe_ptr<write_frame> mixer::create_frame(const void* video_stream_tag, const pixel_format_desc& desc){ return impl_->async_create_frame(video_stream_tag, desc); } \r
-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
-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
-void mixer::clear_transforms(){impl_->clear_transforms();}\r
-void mixer::set_blend_mode(int index, blend_mode::type value){impl_->set_blend_mode(index, value);}\r
-}}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "../StdAfx.h"
+
+#include "mixer.h"
+
+#include "../frame/frame.h"
+
+#include "audio/audio_mixer.h"
+#include "image/image_mixer.h"
+
+#include <common/env.h>
+#include <common/executor.h>
+#include <common/diagnostics/graph.h>
+#include <common/except.h>
+#include <common/future.h>
+#include <common/timer.h>
+
+#include <core/frame/draw_frame.h>
+#include <core/frame/frame_factory.h>
+#include <core/frame/frame_transform.h>
+#include <core/frame/pixel_format.h>
+#include <core/frame/audio_channel_layout.h>
+#include <core/video_format.h>
+
+#include <boost/property_tree/ptree.hpp>
+#include <boost/lexical_cast.hpp>
+
+#include <tbb/concurrent_queue.h>
+#include <tbb/spin_mutex.h>
+#include <tbb/atomic.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace caspar { namespace core {
+
+struct mixer::impl : boost::noncopyable
+{
+ int channel_index_;
+ spl::shared_ptr<diagnostics::graph> graph_;
+ tbb::atomic<int64_t> current_mix_time_;
+ spl::shared_ptr<monitor::subject> monitor_subject_ = spl::make_shared<monitor::subject>("/mixer");
+ audio_mixer audio_mixer_ { graph_ };
+ spl::shared_ptr<image_mixer> image_mixer_;
+
+ bool straighten_alpha_ = false;
+
+ executor executor_ { L"mixer " + boost::lexical_cast<std::wstring>(channel_index_) };
+
+public:
+ impl(int channel_index, spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer)
+ : channel_index_(channel_index)
+ , graph_(std::move(graph))
+ , image_mixer_(std::move(image_mixer))
+ {
+ graph_->set_color("mix-time", diagnostics::color(1.0f, 0.0f, 0.9f, 0.8f));
+ current_mix_time_ = 0;
+ audio_mixer_.monitor_output().attach_parent(monitor_subject_);
+ }
+
+ const_frame operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout)
+ {
+ caspar::timer frame_timer;
+
+ auto frame = executor_.invoke([=]() mutable -> const_frame
+ {
+ try
+ {
+ CASPAR_SCOPED_CONTEXT_MSG(L" '" + executor_.name() + L"' ");
+
+ detail::set_current_aspect_ratio(
+ static_cast<double>(format_desc.square_width)
+ / static_cast<double>(format_desc.square_height));
+
+ for (auto& frame : frames)
+ {
+ frame.second.accept(audio_mixer_);
+ frame.second.transform().image_transform.layer_depth = 1;
+ frame.second.accept(*image_mixer_);
+ }
+
+ auto image = (*image_mixer_)(format_desc, straighten_alpha_);
+ auto audio = audio_mixer_(format_desc, channel_layout);
+
+ auto desc = core::pixel_format_desc(core::pixel_format::bgra);
+ desc.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));
+ return const_frame(std::move(image), std::move(audio), this, desc, channel_layout);
+ }
+ catch(...)
+ {
+ CASPAR_LOG_CURRENT_EXCEPTION();
+ return const_frame::empty();
+ }
+ });
+
+ auto mix_time = frame_timer.elapsed();
+ graph_->set_value("mix-time", mix_time * format_desc.fps * 0.5);
+ current_mix_time_ = static_cast<int64_t>(mix_time * 1000.0);
+
+ return frame;
+ }
+
+ void set_master_volume(float volume)
+ {
+ executor_.begin_invoke([=]
+ {
+ audio_mixer_.set_master_volume(volume);
+ }, task_priority::high_priority);
+ }
+
+ float get_master_volume()
+ {
+ return executor_.invoke([=]
+ {
+ return audio_mixer_.get_master_volume();
+ }, task_priority::high_priority);
+ }
+
+ void set_straight_alpha_output(bool value)
+ {
+ executor_.begin_invoke([=]
+ {
+ straighten_alpha_ = value;
+ }, task_priority::high_priority);
+ }
+
+ bool get_straight_alpha_output()
+ {
+ return executor_.invoke([=]
+ {
+ return straighten_alpha_;
+ }, task_priority::high_priority);
+ }
+
+ std::future<boost::property_tree::wptree> info() const
+ {
+ boost::property_tree::wptree info;
+ info.add(L"mix-time", current_mix_time_);
+
+ return make_ready_future(std::move(info));
+ }
+
+ std::future<boost::property_tree::wptree> delay_info() const
+ {
+ boost::property_tree::wptree info;
+ info.put_value(current_mix_time_);
+
+ return make_ready_future(std::move(info));
+ }
+};
+
+mixer::mixer(int channel_index, spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer)
+ : impl_(new impl(channel_index, std::move(graph), std::move(image_mixer))){}
+void mixer::set_master_volume(float volume) { impl_->set_master_volume(volume); }
+float mixer::get_master_volume() { return impl_->get_master_volume(); }
+void mixer::set_straight_alpha_output(bool value) { impl_->set_straight_alpha_output(value); }
+bool mixer::get_straight_alpha_output() { return impl_->get_straight_alpha_output(); }
+std::future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}
+std::future<boost::property_tree::wptree> mixer::delay_info() const{ return impl_->delay_info(); }
+const_frame mixer::operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc, const core::audio_channel_layout& channel_layout){ return (*impl_)(std::move(frames), format_desc, channel_layout); }
+mutable_frame mixer::create_frame(const void* tag, const core::pixel_format_desc& desc, const core::audio_channel_layout& channel_layout) {return impl_->image_mixer_->create_frame(tag, desc, channel_layout);}
+monitor::subject& mixer::monitor_output() { return *impl_->monitor_subject_; }
+}}