1 #include "../stdafx.h"
\r
4 #include "frame_producer.h"
\r
6 #include "../video_format.h"
\r
8 #include <common/concurrency/executor.h>
\r
9 #include <common/utility/assert.h>
\r
10 #include <common/utility/printable.h>
\r
12 #include <mixer/frame/draw_frame.h>
\r
13 #include <mixer/image/image_mixer.h>
\r
14 #include <mixer/audio/audio_mixer.h>
\r
15 #include <mixer/audio/audio_transform.h>
\r
17 #include <tbb/spin_mutex.h>
\r
19 namespace caspar { namespace core {
\r
21 class frame_producer_remover
\r
24 tbb::atomic<int> count_;
\r
26 void do_remove(safe_ptr<frame_producer>& producer)
\r
28 auto name = producer->print();
\r
29 producer = frame_producer::empty();
\r
30 CASPAR_LOG(info) << name << L" Removed.";
\r
34 frame_producer_remover()
\r
40 void remove(safe_ptr<frame_producer>&& producer)
\r
42 CASPAR_ASSERT(producer.unique());
\r
43 executor_.begin_invoke(std::bind(&frame_producer_remover::do_remove, this, std::move(producer)));
\r
47 frame_producer_remover g_remover;
\r
50 struct layer::implementation : boost::noncopyable
\r
52 mutable tbb::spin_mutex printer_mutex_;
\r
53 printer parent_printer_;
\r
56 safe_ptr<frame_producer> foreground_;
\r
57 safe_ptr<frame_producer> background_;
\r
58 safe_ptr<draw_frame> last_frame_;
\r
61 implementation(int index, const printer& parent_printer)
\r
62 : parent_printer_(parent_printer)
\r
64 , foreground_(frame_producer::empty())
\r
65 , background_(frame_producer::empty())
\r
66 , last_frame_(draw_frame::empty())
\r
67 , is_paused_(false){}
\r
69 void load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load)
\r
71 background_ = frame_producer;
\r
77 void preview(const safe_ptr<frame_producer>& frame_producer)
\r
79 load(frame_producer, true);
\r
88 background_->set_leading_producer(foreground_);
\r
89 foreground_ = background_;
\r
90 CASPAR_LOG(info) << foreground_->print() << L" Added.";
\r
91 background_ = frame_producer::empty();
\r
104 last_frame_ = draw_frame::empty();
\r
105 foreground_ = frame_producer::empty();
\r
110 foreground_ = frame_producer::empty();
\r
111 background_ = frame_producer::empty();
\r
112 last_frame_ = draw_frame::empty();
\r
113 is_paused_ = false;
\r
116 safe_ptr<draw_frame> receive()
\r
120 last_frame_->get_audio_transform().set_gain(0.0);
\r
121 return last_frame_;
\r
126 last_frame_ = foreground_->receive();
\r
127 if(last_frame_ == draw_frame::eof())
\r
129 CASPAR_ASSERT(foreground_ != frame_producer::empty());
\r
131 auto following = foreground_->get_following_producer();
\r
132 following->set_leading_producer(foreground_);
\r
133 following->set_parent_printer(boost::bind(&implementation::print, this));
\r
134 g_remover.remove(std::move(foreground_));
\r
135 foreground_ = following;
\r
136 CASPAR_LOG(info) << foreground_->print() << L" Added.";
\r
138 last_frame_ = receive();
\r
143 CASPAR_LOG(error) << print() << L" Unhandled Exception: ";
\r
144 CASPAR_LOG_CURRENT_EXCEPTION();
\r
148 return last_frame_;
\r
151 std::wstring print() const
\r
153 tbb::spin_mutex::scoped_lock lock(printer_mutex_); // Child-producers may call print asynchronously to the producer thread.
\r
154 return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"layer[" + boost::lexical_cast<std::wstring>(index_) + L"]";
\r
158 layer::layer(int index, const printer& parent_printer) : impl_(new implementation(index, parent_printer)){}
\r
159 layer::layer(layer&& other) : impl_(std::move(other.impl_)){}
\r
160 layer& layer::operator=(layer&& other)
\r
162 impl_ = std::move(other.impl_);
\r
165 void layer::swap(layer& other)
\r
167 impl_.swap(other.impl_);
\r
168 tbb::spin_mutex::scoped_lock lock(impl_->printer_mutex_);
\r
169 std::swap(impl_->parent_printer_, other.impl_->parent_printer_);
\r
170 std::swap(impl_->index_, other.impl_->index_);
\r
172 void layer::load(const safe_ptr<frame_producer>& frame_producer, bool play_on_load){return impl_->load(frame_producer, play_on_load);}
\r
173 void layer::preview(const safe_ptr<frame_producer>& frame_producer){return impl_->preview(frame_producer);}
\r
174 void layer::play(){impl_->play();}
\r
175 void layer::pause(){impl_->pause();}
\r
176 void layer::stop(){impl_->stop();}
\r
177 void layer::clear(){impl_->clear();}
\r
178 safe_ptr<draw_frame> layer::receive() {return impl_->receive();}
\r
179 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}
\r
180 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}
\r
181 std::wstring layer::print() const { return impl_->print();}
\r