]> git.sesse.net Git - casparcg/blobdiff - core/producer/layer.cpp
2.1.0: Make auto-play-delta a boost::optional instead of magic number int.
[casparcg] / core / producer / layer.cpp
index b145b9b88096ad1bb363bc4ea2d878f03241c59a..c2fa8a69a611a11196da9297dcc1ebdb8339d8b2 100644 (file)
+/*\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
+*\r
+* This file is part of CasparCG (www.casparcg.com).\r
+*\r
+* CasparCG is free software: you can redistribute it and/or modify\r
+* it under the terms of the GNU General Public License as published by\r
+* the Free Software Foundation, either version 3 of the License, or\r
+* (at your option) any later version.\r
+*\r
+* CasparCG is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
+*\r
+* Author: Robert Nagy, ronag89@gmail.com\r
+*/\r
+\r
 #include "../stdafx.h"\r
 \r
 #include "layer.h"\r
 \r
-#include "../processor/draw_frame.h"\r
-#include "../producer/frame_producer.h"\r
+#include "frame_producer.h"\r
+#include "frame/basic_frame.h"\r
+#include "frame/frame_transform.h"\r
 \r
-#include "../format/video_format.h"\r
+#include <boost/optional.hpp>\r
+#include <boost/property_tree/ptree.hpp>\r
 \r
 namespace caspar { namespace core {\r
 \r
-struct layer::implementation\r
-{              \r
-       implementation(size_t index) : foreground_(frame_producer::empty()), background_(frame_producer::empty()), last_frame_(draw_frame::empty()), index_(index) {}\r
-       \r
-       void load(const safe_ptr<frame_producer>& frame_producer, load_option::type option)\r
-       {                       \r
-               background_ = frame_producer;\r
-               if(option == load_option::preview)              \r
-               {\r
-                       foreground_ = frame_producer::empty();  \r
-                       last_frame_ = frame_producer->receive();\r
-               }\r
-               else if(option == load_option::auto_play)\r
-                       play();                 \r
+struct layer::impl\r
+{                              \r
+       safe_ptr<frame_producer>        foreground_;\r
+       safe_ptr<frame_producer>        background_;\r
+       int64_t                                         frame_number_;\r
+       boost::optional<int32_t>        auto_play_delta_;\r
+       bool                                            is_paused_;\r
+\r
+public:\r
+       impl() \r
+               : foreground_(frame_producer::empty())\r
+               , background_(frame_producer::empty())\r
+               , frame_number_(0)\r
+               , is_paused_(false)\r
+       {\r
        }\r
        \r
-       void play()\r
-       {                       \r
-               background_->set_leading_producer(foreground_);\r
-               foreground_ = background_;\r
-               background_ = frame_producer::empty();\r
-               is_paused_ = false;\r
-               CASPAR_LOG(info) << L"layer[" << index_ << L"] Started: " << foreground_->print();\r
-       }\r
-\r
        void pause()\r
        {\r
                is_paused_ = true;\r
        }\r
 \r
-       void stop()\r
+       void resume()\r
        {\r
-               foreground_ = frame_producer::empty();\r
-               last_frame_ = draw_frame::empty();\r
+               is_paused_ = false;\r
        }\r
 \r
-       void clear()\r
-       {\r
-               foreground_ = frame_producer::empty();\r
-               background_ = frame_producer::empty();\r
-               last_frame_ = draw_frame::empty();\r
+       void load(const safe_ptr<frame_producer>& producer, bool preview, const boost::optional<int32_t>& auto_play_delta)\r
+       {               \r
+               background_              = producer;\r
+               auto_play_delta_ = auto_play_delta;\r
+\r
+               if(auto_play_delta_ && foreground_ == frame_producer::empty())\r
+                       play();\r
+\r
+               if(preview) // Play the first frame and pause.\r
+               {                       \r
+                       play();\r
+                       receive(frame_producer::NO_FLAG);\r
+                       pause();\r
+               }\r
        }\r
        \r
-       safe_ptr<draw_frame> receive()\r
-       {               \r
-               if(foreground_ == frame_producer::empty() || is_paused_)\r
-                       return last_frame_;\r
+       void play()\r
+       {                       \r
+               if(background_ != frame_producer::empty())\r
+               {\r
+                       background_->set_leading_producer(foreground_);\r
+                       \r
+                       foreground_                     = background_;\r
+                       background_                     = frame_producer::empty();\r
+                       frame_number_           = 0;\r
+                       auto_play_delta_        = nullptr;      \r
+               }\r
 \r
+               is_paused_                      = false;\r
+       }\r
+       \r
+       void stop()\r
+       {\r
+               foreground_                     = frame_producer::empty();\r
+               background_                     = background_;\r
+               frame_number_           = 0;\r
+               auto_play_delta_        = nullptr;      \r
+\r
+               is_paused_                      = true;\r
+       }\r
+               \r
+       safe_ptr<basic_frame> receive(int flags)\r
+       {               \r
                try\r
                {\r
-                       last_frame_ = foreground_->receive();\r
+                       if(is_paused_)\r
+                               return disable_audio(foreground_->last_frame());\r
+               \r
+                       auto frame = receive_and_follow(foreground_, flags);\r
+                       if(frame == core::basic_frame::late())\r
+                               return disable_audio(foreground_->last_frame());\r
 \r
-                       if(last_frame_ == draw_frame::eof())\r
+                       if(auto_play_delta_)\r
                        {\r
-                               CASPAR_LOG(info) << L"layer[" << index_ << L"] Ended:" << foreground_->print();\r
-                               auto following = foreground_->get_following_producer();\r
-                               following->set_leading_producer(foreground_);\r
-                               foreground_ = following;\r
-                               if(foreground_ != frame_producer::empty())\r
-                                       CASPAR_LOG(info) << L"layer[" << index_ << L"] Started:" << foreground_->print();\r
-                               last_frame_ = receive();\r
+                               auto frames_left = static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(++frame_number_) - static_cast<int64_t>(*auto_play_delta_);\r
+                               if(frames_left < 1)\r
+                               {\r
+                                       play();\r
+                                       return receive(flags);\r
+                               }\r
                        }\r
+                               \r
+                       return frame;\r
                }\r
                catch(...)\r
                {\r
-                       try\r
-                       {\r
-                               CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               CASPAR_LOG(warning) << L"layer[" << index_ << L"] Error. Removed " << foreground_->print() << L" from layer.";\r
-                               foreground_ = frame_producer::empty();\r
-                               last_frame_ = draw_frame::empty();\r
-                       }\r
-                       catch(...){}\r
+                       CASPAR_LOG_CURRENT_EXCEPTION();\r
+                       stop();\r
+                       return core::basic_frame::empty();\r
                }\r
+       }\r
 \r
-               return last_frame_;\r
-       }       \r
-               \r
-       tbb::atomic<bool>                       is_paused_;\r
-       safe_ptr<draw_frame>            last_frame_;\r
-       safe_ptr<frame_producer>        foreground_;\r
-       safe_ptr<frame_producer>        background_;\r
-       size_t                                          index_;\r
+       boost::unique_future<std::wstring> call(bool foreground, const std::wstring& param)\r
+       {\r
+               return (foreground ? foreground_ : background_)->call(param);\r
+       }\r
+\r
+       bool empty() const\r
+       {\r
+               return background_ == core::frame_producer::empty() && foreground_ == core::frame_producer::empty();\r
+       }\r
+\r
+       boost::property_tree::wptree info() const\r
+       {\r
+               boost::property_tree::wptree info;\r
+               info.add(L"status",             is_paused_ ? L"paused" : (foreground_ == frame_producer::empty() ? L"stopped" : L"playing"));\r
+               info.add(L"auto_delta", auto_play_delta_);\r
+               info.add(L"frame-number", frame_number_);\r
+\r
+               auto nb_frames = foreground_->nb_frames();\r
+\r
+               info.add(L"nb_frames",   nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);\r
+               info.add(L"frames-left", nb_frames == std::numeric_limits<int64_t>::max() ? -1 : (foreground_->nb_frames() - frame_number_ - auto_play_delta_));\r
+               info.add_child(L"foreground.producer", foreground_->info());\r
+               info.add_child(L"background.producer", background_->info());\r
+               return info;\r
+       }\r
 };\r
 \r
-layer::layer(size_t index) : impl_(new implementation(index)){}\r
-layer::layer(layer&& other) : impl_(std::move(other.impl_)){other.impl_ = nullptr;}\r
+layer::layer() : impl_(new impl()){}\r
+layer::layer(layer&& other) : impl_(std::move(other.impl_)){}\r
 layer& layer::operator=(layer&& other)\r
 {\r
-       impl_ = std::move(other.impl_); \r
-       other.impl_ = nullptr;\r
+       impl_ = std::move(other.impl_);\r
+       return *this;\r
+}\r
+layer::layer(const layer& other) : impl_(new impl(*other.impl_)){}\r
+layer& layer::operator=(const layer& other)\r
+{\r
+       layer temp(other);\r
+       temp.swap(*this);\r
        return *this;\r
 }\r
-void layer::load(const safe_ptr<frame_producer>& frame_producer, load_option::type option){return impl_->load(frame_producer, option);}        \r
+void layer::swap(layer& other)\r
+{      \r
+       impl_.swap(other.impl_);\r
+}\r
+void layer::load(const safe_ptr<frame_producer>& frame_producer, bool preview, const boost::optional<int32_t>& auto_play_delta){return impl_->load(frame_producer, preview, auto_play_delta);} \r
 void layer::play(){impl_->play();}\r
 void layer::pause(){impl_->pause();}\r
 void layer::stop(){impl_->stop();}\r
-void layer::clear(){impl_->clear();}\r
-bool layer::empty() const { return impl_->foreground_ == frame_producer::empty() && impl_->background_ == frame_producer::empty();}\r
-safe_ptr<draw_frame> layer::receive() {return impl_->receive();}\r
+bool layer::is_paused() const{return impl_->is_paused_;}\r
+int64_t layer::frame_number() const{return impl_->frame_number_;}\r
+safe_ptr<basic_frame> layer::receive(int flags) {return impl_->receive(flags);}\r
 safe_ptr<frame_producer> layer::foreground() const { return impl_->foreground_;}\r
 safe_ptr<frame_producer> layer::background() const { return impl_->background_;}\r
+bool layer::empty() const {return impl_->empty();}\r
+boost::unique_future<std::wstring> layer::call(bool foreground, const std::wstring& param){return impl_->call(foreground, param);}\r
+boost::property_tree::wptree layer::info() const{return impl_->info();}\r
 }}
\ No newline at end of file