]> git.sesse.net Git - casparcg/blob - modules/screen/consumer/screen_consumer.cpp
818bdf12e976c237f25faf6c7f946e444b9790c9
[casparcg] / modules / screen / consumer / screen_consumer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "screen_consumer.h"
23
24 #include <GL/glew.h>
25 #include <SFML/Window.hpp>
26
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/memcpy.h>
34 #include <common/utf.h>
35 #include <common/prec_timer.h>
36 #include <common/future.h>
37 #include <common/timer.h>
38 #include <common/param.h>
39 #include <common/os/general_protection_fault.h>
40 #include <common/scope_exit.h>
41
42 //#include <windows.h>
43
44 #include <ffmpeg/producer/filter/filter.h>
45 #include <ffmpeg/producer/util/util.h>
46
47 #include <core/video_format.h>
48 #include <core/frame/frame.h>
49 #include <core/consumer/frame_consumer.h>
50 #include <core/interaction/interaction_sink.h>
51 #include <core/help/help_sink.h>
52 #include <core/help/help_repository.h>
53
54 #include <boost/circular_buffer.hpp>
55 #include <boost/lexical_cast.hpp>
56 #include <boost/property_tree/ptree.hpp>
57 #include <boost/thread.hpp>
58 #include <boost/algorithm/string.hpp>
59
60 #include <tbb/atomic.h>
61 #include <tbb/concurrent_queue.h>
62 #include <tbb/parallel_for.h>
63
64 #include <asmlib.h>
65
66 #include <algorithm>
67 #include <vector>
68
69 #if defined(_MSC_VER)
70 #pragma warning (push)
71 #pragma warning (disable : 4244)
72 #endif
73 extern "C"
74 {
75         #define __STDC_CONSTANT_MACROS
76         #define __STDC_LIMIT_MACROS
77         #include <libavcodec/avcodec.h>
78         #include <libavutil/imgutils.h>
79 }
80 #if defined(_MSC_VER)
81 #pragma warning (pop)
82 #endif
83
84 namespace caspar { namespace screen {
85
86 enum class stretch
87 {
88         none,
89         uniform,
90         fill,
91         uniform_to_fill
92 };
93
94 struct configuration
95 {
96         enum class aspect_ratio
97         {
98                 aspect_4_3 = 0,
99                 aspect_16_9,
100                 aspect_invalid,
101         };
102
103         std::wstring    name                            = L"Screen consumer";
104         int                             screen_index            = 0;
105         screen::stretch stretch                         = screen::stretch::fill;
106         bool                    windowed                        = true;
107         bool                    auto_deinterlace        = true;
108         bool                    key_only                        = false;
109         aspect_ratio    aspect                          = aspect_ratio::aspect_invalid;
110         bool                    vsync                           = false;
111         bool                    interactive                     = true;
112         bool                    borderless                      = false;
113 };
114
115 struct screen_consumer : boost::noncopyable
116 {
117         const configuration                                                                     config_;
118         core::video_format_desc                                                         format_desc_;
119         int                                                                                                     channel_index_;
120
121         GLuint                                                                                          texture_                = 0;
122         std::vector<GLuint>                                                                     pbos_                   = std::vector<GLuint> { 0, 0 };
123
124         float                                                                                           width_;
125         float                                                                                           height_;
126         int                                                                                                     screen_x_;
127         int                                                                                                     screen_y_;
128         int                                                                                                     screen_width_   = format_desc_.width;
129         int                                                                                                     screen_height_  = format_desc_.height;
130         int                                                                                                     square_width_   = format_desc_.square_width;
131         int                                                                                                     square_height_  = format_desc_.square_height;
132
133         sf::Window                                                                                      window_;
134         tbb::atomic<bool>                                                                       polling_event_;
135         std::int64_t                                                                            pts_;
136
137         spl::shared_ptr<diagnostics::graph>                                     graph_;
138         caspar::timer                                                                           perf_timer_;
139         caspar::timer                                                                           tick_timer_;
140
141         caspar::prec_timer                                                                      wait_timer_;
142
143         tbb::concurrent_bounded_queue<core::const_frame>        frame_buffer_;
144         core::interaction_sink*                                                         sink_;
145
146         boost::thread                                                                           thread_;
147         tbb::atomic<bool>                                                                       is_running_;
148         tbb::atomic<int64_t>                                                            current_presentation_age_;
149
150         ffmpeg::filter                                                                          filter_;
151 public:
152         screen_consumer(
153                         const configuration& config,
154                         const core::video_format_desc& format_desc,
155                         int channel_index,
156                         core::interaction_sink* sink)
157                 : config_(config)
158                 , format_desc_(format_desc)
159                 , channel_index_(channel_index)
160                 , pts_(0)
161                 , sink_(sink)
162                 , filter_([&]() -> ffmpeg::filter
163                 {
164                         const auto sample_aspect_ratio =
165                                 boost::rational<int>(
166                                         format_desc.square_width,
167                                         format_desc.square_height) /
168                                 boost::rational<int>(
169                                         format_desc.width,
170                                         format_desc.height);
171
172                         return ffmpeg::filter(
173                                 format_desc.width,
174                                 format_desc.height,
175                                 boost::rational<int>(format_desc.duration, format_desc.time_scale),
176                                 boost::rational<int>(format_desc.time_scale, format_desc.duration),
177                                 sample_aspect_ratio,
178                                 AV_PIX_FMT_BGRA,
179                                 { AV_PIX_FMT_BGRA },
180                                 format_desc.field_mode == core::field_mode::progressive || !config.auto_deinterlace ? "" : "format=pix_fmts=gbrp,YADIF=1:-1");
181                 }())
182         {
183                 if (format_desc_.format == core::video_format::ntsc && config_.aspect == configuration::aspect_ratio::aspect_4_3)
184                 {
185                         // Use default values which are 4:3.
186                 }
187                 else
188                 {
189                         if (config_.aspect == configuration::aspect_ratio::aspect_16_9)
190                                 square_width_ = (format_desc.height*16)/9;
191                         else if (config_.aspect == configuration::aspect_ratio::aspect_4_3)
192                                 square_width_ = (format_desc.height*4)/3;
193                 }
194
195                 frame_buffer_.set_capacity(1);
196
197                 graph_->set_color("tick-time", diagnostics::color(0.0f, 0.6f, 0.9f));
198                 graph_->set_color("frame-time", diagnostics::color(0.1f, 1.0f, 0.1f));
199                 graph_->set_color("dropped-frame", diagnostics::color(0.3f, 0.6f, 0.3f));
200                 graph_->set_text(print());
201                 diagnostics::register_graph(graph_);
202
203                 /*DISPLAY_DEVICE d_device = {sizeof(d_device), 0};
204                 std::vector<DISPLAY_DEVICE> displayDevices;
205                 for(int n = 0; EnumDisplayDevices(NULL, n, &d_device, NULL); ++n)
206                         displayDevices.push_back(d_device);
207
208                 if(config_.screen_index >= displayDevices.size())
209                         CASPAR_LOG(warning) << print() << L" Invalid screen-index: " << config_.screen_index;
210
211                 DEVMODE devmode = {};
212                 if(!EnumDisplaySettings(displayDevices[config_.screen_index].DeviceName, ENUM_CURRENT_SETTINGS, &devmode))
213                         CASPAR_LOG(warning) << print() << L" Could not find display settings for screen-index: " << config_.screen_index;
214
215                 screen_x_               = devmode.dmPosition.x;
216                 screen_y_               = devmode.dmPosition.y;
217                 screen_width_   = config_.windowed ? square_width_ : devmode.dmPelsWidth;
218                 screen_height_  = config_.windowed ? square_height_ : devmode.dmPelsHeight;*/
219                 screen_x_               = 0;
220                 screen_y_               = 0;
221                 screen_width_   = square_width_;
222                 screen_height_  = square_height_;
223
224                 polling_event_ = false;
225                 is_running_ = true;
226                 current_presentation_age_ = 0;
227                 thread_ = boost::thread([this]{run();});
228         }
229
230         ~screen_consumer()
231         {
232                 is_running_ = false;
233                 frame_buffer_.try_push(core::const_frame::empty());
234                 thread_.join();
235         }
236
237         void init()
238         {
239                 auto window_style = config_.borderless
240                         ? sf::Style::None
241                         : (config_.windowed
242                                 ? sf::Style::Resize | sf::Style::Close
243                                 : sf::Style::Fullscreen);
244                 window_.create(sf::VideoMode::getDesktopMode(), u8(print()), window_style);
245
246                 if (config_.windowed)
247                 {
248                         window_.setPosition(sf::Vector2i(screen_x_, screen_y_));
249                         window_.setSize(sf::Vector2u(screen_width_, screen_height_));
250                 }
251                 else
252                 {
253                         screen_width_   = window_.getSize().x;
254                         screen_height_  = window_.getSize().y;
255                 }
256
257                 window_.setMouseCursorVisible(config_.interactive);
258                 window_.setActive();
259
260                 if(!GLEW_VERSION_2_1 && glewInit() != GLEW_OK)
261                         CASPAR_THROW_EXCEPTION(gl::ogl_exception() << msg_info("Failed to initialize GLEW."));
262
263                 if(!GLEW_VERSION_2_1)
264                         CASPAR_THROW_EXCEPTION(not_supported() << msg_info("Missing OpenGL 2.1 support."));
265
266                 GL(glEnable(GL_TEXTURE_2D));
267                 GL(glDisable(GL_DEPTH_TEST));
268                 GL(glClearColor(0.0, 0.0, 0.0, 0.0));
269                 GL(glViewport(0, 0, format_desc_.width, format_desc_.height));
270                 GL(glLoadIdentity());
271
272                 calculate_aspect();
273
274                 GL(glGenTextures(1, &texture_));
275                 GL(glBindTexture(GL_TEXTURE_2D, texture_));
276                 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
277                 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
278                 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP));
279                 GL(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP));
280                 GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, format_desc_.width, format_desc_.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0));
281                 GL(glBindTexture(GL_TEXTURE_2D, 0));
282
283                 GL(glGenBuffers(2, pbos_.data()));
284
285                 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[0]);
286                 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);
287                 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbos_[1]);
288                 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, format_desc_.size, 0, GL_STREAM_DRAW_ARB);
289                 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
290
291                 window_.setVerticalSyncEnabled(config_.vsync);
292
293                 if (config_.vsync)
294                 {
295                         CASPAR_LOG(info) << print() << " Enabled vsync.";
296                 }
297         }
298
299         void uninit()
300         {
301                 if(texture_)
302                         glDeleteTextures(1, &texture_);
303
304                 for (auto& pbo : pbos_)
305                 {
306                         if(pbo)
307                                 glDeleteBuffers(1, &pbo);
308                 }
309         }
310
311         void run()
312         {
313                 ensure_gpf_handler_installed_for_thread("screen-consumer-thread");
314
315                 try
316                 {
317                         init();
318
319                         while(is_running_)
320                         {
321                                 try
322                                 {
323                                         auto poll_event = [this](sf::Event& e)
324                                         {
325                                                 polling_event_ = true;
326                                                 CASPAR_SCOPE_EXIT
327                                                 {
328                                                         polling_event_ = false;
329                                                 };
330                                                 return window_.pollEvent(e);
331                                         };
332
333                                         sf::Event e;
334                                         while(poll_event(e))
335                                         {
336                                                 if (e.type == sf::Event::Resized)
337                                                         calculate_aspect();
338                                                 else if (e.type == sf::Event::Closed)
339                                                         is_running_ = false;
340                                                 else if (config_.interactive && sink_)
341                                                 {
342                                                         switch (e.type)
343                                                         {
344                                                         case sf::Event::MouseMoved:
345                                                                 {
346                                                                         auto& mouse_move = e.mouseMove;
347                                                                         sink_->on_interaction(spl::make_shared<core::mouse_move_event>(
348                                                                                         1,
349                                                                                         static_cast<double>(mouse_move.x) / screen_width_,
350                                                                                         static_cast<double>(mouse_move.y) / screen_height_));
351                                                                 }
352                                                                 break;
353                                                         case sf::Event::MouseButtonPressed:
354                                                         case sf::Event::MouseButtonReleased:
355                                                                 {
356                                                                         auto& mouse_button = e.mouseButton;
357                                                                         sink_->on_interaction(spl::make_shared<core::mouse_button_event>(
358                                                                                         1,
359                                                                                         static_cast<double>(mouse_button.x) / screen_width_,
360                                                                                         static_cast<double>(mouse_button.y) / screen_height_,
361                                                                                         static_cast<int>(mouse_button.button),
362                                                                                         e.type == sf::Event::MouseButtonPressed));
363                                                                 }
364                                                                 break;
365                                                         case sf::Event::MouseWheelMoved:
366                                                                 {
367                                                                         auto& wheel_moved = e.mouseWheel;
368                                                                         sink_->on_interaction(spl::make_shared<core::mouse_wheel_event>(
369                                                                                         1,
370                                                                                         static_cast<double>(wheel_moved.x) / screen_width_,
371                                                                                         static_cast<double>(wheel_moved.y) / screen_height_,
372                                                                                         wheel_moved.delta));
373                                                                 }
374                                                                 break;
375                                                         }
376                                                 }
377                                         }
378
379                                         core::const_frame frame;
380                                         frame_buffer_.pop(frame);
381
382                                         render_and_draw_frame(frame);
383
384                                         /*perf_timer_.restart();
385                                         render(frame);
386                                         graph_->set_value("frame-time", perf_timer_.elapsed()*format_desc_.fps*0.5);
387
388                                         window_.Display();*/
389
390                                         current_presentation_age_ = frame.get_age_millis();
391                                         graph_->set_value("tick-time", tick_timer_.elapsed()*format_desc_.fps*0.5);
392                                         tick_timer_.restart();
393                                 }
394                                 catch(...)
395                                 {
396                                         CASPAR_LOG_CURRENT_EXCEPTION();
397                                         is_running_ = false;
398                                 }
399                         }
400
401                         uninit();
402                 }
403                 catch(...)
404                 {
405                         CASPAR_LOG_CURRENT_EXCEPTION();
406                 }
407         }
408
409         void try_sleep_almost_until_vblank()
410         {
411                 static const double THRESHOLD = 0.003;
412                 double threshold = config_.vsync ? THRESHOLD : 0.0;
413
414                 auto frame_time = 1.0 / (format_desc_.fps * format_desc_.field_count);
415
416                 wait_timer_.tick(frame_time - threshold);
417         }
418
419         void wait_for_vblank_and_display()
420         {
421                 try_sleep_almost_until_vblank();
422                 window_.display();
423                 // Make sure that the next tick measures the duration from this point in time.
424                 wait_timer_.tick(0.0);
425         }
426
427         spl::shared_ptr<AVFrame> get_av_frame()
428         {
429                 auto av_frame = ffmpeg::create_frame();
430
431                 av_frame->linesize[0]           = format_desc_.width*4;
432                 av_frame->format                        = PIX_FMT_BGRA;
433                 av_frame->width                         = format_desc_.width;
434                 av_frame->height                        = format_desc_.height;
435                 av_frame->interlaced_frame      = format_desc_.field_mode != core::field_mode::progressive;
436                 av_frame->top_field_first       = format_desc_.field_mode == core::field_mode::upper ? 1 : 0;
437                 av_frame->pts                           = pts_++;
438
439                 return av_frame;
440         }
441
442         void render_and_draw_frame(core::const_frame input_frame)
443         {
444                 if(static_cast<size_t>(input_frame.image_data().size()) != format_desc_.size)
445                         return;
446
447                 if(screen_width_ == 0 && screen_height_ == 0)
448                         return;
449
450                 perf_timer_.restart();
451                 auto av_frame = get_av_frame();
452                 av_frame->data[0] = const_cast<uint8_t*>(input_frame.image_data().begin());
453
454                 filter_.push(av_frame);
455                 auto frame = filter_.poll();
456
457                 if (!frame)
458                         return;
459
460                 if (!filter_.is_double_rate())
461                 {
462                         render(spl::make_shared_ptr(frame));
463                         graph_->set_value("frame-time", perf_timer_.elapsed() * format_desc_.fps * 0.5);
464
465                         wait_for_vblank_and_display(); // progressive frame
466                 }
467                 else
468                 {
469                         render(spl::make_shared_ptr(frame));
470                         double perf_elapsed = perf_timer_.elapsed();
471
472                         wait_for_vblank_and_display(); // field1
473
474                         perf_timer_.restart();
475                         frame = filter_.poll();
476                         render(spl::make_shared_ptr(frame));
477                         perf_elapsed += perf_timer_.elapsed();
478                         graph_->set_value("frame-time", perf_elapsed * format_desc_.fps * 0.5);
479
480                         wait_for_vblank_and_display(); // field2
481                 }
482         }
483
484         void render(spl::shared_ptr<AVFrame> av_frame)
485         {
486                 CASPAR_LOG_CALL(trace) << "screen_consumer::render() <- " << print();
487                 GL(glBindTexture(GL_TEXTURE_2D, texture_));
488
489                 GL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[0]));
490                 GL(glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, format_desc_.width, format_desc_.height, GL_BGRA, GL_UNSIGNED_BYTE, 0));
491
492                 GL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbos_[1]));
493                 GL(glBufferData(GL_PIXEL_UNPACK_BUFFER, format_desc_.size, 0, GL_STREAM_DRAW));
494
495                 auto ptr = reinterpret_cast<char*>(GL2(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY)));
496                 if(ptr)
497                 {
498                         if(config_.key_only)
499                         {
500                                 tbb::parallel_for(tbb::blocked_range<int>(0, format_desc_.height), [&](const tbb::blocked_range<int>& r)
501                                 {
502                                         for(int n = r.begin(); n != r.end(); ++n)
503                                                 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);
504                                 });
505                         }
506                         else
507                         {
508                                 fast_memcpy(ptr, av_frame->data[0], format_desc_.size);
509                         }
510
511                         GL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER)); // release the mapped buffer
512                 }
513
514                 GL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
515
516                 GL(glClear(GL_COLOR_BUFFER_BIT));
517                 glBegin(GL_QUADS);
518                                 glTexCoord2f(0.0f,        1.0f);        glVertex2f(-width_, -height_);
519                                 glTexCoord2f(1.0f,        1.0f);        glVertex2f( width_, -height_);
520                                 glTexCoord2f(1.0f,        0.0f);        glVertex2f( width_,  height_);
521                                 glTexCoord2f(0.0f,        0.0f);        glVertex2f(-width_,  height_);
522                 glEnd();
523
524                 GL(glBindTexture(GL_TEXTURE_2D, 0));
525
526                 std::rotate(pbos_.begin(), pbos_.begin() + 1, pbos_.end());
527         }
528
529
530         std::future<bool> send(core::const_frame frame)
531         {
532                 if(!frame_buffer_.try_push(frame) && !polling_event_)
533                         graph_->set_tag(diagnostics::tag_severity::WARNING, "dropped-frame");
534
535                 return make_ready_future(is_running_.load());
536         }
537
538         std::wstring channel_and_format() const
539         {
540                 return L"[" + boost::lexical_cast<std::wstring>(channel_index_) + L"|" + format_desc_.name + L"]";
541         }
542
543         std::wstring print() const
544         {
545                 return config_.name + L" " + channel_and_format();
546         }
547
548         void calculate_aspect()
549         {
550                 if(config_.windowed)
551                 {
552                         screen_height_ = window_.getSize().y;
553                         screen_width_ = window_.getSize().x;
554                 }
555
556                 GL(glViewport(0, 0, screen_width_, screen_height_));
557
558                 std::pair<float, float> target_ratio = None();
559                 if (config_.stretch == screen::stretch::fill)
560                         target_ratio = Fill();
561                 else if (config_.stretch == screen::stretch::uniform)
562                         target_ratio = Uniform();
563                 else if (config_.stretch == screen::stretch::uniform_to_fill)
564                         target_ratio = UniformToFill();
565
566                 width_ = target_ratio.first;
567                 height_ = target_ratio.second;
568         }
569
570         std::pair<float, float> None()
571         {
572                 float width = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
573                 float height = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
574
575                 return std::make_pair(width, height);
576         }
577
578         std::pair<float, float> Uniform()
579         {
580                 float aspect = static_cast<float>(square_width_)/static_cast<float>(square_height_);
581                 float width = std::min(1.0f, static_cast<float>(screen_height_)*aspect/static_cast<float>(screen_width_));
582                 float height = static_cast<float>(screen_width_*width)/static_cast<float>(screen_height_*aspect);
583
584                 return std::make_pair(width, height);
585         }
586
587         std::pair<float, float> Fill()
588         {
589                 return std::make_pair(1.0f, 1.0f);
590         }
591
592         std::pair<float, float> UniformToFill()
593         {
594                 float wr = static_cast<float>(square_width_)/static_cast<float>(screen_width_);
595                 float hr = static_cast<float>(square_height_)/static_cast<float>(screen_height_);
596                 float r_inv = 1.0f/std::min(wr, hr);
597
598                 float width = wr*r_inv;
599                 float height = hr*r_inv;
600
601                 return std::make_pair(width, height);
602         }
603 };
604
605
606 struct screen_consumer_proxy : public core::frame_consumer
607 {
608         core::monitor::subject                          monitor_subject_;
609         const configuration                                     config_;
610         std::unique_ptr<screen_consumer>        consumer_;
611         core::interaction_sink*                         sink_;
612
613 public:
614
615         screen_consumer_proxy(const configuration& config, core::interaction_sink* sink)
616                 : config_(config)
617                 , sink_(sink)
618         {
619         }
620
621         // frame_consumer
622
623         void initialize(const core::video_format_desc& format_desc, const core::audio_channel_layout&, int channel_index) override
624         {
625                 consumer_.reset();
626                 consumer_.reset(new screen_consumer(config_, format_desc, channel_index, sink_));
627         }
628
629         int64_t presentation_frame_age_millis() const override
630         {
631                 return consumer_ ? static_cast<int64_t>(consumer_->current_presentation_age_) : 0;
632         }
633
634         std::future<bool> send(core::const_frame frame) override
635         {
636                 return consumer_->send(frame);
637         }
638
639         std::wstring print() const override
640         {
641                 return consumer_ ? consumer_->print() : L"[screen_consumer]";
642         }
643
644         std::wstring name() const override
645         {
646                 return L"screen";
647         }
648
649         boost::property_tree::wptree info() const override
650         {
651                 boost::property_tree::wptree info;
652                 info.add(L"type", L"screen");
653                 info.add(L"key-only", config_.key_only);
654                 info.add(L"windowed", config_.windowed);
655                 info.add(L"auto-deinterlace", config_.auto_deinterlace);
656                 info.add(L"vsync", config_.vsync);
657                 return info;
658         }
659
660         bool has_synchronization_clock() const override
661         {
662                 return false;
663         }
664
665         int buffer_depth() const override
666         {
667                 return 1;
668         }
669
670         int index() const override
671         {
672                 return 600 + (config_.key_only ? 10 : 0) + config_.screen_index;
673         }
674
675         core::monitor::subject& monitor_output()
676         {
677                 return monitor_subject_;
678         }
679 };
680
681 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
682 {
683         sink.short_description(L"Displays the contents of a channel on screen using OpenGL.");
684         sink.syntax(
685                         L"SCREEN "
686                         L"{[screen_index:int]|1} "
687                         L"{[fullscreen:FULLSCREEN]} "
688                         L"{[borderless:BORDERLESS]} "
689                         L"{[key_only:KEY_ONLY]} "
690                         L"{[non_interactive:NON_INTERACTIVE]} "
691                         L"{[no_auto_deinterlace:NO_AUTO_DEINTERLACE]} "
692                         L"{NAME [name:string]}");
693         sink.para()->text(L"Displays the contents of a channel on screen using OpenGL.");
694         sink.definitions()
695                 ->item(L"screen_index", L"Determines which screen the channel should be displayed on. Defaults to 1.")
696                 ->item(L"fullscreen", L"If specified opens the window in fullscreen.")
697                 ->item(L"borderless", L"Makes the window appear without any window decorations.")
698                 ->item(L"key_only", L"Only displays the alpha channel of the video channel if specified.")
699                 ->item(L"non_interactive", L"If specified does not send mouse input to producers on the video channel.")
700                 ->item(L"no_auto_deinterlace", L"If the video mode of the channel is an interlaced mode, specifying this will turn of deinterlacing.")
701                 ->item(L"name", L"Optionally specifies a name of the window to show.");
702         sink.para()->text(L"Examples:");
703         sink.example(L">> ADD 1 SCREEN", L"opens a screen consumer on the default screen.");
704         sink.example(L">> ADD 1 SCREEN 2", L"opens a screen consumer on the screen 2.");
705         sink.example(L">> ADD 1 SCREEN 1 FULLSCREEN", L"opens a screen consumer in fullscreen on screen 1.");
706         sink.example(L">> ADD 1 SCREEN 1 BORDERLESS", L"opens a screen consumer without borders/window decorations on screen 1.");
707 }
708
709 spl::shared_ptr<core::frame_consumer> create_consumer(
710                 const std::vector<std::wstring>& params, core::interaction_sink* sink, std::vector<spl::shared_ptr<core::video_channel>> channels)
711 {
712         if (params.size() < 1 || !boost::iequals(params.at(0), L"SCREEN"))
713                 return core::frame_consumer::empty();
714
715         configuration config;
716
717         if (params.size() > 1)
718                 config.screen_index = boost::lexical_cast<int>(params.at(1));
719
720         config.windowed                 = !contains_param(L"FULLSCREEN", params);
721         config.key_only                 =  contains_param(L"KEY_ONLY", params);
722         config.interactive              = !contains_param(L"NON_INTERACTIVE", params);
723         config.auto_deinterlace = !contains_param(L"NO_AUTO_DEINTERLACE", params);
724         config.borderless               =  contains_param(L"BORDERLESS", params);
725
726         if (contains_param(L"NAME", params))
727                 config.name = get_param(L"NAME", params);
728
729         return spl::make_shared<screen_consumer_proxy>(config, sink);
730 }
731
732 spl::shared_ptr<core::frame_consumer> create_preconfigured_consumer(
733                 const boost::property_tree::wptree& ptree, core::interaction_sink* sink, std::vector<spl::shared_ptr<core::video_channel>> channels)
734 {
735         configuration config;
736         config.name                             = ptree.get(L"name",                            config.name);
737         config.screen_index             = ptree.get(L"device",                          config.screen_index + 1) - 1;
738         config.windowed                 = ptree.get(L"windowed",                        config.windowed);
739         config.key_only                 = ptree.get(L"key-only",                        config.key_only);
740         config.auto_deinterlace = ptree.get(L"auto-deinterlace",        config.auto_deinterlace);
741         config.vsync                    = ptree.get(L"vsync",                           config.vsync);
742         config.interactive              = ptree.get(L"interactive",                     config.interactive);
743         config.borderless               = ptree.get(L"borderless",                      config.borderless);
744
745         auto stretch_str = ptree.get(L"stretch", L"default");
746         if(stretch_str == L"uniform")
747                 config.stretch = screen::stretch::uniform;
748         else if(stretch_str == L"uniform_to_fill")
749                 config.stretch = screen::stretch::uniform_to_fill;
750
751         auto aspect_str = ptree.get(L"aspect-ratio", L"default");
752         if(aspect_str == L"16:9")
753                 config.aspect = configuration::aspect_ratio::aspect_16_9;
754         else if(aspect_str == L"4:3")
755                 config.aspect = configuration::aspect_ratio::aspect_4_3;
756
757         return spl::make_shared<screen_consumer_proxy>(config, sink);
758 }
759
760 }}