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