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