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