X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=core%2Fproducer%2Flayer.cpp;h=c2fa8a69a611a11196da9297dcc1ebdb8339d8b2;hb=fe5b39c0b8f24e1e88603f04fad30aa5d58038bb;hp=614442a0668d45d4ac24eeeaa8642379b0cb1682;hpb=83c5f97e508b267898ddbbdc19f91195df1bf146;p=casparcg diff --git a/core/producer/layer.cpp b/core/producer/layer.cpp index 614442a06..c2fa8a69a 100644 --- a/core/producer/layer.cpp +++ b/core/producer/layer.cpp @@ -1,113 +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() : foreground_(nullptr), background_(nullptr), last_frame_(draw_frame::empty()) {} +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 frame_producer_ptr& frame_producer, load_option::type option) - { - background_ = frame_producer; - if(option == load_option::preview) - { - foreground_ = nullptr; - last_frame_ = frame_producer->receive(); + void pause() + { + is_paused_ = true; + } + + void resume() + { + 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(); } - else if(option == load_option::auto_play) - play(); } void play() { - if(background_ != nullptr) + if(background_ != frame_producer::empty()) { background_->set_leading_producer(foreground_); - foreground_ = std::move(background_); + + foreground_ = background_; + background_ = frame_producer::empty(); + frame_number_ = 0; + auto_play_delta_ = nullptr; } - is_paused_ = false; - } - - void pause() - { - is_paused_ = true; + is_paused_ = false; } - + void stop() { - foreground_ = nullptr; - last_frame_ = draw_frame::empty(); - } + foreground_ = frame_producer::empty(); + background_ = background_; + frame_number_ = 0; + auto_play_delta_ = nullptr; - void clear() - { - foreground_ = nullptr; - background_ = nullptr; - last_frame_ = draw_frame::empty(); + is_paused_ = true; } - - draw_frame receive() + + safe_ptr receive(int flags) { - if(!foreground_ || 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_) { - CASPAR_LOG(info) << L"EOF: " << foreground_->print(); - auto following = foreground_->get_following_producer(); - if(following) - following->set_leading_producer(foreground_); - foreground_ = following; - 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(...) { - try - { - CASPAR_LOG_CURRENT_EXCEPTION(); - CASPAR_LOG(warning) << L"Removed " << (foreground_ ? foreground_->print() : L"empty") << L" from layer."; - foreground_ = nullptr; - last_frame_ = draw_frame::empty(); - } - catch(...){} + CASPAR_LOG_CURRENT_EXCEPTION(); + stop(); + return core::basic_frame::empty(); } + } - return last_frame_; - } - - tbb::atomic is_paused_; - draw_frame last_frame_; - frame_producer_ptr foreground_; - frame_producer_ptr background_; + boost::unique_future call(bool foreground, const std::wstring& param) + { + return (foreground ? foreground_ : background_)->call(param); + } + + 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() : impl_(new implementation()){} -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 frame_producer_ptr& frame_producer, load_option::type option){return impl_->load(frame_producer, option);} +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();} -draw_frame layer::receive() {return impl_->receive();} -frame_producer_ptr layer::foreground() const { return impl_->foreground_;} -frame_producer_ptr layer::background() const { return impl_->background_;} +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