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