]> git.sesse.net Git - casparcg/commitdiff
[scene_producer] Added possibility to CALL/CG PLAY/CG STOP/CG NEXT/CG INVOKE layers...
authorHelge Norberg <helge.norberg@svt.se>
Mon, 20 Feb 2017 18:29:15 +0000 (19:29 +0100)
committerHelge Norberg <helge.norberg@svt.se>
Mon, 20 Feb 2017 18:29:15 +0000 (19:29 +0100)
core/producer/scene/scene.xsd
core/producer/scene/scene_producer.cpp
core/producer/scene/scene_producer.h
core/producer/scene/xml_scene_producer.cpp

index e4a72209740b6b2a3921e824dc772f201d9132bb..4fcffe0bbc0222dd7f8f6edf57acb1092864540b 100644 (file)
@@ -1,7 +1,7 @@
 <xs:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1">
   <xs:element name="scene">
     <xs:complexType>
-      <xs:sequence>
+      <xs:all>
         <xs:element name="variables">
           <xs:annotation>
             <xs:documentation>
             <xs:field xpath="@variable" />
           </xs:unique>
         </xs:element>
-      </xs:sequence>
+        <xs:element name="tasks" minOccurs="0">
+          <xs:annotation>
+            <xs:documentation>
+              Tasks are scheduled to happen whenever a certain condition arises.
+              Use the at attribute to let the condition be that the timeline arrives at a specific frame
+              or the when attribute to specify a custom bool expression that when becomes true triggers the task.
+            </xs:documentation>
+          </xs:annotation>
+          <xs:complexType>
+            <xs:choice minOccurs="0" maxOccurs="unbounded">
+              <xs:element name="call">
+                <xs:annotation><xs:documentation>Performs the equivalent of an AMCP CALL on the specified layer.</xs:documentation></xs:annotation>
+                <xs:complexType>
+                  <xs:sequence maxOccurs="unbounded">
+                    <xs:element name="arg" type="xs:string"><xs:annotation><xs:documentation>See the producer documentation for the possible arguments.</xs:documentation></xs:annotation></xs:element>
+                  </xs:sequence>
+                  <xs:attribute name="at" use="optional" type="xs:nonNegativeInteger"><xs:annotation><xs:documentation>Trigger the CALL when the timeline gets to this specific frame.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="when" use="optional" type="bool_expression"><xs:annotation><xs:documentation>Trigger the CALL when the specified bool expression becomes true.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="to_layer" use="required" type="identifier"><xs:annotation><xs:documentation>The producer on this layer will receive the CALL.</xs:documentation></xs:annotation></xs:attribute>
+                </xs:complexType>
+              </xs:element>
+              <xs:element name="cg_play">
+                <xs:annotation><xs:documentation>Performs the equivalent of a CG PLAY on the specified layer.</xs:documentation></xs:annotation>
+                <xs:complexType>
+                  <xs:attribute name="at" use="optional" type="xs:nonNegativeInteger"><xs:annotation><xs:documentation>Trigger the CG PLAY when the timeline gets to this specific frame.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="when" use="optional" type="bool_expression"><xs:annotation><xs:documentation>Trigger the CG PLAY when the specified bool expression becomes true.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="to_layer" use="required" type="identifier"><xs:annotation><xs:documentation>The CG producer on this layer will receive the CG PLAY.</xs:documentation></xs:annotation></xs:attribute>
+                </xs:complexType>
+              </xs:element>
+              <xs:element name="cg_stop">
+                <xs:annotation><xs:documentation>Performs the equivalent of a CG STOP on the specified layer.</xs:documentation></xs:annotation>
+                <xs:complexType>
+                  <xs:attribute name="at" use="optional" type="xs:nonNegativeInteger"><xs:annotation><xs:documentation>Trigger the CG STOP when the timeline gets to this specific frame.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="when" use="optional" type="bool_expression"><xs:annotation><xs:documentation>Trigger the CG STOP when the specified bool expression becomes true.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="to_layer" use="required" type="identifier"><xs:annotation><xs:documentation>The CG producer on this layer will receive the CG STOP.</xs:documentation></xs:annotation></xs:attribute>
+                </xs:complexType>
+              </xs:element>
+              <xs:element name="cg_next">
+                <xs:annotation><xs:documentation>Performs the equivalent of a CG NEXT on the specified layer.</xs:documentation></xs:annotation>
+                <xs:complexType>
+                  <xs:attribute name="at" use="optional" type="xs:nonNegativeInteger"><xs:annotation><xs:documentation>Trigger the CG NEXT when the timeline gets to this specific frame.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="when" use="optional" type="bool_expression"><xs:annotation><xs:documentation>Trigger the CG NEXT when the specified bool expression becomes true.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="to_layer" use="required" type="identifier"><xs:annotation><xs:documentation>The CG producer on this layer will receive the CG NEXT.</xs:documentation></xs:annotation></xs:attribute>
+                </xs:complexType>
+              </xs:element>
+              <xs:element name="cg_invoke">
+                <xs:annotation><xs:documentation>Performs the equivalent of a CG INVOKE on the specified layer with the specified label.</xs:documentation></xs:annotation>
+                <xs:complexType>
+                  <xs:attribute name="at" use="optional" type="xs:nonNegativeInteger"><xs:annotation><xs:documentation>Trigger the CG INVOKE when the timeline gets to this specific frame.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="when" use="optional" type="bool_expression"><xs:annotation><xs:documentation>Trigger the CG INVOKE when the specified bool expression becomes true.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="to_layer" use="required" type="identifier"><xs:annotation><xs:documentation>The CG producer on this layer will receive the CG INVOKE.</xs:documentation></xs:annotation></xs:attribute>
+                  <xs:attribute name="label" use="required" type="nonEmptyString"><xs:annotation><xs:documentation>The label that will be invoked.</xs:documentation></xs:annotation></xs:attribute>
+                </xs:complexType>
+              </xs:element>
+            </xs:choice>
+          </xs:complexType>
+        </xs:element>
+      </xs:all>
       <xs:attribute type="xs:positiveInteger" name="width" use="required" />
       <xs:attribute type="xs:positiveInteger" name="height" use="required" />
     </xs:complexType>
index 7c3466b110334ab373847def24c121158fb14fd6..efcfa3516460e6f94b8149be1a0a8d44fc663b53 100644 (file)
@@ -199,6 +199,15 @@ struct scene_producer::impl
                layers_.reverse();
        }
 
+       layer& get_layer(const std::wstring& name)
+       {
+               for (auto& layer : layers_)
+                       if (layer.name.get() == name)
+                               return layer;
+
+               CASPAR_THROW_EXCEPTION(user_error() << msg_info(name + L" not found in scene"));
+       }
+
        void store_keyframe(void* timeline_identity, const keyframe& k)
        {
                timelines_[timeline_identity].keyframes.insert(std::make_pair(k.destination_frame, k));
@@ -216,6 +225,27 @@ struct scene_producer::impl
                markers_by_frame_.insert(std::make_pair(frame, marker(action, label)));
        }
 
+       void add_task(binding<bool> when, std::function<void ()> task)
+       {
+               auto subscription = when.on_change([=]
+               {
+                       if (when.get())
+                       {
+                               try
+                               {
+                                       task();
+                               }
+                               catch (...)
+                               {
+                                       CASPAR_LOG_CURRENT_EXCEPTION_AT_LEVEL(debug);
+                                       CASPAR_LOG(error) << print() << " Error when invoking scene task. Turn on log level debug for stacktrace.";
+                               }
+                       }
+               });
+
+               task_subscriptions_.push_back(std::move(subscription));
+       }
+
        core::variable& get_variable(const std::wstring& name)
        {
                auto found = variables_.find(name);
@@ -614,10 +644,16 @@ layer& scene_producer::create_layer(
        return impl_->create_layer(producer, 0, 0, name);
 }
 
-void scene_producer::reverse_layers() {
+void scene_producer::reverse_layers()
+{
        impl_->reverse_layers();
 }
 
+layer& scene_producer::get_layer(const std::wstring& name)
+{
+       return impl_->get_layer(name);
+}
+
 binding<int64_t> scene_producer::timeline_frame()
 {
        return impl_->timeline_frame();
@@ -681,6 +717,11 @@ void scene_producer::add_mark(int64_t frame, mark_action action, const std::wstr
        impl_->add_mark(frame, action, label);
 }
 
+void scene_producer::add_task(binding<bool> when, std::function<void ()> task)
+{
+       impl_->add_task(std::move(when), std::move(task));
+}
+
 core::variable& scene_producer::get_variable(const std::wstring& name)
 {
        return impl_->get_variable(name);
index 4ae0b86c5c205f04f1d57f19fcc0318f9ecbfd35..a7c37710c31e7d7a1674cdfadccf0f4b2e8352a8 100644 (file)
@@ -133,6 +133,7 @@ public:
        layer& create_layer(
                        const spl::shared_ptr<frame_producer>& producer, const std::wstring& name);
        void reverse_layers();
+       layer& get_layer(const std::wstring& name);
 
        binding<int64_t> timeline_frame();
        binding<double> speed();
@@ -225,7 +226,8 @@ public:
                store_keyframe(to_affect.identity(), k);
        }
 
-       void add_mark(int64_t frame, mark_action action, const std::wstring& label);
+       void add_mark(int64_t at_frame, mark_action action, const std::wstring& label);
+       void add_task(binding<bool> when, std::function<void ()> task);
 
        core::variable& get_variable(const std::wstring& name) override;
        const std::vector<std::wstring>& get_variables() const override;
index 3c1bc18ca13d1da9e30d66774bb01f4b52cd29f3..10c4aca6db751395504ac21c4bad8dc005423b86 100644 (file)
@@ -38,6 +38,7 @@
 #include <core/help/help_sink.h>
 
 #include <future>
+#include <vector>
 
 #include "scene_producer.h"
 #include "scene_cg_proxy.h"
@@ -236,8 +237,8 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
 
                                ptree_verify_element_name(k, L"keyframe");
 
-                               auto easing = k.second.get(L"<xmlattr>.easing", L"");
-                               auto at = ptree_get<int64_t>(k.second, L"<xmlattr>.at");
+                               auto easing     = k.second.get(L"<xmlattr>.easing", L"");
+                               auto at         = ptree_get<int64_t>(k.second, L"<xmlattr>.at");
 
                                auto keyframe_variable_name = L"timeline." + variable_name + L"." + boost::lexical_cast<std::wstring>(at);
 
@@ -254,6 +255,68 @@ spl::shared_ptr<core::frame_producer> create_xml_scene_producer(
                return scene->get_variable(name);
        };
 
+       int task_id = 0;
+       if (root.get_child_optional(L"scene.tasks"))
+       {
+               for (auto& task : root | witerate_children(L"scene.tasks") | welement_context_iteration)
+               {
+                       auto& element_name = task.first;
+
+                       std::vector<std::wstring> call_params;
+
+                       if (element_name == L"call")
+                       {
+                               for (auto& arg : task.second)
+                               {
+                                       if (arg.first == L"<xmlattr>")
+                                               continue;
+
+                                       ptree_verify_element_name(arg, L"arg");
+                                       call_params.push_back(ptree_get_value<std::wstring>(arg.second));
+                               }
+                       }
+                       else if (element_name == L"cg_play")
+                               call_params.push_back(L"play()");
+                       else if (element_name == L"cg_stop")
+                               call_params.push_back(L"stop()");
+                       else if (element_name == L"cg_next")
+                               call_params.push_back(L"next()");
+                       else if (element_name == L"cg_invoke")
+                       {
+                               call_params.push_back(L"invoke()");
+                               call_params.push_back(ptree_get<std::wstring>(task.second, L"<xmlattr>.label"));
+                       }
+                       else
+                               CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Only valid element names in <tasks /> are call, cg_play, cg_stop, cg_next and cg_invoke"));
+
+                       auto at_frame   = task.second.get_optional<int64_t>(L"<xmlattr>.at");
+                       auto when               = task.second.get_optional<std::wstring>(L"<xmlattr>.when");
+                       auto to_layer   = ptree_get<std::wstring>(task.second, L"<xmlattr>.to_layer");
+                       auto producer   = scene->get_layer(to_layer).producer.get();
+
+                       binding<bool> condition;
+
+                       if (at_frame)
+                               condition = scene->timeline_frame() == *at_frame;
+                       else if (when)
+                               condition = scene->create_variable<bool>(L"tasks.task_" + boost::lexical_cast<std::wstring>(task_id), false, *when);
+                       else
+                               CASPAR_THROW_EXCEPTION(user_error() << msg_info(L"Task elements must have either an at attribute or a when attribute"));
+
+                       auto weak_producer = static_cast<std::weak_ptr<frame_producer>>(producer);
+
+                       scene->add_task(std::move(condition), [=]
+                       {
+                               auto strong = weak_producer.lock();
+
+                               if (strong)
+                                       strong->call(call_params);
+                       });
+
+                       ++task_id;
+               }
+       }
+
        for (auto& var_name : scene->get_variables())
        {
                CASPAR_SCOPED_CONTEXT_MSG(L"/scene/variables/variable[@id=" + var_name + L"]/text()");