]> git.sesse.net Git - casparcg/commitdiff
2.0.0.2:
authorronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Sat, 8 Jan 2011 17:58:48 +0000 (17:58 +0000)
committerronag <ronag@362d55ac-95cf-4e76-9f9a-cbaa9c17b72d>
Sat, 8 Jan 2011 17:58:48 +0000 (17:58 +0000)
 - Timer improvements.

git-svn-id: https://casparcg.svn.sourceforge.net/svnroot/casparcg/server/branches/2.0.0.2@320 362d55ac-95cf-4e76-9f9a-cbaa9c17b72d

common/common.vcxproj
common/common.vcxproj.filters
common/utility/timer.h [new file with mode: 0644]
core/channel.cpp
core/consumer/frame_consumer.h
core/consumer/frame_consumer_device.cpp
core/producer/flash/flash_producer.cpp
shell/caspar.config

index 4009dbdefa743a736bb2c711d1b88ebe8d99c5a5..e538775d2d1b106363414073558ea03a17734582 100644 (file)
     <ClInclude Include="utility\safe_ptr.h" />\r
     <ClInclude Include="utility\singleton_pool.h" />\r
     <ClInclude Include="utility\string_convert.h" />\r
+    <ClInclude Include="utility\timer.h" />\r
   </ItemGroup>\r
   <ItemGroup>\r
     <ClCompile Include="concurrency\Thread.cpp">\r
index 626d60cc0f97b24127d4f9e1f7804a622a6ec3e7..2dce8635bf64a58d5f22b6f8b40c2e6ebcf0022f 100644 (file)
     <ClInclude Include="utility\singleton_pool.h">\r
       <Filter>Source\utility</Filter>\r
     </ClInclude>\r
+    <ClInclude Include="utility\timer.h">\r
+      <Filter>Source\utility</Filter>\r
+    </ClInclude>\r
   </ItemGroup>\r
 </Project>
\ No newline at end of file
diff --git a/common/utility/timer.h b/common/utility/timer.h
new file mode 100644 (file)
index 0000000..f026f1f
--- /dev/null
@@ -0,0 +1,65 @@
+#pragma once\r
+\r
+#include <windows.h>\r
+\r
+namespace caspar {\r
+       \r
+class timer\r
+{\r
+public:\r
+       timer(int fps = 25) : fps_(fps)\r
+       {\r
+               QueryPerformanceFrequency(&freq_);\r
+               time_.QuadPart = 0;\r
+       }\r
+               \r
+       // Author: Ryan M. Geiss\r
+       // http://www.geisswerks.com/ryan/FAQS/timing.html\r
+       void wait()\r
+       {       \r
+               LARGE_INTEGER t;\r
+               QueryPerformanceCounter(&t);\r
+\r
+               if (time_.QuadPart != 0)\r
+               {\r
+                       int ticks_to_wait = static_cast<int>(freq_.QuadPart / fps_);\r
+                       int done = 0;\r
+                       do\r
+                       {\r
+                               QueryPerformanceCounter(&t);\r
+                               \r
+                               int ticks_passed = static_cast<int>(static_cast<__int64>(t.QuadPart) - static_cast<__int64>(time_.QuadPart));\r
+                               int ticks_left = ticks_to_wait - ticks_passed;\r
+\r
+                               if (t.QuadPart < time_.QuadPart)    // time wrap\r
+                                       done = 1;\r
+                               if (ticks_passed >= ticks_to_wait)\r
+                                       done = 1;\r
+                               \r
+                               if (!done)\r
+                               {\r
+                                       // if > 0.002s left, do Sleep(1), which will actually sleep some \r
+                                       //   steady amount, probably 1-2 ms,\r
+                                       //   and do so in a nice way (cpu meter drops; laptop battery spared).\r
+                                       // otherwise, do a few Sleep(0)'s, which just give up the timeslice,\r
+                                       //   but don't really save cpu or battery, but do pass a tiny\r
+                                       //   amount of time.\r
+                                       if (ticks_left > static_cast<int>((freq_.QuadPart*2)/1000))\r
+                                               Sleep(1);\r
+                                       else                        \r
+                                               for (int i = 0; i < 10; ++i) \r
+                                                       Sleep(0);  // causes thread to give up its timeslice\r
+                               }\r
+                       }\r
+                       while (!done);            \r
+               }\r
+\r
+               time_ = t;\r
+       }\r
+private:\r
+       LARGE_INTEGER freq_;\r
+       LARGE_INTEGER time_;\r
+       int fps_;\r
+};\r
+\r
+}
\ No newline at end of file
index 76c2d2420ac02cdd7e48ba5218fac7665ff3d2ce..000052dea070815ecd712dd2984c208e1281ddad 100644 (file)
 \r
 namespace caspar { namespace core {\r
 \r
-class clock\r
-{\r
-public:\r
-       clock(int fps = 25) : fps_(fps)\r
-       {\r
-               QueryPerformanceFrequency(&freq_);\r
-               time_.QuadPart = 0;\r
-       }\r
-               \r
-       // Author: Ryan M. Geiss\r
-       // http://www.geisswerks.com/ryan/FAQS/timing.html\r
-       void wait()\r
-       {       \r
-               LARGE_INTEGER t;\r
-               QueryPerformanceCounter(&t);\r
-\r
-               if (time_.QuadPart != 0)\r
-               {\r
-                       int ticks_to_wait = static_cast<int>(freq_.QuadPart / fps_);\r
-                       int done = 0;\r
-                       do\r
-                       {\r
-                               QueryPerformanceCounter(&t);\r
-                               \r
-                               int ticks_passed = static_cast<int>(static_cast<__int64>(t.QuadPart) - static_cast<__int64>(time_.QuadPart));\r
-                               int ticks_left = ticks_to_wait - ticks_passed;\r
-\r
-                               if (t.QuadPart < time_.QuadPart)    // time wrap\r
-                                       done = 1;\r
-                               if (ticks_passed >= ticks_to_wait)\r
-                                       done = 1;\r
-                               \r
-                               if (!done)\r
-                               {\r
-                                       // if > 0.002s left, do Sleep(1), which will actually sleep some \r
-                                       //   steady amount, probably 1-2 ms,\r
-                                       //   and do so in a nice way (cpu meter drops; laptop battery spared).\r
-                                       // otherwise, do a few Sleep(0)'s, which just give up the timeslice,\r
-                                       //   but don't really save cpu or battery, but do pass a tiny\r
-                                       //   amount of time.\r
-                                       if (ticks_left > static_cast<int>((freq_.QuadPart*2)/1000))\r
-                                               Sleep(1);\r
-                                       else                        \r
-                                               for (int i = 0; i < 10; ++i) \r
-                                                       Sleep(0);  // causes thread to give up its timeslice\r
-                               }\r
-                       }\r
-                       while (!done);            \r
-               }\r
-\r
-               time_ = t;\r
-       }\r
-private:\r
-       LARGE_INTEGER freq_;\r
-       LARGE_INTEGER time_;\r
-       int fps_;\r
-};\r
-\r
 struct channel::implementation : boost::noncopyable\r
 {      \r
        implementation(const video_format_desc& format_desc, const std::vector<safe_ptr<frame_consumer>>& consumers)  \r
@@ -93,8 +35,6 @@ struct channel::implementation : boost::noncopyable
                consumer_device_.consume(std::move(processed_frame));\r
 \r
                executor_.begin_invoke([=]{tick();});\r
-\r
-               clock_.wait();\r
        }\r
        \r
        safe_ptr<draw_frame> draw()\r
@@ -206,8 +146,6 @@ struct channel::implementation : boost::noncopyable
                });\r
        }\r
 \r
-       clock clock_;\r
-\r
        mutable executor executor_;\r
                                \r
        safe_ptr<frame_processor_device> processor_device_;\r
index b0b59d7f4eb844c694b6dba680deed2b9efd21ea..f639b026ba50732e5cd594d249929db435b3970a 100644 (file)
@@ -29,10 +29,16 @@ class read_frame;
 \r
 struct frame_consumer : boost::noncopyable\r
 {\r
+       enum sync_mode\r
+       {\r
+               ready = 0,\r
+               clock           \r
+       };\r
+\r
        virtual ~frame_consumer() {}\r
 \r
        virtual void send(const safe_ptr<const read_frame>& frame) = 0;\r
-       virtual void synchronize() = 0;\r
+       virtual sync_mode synchronize() = 0;\r
        virtual size_t buffer_depth() const = 0;\r
 };\r
 \r
index 000a10ca89d11a8be5b1ce321442a7ff564e43a1..5c0552d99f40d59e1b3486ccdfdb43dcf3d6f9cd 100644 (file)
@@ -9,6 +9,7 @@
 #include "../format/video_format.h"\r
 \r
 #include <common/concurrency/executor.h>\r
+#include <common/utility/timer.h>\r
 \r
 #include <tbb/concurrent_queue.h>\r
 #include <tbb/atomic.h>\r
@@ -34,7 +35,7 @@ public:
        void tick(const safe_ptr<const read_frame>& frame)\r
        {\r
                buffer_.push_back(frame);\r
-\r
+       \r
                boost::range::for_each(consumers_, [&](const safe_ptr<frame_consumer>& consumer)\r
                {\r
                        size_t offset = max_depth_ - consumer->buffer_depth();\r
@@ -42,6 +43,7 @@ public:
                                consumer->send(*(buffer_.begin() + offset));\r
                });\r
                        \r
+               frame_consumer::sync_mode sync = frame_consumer::ready;\r
                boost::range::for_each(consumers_, [&](const safe_ptr<frame_consumer>& consumer)\r
                {\r
                        try\r
@@ -50,7 +52,8 @@ public:
                                if(offset >= buffer_.size())\r
                                        return;\r
 \r
-                               consumer->synchronize();\r
+                               if(consumer->synchronize() == frame_consumer::clock)\r
+                                       sync = frame_consumer::clock;\r
                        }\r
                        catch(...)\r
                        {\r
@@ -60,6 +63,9 @@ public:
                        }\r
                });\r
        \r
+               if(sync != frame_consumer::clock)\r
+                       clock_.wait();\r
+\r
                if(buffer_.size() >= max_depth_)\r
                        buffer_.pop_front();\r
        }\r
@@ -72,6 +78,7 @@ public:
                        executor_.invoke([=]{tick(frame);});\r
        }\r
 \r
+       timer clock_;\r
        executor executor_;     \r
 \r
        size_t max_depth_;\r
index 86fd02728f79a93bdb34cf90410345521cd1a424..d1ea6a99bb7cd309e9b2a4f4a00e6d01d2fc2faa 100644 (file)
@@ -34,6 +34,7 @@
 #include "../../processor/frame_processor_device.h"\r
 \r
 #include <common/concurrency/executor.h>\r
+#include <common/utility/timer.h>\r
 \r
 #include <boost/filesystem.hpp>\r
 \r
@@ -106,6 +107,9 @@ public:
        \r
        safe_ptr<draw_frame> render_frame()\r
        {\r
+               if(ax_->IsEmpty())\r
+                       return draw_frame::empty();\r
+\r
                auto frame = render_simple_frame();\r
                \r
                auto running_fps = ax_->GetFPS();\r
@@ -122,8 +126,7 @@ private:
 \r
        safe_ptr<draw_frame> render_simple_frame()\r
        {\r
-               if(!ax_->IsEmpty())\r
-                       ax_->Tick();\r
+               ax_->Tick();\r
 \r
                if(ax_->InvalidRect())\r
                {                       \r
@@ -163,7 +166,7 @@ struct flash_producer::implementation
                        ::OleInitialize(nullptr);\r
                });\r
 \r
-               frame_buffer_.set_capacity(2);\r
+               frame_buffer_.set_capacity(8);\r
                while(frame_buffer_.try_push(draw_frame::empty())){}\r
        }\r
 \r
@@ -184,23 +187,32 @@ struct flash_producer::implementation
 \r
                executor_.begin_invoke([=]\r
                {\r
+                       clock_.wait(); // Use high precision timer to sync rendering. Flash has free-running timers which can get out of sync with tick.\r
+\r
                        auto frame = draw_frame::empty();\r
-                       try\r
-                       {\r
-                               if(renderer_)\r
-                                       frame = renderer_->render_frame();\r
-                       }\r
-                       catch(...)\r
-                       {\r
-                               CASPAR_LOG_CURRENT_EXCEPTION();\r
-                               renderer_ = nullptr;\r
-                       }\r
-                       frame_buffer_.try_push(frame);\r
+                       do{frame = render_frame();} // Fill framebuffer when rendering empty frames.\r
+                       while(frame_buffer_.try_push(frame) && frame == draw_frame::empty());\r
                });     \r
                                \r
                return tail_;\r
        }\r
        \r
+       safe_ptr<draw_frame> render_frame()\r
+       {\r
+               auto frame = draw_frame::empty();\r
+               try\r
+               {\r
+                       if(renderer_)\r
+                               frame = renderer_->render_frame();\r
+               }\r
+               catch(...)\r
+               {\r
+                       CASPAR_LOG_CURRENT_EXCEPTION();\r
+                       renderer_ = nullptr;\r
+               }\r
+               return frame;\r
+       }\r
+\r
        virtual void initialize(const safe_ptr<frame_processor_device>& frame_processor)\r
        {\r
                frame_processor_ = frame_processor;\r
@@ -225,6 +237,8 @@ struct flash_producer::implementation
                        }\r
                });\r
        }\r
+\r
+       timer clock_;\r
        \r
        safe_ptr<draw_frame> tail_;\r
        tbb::concurrent_bounded_queue<safe_ptr<draw_frame>> frame_buffer_;\r
index ea5a9d5403661e6b9f69b5fd1f35647f6723d342..f38e5fa61eb227519875061cdd4498e51ba9a321 100644 (file)
@@ -15,7 +15,7 @@
           <stretch>uniform</stretch>\r
           <windowed>true</windowed>\r
         </ogl>\r
-        <audio/>\r
+        <!--audio/-->\r
         <!--decklink>          \r
         </decklink>\r
         <bluefish>\r