-#include "../stdafx.h"\r
-\r
-#include "layer.h"\r
-#include "frame_producer.h"\r
-\r
-#include "../video_format.h"\r
-\r
-#include <common/concurrency/executor.h>\r
-#include <common/utility/assert.h>\r
-#include <common/utility/printer.h>\r
-\r
-#include <mixer/frame/draw_frame.h>\r
-#include <mixer/image/image_mixer.h>\r
-#include <mixer/audio/audio_mixer.h>\r
-#include <mixer/audio/audio_transform.h>\r
-\r
-#include <tbb/spin_mutex.h>\r
-\r
-namespace caspar { namespace core {\r
-\r
-class frame_producer_remover\r
-{\r
- executor executor_;\r
- tbb::atomic<int> count_;\r
-\r
- void do_remove(safe_ptr<frame_producer>& producer)\r
- {\r
- auto name = producer->print();\r
- producer = frame_producer::empty();\r
- CASPAR_LOG(info) << name << L" Removed.";\r
- }\r
-public:\r
-\r
- frame_producer_remover()\r
- {\r
- executor_.start();\r
- count_ = 0;\r
- }\r
-\r
- void remove(safe_ptr<frame_producer>&& producer)\r
- {\r
- CASPAR_VERIFY(producer.unique());\r
- executor_.begin_invoke(std::bind(&frame_producer_remover::do_remove, this, std::move(producer)));\r
- }\r
-};\r
-\r
-frame_producer_remover g_remover;\r
-\r
-\r
-struct layer::implementation : boost::noncopyable\r
-{ \r
- mutable tbb::spin_mutex printer_mutex_;\r
- printer parent_printer_;\r
- int index_;\r
- \r
- safe_ptr<frame_producer> foreground_;\r
- safe_ptr<frame_producer> background_;\r
- safe_ptr<draw_frame> last_frame_;\r
- bool is_paused_;\r
-public:\r
- implementation(int index, const printer& parent_printer) \r
- : parent_printer_(parent_printer)\r
- , index_(index)\r
- , foreground_(frame_producer::empty())\r
- , background_(frame_producer::empty())\r
- , last_frame_(draw_frame::empty())\r
- , is_paused_(false){}\r
- \r
- void load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load, bool preview)\r
- { \r
- background_ = frame_producer;\r
- is_paused_ = false;\r
-\r
- if(preview)\r
- {\r
- play();\r
- receive();\r
- pause();\r
- }\r
-\r
- if(play_on_load)\r
- play(); \r
- }\r
-\r
- void play()\r
- { \r
- if(!is_paused_) \r
- {\r
- background_->set_leading_producer(foreground_);\r
- foreground_ = background_;\r
- CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
- background_ = frame_producer::empty();\r
- }\r
- is_paused_ = false;\r
- }\r
-\r
- void pause()\r
- {\r
- is_paused_ = true;\r
- }\r
-\r
- void stop()\r
- {\r
- pause();\r
- last_frame_ = draw_frame::empty();\r
- foreground_ = frame_producer::empty();\r
- }\r
-\r
- void clear()\r
- { \r
- foreground_ = frame_producer::empty();\r
- background_ = frame_producer::empty();\r
- last_frame_ = draw_frame::empty();\r
- is_paused_ = false;\r
- }\r
- \r
- safe_ptr<draw_frame> receive()\r
- { \r
- if(is_paused_)\r
- {\r
- last_frame_->get_audio_transform().set_gain(0.0);\r
- return last_frame_;\r
- }\r
-\r
- try\r
- {\r
- last_frame_ = foreground_->receive(); \r
- if(last_frame_ == draw_frame::eof())\r
- {\r
- CASPAR_VERIFY(foreground_ != frame_producer::empty());\r
-\r
- auto following = foreground_->get_following_producer();\r
- following->set_leading_producer(foreground_);\r
- following->set_parent_printer(boost::bind(&implementation::print, this));\r
- g_remover.remove(std::move(foreground_));\r
- foreground_ = following;\r
- CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
-\r
- last_frame_ = receive();\r
- }\r
- }\r
- catch(...)\r
- {\r
- CASPAR_LOG(error) << print() << L" Unhandled Exception: ";\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- stop();\r
- }\r
-\r
- return last_frame_;\r
- }\r
- \r
- std::wstring print() const\r
- {\r
- tbb::spin_mutex::scoped_lock lock(printer_mutex_); // Child-producers may call print asynchronously to the producer thread.\r
- return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"layer[" + boost::lexical_cast<std::wstring>(index_) + L"]";\r
- }\r
-};\r
-\r
-layer::layer(int index, const printer& parent_printer) : impl_(new implementation(index, parent_printer)){}\r
-layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
-layer& layer::operator=(layer&& other)\r
-{\r
- impl_ = std::move(other.impl_);\r
- return *this;\r
-}\r
-void layer::swap(layer& other)\r
-{\r
- impl_.swap(other.impl_);\r
- // Printer state is not swapped.\r
- tbb::spin_mutex::scoped_lock lock(impl_->printer_mutex_);\r
- std::swap(impl_->parent_printer_, other.impl_->parent_printer_);\r
- std::swap(impl_->index_, other.impl_->index_);\r
-}\r
-void layer::load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load, bool preview){return impl_->load(frame_producer, play_on_load, preview);} \r
-void layer::play(){impl_->play();}\r
-void layer::pause(){impl_->pause();}\r
-void layer::stop(){impl_->stop();}\r
-void layer::clear(){impl_->clear();}\r
-safe_ptr<draw_frame> layer::receive() {return impl_->receive();}\r
-safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
-safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
-std::wstring layer::print() const { return impl_->print();}\r
+/*
+* 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 "layer.h"
+
+#include "frame_producer.h"
+
+#include "../video_format.h"
+#include "../frame/draw_frame.h"
+#include "../frame/frame_transform.h"
+
+#include <boost/optional.hpp>
+#include <boost/thread/future.hpp>
+
+namespace caspar { namespace core {
+
+struct layer::impl
+{
+ spl::shared_ptr<monitor::subject> monitor_subject_;
+ spl::shared_ptr<frame_producer> foreground_;
+ spl::shared_ptr<frame_producer> background_;
+ boost::optional<int32_t> auto_play_delta_;
+ bool is_paused_;
+
+public:
+ impl(int index)
+ : monitor_subject_(spl::make_shared<monitor::subject>(
+ "/layer/" + boost::lexical_cast<std::string>(index)))
+// , foreground_event_subject_("")
+// , background_event_subject_("background")
+ , foreground_(frame_producer::empty())
+ , background_(frame_producer::empty())
+ , is_paused_(false)
+ {
+// foreground_event_subject_.subscribe(event_subject_);
+// background_event_subject_.subscribe(event_subject_);
+ }
+
+ void set_foreground(spl::shared_ptr<frame_producer> producer)
+ {
+ foreground_->monitor_output().detach_parent();
+ foreground_ = std::move(producer);
+ foreground_->monitor_output().attach_parent(monitor_subject_);
+ }
+
+ void pause()
+ {
+ foreground_->paused(true);
+ is_paused_ = true;
+ }
+
+ void load(spl::shared_ptr<frame_producer> producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
+ {
+// background_->unsubscribe(background_event_subject_);
+ background_ = std::move(producer);
+// background_->subscribe(background_event_subject_);
+
+ auto_play_delta_ = auto_play_delta;
+
+ if(preview)
+ {
+ play();
+ foreground_->paused(true);
+ is_paused_ = true;
+ }
+
+ if(auto_play_delta_ && foreground_ == frame_producer::empty())
+ play();
+ }
+
+ void play()
+ {
+ if(background_ != frame_producer::empty())
+ {
+ background_->leading_producer(foreground_);
+
+ set_foreground(background_);
+ background_ = std::move(frame_producer::empty());
+
+ auto_play_delta_.reset();
+ }
+
+ foreground_->paused(false);
+ is_paused_ = false;
+ }
+
+ void stop()
+ {
+ set_foreground(frame_producer::empty());
+
+ auto_play_delta_.reset();
+ }
+
+ draw_frame receive(const video_format_desc& format_desc)
+ {
+ try
+ {
+ *monitor_subject_ << monitor::message("/paused") % is_paused_;
+
+ auto frame = foreground_->receive();
+
+ if(frame == core::draw_frame::late())
+ return foreground_->last_frame();
+
+ if(auto_play_delta_)
+ {
+ auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - foreground_->frame_number() - static_cast<int64_t>(*auto_play_delta_);
+ if(frames_left < 1)
+ {
+ play();
+ return receive(format_desc);
+ }
+ }
+
+ //event_subject_ << monitor::event("time") % monitor::duration(foreground_->frame_number()/format_desc.fps)
+ // % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
+ // << monitor::event("frame") % static_cast<int64_t>(foreground_->frame_number())
+ // % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
+
+ //foreground_event_subject_ << monitor::event("type") % foreground_->name();
+ //background_event_subject_ << monitor::event("type") % background_->name();
+
+ return frame;
+ }
+ catch(...)
+ {
+ CASPAR_LOG_CURRENT_EXCEPTION();
+ stop();
+ return core::draw_frame::empty();
+ }
+ }
+
+ boost::property_tree::wptree info() const
+ {
+ boost::property_tree::wptree info;
+ info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast<std::wstring>(*auto_play_delta_) : L"null"));
+ info.add(L"frame-number", foreground_->frame_number());
+
+ auto nb_frames = foreground_->nb_frames();
+
+ info.add(L"nb_frames", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);
+ info.add(L"frames-left", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : (foreground_->nb_frames() - foreground_->frame_number() - (auto_play_delta_ ? *auto_play_delta_ : 0)));
+ info.add_child(L"producer", foreground_->info());
+ info.add_child(L"background.producer", background_->info());
+ return info;
+ }
+
+ void on_interaction(const interaction_event::ptr& event)
+ {
+ foreground_->on_interaction(event);
+ }
+
+ bool collides(double x, double y) const
+ {
+ return foreground_->collides(x, y);
+ }
+};
+
+layer::layer(int index) : impl_(new impl(index)){}
+layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
+layer& layer::operator=(layer&& other)
+{
+ other.swap(*this);
+ return *this;
+}
+void layer::swap(layer& other)
+{
+ impl_.swap(other.impl_);
+}
+void layer::load(spl::shared_ptr<frame_producer> frame_producer, bool preview, const boost::optional<int32_t>& auto_play_delta){return impl_->load(std::move(frame_producer), preview, auto_play_delta);}
+void layer::play(){impl_->play();}
+void layer::pause(){impl_->pause();}
+void layer::stop(){impl_->stop();}
+draw_frame layer::receive(const video_format_desc& format_desc) {return impl_->receive(format_desc);}
+spl::shared_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
+spl::shared_ptr<frame_producer> layer::background() const { return impl_->background_;}
+boost::property_tree::wptree layer::info() const{return impl_->info();}
+monitor::subject& layer::monitor_output() {return *impl_->monitor_subject_;}
+void layer::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); }
+bool layer::collides(double x, double y) const { return impl_->collides(x, y); }
}}
\ No newline at end of file