2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "screen_consumer.h"
25 #include <SFML/Window.hpp>
27 #include <common/diagnostics/graph.h>
28 #include <common/gl/gl_check.h>
29 #include <common/log.h>
30 #include <common/memory.h>
31 #include <common/array.h>
32 #include <common/memshfl.h>
33 #include <common/utf.h>
34 #include <common/prec_timer.h>
35 #include <common/future.h>
36 #include <common/timer.h>
37 #include <common/param.h>
38 #include <common/os/general_protection_fault.h>
40 //#include <windows.h>
42 #include <ffmpeg/producer/filter/filter.h>
44 #include <core/video_format.h>
45 #include <core/frame/frame.h>
46 #include <core/consumer/frame_consumer.h>
47 #include <core/interaction/interaction_sink.h>
48 #include <core/help/help_sink.h>
49 #include <core/help/help_repository.h>
51 #include <boost/circular_buffer.hpp>
52 #include <boost/lexical_cast.hpp>
53 #include <boost/property_tree/ptree.hpp>
54 #include <boost/thread.hpp>
55 #include <boost/algorithm/string.hpp>
57 #include <tbb/atomic.h>
58 #include <tbb/concurrent_queue.h>
59 #include <tbb/parallel_for.h>
67 #pragma warning (push)
68 #pragma warning (disable : 4244)
72 #define __STDC_CONSTANT_MACROS
73 #define __STDC_LIMIT_MACROS
74 #include <libavcodec/avcodec.h>
75 #include <libavutil/imgutils.h>
81 namespace caspar { namespace screen {
93 enum class aspect_ratio
100 std::wstring name = L"ogl";
101 int screen_index = 0;
102 screen::stretch stretch = screen::stretch::fill;
103 bool windowed = true;
104 bool auto_deinterlace = true;
105 bool key_only = false;
106 aspect_ratio aspect = aspect_ratio::aspect_invalid;
108 bool interactive = true;
111 struct screen_consumer : boost::noncopyable
113 const configuration config_;
114 core::video_format_desc format_desc_;
118 std::vector<GLuint> pbos_ = std::vector<GLuint> { 0, 0 };
124 int screen_width_ = format_desc_.width;
125 int screen_height_ = format_desc_.height;
126 int square_width_ = format_desc_.square_width;
127 int square_height_ = format_desc_.square_height;
131 spl::shared_ptr<diagnostics::graph> graph_;
132 caspar::timer perf_timer_;
133 caspar::timer tick_timer_;
135 caspar::prec_timer wait_timer_;
137 tbb::concurrent_bounded_queue<core::const_frame> frame_buffer_;
138 core::interaction_sink* sink_;
140 boost::thread thread_;
141 tbb::atomic<bool> is_running_;
142 tbb::atomic<int64_t> current_presentation_age_;
144 ffmpeg::filter filter_;
147 const configuration& config,
148 const core::video_format_desc& format_desc,
150 core::interaction_sink* sink)
152 , format_desc_(format_desc)
153 , channel_index_(channel_index)
155 , filter_([&]() -> ffmpeg::filter
157 const auto sample_aspect_ratio =
158 boost::rational<int>(
159 format_desc.square_width,
160 format_desc.square_height) /
161 boost::rational<int>(
165 return ffmpeg::filter(
168 boost::rational<int>(format_desc.duration, format_desc.time_scale),
169 boost::rational<int>(format_desc.time_scale, format_desc.duration),
173 format_desc.field_mode == core::field_mode::progressive || !config.auto_deinterlace ? "" : "YADIF=1:-1");
176 if (format_desc_.format == core::video_format::ntsc && config_.aspect == configuration::aspect_ratio::aspect_4_3)
178 // Use default values which are 4:3.
182 if (config_.aspect == configuration::aspect_ratio::aspect_16_9)
183 square_width_ = (format_desc.height*16)/9;
184 else if (config_.aspect == configuration::aspect_ratio::aspect_4_3)
185 square_width_ = (format_desc.height*4)/3;
188 frame_buffer_.set_capacity(1);
190 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
191 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
192 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
193 graph_->set_text(print());
194 diagnostics::register_graph(graph_);
196 /*DISPLAY_DEVICE d_device = {sizeof(d_device), 0};
197 std::vector<DISPLAY_DEVICE> displayDevices;
198 for(int n = 0; EnumDisplayDevices(NULL, n, &d_device, NULL); ++n)
199 displayDevices.push_back(d_device);
201 if(config_.screen_index >= displayDevices.size())
202 CASPAR_LOG(warning) << print() << L" Invalid screen-index: " << config_.screen_index;
204 DEVMODE devmode = {};
205 if(!EnumDisplaySettings(displayDevices[config_.screen_index].DeviceName, ENUM_CURRENT_SETTINGS, &devmode))
206 CASPAR_LOG(warning) << print() << L" Could not find display settings for screen-index: " << config_.screen_index;
208 screen_x_ = devmode.dmPosition.x;
209 screen_y_ = devmode.dmPosition.y;
210 screen_width_ = config_.windowed ? square_width_ : devmode.dmPelsWidth;
211 screen_height_ = config_.windowed ? square_height_ : devmode.dmPelsHeight;*/
214 screen_width_ = square_width_;
215 screen_height_ = square_height_;
218 current_presentation_age_ = 0;
219 thread_ = boost::thread([this]{run();});
225 frame_buffer_.try_push(core::const_frame::empty());
231 window_.create(sf::VideoMode(screen_width_, screen_height_, 32), u8(L"Screen consumer " + channel_and_format()), config_.windowed ? sf::Style::Resize | sf::Style::Close : sf::Style::Fullscreen);
232 window_.setMouseCursorVisible(config_.interactive);
233 window_.setPosition(sf::Vector2i(screen_x_, screen_y_));
234 window_.setSize(sf::Vector2u(screen_width_, screen_height_));
237 if(!GLEW_VERSION_2_1 && glewInit() != GLEW_OK)
238 CASPAR_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
240 if(!GLEW_VERSION_2_1)
241 CASPAR_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 2.1 support."));
243 GL(glEnable(GL_TEXTURE_2D));
244 GL(glDisable(GL_DEPTH_TEST));
245 GL(glClearColor(0.0, 0.0, 0.0, 0.0));
246 GL(glViewport(0, 0, format_desc_.width, format_desc_.height));
247 GL(glLoadIdentity());
251 GL(glGenTextures(1, &texture_));
252 GL(glBindTexture(GL_TEXTURE_2D, texture_));
253 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
254 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
255 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
256 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
257 GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, format_desc_.width, format_desc_.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0));
258 GL(glBindTexture(GL_TEXTURE_2D, 0));
260 GL(glGenBuffers(2, pbos_.data()));
262 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[0]);
263 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);
264 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[1]);
265 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);
266 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
268 window_.setVerticalSyncEnabled(config_.vsync);
272 CASPAR_LOG(info) << print() << " Enabled vsync.";
274 /*auto wglSwapIntervalEXT = reinterpret_cast<void(APIENTRY*)(int)>(wglGetProcAddress("wglSwapIntervalEXT"));
275 if(wglSwapIntervalEXT)
279 wglSwapIntervalEXT(1);
280 CASPAR_LOG(info) << print() << " Enabled vsync.";
283 wglSwapIntervalEXT(0);
286 CASPAR_LOG(info) << print() << " Successfully Initialized.";
292 glDeleteTextures(1, &texture_);
294 for (auto& pbo : pbos_)
297 glDeleteBuffers(1, &pbo);
303 ensure_gpf_handler_installed_for_thread("screen-consumer-thread");
314 while(window_.pollEvent(e))
316 if (e.type == sf::Event::Resized)
318 else if (e.type == sf::Event::Closed)
320 else if (config_.interactive && sink_)
324 case sf::Event::MouseMoved:
326 auto& mouse_move = e.mouseMove;
327 sink_->on_interaction(spl::make_shared<core::mouse_move_event>(
329 static_cast<double>(mouse_move.x) / screen_width_,
330 static_cast<double>(mouse_move.y) / screen_height_));
333 case sf::Event::MouseButtonPressed:
334 case sf::Event::MouseButtonReleased:
336 auto& mouse_button = e.mouseButton;
337 sink_->on_interaction(spl::make_shared<core::mouse_button_event>(
339 static_cast<double>(mouse_button.x) / screen_width_,
340 static_cast<double>(mouse_button.y) / screen_height_,
341 static_cast<int>(mouse_button.button),
342 e.type == sf::Event::MouseButtonPressed));
345 case sf::Event::MouseWheelMoved:
347 auto& wheel_moved = e.mouseWheel;
348 sink_->on_interaction(spl::make_shared<core::mouse_wheel_event>(
350 static_cast<double>(wheel_moved.x) / screen_width_,
351 static_cast<double>(wheel_moved.y) / screen_height_,
359 auto frame = core::const_frame::empty();
360 frame_buffer_.pop(frame);
362 render_and_draw_frame(frame);
364 /*perf_timer_.restart();
366 graph_->set_value("frame-time", perf_timer_.elapsed()*format_desc_.fps*0.5);
370 current_presentation_age_ = frame.get_age_millis();
371 graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);
372 tick_timer_.restart();
376 CASPAR_LOG_CURRENT_EXCEPTION();
385 CASPAR_LOG_CURRENT_EXCEPTION();
389 void try_sleep_almost_until_vblank()
391 static const double THRESHOLD = 0.003;
392 double threshold = config_.vsync ? THRESHOLD : 0.0;
394 auto frame_time = 1.0 / (format_desc_.fps * format_desc_.field_count);
396 wait_timer_.tick(frame_time - threshold);
399 void wait_for_vblank_and_display()
401 try_sleep_almost_until_vblank();
403 // Make sure that the next tick measures the duration from this point in time.
404 wait_timer_.tick(0.0);
407 spl::shared_ptr<AVFrame> get_av_frame()
409 spl::shared_ptr<AVFrame> av_frame(avcodec_alloc_frame(), av_free);
410 avcodec_get_frame_defaults(av_frame.get());
412 av_frame->linesize[0] = format_desc_.width*4;
413 av_frame->format = PIX_FMT_BGRA;
414 av_frame->width = format_desc_.width;
415 av_frame->height = format_desc_.height;
416 av_frame->interlaced_frame = format_desc_.field_mode != core::field_mode::progressive;
417 av_frame->top_field_first = format_desc_.field_mode == core::field_mode::upper ? 1 : 0;
422 void render_and_draw_frame(core::const_frame frame)
424 if(static_cast<size_t>(frame.image_data().size()) != format_desc_.size)
427 if(screen_width_ == 0 && screen_height_ == 0)
430 perf_timer_.restart();
431 auto av_frame = get_av_frame();
432 av_frame->data[0] = const_cast<uint8_t*>(frame.image_data().begin());
434 filter_.push(av_frame);
435 auto frames = filter_.poll_all();
440 if (frames.size() == 1)
443 graph_->set_value("frame-time", perf_timer_.elapsed() * format_desc_.fps * 0.5);
445 wait_for_vblank_and_display(); // progressive frame
447 else if (frames.size() == 2)
450 double perf_elapsed = perf_timer_.elapsed();
452 wait_for_vblank_and_display(); // field1
454 perf_timer_.restart();
456 perf_elapsed += perf_timer_.elapsed();
457 graph_->set_value("frame-time", perf_elapsed * format_desc_.fps * 0.5);
459 wait_for_vblank_and_display(); // field2
463 void render(spl::shared_ptr<AVFrame> av_frame)
465 GL(glBindTexture(GL_TEXTURE_2D, texture_));
467 GL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[0]));
468 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, format_desc_.width, format_desc_.height, GL_BGRA, GL_UNSIGNED_BYTE, 0));
470 GL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[1]));
471 GL(glBufferData(GL_PIXEL_UNPACK_BUFFER, format_desc_.size, 0, GL_STREAM_DRAW));
473 auto ptr = reinterpret_cast<char*>(GL2(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)));
478 tbb::parallel_for(tbb::blocked_range<int>(0, format_desc_.height), [&](const tbb::blocked_range<int>& r)
480 for(int n = r.begin(); n != r.end(); ++n)
481 aligned_memshfl(ptr+n*format_desc_.width*4, av_frame->data[0]+n*av_frame->linesize[0], format_desc_.width*4, 0x0F0F0F0F, 0x0B0B0B0B, 0x07070707, 0x03030303);
486 tbb::parallel_for(tbb::blocked_range<int>(0, format_desc_.height), [&](const tbb::blocked_range<int>& r)
488 for(int n = r.begin(); n != r.end(); ++n)
489 A_memcpy(ptr+n*format_desc_.width*4, av_frame->data[0]+n*av_frame->linesize[0], format_desc_.width*4);
493 GL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER)); // release the mapped buffer
496 GL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
498 GL(glClear(GL_COLOR_BUFFER_BIT));
500 glTexCoord2f(0.0f, 1.0f); glVertex2f(-width_, -height_);
501 glTexCoord2f(1.0f, 1.0f); glVertex2f( width_, -height_);
502 glTexCoord2f(1.0f, 0.0f); glVertex2f( width_, height_);
503 glTexCoord2f(0.0f, 0.0f); glVertex2f(-width_, height_);
506 GL(glBindTexture(GL_TEXTURE_2D, 0));
508 std::rotate(pbos_.begin(), pbos_.begin() + 1, pbos_.end());
512 std::future<bool> send(core::const_frame frame)
514 if(!frame_buffer_.try_push(frame))
515 graph_->set_tag("dropped-frame");
517 return make_ready_future(is_running_.load());
520 std::wstring channel_and_format() const
522 return L"[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";
525 std::wstring print() const
527 return config_.name + channel_and_format();
530 void calculate_aspect()
534 screen_height_ = window_.getSize().y;
535 screen_width_ = window_.getSize().x;
538 GL(glViewport(0, 0, screen_width_, screen_height_));
540 std::pair<float, float> target_ratio = None();
541 if (config_.stretch == screen::stretch::fill)
542 target_ratio = Fill();
543 else if (config_.stretch == screen::stretch::uniform)
544 target_ratio = Uniform();
545 else if (config_.stretch == screen::stretch::uniform_to_fill)
546 target_ratio = UniformToFill();
548 width_ = target_ratio.first;
549 height_ = target_ratio.second;
552 std::pair<float, float> None()
554 float width = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
555 float height = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
557 return std::make_pair(width, height);
560 std::pair<float, float> Uniform()
562 float aspect = static_cast<float>(square_width_)/static_cast<float>(square_height_);
563 float width = std::min(1.0f, static_cast<float>(screen_height_)*aspect/static_cast<float>(screen_width_));
564 float height = static_cast<float>(screen_width_*width)/static_cast<float>(screen_height_*aspect);
566 return std::make_pair(width, height);
569 std::pair<float, float> Fill()
571 return std::make_pair(1.0f, 1.0f);
574 std::pair<float, float> UniformToFill()
576 float wr = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
577 float hr = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
578 float r_inv = 1.0f/std::min(wr, hr);
580 float width = wr*r_inv;
581 float height = hr*r_inv;
583 return std::make_pair(width, height);
588 struct screen_consumer_proxy : public core::frame_consumer
590 core::monitor::subject monitor_subject_;
591 const configuration config_;
592 std::unique_ptr<screen_consumer> consumer_;
593 core::interaction_sink* sink_;
597 screen_consumer_proxy(const configuration& config, core::interaction_sink* sink)
605 void initialize(const core::video_format_desc& format_desc, int channel_index) override
608 consumer_.reset(new screen_consumer(config_, format_desc, channel_index, sink_));
611 int64_t presentation_frame_age_millis() const override
613 return consumer_ ? static_cast<int64_t>(consumer_->current_presentation_age_) : 0;
616 std::future<bool> send(core::const_frame frame) override
618 return consumer_->send(frame);
621 std::wstring print() const override
623 return consumer_ ? consumer_->print() : L"[screen_consumer]";
626 std::wstring name() const override
631 boost::property_tree::wptree info() const override
633 boost::property_tree::wptree info;
634 info.add(L"type", L"screen");
635 info.add(L"key-only", config_.key_only);
636 info.add(L"windowed", config_.windowed);
637 info.add(L"auto-deinterlace", config_.auto_deinterlace);
638 info.add(L"vsync", config_.vsync);
642 bool has_synchronization_clock() const override
647 int buffer_depth() const override
652 int index() const override
654 return 600 + (config_.key_only ? 10 : 0) + config_.screen_index;
657 core::monitor::subject& monitor_output()
659 return monitor_subject_;
663 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
665 sink.short_description(L"Displays the contents of a channel on screen using OpenGL.");
668 L"{[screen_index:int]|1} "
669 L"{[fullscreen:FULLSCREEN]} "
670 L"{[key_only:KEY_ONLY]} "
671 L"{[non_interactive:NON_INTERACTIVE]} "
672 L"{[no_auto_deinterlace:NO_AUTO_DEINTERLACE]} "
673 L"{NAME [name:string]}");
674 sink.para()->text(L"Displays the contents of a channel on screen using OpenGL.");
676 ->item(L"screen_index", L"Determines which screen the channel should be displayed on. Defaults to 1.")
677 ->item(L"fullscreen", L"If specified opens the window in fullscreen.")
678 ->item(L"key_only", L"Only displays the alpha channel of the video channel if specified.")
679 ->item(L"non_interactive", L"If specified does not send mouse input to producers on the video channel.")
680 ->item(L"no_auto_deinterlace", L"If the video mode of the channel is an interlaced mode, specifying this will turn of deinterlacing.")
681 ->item(L"name", L"Optionally specifies a name of the window to show.");
682 sink.para()->text(L"Examples:");
683 sink.example(L">> ADD 1 SCREEN", L"opens a screen consumer on the default screen.");
684 sink.example(L">> ADD 1 SCREEN 2", L"opens a screen consumer on the screen 2.");
685 sink.example(L">> ADD 1 SCREEN 1 FULLSCREEN", L"opens a screen consumer in fullscreen on screen 1.");
688 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params, core::interaction_sink* sink)
690 if (params.size() < 1 || !boost::iequals(params.at(0), L"SCREEN"))
691 return core::frame_consumer::empty();
693 configuration config;
695 if (params.size() > 1)
696 config.screen_index = boost::lexical_cast<int>(params.at(1));
698 config.windowed = !contains_param(L"FULLSCREEN", params);
699 config.key_only = contains_param(L"KEY_ONLY", params);
700 config.interactive = !contains_param(L"NON_INTERACTIVE", params);
701 config.auto_deinterlace = !contains_param(L"NO_AUTO_DEINTERLACE", params);
703 if (contains_param(L"NAME", params))
704 config.name = get_param(L"NAME", params);
706 return spl::make_shared<screen_consumer_proxy>(config, sink);
709 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(const boost::property_tree::wptree& ptree, core::interaction_sink* sink)
711 configuration config;
712 config.name = ptree.get(L"name", config.name);
713 config.screen_index = ptree.get(L"device", config.screen_index + 1) - 1;
714 config.windowed = ptree.get(L"windowed", config.windowed);
715 config.key_only = ptree.get(L"key-only", config.key_only);
716 config.auto_deinterlace = ptree.get(L"auto-deinterlace", config.auto_deinterlace);
717 config.vsync = ptree.get(L"vsync", config.vsync);
718 config.interactive = ptree.get(L"interactive", config.interactive);
720 auto stretch_str = ptree.get(L"stretch", L"default");
721 if(stretch_str == L"uniform")
722 config.stretch = screen::stretch::uniform;
723 else if(stretch_str == L"uniform_to_fill")
724 config.stretch = screen::stretch::uniform_to_fill;
726 auto aspect_str = ptree.get(L"aspect-ratio", L"default");
727 if(aspect_str == L"16:9")
728 config.aspect = configuration::aspect_ratio::aspect_16_9;
729 else if(aspect_str == L"4:3")
730 config.aspect = configuration::aspect_ratio::aspect_4_3;
732 return spl::make_shared<screen_consumer_proxy>(config, sink);