X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=core%2Fproducer%2Flayer.cpp;h=c2fa8a69a611a11196da9297dcc1ebdb8339d8b2;hb=fe5b39c0b8f24e1e88603f04fad30aa5d58038bb;hp=c0fb25c5429d7ace274af1cb4762e1536c8808da;hpb=f8df2c6d14701ccebac616ffef43ddb75cd2f5fc;p=casparcg diff --git a/core/producer/layer.cpp b/core/producer/layer.cpp index c0fb25c54..c2fa8a69a 100644 --- a/core/producer/layer.cpp +++ b/core/producer/layer.cpp @@ -1,126 +1,191 @@ +/* +* 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 "../processor/draw_frame.h" -#include "../producer/frame_producer.h" +#include "frame_producer.h" +#include "frame/basic_frame.h" +#include "frame/frame_transform.h" -#include "../format/video_format.h" +#include +#include namespace caspar { namespace core { -struct layer::implementation -{ - implementation(size_t index) : foreground_(frame_producer::empty()), background_(frame_producer::empty()), last_frame_(draw_frame::empty()), index_(index) {} +struct layer::impl +{ + safe_ptr foreground_; + safe_ptr background_; + int64_t frame_number_; + boost::optional auto_play_delta_; + bool is_paused_; + +public: + impl() + : foreground_(frame_producer::empty()) + , background_(frame_producer::empty()) + , frame_number_(0) + , is_paused_(false) + { + } - void load(const safe_ptr& frame_producer, bool autoplay) - { - background_ = frame_producer; - CASPAR_LOG(info) << print() << " " << frame_producer->print() << " => background"; - if(autoplay) - play(); + void pause() + { + is_paused_ = true; } - void preview(const safe_ptr& frame_producer) + void resume() { - stop(); - load(frame_producer, false); - try - { - last_frame_ = frame_producer->receive(); - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - CASPAR_LOG(warning) << print() << L" empty => background"; - background_ = frame_producer::empty(); + is_paused_ = false; + } + + void load(const safe_ptr& producer, bool preview, const boost::optional& auto_play_delta) + { + background_ = producer; + auto_play_delta_ = auto_play_delta; + + if(auto_play_delta_ && foreground_ == frame_producer::empty()) + play(); + + if(preview) // Play the first frame and pause. + { + play(); + receive(frame_producer::NO_FLAG); + pause(); } } void play() { - background_->set_leading_producer(foreground_); - foreground_ = background_; - background_ = frame_producer::empty(); - is_paused_ = false; - CASPAR_LOG(info) << print() << L" background => foreground"; - } + if(background_ != frame_producer::empty()) + { + background_->set_leading_producer(foreground_); + + foreground_ = background_; + background_ = frame_producer::empty(); + frame_number_ = 0; + auto_play_delta_ = nullptr; + } - void pause() - { - is_paused_ = true; + is_paused_ = false; } - + void stop() { - foreground_ = frame_producer::empty(); - last_frame_ = draw_frame::empty(); - } + foreground_ = frame_producer::empty(); + background_ = background_; + frame_number_ = 0; + auto_play_delta_ = nullptr; - void clear() - { - foreground_ = frame_producer::empty(); - background_ = frame_producer::empty(); - last_frame_ = draw_frame::empty(); + is_paused_ = true; } - - safe_ptr receive() + + safe_ptr receive(int flags) { - if(foreground_ == frame_producer::empty() || is_paused_) - return last_frame_; - try { - last_frame_ = foreground_->receive(); + if(is_paused_) + return disable_audio(foreground_->last_frame()); + + auto frame = receive_and_follow(foreground_, flags); + if(frame == core::basic_frame::late()) + return disable_audio(foreground_->last_frame()); - if(last_frame_ == draw_frame::eof()) + if(auto_play_delta_) { - auto following = foreground_->get_following_producer(); - following->set_leading_producer(foreground_); - foreground_ = following; - if(foreground_ != frame_producer::empty()) - CASPAR_LOG(info) << print() << L" [EOF] " << foreground_->print() << " => foreground"; - else - CASPAR_LOG(info) << print() << L" [EOF] empty => foreground"; - last_frame_ = receive(); + auto frames_left = static_cast(foreground_->nb_frames()) - static_cast(++frame_number_) - static_cast(*auto_play_delta_); + if(frames_left < 1) + { + play(); + return receive(flags); + } } + + return frame; } catch(...) { CASPAR_LOG_CURRENT_EXCEPTION(); - CASPAR_LOG(warning) << print() << L" empty => foreground"; - foreground_ = frame_producer::empty(); - last_frame_ = draw_frame::empty(); + stop(); + return core::basic_frame::empty(); } + } - return last_frame_; + boost::unique_future call(bool foreground, const std::wstring& param) + { + return (foreground ? foreground_ : background_)->call(param); } - std::wstring print() const { return L"layer[" + boost::lexical_cast(index_) + L"]"; } - - tbb::atomic is_paused_; - safe_ptr last_frame_; - safe_ptr foreground_; - safe_ptr background_; - size_t index_; + bool empty() const + { + return background_ == core::frame_producer::empty() && foreground_ == core::frame_producer::empty(); + } + + boost::property_tree::wptree info() const + { + boost::property_tree::wptree info; + info.add(L"status", is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing")); + info.add(L"auto_delta", auto_play_delta_); + info.add(L"frame-number", 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() - frame_number_ - auto_play_delta_)); + info.add_child(L"foreground.producer", foreground_->info()); + info.add_child(L"background.producer", background_->info()); + return info; + } }; -layer::layer(size_t index) : impl_(new implementation(index)){} -layer::layer(layer&& other) : impl_(std::move(other.impl_)){other.impl_ = nullptr;} +layer::layer() : impl_(new impl()){} +layer::layer(layer&& other) : impl_(std::move(other.impl_)){} layer& layer::operator=(layer&& other) { - impl_ = std::move(other.impl_); - other.impl_ = nullptr; + impl_ = std::move(other.impl_); + return *this; +} +layer::layer(const layer& other) : impl_(new impl(*other.impl_)){} +layer& layer::operator=(const layer& other) +{ + layer temp(other); + temp.swap(*this); return *this; } -void layer::load(const safe_ptr& frame_producer, bool autoplay){return impl_->load(frame_producer, autoplay);} -void layer::preview(const safe_ptr& frame_producer){return impl_->preview(frame_producer);} +void layer::swap(layer& other) +{ + impl_.swap(other.impl_); +} +void layer::load(const safe_ptr& frame_producer, bool preview, const boost::optional& 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();} -void layer::clear(){impl_->clear();} -bool layer::empty() const { return impl_->foreground_ == frame_producer::empty() && impl_->background_ == frame_producer::empty();} -safe_ptr layer::receive() {return impl_->receive();} +bool layer::is_paused() const{return impl_->is_paused_;} +int64_t layer::frame_number() const{return impl_->frame_number_;} +safe_ptr layer::receive(int flags) {return impl_->receive(flags);} safe_ptr layer::foreground() const { return impl_->foreground_;} safe_ptr layer::background() const { return impl_->background_;} +bool layer::empty() const {return impl_->empty();} +boost::unique_future layer::call(bool foreground, const std::wstring& param){return impl_->call(foreground, param);} +boost::property_tree::wptree layer::info() const{return impl_->info();} }} \ No newline at end of file