X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=core%2Fproducer%2Flayer.cpp;h=76473cc4eada0f3010e69cbb4d4ad0f7b4a6397a;hb=6c9fcb31392479e3985840d311d9b0a486d56171;hp=2a5cfe5368a2fb8fed943562dfdc3550435b1c8b;hpb=2e34146cbc85f7069f3a6d81d95a2270de979681;p=casparcg diff --git a/core/producer/layer.cpp b/core/producer/layer.cpp index 2a5cfe536..76473cc4e 100644 --- a/core/producer/layer.cpp +++ b/core/producer/layer.cpp @@ -1,187 +1,223 @@ -/* -* 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 "frame/basic_frame.h" - -#include - -namespace caspar { namespace core { - -struct layer::implementation -{ - safe_ptr foreground_; - safe_ptr background_; - int64_t frame_number_; - int32_t auto_play_delta_; - bool is_paused_; - -public: - implementation() - : foreground_(frame_producer::empty()) - , background_(frame_producer::empty()) - , frame_number_(0) - , auto_play_delta_(-1) - , is_paused_(false) - { - } - - void pause() - { - is_paused_ = true; - } - - void resume() - { - is_paused_ = false; - } - - void load(const safe_ptr& producer, bool preview, int auto_play_delta) - { - background_ = producer; - auto_play_delta_ = auto_play_delta; - - if(auto_play_delta_ > -1 && foreground_ == frame_producer::empty()) - play(); - - if(preview) // Play the first frame and pause. - { - play(); - receive(frame_producer::NO_HINT); - pause(); - } - } - - void play() - { - if(background_ != frame_producer::empty()) - { - background_->set_leading_producer(foreground_); - - foreground_ = background_; - background_ = frame_producer::empty(); - frame_number_ = 0; - auto_play_delta_ = -1; - } - - is_paused_ = false; - } - - void stop() - { - foreground_ = frame_producer::empty(); - background_ = background_; - frame_number_ = 0; - auto_play_delta_ = -1; - - is_paused_ = true; - } - - safe_ptr receive(int hints) - { - try - { - if(is_paused_) - return disable_audio(foreground_->last_frame()); - - auto frame = receive_and_follow(foreground_, hints); - if(frame == core::basic_frame::late()) - return foreground_->last_frame(); - - auto frames_left = static_cast(foreground_->nb_frames()) - static_cast(++frame_number_) - static_cast(auto_play_delta_); - if(auto_play_delta_ > -1 && frames_left < 1) - { - play(); - return receive(hints); - } - - return frame; - } - catch(...) - { - CASPAR_LOG_CURRENT_EXCEPTION(); - stop(); - return core::basic_frame::empty(); - } - } - - 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", foreground_->info()); - info.add_child(L"background", background_->info()); - return info; - } -}; - -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; -} -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_); -} -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_;} -safe_ptr layer::receive(int hints) {return impl_->receive(hints);} -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 +/* +* 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 "../video_format.h" +#include "../frame/draw_frame.h" +#include "../frame/frame_transform.h" + +#include +#include + +namespace caspar { namespace core { + +struct layer::impl +{ + spl::shared_ptr monitor_subject_; + spl::shared_ptr foreground_ = frame_producer::empty(); + spl::shared_ptr background_ = frame_producer::empty();; + boost::optional auto_play_delta_; + bool is_paused_ = false; + int64_t current_frame_age_ = 0; + +public: + impl(int index) + : monitor_subject_(spl::make_shared( + "/layer/" + boost::lexical_cast(index))) +// , foreground_event_subject_("") +// , background_event_subject_("background") + { +// foreground_event_subject_.subscribe(event_subject_); +// background_event_subject_.subscribe(event_subject_); + } + + void set_foreground(spl::shared_ptr producer) + { + foreground_->monitor_output().detach_parent(); + foreground_ = std::move(producer); + foreground_->monitor_output().attach_parent(monitor_subject_); + } + + void pause() + { + foreground_->paused(true); + is_paused_ = true; + } + + void resume() + { + foreground_->paused(false); + is_paused_ = false; + } + + void load(spl::shared_ptr producer, bool preview, const boost::optional& auto_play_delta) + { +// background_->unsubscribe(background_event_subject_); + background_ = std::move(producer); +// background_->subscribe(background_event_subject_); + + auto_play_delta_ = auto_play_delta; + + if(preview) + { + play(); + receive(video_format::invalid); + foreground_->paused(true); + is_paused_ = true; + } + + if(auto_play_delta_ && foreground_ == frame_producer::empty()) + play(); + } + + void play() + { + if(background_ != frame_producer::empty()) + { + background_->leading_producer(foreground_); + + set_foreground(background_); + background_ = std::move(frame_producer::empty()); + + auto_play_delta_.reset(); + } + + foreground_->paused(false); + is_paused_ = false; + } + + void stop() + { + set_foreground(frame_producer::empty()); + + auto_play_delta_.reset(); + } + + draw_frame receive(const video_format_desc& format_desc) + { + try + { + *monitor_subject_ << monitor::message("/paused") % is_paused_; + + caspar::timer produce_timer; + auto frame = foreground_->receive(); + auto produce_time = produce_timer.elapsed(); + + *monitor_subject_ << monitor::message("/profiler/time") % produce_time % (1.0 / format_desc.fps); + + 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(); + + current_frame_age_ = frame.get_and_record_age_millis(); + + return frame; + } + catch(...) + { + CASPAR_LOG_CURRENT_EXCEPTION(); + stop(); + return core::draw_frame::empty(); + } + } + + boost::property_tree::wptree info() const + { + 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(L"frame-age", current_frame_age_); + info.add_child(L"producer", foreground_->info()); + info.add_child(L"background.producer", background_->info()); + return info; + } + + boost::property_tree::wptree delay_info() const + { + boost::property_tree::wptree info; + info.add(L"producer", foreground_->print()); + info.add(L"frame-age", current_frame_age_); + return info; + } + + void on_interaction(const interaction_event::ptr& event) + { + foreground_->on_interaction(event); + } + + bool collides(double x, double y) const + { + return foreground_->collides(x, y); + } +}; + +layer::layer(int index) : impl_(new impl(index)){} +layer::layer(layer&& other) : impl_(std::move(other.impl_)){} +layer& layer::operator=(layer&& other) +{ + other.swap(*this); + return *this; +} +void layer::swap(layer& other) +{ + impl_.swap(other.impl_); +} +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::resume(){impl_->resume();} +void layer::stop(){impl_->stop();} +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();} +boost::property_tree::wptree layer::delay_info() const{return impl_->delay_info();} +monitor::subject& layer::monitor_output() {return *impl_->monitor_subject_;} +void layer::on_interaction(const interaction_event::ptr& event) { impl_->on_interaction(event); } +bool layer::collides(double x, double y) const { return impl_->collides(x, y); } +}}