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