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