X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=core%2Fproducer%2Flayer.cpp;h=0051ef3443e49dc2d2aecc4212aa45716dc2e0a3;hb=f6a130bfe0eb0dd0c7c2d26908d358db3e2e8927;hp=1f30234320af91da7b17893c9aa7b90c01bcab98;hpb=5140cfe5d4e4f312ca6b1c5ebf9d78280bc64ec3;p=casparcg diff --git a/core/producer/layer.cpp b/core/producer/layer.cpp index 1f3023432..0051ef344 100644 --- a/core/producer/layer.cpp +++ b/core/producer/layer.cpp @@ -1,170 +1,177 @@ +/* +* copyright (c) 2010 Sveriges Television AB +* +* This file is part of CasparCG. +* +* 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 . +* +*/ + #include "../stdafx.h" #include "layer.h" -#include "frame_producer.h" - -#include -#include -#include -#include - -#include "../producer/frame/basic_frame.h" -#include "../producer/frame/audio_transform.h" -#include +#include "frame_producer.h" +#include "frame/basic_frame.h" namespace caspar { namespace core { + +struct layer::implementation +{ + safe_ptr foreground_; + safe_ptr background_; + int64_t frame_number_; + int auto_play_delta_; + bool is_paused_; -class frame_producer_remover -{ - executor executor_; - - void do_remove(safe_ptr& producer) +public: + implementation() + : foreground_(frame_producer::empty()) + , background_(frame_producer::empty()) + , frame_number_(0) + , auto_play_delta_(-1) + , is_paused_(false) { - auto name = producer->print(); - producer = frame_producer::empty(); - CASPAR_LOG(info) << name << L" Removed."; } - -public: - - frame_producer_remover() : executor_(L"frame_producer_remover") + + void pause() { - executor_.start(); + is_paused_ = true; } - void remove(safe_ptr&& producer) + void resume() { - if(producer != frame_producer::empty() && !producer.unique()) - CASPAR_LOG(debug) << producer->print() << L" was not destroyed on dedicated destruction thread."; - executor_.begin_invoke(std::bind(&frame_producer_remover::do_remove, this, std::move(producer))); + is_paused_ = false; } -}; -frame_producer_remover g_remover; - -struct layer::implementation : boost::noncopyable -{ - mutable tbb::spin_mutex printer_mutex_; - printer parent_printer_; - int index_; - - safe_ptr foreground_; - safe_ptr background_; - safe_ptr last_frame_; - bool is_paused_; -public: - implementation(int index, const printer& parent_printer) - : parent_printer_(parent_printer) - , index_(index) - , foreground_(frame_producer::empty()) - , background_(frame_producer::empty()) - , last_frame_(basic_frame::empty()) - , is_paused_(false){} - - void load(const safe_ptr& frame_producer, bool play_on_load, bool preview) + void load(const safe_ptr& producer, bool preview, int auto_play_delta) { - background_ = frame_producer; - is_paused_ = false; + background_ = producer; + auto_play_delta_ = auto_play_delta; - if(preview) - { + if(preview) // Play the first frame and pause. + { play(); receive(); pause(); } - - if(play_on_load) - play(); } - + void play() { - if(!is_paused_) + if(background_ != frame_producer::empty()) { background_->set_leading_producer(foreground_); - foreground_ = background_; - CASPAR_LOG(info) << foreground_->print() << L" Added."; - background_ = frame_producer::empty(); + + foreground_ = background_; + background_ = frame_producer::empty(); + frame_number_ = 0; + auto_play_delta_ = -1; } - is_paused_ = false; - } - void pause() - { - is_paused_ = true; + is_paused_ = false; } - + void stop() { - pause(); - last_frame_ = basic_frame::empty(); - foreground_ = frame_producer::empty(); + foreground_ = frame_producer::empty(); + background_ = background_; + frame_number_ = 0; + auto_play_delta_ = -1; + + is_paused_ = true; } safe_ptr receive() { - if(is_paused_) - { - last_frame_->get_audio_transform().set_gain(0.0); - return last_frame_; - } - try { - last_frame_ = foreground_->receive(); - if(last_frame_ == basic_frame::eof()) - { - CASPAR_VERIFY(foreground_ != frame_producer::empty()); - - auto following = foreground_->get_following_producer(); - following->set_leading_producer(foreground_); - following->set_parent_printer([=]{return print();}); - g_remover.remove(std::move(foreground_)); - foreground_ = following; - CASPAR_LOG(info) << foreground_->print() << L" Added."; + if(is_paused_) + return disable_audio(foreground_->last_frame()); + + auto frame = receive_and_follow(foreground_, frame_producer::NO_HINT); + if(frame == core::basic_frame::late()) + return foreground_->last_frame(); - last_frame_ = receive(); + auto frames_left = foreground_->nb_frames() - (++frame_number_) - auto_play_delta_; + if(auto_play_delta_ > -1 && frames_left < 1) + { + play(); + return receive(); } + + return frame; } catch(...) { - CASPAR_LOG(error) << print() << L" Unhandled Exception: "; CASPAR_LOG_CURRENT_EXCEPTION(); stop(); + return core::basic_frame::empty(); } + } - return last_frame_; + layer_status status() const + { + layer_status status; + status.foreground = foreground_->print(); + status.background = background_->print(); + status.is_paused = is_paused_; + status.total_frames = foreground_->nb_frames(); + status.current_frame = frame_number_; + + return status; } - - std::wstring print() const + + std::wstring call(bool foreground, const std::wstring& param) { - tbb::spin_mutex::scoped_lock lock(printer_mutex_); // Child-producers may call print asynchronously to the producer thread. - return (parent_printer_ ? parent_printer_() + L"/" : L"") + L"layer[" + boost::lexical_cast(index_) + L"]"; + return (foreground ? foreground_ : background_)->call(param); + } + + bool empty() const + { + return background_ == core::frame_producer::empty() && foreground_ == core::frame_producer::empty(); } }; -layer::layer(int index, const printer& parent_printer) : impl_(new implementation(index, parent_printer)){} +layer::layer() : impl_(new implementation()){} layer::layer(layer&& other) : impl_(std::move(other.impl_)){} layer& layer::operator=(layer&& other) { impl_ = std::move(other.impl_); return *this; } -void layer::swap(layer& other) +layer::layer(const layer& other) : impl_(new implementation(*other.impl_)){} +layer& layer::operator=(const layer& other) { + layer temp(other); + temp.swap(*this); + return *this; +} +void layer::swap(layer& other) +{ impl_.swap(other.impl_); - // Printer state is not swapped. - tbb::spin_mutex::scoped_lock lock(impl_->printer_mutex_); - std::swap(impl_->parent_printer_, other.impl_->parent_printer_); - std::swap(impl_->index_, other.impl_->index_); } -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(const safe_ptr& frame_producer, bool preview, int auto_play_delta){return impl_->load(frame_producer, preview, auto_play_delta);} void layer::play(){impl_->play();} void layer::pause(){impl_->pause();} void layer::stop(){impl_->stop();} +bool layer::is_paused() const{return impl_->is_paused_;} +int64_t layer::frame_number() const{return impl_->frame_number_;} +layer_status layer::status() const {return impl_->status();} 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();} +bool layer::empty() const {return impl_->empty();} +std::wstring layer::call(bool foreground, const std::wstring& param){return impl_->call(foreground, param);} }} \ No newline at end of file