]> git.sesse.net Git - casparcg/blobdiff - core/producer/layer.cpp
#40 Fixed problem with LOAD command not working
[casparcg] / core / producer / layer.cpp
index bc5267fa91e24d6382b47c458a89618a41a64af3..76473cc4eada0f3010e69cbb4d4ad0f7b4a6397a 100644 (file)
@@ -19,7 +19,7 @@
 * Author: Robert Nagy, ronag89@gmail.com
 */
 
-#include "../stdafx.h"
+#include "../StdAfx.h"
 
 #include "layer.h"
 
@@ -36,42 +36,57 @@ namespace caspar { namespace core {
 
 struct layer::impl
 {                              
-       monitor::basic_subject                          event_subject_;
-       monitor::basic_subject                          foreground_event_subject_;
-       monitor::basic_subject                          background_event_subject_;
-       spl::shared_ptr<frame_producer>         foreground_;
-       spl::shared_ptr<frame_producer>         background_;
+       spl::shared_ptr<monitor::subject>       monitor_subject_;
+       spl::shared_ptr<frame_producer>         foreground_                     = frame_producer::empty();
+       spl::shared_ptr<frame_producer>         background_                     = frame_producer::empty();;
        boost::optional<int32_t>                        auto_play_delta_;
+       bool                                                            is_paused_                      = false;
+       int64_t                                                         current_frame_age_      = 0;
 
 public:
        impl(int index) 
-               : event_subject_(monitor::path("layer") % index)
-               , foreground_event_subject_("")
-               , background_event_subject_("background")
-               , foreground_(frame_producer::empty())
-               , background_(frame_producer::empty())
+               : monitor_subject_(spl::make_shared<monitor::subject>(
+                               "/layer/" + boost::lexical_cast<std::string>(index)))
+//             , foreground_event_subject_("")
+//             , background_event_subject_("background")
        {
-               foreground_event_subject_.subscribe(event_subject_);
-               background_event_subject_.subscribe(event_subject_);
+//             foreground_event_subject_.subscribe(event_subject_);
+//             background_event_subject_.subscribe(event_subject_);
+       }
+
+       void set_foreground(spl::shared_ptr<frame_producer> 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<frame_producer> producer, bool preview, const boost::optional<int32_t>& auto_play_delta)
        {               
-               background_->unsubscribe(background_event_subject_);
+//             background_->unsubscribe(background_event_subject_);
                background_ = std::move(producer);
-               background_->subscribe(background_event_subject_);
+//             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())
@@ -84,25 +99,19 @@ public:
                {
                        background_->leading_producer(foreground_);
 
-                       background_->unsubscribe(background_event_subject_);
-                       foreground_->unsubscribe(foreground_event_subject_);
-
-                       foreground_ = std::move(background_);
+                       set_foreground(background_);
                        background_ = std::move(frame_producer::empty());
-                       
-                       foreground_->subscribe(foreground_event_subject_);
 
                        auto_play_delta_.reset();
                }
 
                foreground_->paused(false);
+               is_paused_ = false;
        }
        
        void stop()
        {
-               foreground_->unsubscribe(foreground_event_subject_);
-
-               foreground_ = std::move(frame_producer::empty());
+               set_foreground(frame_producer::empty());
 
                auto_play_delta_.reset();
        }
@@ -111,7 +120,13 @@ public:
        {               
                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();
@@ -126,13 +141,15 @@ public:
                                }
                        }
 
-                       event_subject_  << monitor::event("time")       % monitor::duration(foreground_->frame_number()/format_desc.fps)
-                                                                                                               % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
-                                                       << monitor::event("frame")      % static_cast<int64_t>(foreground_->frame_number())
-                                                                                                               % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
+                       //event_subject_        << monitor::event("time")       % monitor::duration(foreground_->frame_number()/format_desc.fps)
+                       //                                                                                      % monitor::duration(static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)/format_desc.fps)
+                       //                              << monitor::event("frame")      % static_cast<int64_t>(foreground_->frame_number())
+                       //                                                                                      % static_cast<int64_t>((static_cast<int64_t>(foreground_->nb_frames()) - static_cast<int64_t>(auto_play_delta_ ? *auto_play_delta_ : 0)));
+
+                       //foreground_event_subject_ << monitor::event("type") % foreground_->name();
+                       //background_event_subject_ << monitor::event("type") % background_->name();
 
-                       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;
                }
@@ -154,11 +171,20 @@ public:
 
                info.add(L"nb_frames",   nb_frames == std::numeric_limits<int64_t>::max() ? -1 : nb_frames);
                info.add(L"frames-left", nb_frames == std::numeric_limits<int64_t>::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);
@@ -184,13 +210,14 @@ void layer::swap(layer& other)
 void layer::load(spl::shared_ptr<frame_producer> frame_producer, bool preview, const boost::optional<int32_t>& 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<frame_producer> layer::foreground() const { return impl_->foreground_;}
 spl::shared_ptr<frame_producer> layer::background() const { return impl_->background_;}
 boost::property_tree::wptree layer::info() const{return impl_->info();}
-void layer::subscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.subscribe(o);}
-void layer::unsubscribe(const monitor::observable::observer_ptr& o) {impl_->event_subject_.unsubscribe(o);}
+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); }
-}}
\ No newline at end of file
+}}