]> git.sesse.net Git - casparcg/blobdiff - modules/ogl/consumer/ogl_consumer.cpp
ogl_consumer: Add "FULLSCREEN" option to ADD SCREEN.
[casparcg] / modules / ogl / consumer / ogl_consumer.cpp
index 9550a5a1e81f99e783f7f444ee066021b83a3a7c..56629197da28d30580a43a17afd928ea33a91cf9 100644 (file)
@@ -1,21 +1,22 @@
 /*\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
 #include "ogl_consumer.h"\r
@@ -42,6 +43,7 @@
 #include <boost/circular_buffer.hpp>\r
 #include <boost/foreach.hpp>\r
 #include <boost/thread.hpp>\r
+#include <boost/property_tree/ptree.hpp>\r
 \r
 #include <tbb/atomic.h>\r
 #include <tbb/concurrent_queue.h>\r
@@ -66,6 +68,8 @@ extern "C"
 #pragma warning (pop)\r
 #endif\r
 \r
+typedef int (*PFNWGLEXTGETSWAPINTERVALPROC) (void);\r
\r
 namespace caspar { namespace ogl {\r
                \r
 enum stretch\r
@@ -78,18 +82,31 @@ enum stretch
 \r
 struct configuration\r
 {\r
-       size_t          screen_index;\r
-       stretch         stretch;\r
-       bool            windowed;\r
-       bool            auto_deinterlace;\r
-       bool            key_only;\r
+       enum aspect_ratio\r
+       {\r
+               aspect_4_3 = 0,\r
+               aspect_16_9,\r
+               aspect_invalid,\r
+       };\r
+               \r
+       std::wstring    name;\r
+       size_t                  screen_index;\r
+       stretch                 stretch;\r
+       bool                    windowed;\r
+       bool                    auto_deinterlace;\r
+       bool                    key_only;\r
+       aspect_ratio    aspect; \r
+       bool                    vsync;\r
 \r
        configuration()\r
-               : screen_index(0)\r
+               : name(L"ogl")\r
+               , screen_index(0)\r
                , stretch(fill)\r
                , windowed(true)\r
                , auto_deinterlace(true)\r
                , key_only(false)\r
+               , aspect(aspect_invalid)\r
+               , vsync(false)\r
        {\r
        }\r
 };\r
@@ -98,10 +115,11 @@ struct ogl_consumer : boost::noncopyable
 {              \r
        const configuration             config_;\r
        core::video_format_desc format_desc_;\r
-       \r
+       int                                             channel_index_;\r
+\r
        GLuint                                  texture_;\r
        std::vector<GLuint>             pbos_;\r
-       \r
+                       \r
        float                                   width_;\r
        float                                   height_;        \r
        unsigned int                    screen_x_;\r
@@ -115,34 +133,46 @@ struct ogl_consumer : boost::noncopyable
        \r
        safe_ptr<diagnostics::graph>    graph_;\r
        boost::timer                                    perf_timer_;\r
+       boost::timer                                    tick_timer_;\r
 \r
-       boost::circular_buffer<safe_ptr<core::read_frame>>                      input_buffer_;\r
        tbb::concurrent_bounded_queue<safe_ptr<core::read_frame>>       frame_buffer_;\r
 \r
        boost::thread                   thread_;\r
        tbb::atomic<bool>               is_running_;\r
-\r
        \r
        ffmpeg::filter                  filter_;\r
 public:\r
-       ogl_consumer(const configuration& config, const core::video_format_desc& format_desc) \r
+       ogl_consumer(const configuration& config, const core::video_format_desc& format_desc, int channel_index\r
                : config_(config)\r
                , format_desc_(format_desc)\r
+               , channel_index_(channel_index)\r
                , texture_(0)\r
                , pbos_(2, 0)   \r
                , screen_width_(format_desc.width)\r
                , screen_height_(format_desc.height)\r
                , square_width_(format_desc.square_width)\r
                , square_height_(format_desc.square_height)\r
-               , graph_(diagnostics::create_graph(narrow(print())))\r
-               , input_buffer_(core::consumer_buffer_depth()-1)\r
                , filter_(format_desc.field_mode == core::field_mode::progressive || !config.auto_deinterlace ? L"" : L"YADIF=0:-1", boost::assign::list_of(PIX_FMT_BGRA))\r
        {               \r
-               frame_buffer_.set_capacity(2);\r
+               if(format_desc_.format == core::video_format::ntsc && config_.aspect == configuration::aspect_4_3)\r
+               {\r
+                       // Use default values which are 4:3.\r
+               }\r
+               else\r
+               {\r
+                       if(config_.aspect == configuration::aspect_16_9)\r
+                               square_width_ = (format_desc.height*16)/9;\r
+                       else if(config_.aspect == configuration::aspect_4_3)\r
+                               square_width_ = (format_desc.height*4)/3;\r
+               }\r
 \r
-               graph_->add_guide("frame-time", 0.5);\r
-               graph_->set_color("frame-time", diagnostics::color(1.0f, 0.0f, 0.0f));\r
+               frame_buffer_.set_capacity(2);\r
+               \r
+               graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));   \r
+               graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));\r
                graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));\r
+               graph_->set_text(print());\r
+               diagnostics::register_graph(graph_);\r
                                                                        \r
                DISPLAY_DEVICE d_device = {sizeof(d_device), 0};                        \r
                std::vector<DISPLAY_DEVICE> displayDevices;\r
@@ -150,17 +180,17 @@ public:
                        displayDevices.push_back(d_device);\r
 \r
                if(config_.screen_index >= displayDevices.size())\r
-                       BOOST_THROW_EXCEPTION(out_of_range() << arg_name_info("screen_index_") << msg_info(narrow(print())));\r
+                       CASPAR_LOG(warning) << print() << L" Invalid screen-index: " << config_.screen_index;\r
                \r
                DEVMODE devmode = {};\r
                if(!EnumDisplaySettings(displayDevices[config_.screen_index].DeviceName, ENUM_CURRENT_SETTINGS, &devmode))\r
-                       BOOST_THROW_EXCEPTION(invalid_operation() << arg_name_info("screen_index") << msg_info(narrow(print()) + " EnumDisplaySettings"));\r
+                       CASPAR_LOG(warning) << print() << L" Could not find display settings for screen-index: " << config_.screen_index;\r
                \r
                screen_x_               = devmode.dmPosition.x;\r
                screen_y_               = devmode.dmPosition.y;\r
                screen_width_   = config_.windowed ? square_width_ : devmode.dmPelsWidth;\r
                screen_height_  = config_.windowed ? square_height_ : devmode.dmPelsHeight;\r
-               \r
+\r
                is_running_ = true;\r
                thread_ = boost::thread([this]{run();});\r
        }\r
@@ -177,7 +207,7 @@ public:
                if(!GLEW_VERSION_2_1)\r
                        BOOST_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 2.1 support."));\r
 \r
-               window_.Create(sf::VideoMode(screen_width_, screen_height_, 32), narrow(print()), config_.windowed ? sf::Style::Resize : sf::Style::Fullscreen);\r
+               window_.Create(sf::VideoMode(screen_width_, screen_height_, 32), narrow(print()), config_.windowed ? sf::Style::Resize | sf::Style::Close : sf::Style::Fullscreen);\r
                window_.ShowMouseCursor(false);\r
                window_.SetPosition(screen_x_, screen_y_);\r
                window_.SetSize(screen_width_, screen_height_);\r
@@ -192,8 +222,8 @@ public:
                        \r
                GL(glGenTextures(1, &texture_));\r
                GL(glBindTexture(GL_TEXTURE_2D, texture_));\r
-               GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST));\r
-               GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST));\r
+               GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));\r
+               GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));\r
                GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));\r
                GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));\r
                GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, format_desc_.width, format_desc_.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0));\r
@@ -206,8 +236,20 @@ public:
                glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[1]);\r
                glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);\r
                glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);\r
+                               \r
+               if(config_.vsync)\r
+               {\r
+                       auto wglSwapIntervalEXT = reinterpret_cast<void(APIENTRY*)(int)>(wglGetProcAddress("wglSwapIntervalEXT"));\r
+                       if(wglSwapIntervalEXT)\r
+                       {\r
+                               wglSwapIntervalEXT(1);\r
+                               CASPAR_LOG(info) << print() << " Successfully enabled vsync.";\r
+                       }\r
+                       else\r
+                               CASPAR_LOG(info) << print() << " Failed to enable vsync.";\r
+               }\r
 \r
-               CASPAR_LOG(info) << print() << " Sucessfully Initialized.";\r
+               CASPAR_LOG(info) << print() << " Successfully Initialized.";\r
        }\r
 \r
        void uninit()\r
@@ -220,8 +262,6 @@ public:
                        if(pbo)\r
                                glDeleteBuffers(1, &pbo);\r
                }\r
-\r
-               CASPAR_LOG(info) << print() << " Sucessfully Uninitialized.";\r
        }\r
 \r
        void run()\r
@@ -234,22 +274,27 @@ public:
                        {                       \r
                                try\r
                                {\r
-                                       perf_timer_.restart();\r
 \r
                                        sf::Event e;            \r
                                        while(window_.GetEvent(e))\r
                                        {\r
                                                if(e.Type == sf::Event::Resized)\r
                                                        calculate_aspect();\r
+                                               else if(e.Type == sf::Event::Closed)\r
+                                                       is_running_ = false;\r
                                        }\r
                        \r
                                        safe_ptr<core::read_frame> frame;\r
                                        frame_buffer_.pop(frame);\r
+                                       \r
+                                       perf_timer_.restart();\r
                                        render(frame);\r
+                                       graph_->set_value("frame-time", perf_timer_.elapsed()*format_desc_.fps*0.5);    \r
 \r
                                        window_.Display();\r
-\r
-                                       graph_->update_value("frame-time", static_cast<float>(perf_timer_.elapsed()*format_desc_.fps*0.5));     \r
+                                       \r
+                                       graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);     \r
+                                       tick_timer_.restart();\r
                                }\r
                                catch(...)\r
                                {\r
@@ -266,11 +311,6 @@ public:
                }\r
        }\r
        \r
-       const core::video_format_desc& get_video_format_desc() const\r
-       {\r
-               return format_desc_;\r
-       }\r
-\r
        safe_ptr<AVFrame> get_av_frame()\r
        {               \r
                safe_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);     \r
@@ -288,13 +328,14 @@ public:
 \r
        void render(const safe_ptr<core::read_frame>& frame)\r
        {                       \r
-               if(frame->image_data().empty())\r
+               if(static_cast<size_t>(frame->image_data().size()) != format_desc_.size)\r
                        return;\r
                                        \r
                auto av_frame = get_av_frame();\r
                av_frame->data[0] = const_cast<uint8_t*>(frame->image_data().begin());\r
 \r
-               auto frames = filter_.execute(av_frame);\r
+               filter_.push(av_frame);\r
+               auto frames = filter_.poll_all();\r
 \r
                if(frames.empty())\r
                        return;\r
@@ -352,20 +393,16 @@ public:
                std::rotate(pbos_.begin(), pbos_.begin() + 1, pbos_.end());\r
        }\r
 \r
-       void send(const safe_ptr<core::read_frame>& frame)\r
+       bool send(const safe_ptr<core::read_frame>& frame)\r
        {\r
-               input_buffer_.push_back(frame);\r
-\r
-               if(input_buffer_.full())\r
-               {\r
-                       if(!frame_buffer_.try_push(input_buffer_.front()))\r
-                               graph_->add_tag("dropped-frame");\r
-               }\r
+               if(!frame_buffer_.try_push(frame))\r
+                       graph_->set_tag("dropped-frame");\r
+               return is_running_;\r
        }\r
                \r
        std::wstring print() const\r
        {       \r
-               return  L"ogl[" + boost::lexical_cast<std::wstring>(config_.screen_index) + L"|" + format_desc_.name + L"]";\r
+               return config_.name + L"[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";\r
        }\r
        \r
        void calculate_aspect()\r
@@ -436,30 +473,58 @@ public:
        ogl_consumer_proxy(const configuration& config)\r
                : config_(config){}\r
        \r
-       virtual void initialize(const core::video_format_desc& format_desc)\r
+       ~ogl_consumer_proxy()\r
        {\r
-               consumer_.reset(new ogl_consumer(config_, format_desc));\r
+               if(consumer_)\r
+               {\r
+                       auto str = print();\r
+                       consumer_.reset();\r
+                       CASPAR_LOG(info) << str << L" Successfully Uninitialized.";     \r
+               }\r
+       }\r
+\r
+       // frame_consumer\r
+\r
+       virtual void initialize(const core::video_format_desc& format_desc, int channel_index) override\r
+       {\r
+               consumer_.reset();\r
+               consumer_.reset(new ogl_consumer(config_, format_desc, channel_index));\r
+               CASPAR_LOG(info) << print() << L" Successfully Initialized.";   \r
        }\r
        \r
-       virtual bool send(const safe_ptr<core::read_frame>& frame)\r
+       virtual bool send(const safe_ptr<core::read_frame>& frame) override\r
        {\r
-               consumer_->send(frame);\r
-               return true;\r
+               return consumer_->send(frame);\r
        }\r
        \r
-       virtual std::wstring print() const\r
+       virtual std::wstring print() const override\r
+       {\r
+               return consumer_ ? consumer_->print() : L"[ogl_consumer]";\r
+       }\r
+\r
+       virtual boost::property_tree::wptree info() const override\r
        {\r
-               return consumer_->print();\r
+               boost::property_tree::wptree info;\r
+               info.add(L"type", L"ogl-consumer");\r
+               info.add(L"key-only", config_.key_only);\r
+               info.add(L"windowed", config_.windowed);\r
+               info.add(L"auto-deinterlace", config_.auto_deinterlace);\r
+               return info;\r
        }\r
 \r
-       virtual bool has_synchronization_clock() const \r
+       virtual bool has_synchronization_clock() const override\r
        {\r
                return false;\r
        }\r
+       \r
+       virtual size_t buffer_depth() const override\r
+       {\r
+               return 1;\r
+       }\r
 \r
-       virtual const core::video_format_desc& get_video_format_desc() const\r
+       virtual int index() const override\r
        {\r
-               return consumer_->get_video_format_desc();\r
+               return 600;\r
        }\r
 };     \r
 \r
@@ -470,30 +535,41 @@ safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>&
        \r
        configuration config;\r
                \r
-       if(params.size() > 1) \r
-               config.screen_index = lexical_cast_or_default<int>(params[2], config.screen_index);\r
-\r
-       if(params.size() > 2) \r
-               config.windowed = lexical_cast_or_default<bool>(params[3], config.windowed);\r
-\r
+       auto device_it = std::find(params.begin(), params.end(), L"DEVICE");\r
+       if(device_it != params.end() && ++device_it != params.end())\r
+               config.screen_index = boost::lexical_cast<int>(*device_it);\r
+               \r
+       config.windowed = std::find(params.begin(), params.end(), L"FULLSCREEN") == params.end();\r
        config.key_only = std::find(params.begin(), params.end(), L"KEY_ONLY") != params.end();\r
 \r
+       auto name_it    = std::find(params.begin(), params.end(), L"NAME");\r
+       if(name_it != params.end() && ++name_it != params.end())\r
+               config.name = *name_it;\r
+\r
        return make_safe<ogl_consumer_proxy>(config);\r
 }\r
 \r
-safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::ptree& ptree) \r
+safe_ptr<core::frame_consumer> create_consumer(const boost::property_tree::wptree& ptree) \r
 {\r
        configuration config;\r
-       config.screen_index             = ptree.get("device",   config.screen_index);\r
-       config.windowed                 = ptree.get("windowed", config.windowed);\r
-       config.key_only                 = ptree.get("key-only", config.key_only);\r
-       config.auto_deinterlace = ptree.get("auto-deinterlace", config.auto_deinterlace);\r
-       \r
-       auto stretch_str = ptree.get("stretch", "default");\r
-       if(stretch_str == "uniform")\r
+       config.name                             = ptree.get(L"name",     config.name);\r
+       config.screen_index             = ptree.get(L"device",   config.screen_index+1)-1;\r
+       config.windowed                 = ptree.get(L"windowed", config.windowed);\r
+       config.key_only                 = ptree.get(L"key-only", config.key_only);\r
+       config.auto_deinterlace = ptree.get(L"auto-deinterlace", config.auto_deinterlace);\r
+       config.vsync                    = ptree.get(L"vsync", config.vsync);\r
+\r
+       auto stretch_str = ptree.get(L"stretch", L"default");\r
+       if(stretch_str == L"uniform")\r
                config.stretch = stretch::uniform;\r
-       else if(stretch_str == "uniform_to_fill")\r
+       else if(stretch_str == L"uniform_to_fill")\r
                config.stretch = stretch::uniform_to_fill;\r
+\r
+       auto aspect_str = ptree.get(L"aspect-ratio", L"default");\r
+       if(aspect_str == L"16:9")\r
+               config.aspect = configuration::aspect_16_9;\r
+       else if(aspect_str == L"4:3")\r
+               config.aspect = configuration::aspect_4_3;\r
        \r
        return make_safe<ogl_consumer_proxy>(config);\r
 }\r