#include "../stdafx.h"\r
\r
#include "layer.h"\r
+#include "frame_producer.h"\r
\r
-#include "../producer/frame_producer.h"\r
+#include "../video_format.h"\r
\r
-#include "../format/video_format.h"\r
+#include <common/concurrency/executor.h>\r
+#include <common/utility/assert.h>\r
+#include <common/utility/printable.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
-struct layer::implementation\r
-{ \r
- implementation() : foreground_(nullptr), background_(nullptr), last_frame_(nullptr) {}\r
- \r
- void load(const frame_producer_ptr& frame_producer, load_option::type option)\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
- if(frame_producer == nullptr) \r
- BOOST_THROW_EXCEPTION(null_argument() << arg_name_info("frame_producer"));\r
- \r
+ CASPAR_ASSERT(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)\r
+ { \r
background_ = frame_producer;\r
- if(option == load_option::preview) \r
- {\r
- foreground_ = nullptr; \r
- last_frame_ = frame_producer->render_frame();\r
- if(last_frame_ != nullptr)\r
- last_frame_->get_audio_data().clear(); // No audio\r
- }\r
- else if(option == load_option::auto_play)\r
- play(); \r
+ is_paused_ = false;\r
+ if(play_on_load)\r
+ play(); \r
+ }\r
+\r
+ void preview(const safe_ptr<frame_producer>& frame_producer)\r
+ {\r
+ load(frame_producer, true);\r
+ receive();\r
+ pause();\r
}\r
\r
void play()\r
{ \r
- if(background_ != nullptr)\r
+ if(!is_paused_) \r
{\r
background_->set_leading_producer(foreground_);\r
- foreground_ = std::move(background_);\r
+ foreground_ = background_;\r
+ CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
+ background_ = frame_producer::empty();\r
}\r
-\r
is_paused_ = false;\r
}\r
\r
\r
void stop()\r
{\r
- foreground_ = nullptr;\r
- last_frame_ = nullptr;\r
+ pause();\r
+ last_frame_ = draw_frame::empty();\r
+ foreground_ = frame_producer::empty();\r
}\r
\r
void clear()\r
- {\r
- foreground_ = nullptr;\r
- background_ = nullptr;\r
- last_frame_ = nullptr;\r
+ { \r
+ foreground_ = frame_producer::empty();\r
+ background_ = frame_producer::empty();\r
+ last_frame_ = draw_frame::empty();\r
+ is_paused_ = false;\r
}\r
\r
- frame_ptr render_frame()\r
+ safe_ptr<draw_frame> receive()\r
{ \r
- if(!foreground_ || is_paused_)\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_->render_frame();\r
-\r
- if(last_frame_ == nullptr)\r
+ last_frame_ = foreground_->receive(); \r
+ if(last_frame_ == draw_frame::eof())\r
{\r
- CASPAR_LOG(info) << L"EOF: " << foreground_->print();\r
+ CASPAR_ASSERT(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
- last_frame_ = render_frame();\r
+ CASPAR_LOG(info) << foreground_->print() << L" Added.";\r
+\r
+ last_frame_ = receive();\r
}\r
}\r
catch(...)\r
{\r
- try\r
- {\r
- CASPAR_LOG_CURRENT_EXCEPTION();\r
- CASPAR_LOG(warning) << L"Removed " << (foreground_ ? foreground_->print() : L"empty") << L" from layer.";\r
- foreground_ = nullptr;\r
- last_frame_ = nullptr;\r
- }\r
- catch(...){}\r
+ CASPAR_LOG(error) << print() << L" Unhandled Exception: ";\r
+ CASPAR_LOG_CURRENT_EXCEPTION();\r
+ stop();\r
}\r
\r
return last_frame_;\r
- } \r
+ }\r
\r
- tbb::atomic<bool> is_paused_;\r
- frame_ptr last_frame_;\r
- frame_producer_ptr foreground_;\r
- frame_producer_ptr background_;\r
+ std::wstring print() const\r
+ {\r
+ tbb::spin_mutex::scoped_lock lock(printer_mutex_);\r
+ return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"layer[" + boost::lexical_cast<std::wstring>(index_) + L"]";\r
+ }\r
};\r
\r
-layer::layer() : impl_(new implementation()){}\r
-layer::layer(layer&& other) : impl_(std::move(other.impl_)){other.impl_ = nullptr;}\r
+layer::layer(int index, const printer& parent_printer)\r
+{\r
+ impl_ = new implementation(index, parent_printer);\r
+}\r
+layer::~layer()\r
+{\r
+ if(!impl_)\r
+ return;\r
+\r
+ impl_->clear();\r
+ delete impl_.fetch_and_store(nullptr);\r
+}\r
+layer::layer(layer&& other)\r
+{\r
+ impl_ = other.impl_.fetch_and_store(nullptr);\r
+}\r
layer& layer::operator=(layer&& other)\r
{\r
- impl_ = std::move(other.impl_); \r
- other.impl_ = nullptr;\r
+ impl_ = other.impl_.fetch_and_store(nullptr);\r
return *this;\r
}\r
-void layer::load(const frame_producer_ptr& frame_producer, load_option::type option){return impl_->load(frame_producer, option);} \r
+void layer::swap(layer& other)\r
+{\r
+ impl_ = other.impl_.compare_and_swap(impl_, other.impl_);\r
+ tbb::spin_mutex::scoped_lock lock(other.impl_->printer_mutex_);\r
+ std::swap(impl_->index_, other.impl_->index_);\r
+ std::swap(impl_->parent_printer_, other.impl_->parent_printer_);\r
+}\r
+void layer::load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load){return impl_->load(frame_producer, play_on_load);} \r
+void layer::preview(const safe_ptr<frame_producer>& frame_producer){return impl_->preview(frame_producer);} \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
-frame_ptr layer::render_frame() {return impl_->render_frame();}\r
-frame_producer_ptr layer::foreground() const { return impl_->foreground_;}\r
-frame_producer_ptr layer::background() const { return impl_->background_;}\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
}}
\ No newline at end of file