X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=core%2Fproducer%2Flayer.cpp;h=7aebe0cf4354fb919236cf945aae170d1fc631a9;hb=606051ad4fd051a1e9aca7ae4a3b405ee9e09209;hp=a4b2e6410156ede6624d8ed737eb4e3e94ac3d0b;hpb=4b4239aa94ca0e1814dcb1ecd7500c30294eb120;p=casparcg diff --git a/core/producer/layer.cpp b/core/producer/layer.cpp index a4b2e6410..7aebe0cf4 100644 --- a/core/producer/layer.cpp +++ b/core/producer/layer.cpp @@ -1,111 +1,185 @@ +/* +* Copyright (c) 2011 Sveriges Television AB +* +* 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 . +* +* Author: Robert Nagy, ronag89@gmail.com +*/ + #include "../stdafx.h" #include "layer.h" + #include "frame_producer.h" -#include "../producer/frame/basic_frame.h" -#include "../producer/frame/audio_transform.h" +#include "../video_format.h" +#include "../frame/draw_frame.h" +#include "../frame/frame_transform.h" + +#include +#include namespace caspar { namespace core { - -struct layer::implementation : boost::noncopyable + +struct layer::impl { - int index_; - safe_ptr foreground_; - safe_ptr background_; - safe_ptr last_frame_; - bool is_paused_; + monitor::basic_subject event_subject_; + monitor::basic_subject foreground_event_subject_; + monitor::basic_subject background_event_subject_; + spl::shared_ptr foreground_; + spl::shared_ptr background_; + boost::optional auto_play_delta_; + public: - implementation(int index) - : index_(index) + impl(int index) + : event_subject_(monitor::path("layer") % index) + , foreground_event_subject_("") + , background_event_subject_("background") , foreground_(frame_producer::empty()) , background_(frame_producer::empty()) - , last_frame_(basic_frame::empty()) - , is_paused_(false){} - - void pause() { - is_paused_ = true; + foreground_event_subject_.subscribe(event_subject_); + background_event_subject_.subscribe(event_subject_); } - void resume() + void pause() { - if(is_paused_) - CASPAR_LOG(info) << foreground_->print() << L" Resumed."; - is_paused_ = false; + foreground_->paused(true); } - - void load(const safe_ptr& producer, bool play_on_load, bool preview) + + void load(spl::shared_ptr producer, bool preview, const boost::optional& auto_play_delta) { - background_ = producer; + background_->unsubscribe(background_event_subject_); + background_ = std::move(producer); + background_->subscribe(background_event_subject_); + + auto_play_delta_ = auto_play_delta; - if(play_on_load) - play(); - else if(preview) + if(preview) { play(); - receive(); - pause(); + foreground_->receive(); + foreground_->paused(true); } - CASPAR_LOG(info) << producer->print() << L" Loaded."; + if(auto_play_delta_ && foreground_ == frame_producer::empty()) + play(); } void play() { if(background_ != frame_producer::empty()) { - background_->set_leading_producer(foreground_); - foreground_ = background_; - background_ = frame_producer::empty(); - CASPAR_LOG(info) << foreground_->print() << L" Active."; + background_->leading_producer(foreground_); + + background_->unsubscribe(background_event_subject_); + foreground_->unsubscribe(foreground_event_subject_); + + foreground_ = std::move(background_); + background_ = std::move(frame_producer::empty()); + + foreground_->subscribe(foreground_event_subject_); + + auto_play_delta_.reset(); } - resume(); + + foreground_->paused(false); } void stop() { - pause(); - last_frame_ = basic_frame::empty(); - foreground_ = frame_producer::empty(); + foreground_->unsubscribe(foreground_event_subject_); + + foreground_ = std::move(frame_producer::empty()); + + auto_play_delta_.reset(); } - safe_ptr receive() + draw_frame receive(const video_format_desc& format_desc) { - if(is_paused_) - last_frame_->get_audio_transform().set_has_audio(false); - else - last_frame_ = core::receive(foreground_); + try + { + auto frame = foreground_->receive(); - return last_frame_; + if(frame == core::draw_frame::late()) + return foreground_->last_frame(); + + if(auto_play_delta_) + { + auto frames_left = static_cast(foreground_->nb_frames()) - foreground_->frame_number() - static_cast(*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(foreground_->nb_frames()) - static_cast(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps) + << monitor::event("frame") % static_cast(foreground_->frame_number()) + % static_cast((static_cast(foreground_->nb_frames()) - static_cast(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(); + } } - - std::wstring print() const + + boost::property_tree::wptree info() const { - return L"layer[" + boost::lexical_cast(index_) + L"]"; + boost::property_tree::wptree info; + info.add(L"auto_delta", (auto_play_delta_ ? boost::lexical_cast(*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::max() ? -1 : nb_frames); + info.add(L"frames-left", nb_frames == std::numeric_limits::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; } }; -layer::layer(int index) : impl_(new implementation(index)){} +layer::layer(int index) : impl_(new impl(index)){} layer::layer(layer&& other) : impl_(std::move(other.impl_)){} layer& layer::operator=(layer&& other) { - impl_ = std::move(other.impl_); + other.swap(*this); return *this; } void layer::swap(layer& other) { - std::swap(impl_->foreground_, other.impl_->foreground_); - std::swap(impl_->background_, other.impl_->background_); - std::swap(impl_->last_frame_, other.impl_->last_frame_); - std::swap(impl_->is_paused_ , other.impl_->is_paused_ ); + impl_.swap(other.impl_); } -void layer::load(const safe_ptr& frame_producer, bool play_on_load, bool preview){return impl_->load(frame_producer, play_on_load, preview);} +void layer::load(spl::shared_ptr frame_producer, bool preview, const boost::optional& 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();} -safe_ptr layer::receive() {return impl_->receive();} -safe_ptr layer::foreground() const { return impl_->foreground_;} -safe_ptr layer::background() const { return impl_->background_;} -std::wstring layer::print() const { return impl_->print();} +draw_frame layer::receive(const video_format_desc& format_desc) {return impl_->receive(format_desc);} +spl::shared_ptr layer::foreground() const { return impl_->foreground_;} +spl::shared_ptr layer::background() const { return impl_->background_;} +boost::property_tree::wptree layer::info() const{return impl_->info();} +void layer::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);} +void layer::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);} }} \ No newline at end of file