]> git.sesse.net Git - casparcg/blobdiff - core/mixer/mixer.cpp
* Replaced boost::timer with caspar::timer because of too low resolution on Linux...
[casparcg] / core / mixer / mixer.cpp
index 2eb94c2d90d3cfb0a064890e17bce59cd435834c..4f269b144b528eb54cb3b03fbc69e16025e11b5d 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\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
-* Author: Robert Nagy, ronag89@gmail.com\r
-*/\r
-\r
-#include "../StdAfx.h"\r
-\r
-#include "mixer.h"\r
-\r
-#include "../frame/data_frame.h"\r
-\r
-#include "audio/audio_mixer.h"\r
-#include "image/image_mixer.h"\r
-\r
-#include <common/env.h>\r
-#include <common/concurrency/executor.h>\r
-#include <common/diagnostics/graph.h>\r
-#include <common/except.h>\r
-#include <common/gl/gl_check.h>\r
-\r
-#include <core/frame/draw_frame.h>\r
-#include <core/frame/frame_factory.h>\r
-#include <core/frame/frame_transform.h>\r
-#include <core/frame/pixel_format.h>\r
-#include <core/video_format.h>\r
-\r
-#include <boost/foreach.hpp>\r
-#include <boost/timer.hpp>\r
-#include <boost/property_tree/ptree.hpp>\r
-#include <boost/range/algorithm_ext.hpp>\r
-\r
-#include <tbb/concurrent_queue.h>\r
-#include <tbb/spin_mutex.h>\r
-\r
-#include <unordered_map>\r
-#include <vector>\r
-\r
-namespace caspar { namespace core {\r
-\r
-struct mixed_frame : public data_frame\r
-{\r
-       mutable boost::unique_future<boost::iterator_range<const uint8_t*>>     image_data_;\r
-       const audio_buffer                                                                                                      audio_data_;\r
-       const video_format_desc                                                                                         video_desc_;\r
-       pixel_format_desc                                                                                                       pixel_desc_;\r
-       const void*                                                                                                                     tag_;\r
-\r
-public:\r
-       mixed_frame(const void* tag, boost::unique_future<boost::iterator_range<const uint8_t*>>&& image_data, audio_buffer&& audio_data, const video_format_desc& format_desc) \r
-               : tag_(tag)\r
-               , image_data_(std::move(image_data))\r
-               , audio_data_(std::move(audio_data))\r
-               , video_desc_(format_desc)\r
-               , pixel_desc_(core::pixel_format::bgra)\r
-       {\r
-               pixel_desc_.planes.push_back(core::pixel_format_desc::plane(format_desc.width, format_desc.height, 4));\r
-       }       \r
-       \r
-       const boost::iterator_range<const uint8_t*> image_data(int index = 0) const override\r
-       {\r
-               return image_data_.get();\r
-       }\r
-               \r
-       const boost::iterator_range<uint8_t*> image_data(int) override\r
-       {\r
-               BOOST_THROW_EXCEPTION(invalid_operation());\r
-       }\r
-       \r
-       virtual const struct pixel_format_desc& get_pixel_format_desc() const override\r
-       {\r
-               return pixel_desc_;\r
-       }\r
-\r
-       virtual const audio_buffer& audio_data() const override\r
-       {\r
-               return audio_data_;\r
-       }\r
-\r
-       virtual audio_buffer& audio_data() override\r
-       {\r
-               BOOST_THROW_EXCEPTION(invalid_operation());\r
-       }\r
-\r
-       virtual double get_frame_rate() const override\r
-       {\r
-               return video_desc_.fps;\r
-       }\r
-\r
-       virtual int width() const override\r
-       {\r
-               return video_desc_.width;\r
-       }\r
-\r
-       virtual int height() const override\r
-       {\r
-               return video_desc_.height;\r
-       }\r
-\r
-       virtual const void* tag() const override\r
-       {\r
-               return tag_;\r
-       }\r
-};\r
-               \r
-struct mixer::impl : boost::noncopyable\r
-{                              \r
-       audio_mixer                                                     audio_mixer_;\r
-       spl::shared_ptr<image_mixer>            image_mixer_;\r
-       \r
-       std::unordered_map<int, blend_mode>     blend_modes_;\r
-                       \r
-       executor executor_;\r
-\r
-public:\r
-       impl(spl::shared_ptr<image_mixer> image_mixer) \r
-               : audio_mixer_()\r
-               , image_mixer_(std::move(image_mixer))\r
-               , executor_(L"mixer")\r
-       {                       \r
-       }       \r
-       \r
-       spl::shared_ptr<const data_frame> operator()(std::map<int, spl::shared_ptr<draw_frame>> frames, const video_format_desc& format_desc)\r
-       {               \r
-               return executor_.invoke([=]() mutable -> spl::shared_ptr<const struct data_frame>\r
-               {               \r
-                       try\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
-                                       frame.second->accept(audio_mixer_);                                     \r
-                                       frame.second->accept(*image_mixer_);\r
-\r
-                                       image_mixer_->end_layer();\r
-                               }\r
-\r
-                               auto image = (*image_mixer_)(format_desc);\r
-                               auto audio = audio_mixer_(format_desc);\r
-\r
-                               return spl::make_shared<mixed_frame>(this, std::move(image), std::move(audio), format_desc);    \r
-                       }\r
-                       catch(...)\r
-                       {\r
-                               CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               return data_frame::empty();\r
-                       }       \r
-               });             \r
-       }\r
-                                       \r
-       void set_blend_mode(int index, blend_mode value)\r
-       {\r
-               executor_.begin_invoke([=]\r
-               {\r
-                       auto it = blend_modes_.find(index);\r
-                       if(it == blend_modes_.end())\r
-                               blend_modes_.insert(std::make_pair(index, value));\r
-                       else\r
-                               it->second = value;\r
-               }, task_priority::high_priority);\r
-       }\r
-       \r
-       boost::unique_future<boost::property_tree::wptree> info() const\r
-       {\r
-               boost::promise<boost::property_tree::wptree> info;\r
-               info.set_value(boost::property_tree::wptree());\r
-               return info.get_future();\r
-       }\r
-};\r
-       \r
-mixer::mixer(spl::shared_ptr<image_mixer> image_mixer) \r
-       : impl_(new impl(std::move(image_mixer))){}\r
-void mixer::set_blend_mode(int index, blend_mode value){impl_->set_blend_mode(index, value);}\r
-boost::unique_future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}\r
-spl::shared_ptr<const data_frame> mixer::operator()(std::map<int, spl::shared_ptr<draw_frame>> frames, const struct video_format_desc& format_desc){return (*impl_)(std::move(frames), format_desc);}\r
-spl::shared_ptr<write_frame> mixer::create_frame(const void* tag, const core::pixel_format_desc& desc) {return impl_->image_mixer_->create_frame(tag, desc);}\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/video_format.h>
+
+#include <boost/property_tree/ptree.hpp>
+
+#include <tbb/concurrent_queue.h>
+#include <tbb/spin_mutex.h>
+
+#include <unordered_map>
+#include <vector>
+
+namespace caspar { namespace core {
+
+struct mixer::impl : boost::noncopyable
+{                              
+       spl::shared_ptr<diagnostics::graph> graph_;
+       audio_mixer                                                     audio_mixer_;
+       spl::shared_ptr<image_mixer>            image_mixer_;
+       
+       std::unordered_map<int, blend_mode>     blend_modes_;
+                       
+       executor executor_                                                                      { L"mixer" };
+
+public:
+       impl(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) 
+               : 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));
+       }       
+       
+       const_frame operator()(std::map<int, draw_frame> frames, const video_format_desc& format_desc)
+       {               
+               caspar::timer frame_timer;
+
+               auto frame = executor_.invoke([=]() mutable -> const_frame
+               {               
+                       try
+                       {       
+                               for (auto& frame : frames)
+                               {
+                                       auto blend_it = blend_modes_.find(frame.first);
+                                       image_mixer_->begin_layer(blend_it != blend_modes_.end() ? blend_it->second : blend_mode::normal);
+                                                                                                       
+                                       frame.second.accept(audio_mixer_);                                      
+                                       frame.second.accept(*image_mixer_);
+
+                                       image_mixer_->end_layer();
+                               }
+                               
+                               auto image = (*image_mixer_)(format_desc);
+                               auto audio = audio_mixer_(format_desc);
+
+                               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);     
+                       }
+                       catch(...)
+                       {
+                               CASPAR_LOG_CURRENT_EXCEPTION();
+                               return const_frame::empty();
+                       }       
+               });             
+                               
+               graph_->set_value("mix-time", frame_timer.elapsed()*format_desc.fps*0.5);
+
+               return frame;
+       }
+                                       
+       void set_blend_mode(int index, blend_mode value)
+       {
+               executor_.begin_invoke([=]
+               {
+                       auto it = blend_modes_.find(index);
+                       if(it == blend_modes_.end())
+                               blend_modes_.insert(std::make_pair(index, value));
+                       else
+                               it->second = value;
+               }, task_priority::high_priority);
+       }
+
+       void clear_blend_mode(int index)
+       {
+               executor_.begin_invoke([=]
+               {
+                       blend_modes_.erase(index);
+               }, task_priority::high_priority);
+       }
+
+       void clear_blend_modes()
+       {
+               executor_.begin_invoke([=]
+               {
+                       blend_modes_.clear();
+               }, task_priority::high_priority);
+       }
+
+       void set_master_volume(float volume)
+       {
+               executor_.begin_invoke([=]
+               {
+                       audio_mixer_.set_master_volume(volume);
+               }, task_priority::high_priority);
+       }
+
+       std::future<boost::property_tree::wptree> info() const
+       {
+               return make_ready_future(boost::property_tree::wptree());
+       }
+};
+       
+mixer::mixer(spl::shared_ptr<diagnostics::graph> graph, spl::shared_ptr<image_mixer> image_mixer) 
+       : impl_(new impl(std::move(graph), std::move(image_mixer))){}
+void mixer::set_blend_mode(int index, blend_mode value){impl_->set_blend_mode(index, value);}
+void mixer::clear_blend_mode(int index) { impl_->clear_blend_mode(index); }
+void mixer::clear_blend_modes() { impl_->clear_blend_modes(); }
+void mixer::set_master_volume(float volume) { impl_->set_master_volume(volume); }
+std::future<boost::property_tree::wptree> mixer::info() const{return impl_->info();}
+const_frame mixer::operator()(std::map<int, draw_frame> frames, const struct video_format_desc& format_desc){return (*impl_)(std::move(frames), format_desc);}
+mutable_frame mixer::create_frame(const void* tag, const core::pixel_format_desc& desc) {return impl_->image_mixer_->create_frame(tag, desc);}
+}}