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