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