]> git.sesse.net Git - casparcg/blobdiff - modules/flash/producer/flash_producer.cpp
Use asmlib instead of our own custom memory functions.
[casparcg] / modules / flash / producer / flash_producer.cpp
index aa03e29807fa4c7db71db952e7c9828c25b8a921..c21004b8e24983b04e34414eb00e03865ac6d079 100644 (file)
@@ -1,23 +1,24 @@
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
 *\r
-*  This file is part of CasparCG.\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
+* 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
+* 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
+\r
 #include "../stdafx.h"\r
 \r
 #if defined(_MSC_VER)\r
 #include <common/env.h>\r
 #include <common/concurrency/com_context.h>\r
 #include <common/diagnostics/graph.h>\r
-#include <common/memory/memcpy.h>\r
-#include <common/memory/memclr.h>\r
 #include <common/utility/timer.h>\r
 \r
 #include <boost/filesystem.hpp>\r
+#include <boost/property_tree/ptree.hpp>\r
 #include <boost/thread.hpp>\r
 #include <boost/timer.hpp>\r
 #include <boost/algorithm/string.hpp>\r
@@ -63,10 +63,10 @@ public:
                memset(&info, 0, sizeof(BITMAPINFO));\r
                info.bmiHeader.biBitCount = 32;\r
                info.bmiHeader.biCompression = BI_RGB;\r
-               info.bmiHeader.biHeight = -height;\r
+               info.bmiHeader.biHeight = static_cast<LONG>(-height);\r
                info.bmiHeader.biPlanes = 1;\r
                info.bmiHeader.biSize = sizeof(BITMAPINFO);\r
-               info.bmiHeader.biWidth = width;\r
+               info.bmiHeader.biWidth = static_cast<LONG>(width);\r
 \r
                bmp_.reset(CreateDIBSection(static_cast<HDC>(hdc_.get()), &info, DIB_RGB_COLORS, reinterpret_cast<void**>(&bmp_data_), 0, 0), DeleteObject);\r
                SelectObject(static_cast<HDC>(hdc_.get()), bmp_.get()); \r
@@ -88,10 +88,10 @@ private:
 \r
 struct template_host\r
 {\r
-       std::string  field_mode;\r
-       std::string  filename;\r
-       size_t           width;\r
-       size_t           height;\r
+       std::wstring  video_mode;\r
+       std::wstring  filename;\r
+       int                       width;\r
+       int                       height;\r
 };\r
 \r
 template_host get_template_host(const core::video_format_desc& desc)\r
@@ -99,45 +99,43 @@ template_host get_template_host(const core::video_format_desc& desc)
        try\r
        {\r
                std::vector<template_host> template_hosts;\r
-               BOOST_FOREACH(auto& xml_mapping, env::properties().get_child("configuration.producers.template-hosts"))\r
+               BOOST_FOREACH(auto& xml_mapping, env::properties().get_child(L"configuration.template-hosts"))\r
                {\r
                        try\r
                        {\r
                                template_host template_host;\r
-                               template_host.field_mode                = xml_mapping.second.get("video-mode", narrow(desc.name));\r
-                               template_host.filename                  = xml_mapping.second.get("filename", "cg.fth");\r
-                               template_host.width                             = xml_mapping.second.get("width", desc.width);\r
-                               template_host.height                    = xml_mapping.second.get("height", desc.height);\r
+                               template_host.video_mode                = xml_mapping.second.get(L"video-mode", L"");\r
+                               template_host.filename                  = xml_mapping.second.get(L"filename",   L"cg.fth");\r
+                               template_host.width                             = xml_mapping.second.get(L"width",              desc.width);\r
+                               template_host.height                    = xml_mapping.second.get(L"height",             desc.height);\r
                                template_hosts.push_back(template_host);\r
                        }\r
                        catch(...){}\r
                }\r
 \r
-               auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.field_mode == narrow(desc.name);});\r
+               auto template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == desc.name;});\r
                if(template_host_it == template_hosts.end())\r
-                       template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.field_mode == "";});\r
+                       template_host_it = boost::find_if(template_hosts, [&](template_host template_host){return template_host.video_mode == L"";});\r
 \r
                if(template_host_it != template_hosts.end())\r
                        return *template_host_it;\r
        }\r
-       catch(...)\r
-       {\r
-       }\r
+       catch(...){}\r
                \r
        template_host template_host;\r
-       template_host.filename = "cg.fth";\r
+       template_host.filename = L"cg.fth";\r
 \r
-       for(auto it = boost::filesystem2::wdirectory_iterator(env::template_folder()); it != boost::filesystem2::wdirectory_iterator(); ++it)\r
+       for(auto it = boost::filesystem::directory_iterator(env::template_folder()); it != boost::filesystem::directory_iterator(); ++it)\r
        {\r
-               if(boost::iequals(it->path().extension(), L"." + desc.name))\r
+               if(boost::iequals(it->path().extension().wstring(), L"." + desc.name))\r
                {\r
-                       template_host.filename = narrow(it->filename());\r
+                       template_host.filename = it->path().filename().wstring();\r
                        break;\r
                }\r
        }\r
 \r
-       template_host.width = desc.width;\r
-       template_host.height = desc.height;\r
+       template_host.width =  desc.square_width;\r
+       template_host.height = desc.square_height;\r
        return template_host;\r
 }\r
 \r
@@ -157,8 +155,8 @@ class flash_renderer
 \r
        high_prec_timer timer_;\r
 \r
-       const size_t width_;\r
-       const size_t height_;\r
+       const int width_;\r
+       const int height_;\r
        \r
 public:\r
        flash_renderer(const safe_ptr<diagnostics::graph>& graph, const std::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int width, int height) \r
@@ -179,29 +177,28 @@ public:
                graph_->set_color("skip-sync", diagnostics::color(0.8f, 0.3f, 0.2f));                   \r
                \r
                if(FAILED(CComObject<caspar::flash::FlashAxContainer>::CreateInstance(&ax_)))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to create FlashAxContainer"));\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << wmsg_info(print() + L" Failed to create FlashAxContainer"));\r
                \r
                ax_->set_print([this]{return L"flash_renderer";});\r
 \r
                if(FAILED(ax_->CreateAxControl()))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Create FlashAxControl"));\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << wmsg_info(print() + L" Failed to Create FlashAxControl"));\r
                \r
                CComPtr<IShockwaveFlash> spFlash;\r
                if(FAILED(ax_->QueryControl(&spFlash)))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Query FlashAxControl"));\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << wmsg_info(print() + L" Failed to Query FlashAxControl"));\r
                                                                                                \r
                if(FAILED(spFlash->put_Playing(true)) )\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to start playing Flash"));\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << wmsg_info(print() + L" Failed to start playing Flash"));\r
 \r
                if(FAILED(spFlash->put_Movie(CComBSTR(filename.c_str()))))\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Load Template Host"));\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << wmsg_info(print() + L" Failed to Load Template Host"));\r
                                                                                \r
                if(FAILED(spFlash->put_ScaleMode(2)))  //Exact fit. Scale without respect to the aspect ratio.\r
-                       BOOST_THROW_EXCEPTION(caspar_exception() << msg_info(narrow(print()) + " Failed to Set Scale Mode"));\r
+                       BOOST_THROW_EXCEPTION(caspar_exception() << wmsg_info(print() + L" Failed to Set Scale Mode"));\r
                                                \r
                ax_->SetSize(width_, height_);          \r
        \r
-               CASPAR_LOG(info) << print() << L" Thread started.";\r
                CASPAR_LOG(info) << print() << L" Successfully initialized with template-host: " << filename << L" width: " << width_ << L" height: " << height_ << L".";\r
        }\r
 \r
@@ -212,21 +209,25 @@ public:
                        ax_->DestroyAxControl();\r
                        ax_->Release();\r
                }\r
-               CASPAR_LOG(info) << print() << L" Thread ended.";\r
+               CASPAR_LOG(info) << print() << L" Uninitialized.";\r
        }\r
        \r
-       void param(const std::wstring& param)\r
+       std::wstring call(const std::wstring& param)\r
        {               \r
-               if(!ax_->FlashCall(param))\r
-                       CASPAR_LOG(warning) << print() << L" Flash call failed:" << param;//BOOST_THROW_EXCEPTION(invalid_operation() << msg_info("Flash function call failed.") << arg_name_info("param") << arg_value_info(narrow(param)));\r
+               std::wstring result;\r
+\r
+               if(!ax_->FlashCall(param, result))\r
+                       CASPAR_LOG(warning) << print() << L" Flash call failed:" << param;//BOOST_THROW_EXCEPTION(invalid_operation() << wmsg_info("Flash function call failed.") << arg_name_info("param") << arg_value_info(u8(param)));\r
                graph_->add_tag("param");\r
+\r
+               return result;\r
        }\r
        \r
        safe_ptr<core::basic_frame> render_frame(bool has_underflow)\r
        {\r
                float frame_time = 1.0f/ax_->GetFPS();\r
 \r
-               graph_->update_value("tick-time", static_cast<float>(tick_timer_.elapsed()/frame_time)*0.5f);\r
+               graph_->update_value("tick-time", (tick_timer_.elapsed()/frame_time)*0.5f);\r
                tick_timer_.restart();\r
 \r
                if(ax_->IsEmpty())\r
@@ -242,11 +243,15 @@ public:
                ax_->Tick();\r
                if(ax_->InvalidRect())\r
                {                       \r
-                       fast_memclr(bmp_.data(), width_*height_*4);\r
+                       memset(bmp_.data(), 0, width_*height_*4);\r
                        ax_->DrawControl(bmp_);\r
                \r
-                       auto frame = frame_factory_->create_frame(this, width_, height_);\r
-                       fast_memcpy(frame->image_data().begin(), bmp_.data(), width_*height_*4);\r
+                       core::pixel_format_desc desc;\r
+                       desc.pix_fmt = core::pixel_format::bgra;\r
+                       desc.planes.push_back(core::pixel_format_desc::plane(width_, height_, 4));\r
+                       auto frame = frame_factory_->create_frame(this, desc);\r
+\r
+                       memcpy(frame->image_data().begin(), bmp_.data(), width_*height_*4);\r
                        frame->commit();\r
                        head_ = frame;\r
                }               \r
@@ -267,45 +272,47 @@ public:
        \r
        std::wstring print()\r
        {\r
-               return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"]";               \r
+               return L"flash[" + boost::filesystem::path(filename_).filename().wstring() + L"]";              \r
        }\r
 };\r
 \r
 struct flash_producer : public core::frame_producer\r
 {      \r
-       const std::wstring filename_;   \r
-       const safe_ptr<core::frame_factory> frame_factory_;\r
+       const std::wstring                                                                                      filename_;      \r
+       const safe_ptr<core::frame_factory>                                                     frame_factory_;\r
 \r
-       tbb::atomic<int> fps_;\r
+       tbb::atomic<int>                                                                                        fps_;\r
 \r
-       std::shared_ptr<diagnostics::graph> graph_;\r
+       safe_ptr<diagnostics::graph> graph_;\r
 \r
-       tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>> frame_buffer_;\r
+       tbb::concurrent_bounded_queue<safe_ptr<core::basic_frame>>      frame_buffer_;\r
 \r
-       mutable tbb::spin_mutex         last_frame_mutex_;\r
-       safe_ptr<core::basic_frame>     last_frame_;\r
-                               \r
-       com_context<flash_renderer> context_;   \r
+       mutable tbb::spin_mutex                                                                         last_frame_mutex_;\r
+       safe_ptr<core::basic_frame>                                                                     last_frame_;\r
+               \r
+       int                                                                                                                     width_;\r
+       int                                                                                                                     height_;\r
+\r
+       com_context<flash_renderer>                                                                     context_;       \r
 \r
-       int width_;\r
-       int height_;\r
 public:\r
-       flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, size_t width, size_t height) \r
+       flash_producer(const safe_ptr<core::frame_factory>& frame_factory, const std::wstring& filename, int width, int height) \r
                : filename_(filename)           \r
                , frame_factory_(frame_factory)\r
-               , context_(L"flash_producer")\r
                , last_frame_(core::basic_frame::empty())\r
                , width_(width > 0 ? width : frame_factory->get_video_format_desc().width)\r
                , height_(height > 0 ? height : frame_factory->get_video_format_desc().height)\r
+               , context_(L"flash_producer")\r
        {       \r
                if(!boost::filesystem::exists(filename))\r
-                       BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(narrow(filename)));  \r
+                       BOOST_THROW_EXCEPTION(file_not_found() << werr_file_name_info(filename));       \r
 \r
                fps_ = 0;\r
 \r
-               graph_ = diagnostics::create_graph(narrow(print()));\r
                graph_->set_color("output-buffer-count", diagnostics::color(1.0f, 1.0f, 0.0f));          \r
                graph_->set_color("underflow", diagnostics::color(0.6f, 0.3f, 0.9f));   \r
+               graph_->set_text(print());\r
+               diagnostics::register_graph(graph_);\r
                \r
                frame_buffer_.set_capacity(frame_factory_->get_video_format_desc().fps > 30.0 ? 2 : 1);\r
 \r
@@ -319,7 +326,7 @@ public:
 \r
        // frame_producer\r
                \r
-       virtual safe_ptr<core::basic_frame> receive(int)\r
+       virtual safe_ptr<core::basic_frame> receive(int) override\r
        {                               \r
                graph_->set_value("output-buffer-count", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));\r
 \r
@@ -330,22 +337,22 @@ public:
                return frame;\r
        }\r
 \r
-       virtual safe_ptr<core::basic_frame> last_frame() const\r
+       virtual safe_ptr<core::basic_frame> last_frame() const override\r
        {\r
                tbb::spin_mutex::scoped_lock lock(last_frame_mutex_);\r
                return last_frame_;\r
        }               \r
        \r
-       virtual void param(const std::wstring& param) \r
+       virtual boost::unique_future<std::wstring> call(const std::wstring& param) override\r
        {       \r
-               context_.begin_invoke([=]\r
+               return context_.begin_invoke([=]() -> std::wstring\r
                {\r
                        if(!context_)\r
                                initialize();\r
 \r
                        try\r
                        {\r
-                               context_->param(param); \r
+                               return context_->call(param);   \r
 \r
                                //const auto& format_desc = frame_factory_->get_video_format_desc();\r
                                //if(abs(context_->fps() - format_desc.fps) > 0.01 && abs(context_->fps()/2.0 - format_desc.fps) > 0.01)\r
@@ -357,14 +364,23 @@ public:
                                context_.reset(nullptr);\r
                                frame_buffer_.push(core::basic_frame::empty());\r
                        }\r
+\r
+                       return L"";\r
                });\r
        }\r
                \r
-       virtual std::wstring print() const\r
+       virtual std::wstring print() const override\r
        { \r
-               return L"flash[" + boost::filesystem::wpath(filename_).filename() + L"|" + boost::lexical_cast<std::wstring>(fps_) + L"]";              \r
+               return L"flash[" + boost::filesystem::wpath(filename_).filename().wstring() + L"|" + boost::lexical_cast<std::wstring>(fps_) + L"]";            \r
        }       \r
 \r
+       virtual boost::property_tree::wptree info() const override\r
+       {\r
+               boost::property_tree::wptree info;\r
+               info.add(L"type", L"flash-producer");\r
+               return info;\r
+       }\r
+\r
        // flash_producer\r
 \r
        void initialize()\r
@@ -419,7 +435,7 @@ public:
 \r
                                graph_->set_value("output-buffer-count", static_cast<float>(frame_buffer_.size())/static_cast<float>(frame_buffer_.capacity()));        \r
                                fps_.fetch_and_store(static_cast<int>(context_->fps()*100.0));                          \r
-                               graph_->update_text(narrow(print()));\r
+                               graph_->set_text(print());\r
 \r
                                render(renderer);\r
                        }\r
@@ -437,7 +453,7 @@ safe_ptr<core::frame_producer> create_producer(const safe_ptr<core::frame_factor
 {\r
        auto template_host = get_template_host(frame_factory->get_video_format_desc());\r
        \r
-       return make_safe<flash_producer>(frame_factory, env::template_folder() + L"\\" + widen(template_host.filename), template_host.width, template_host.height);\r
+       return create_producer_destroy_proxy(make_safe<flash_producer>(frame_factory, env::template_folder() + L"\\" + template_host.filename, template_host.width, template_host.height));\r
 }\r
 \r
 std::wstring find_template(const std::wstring& template_name)\r
@@ -447,6 +463,9 @@ std::wstring find_template(const std::wstring& template_name)
        \r
        if(boost::filesystem::exists(template_name + L".ct"))\r
                return template_name + L".ct";\r
+       \r
+       if(boost::filesystem::exists(template_name + L".swf"))\r
+               return template_name + L".swf";\r
 \r
        return L"";\r
 }\r