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 if(producer != frame_producer::empty() && !producer.unique())
\r
39 CASPAR_LOG(debug) << producer->print() << L" was not destroyed on dedicated destruction thread.";
\r
40 executor_.begin_invoke(std::bind(&frame_producer_remover::do_remove, this, std::move(producer)));
\r
44 frame_producer_remover g_remover;
\r
46 struct layer::implementation : public object, boost::noncopyable
\r
48 mutable tbb::spin_mutex printer_mutex_;
\r
49 printer parent_printer_;
\r
52 safe_ptr<frame_producer> foreground_;
\r
53 safe_ptr<frame_producer> background_;
\r
54 safe_ptr<basic_frame> last_frame_;
\r
57 implementation(const object* parent, int index)
\r
60 , foreground_(frame_producer::empty())
\r
61 , background_(frame_producer::empty())
\r
62 , last_frame_(basic_frame::empty())
\r
63 , is_paused_(false){}
\r
65 void load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load, bool preview)
\r
67 frame_producer->set_parent(this);
\r
68 background_ = frame_producer;
\r
86 background_->set_leading_producer(foreground_);
\r
87 foreground_ = background_;
\r
88 CASPAR_LOG(info) << foreground_->print() << L" Added.";
\r
89 background_ = frame_producer::empty();
\r
102 last_frame_ = basic_frame::empty();
\r
103 foreground_ = frame_producer::empty();
\r
106 safe_ptr<basic_frame> receive()
\r
110 last_frame_->get_audio_transform().set_gain(0.0);
\r
111 return last_frame_;
\r
116 last_frame_ = foreground_->receive();
\r
117 if(last_frame_ == basic_frame::eof())
\r
119 CASPAR_VERIFY(foreground_ != frame_producer::empty());
\r
121 auto following = foreground_->get_following_producer();
\r
122 following->set_leading_producer(foreground_);
\r
123 following->set_parent(this);
\r
124 g_remover.remove(std::move(foreground_));
\r
125 foreground_ = following;
\r
126 CASPAR_LOG(info) << foreground_->print() << L" Added.";
\r
128 last_frame_ = receive();
\r
133 CASPAR_LOG(error) << print() << L" Unhandled Exception: ";
\r
134 CASPAR_LOG_CURRENT_EXCEPTION();
\r
138 return last_frame_;
\r
141 std::wstring print() const
\r
143 tbb::spin_mutex::scoped_lock lock(printer_mutex_); // Child-producers may call print asynchronously to the producer thread.
\r
144 return object::print() + L"layer[" + boost::lexical_cast<std::wstring>(index_) + L"]";
\r
148 layer::layer(const object* parent, int index) : impl_(new implementation(parent, index)){}
\r
149 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
\r
150 layer& layer::operator=(layer&& other)
\r
152 impl_ = std::move(other.impl_);
\r
155 void layer::swap(layer& other)
\r
157 impl_.swap(other.impl_);
\r
158 // Printer state is not swapped.
\r
159 tbb::spin_mutex::scoped_lock lock(impl_->printer_mutex_);
\r
160 std::swap(impl_->parent_printer_, other.impl_->parent_printer_);
\r
161 std::swap(impl_->index_, other.impl_->index_);
\r
163 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
164 void layer::play(){impl_->play();}
\r
165 void layer::pause(){impl_->pause();}
\r
166 void layer::stop(){impl_->stop();}
\r
167 safe_ptr<basic_frame> layer::receive() {return impl_->receive();}
\r
168 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
\r
169 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}
\r
170 std::wstring layer::print() const { return impl_->print();}
\r