1 #include "../stdafx.h"
\r
4 #include "frame_producer.h"
\r
6 #include <common/concurrency/executor.h>
\r
7 #include <common/exception/exceptions.h>
\r
8 #include <common/utility/assert.h>
\r
9 #include <common/utility/printer.h>
\r
11 #include "../producer/frame/basic_frame.h"
\r
12 #include "../producer/frame/audio_transform.h"
\r
14 #include <tbb/spin_mutex.h>
\r
16 namespace caspar { namespace core {
\r
18 class frame_producer_remover
\r
22 void do_remove(safe_ptr<frame_producer>& producer)
\r
24 auto name = producer->print();
\r
25 producer = frame_producer::empty();
\r
26 CASPAR_LOG(info) << name << L" Removed.";
\r
31 frame_producer_remover() : executor_(L"frame_producer_remover")
\r
36 void remove(safe_ptr<frame_producer>&& producer)
\r
38 CASPAR_VERIFY(producer == frame_producer::empty() || producer.unique());
\r
39 executor_.begin_invoke(std::bind(&frame_producer_remover::do_remove, this, std::move(producer)));
\r
43 frame_producer_remover g_remover;
\r
45 struct layer::implementation : boost::noncopyable
\r
47 mutable tbb::spin_mutex printer_mutex_;
\r
48 printer parent_printer_;
\r
51 safe_ptr<frame_producer> foreground_;
\r
52 safe_ptr<frame_producer> background_;
\r
53 safe_ptr<basic_frame> last_frame_;
\r
56 implementation(int index, const printer& parent_printer)
\r
57 : parent_printer_(parent_printer)
\r
59 , foreground_(frame_producer::empty())
\r
60 , background_(frame_producer::empty())
\r
61 , last_frame_(basic_frame::empty())
\r
62 , is_paused_(false){}
\r
64 void load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load, bool preview)
\r
66 background_ = frame_producer;
\r
84 background_->set_leading_producer(foreground_);
\r
85 foreground_ = background_;
\r
86 CASPAR_LOG(info) << foreground_->print() << L" Added.";
\r
87 background_ = frame_producer::empty();
\r
100 last_frame_ = basic_frame::empty();
\r
101 foreground_ = frame_producer::empty();
\r
104 safe_ptr<basic_frame> receive()
\r
108 last_frame_->get_audio_transform().set_gain(0.0);
\r
109 return last_frame_;
\r
114 last_frame_ = foreground_->receive();
\r
115 if(last_frame_ == basic_frame::eof())
\r
117 CASPAR_VERIFY(foreground_ != frame_producer::empty());
\r
119 auto following = foreground_->get_following_producer();
\r
120 following->set_leading_producer(foreground_);
\r
121 following->set_parent_printer([=]{return print();});
\r
122 g_remover.remove(std::move(foreground_));
\r
123 foreground_ = following;
\r
124 CASPAR_LOG(info) << foreground_->print() << L" Added.";
\r
126 last_frame_ = receive();
\r
131 CASPAR_LOG(error) << print() << L" Unhandled Exception: ";
\r
132 CASPAR_LOG_CURRENT_EXCEPTION();
\r
136 return last_frame_;
\r
139 std::wstring print() const
\r
141 tbb::spin_mutex::scoped_lock lock(printer_mutex_); // Child-producers may call print asynchronously to the producer thread.
\r
142 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"layer[" + boost::lexical_cast<std::wstring>(index_) + L"]";
\r
146 layer::layer(int index, const printer& parent_printer) : impl_(new implementation(index, parent_printer)){}
\r
147 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
\r
148 layer& layer::operator=(layer&& other)
\r
150 impl_ = std::move(other.impl_);
\r
153 void layer::swap(layer& other)
\r
155 impl_.swap(other.impl_);
\r
156 // Printer state is not swapped.
\r
157 tbb::spin_mutex::scoped_lock lock(impl_->printer_mutex_);
\r
158 std::swap(impl_->parent_printer_, other.impl_->parent_printer_);
\r
159 std::swap(impl_->index_, other.impl_->index_);
\r
161 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
162 void layer::play(){impl_->play();}
\r
163 void layer::pause(){impl_->pause();}
\r
164 void layer::stop(){impl_->stop();}
\r
165 safe_ptr<basic_frame> layer::receive() {return impl_->receive();}
\r
166 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
\r
167 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}
\r
168 std::wstring layer::print() const { return impl_->print();}
\r