]> git.sesse.net Git - vlc/blob - modules/video_output/ios2.m
decoder: fix data race in input_DecoderFrameNext()
[vlc] / modules / video_output / ios2.m
1 /*****************************************************************************
2  * ios2.m: iOS OpenGL ES 2 provider
3  *****************************************************************************
4  * Copyright (C) 2001-2014 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Pierre d'Herbemont <pdherbemont at videolan dot org>
8  *          Felix Paul Kühne <fkuehne at videolan dot org>
9  *          David Fuhrmann <david dot fuhrmann at googlemail dot com>
10  *          Rémi Denis-Courmont
11  *          Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
12  *          Eric Petit <titer@m0k.org>
13  *
14  *
15  * This program is free software; you can redistribute it and/or modify it
16  * under the terms of the GNU Lesser General Public License as published by
17  * the Free Software Foundation; either version 2.1 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public License
26  * along with this program; if not, write to the Free Software Foundation,
27  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
28  *****************************************************************************/
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33
34 #import <UIKit/UIKit.h>
35 #import <OpenGLES/EAGL.h>
36 #import <OpenGLES/ES2/gl.h>
37 #import <QuartzCore/QuartzCore.h>
38 #import <dlfcn.h>
39
40 #ifdef HAVE_CONFIG_H
41 # include "config.h"
42 #endif
43
44 #include <vlc_common.h>
45 #include <vlc_plugin.h>
46 #include <vlc_vout_display.h>
47 #include <vlc_opengl.h>
48 #include <vlc_dialog.h>
49 #include "opengl.h"
50
51 /**
52  * Forward declarations
53  */
54 static int Open(vlc_object_t *);
55 static void Close(vlc_object_t *);
56
57 static picture_pool_t* PicturePool(vout_display_t *vd, unsigned requested_count);
58 static void PictureRender(vout_display_t* vd, picture_t *pic, subpicture_t *subpicture);
59 static void PictureDisplay(vout_display_t* vd, picture_t *pic, subpicture_t *subpicture);
60 static int Control(vout_display_t* vd, int query, va_list ap);
61
62 static void *OurGetProcAddress(vlc_gl_t *, const char *);
63
64 static int OpenglESClean(vlc_gl_t* gl);
65 static void OpenglESSwap(vlc_gl_t* gl);
66
67 /**
68  * Module declaration
69  */
70 vlc_module_begin ()
71     set_shortname("iOS vout")
72     set_description(N_("iOS OpenGL video output"))
73     set_category(CAT_VIDEO)
74     set_subcategory(SUBCAT_VIDEO_VOUT)
75     set_capability("vout display", 300)
76     set_callbacks(Open, Close)
77
78     add_shortcut("vout_ios2")
79 vlc_module_end ()
80
81 @interface VLCOpenGLES2VideoView : UIView {
82     vout_display_t *_voutDisplay;
83     EAGLContext *_eaglContext;
84     GLuint _renderBuffer;
85     GLuint _frameBuffer;
86
87     BOOL _bufferNeedReset;
88     BOOL _appActive;
89 }
90 @property (readwrite) vout_display_t* voutDisplay;
91 @property (readonly) EAGLContext* eaglContext;
92 @property (readonly) BOOL isAppActive;
93
94 - (void)createBuffers;
95 - (void)destroyBuffers;
96 - (void)resetBuffers;
97 @end
98
99 struct vout_display_sys_t
100 {
101     VLCOpenGLES2VideoView *glESView;
102     UIView* viewContainer;
103     UITapGestureRecognizer *tapRecognizer;
104
105     vlc_gl_t gl;
106     vout_display_opengl_t *vgl;
107
108     picture_pool_t *picturePool;
109     bool has_first_frame;
110
111     vout_display_place_t place;
112 };
113
114 static void *OurGetProcAddress(vlc_gl_t *gl, const char *name)
115 {
116     VLC_UNUSED(gl);
117
118     return dlsym(RTLD_DEFAULT, name);
119 }
120
121 static int Open(vlc_object_t *this)
122 {
123     vout_display_t *vd = (vout_display_t *)this;
124
125     if (vout_display_IsWindowed(vd))
126         return VLC_EGENERIC;
127
128     vout_display_sys_t *sys = calloc (1, sizeof(*sys));
129     NSAutoreleasePool *autoreleasePool = nil;
130
131     if (!sys)
132         return VLC_ENOMEM;
133
134     vd->sys = sys;
135     sys->picturePool = NULL;
136     sys->gl.sys = NULL;
137
138     autoreleasePool = [[NSAutoreleasePool alloc] init];
139
140     /* get the object we will draw into */
141     UIView* viewContainer = var_CreateGetAddress (vd, "drawable-nsobject");
142     if (!viewContainer || ![viewContainer isKindOfClass:[UIView class]])
143         goto bailout;
144
145     /* This will be released in Close(), on
146      * main thread, after we are done using it. */
147     sys->viewContainer = [viewContainer retain];
148
149     /* setup the actual OpenGL ES view */
150     sys->glESView = [[VLCOpenGLES2VideoView alloc] initWithFrame:[viewContainer bounds]];
151
152     if (!sys->glESView)
153         goto bailout;
154
155     [sys->glESView setVoutDisplay:vd];
156
157     [sys->viewContainer performSelectorOnMainThread:@selector(addSubview:)
158                                          withObject:sys->glESView
159                                       waitUntilDone:YES];
160
161     /* add tap gesture recognizer for DVD menus and stuff */
162     sys->tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:sys->glESView
163                                                                  action:@selector(tapRecognized:)];
164     if (sys->viewContainer.window) {
165         if (sys->viewContainer.window.rootViewController) {
166             if (sys->viewContainer.window.rootViewController.view)
167                 [sys->viewContainer.superview addGestureRecognizer:sys->tapRecognizer];
168         }
169     }
170     sys->tapRecognizer.cancelsTouchesInView = NO;
171
172     /* Initialize common OpenGL video display */
173     sys->gl.lock = OpenglESClean;
174     sys->gl.unlock = nil;
175     sys->gl.swap = OpenglESSwap;
176     sys->gl.getProcAddress = OurGetProcAddress;
177     sys->gl.sys = sys;
178     const vlc_fourcc_t *subpicture_chromas;
179     video_format_t fmt = vd->fmt;
180
181     sys->vgl = vout_display_opengl_New(&vd->fmt, &subpicture_chromas, &sys->gl);
182     if (!sys->vgl) {
183         sys->gl.sys = NULL;
184         goto bailout;
185     }
186
187     /* */
188     vout_display_info_t info = vd->info;
189     info.has_pictures_invalid = false;
190     info.has_event_thread = true;
191     info.subpicture_chromas = subpicture_chromas;
192     info.has_hide_mouse = false;
193
194     /* Setup vout_display_t once everything is fine */
195     vd->info = info;
196
197     vd->pool = PicturePool;
198     vd->prepare = PictureRender;
199     vd->display = PictureDisplay;
200     vd->control = Control;
201
202     /* forward our dimensions to the vout core */
203     CGSize viewSize = sys->viewContainer.frame.size;
204     vout_display_SendEventFullscreen(vd, false);
205     vout_display_SendEventDisplaySize(vd, (int)viewSize.width, (int)viewSize.height);
206
207     /* */
208     [[NSNotificationCenter defaultCenter] addObserver:sys->glESView
209                                              selector:@selector(applicationStateChanged:)
210                                                  name:UIApplicationWillResignActiveNotification
211                                                object:nil];
212     [[NSNotificationCenter defaultCenter] addObserver:sys->glESView
213                                              selector:@selector(applicationStateChanged:)
214                                                  name:UIApplicationDidBecomeActiveNotification
215                                                object:nil];
216     [sys->glESView performSelectorOnMainThread:@selector(reshape)
217                                     withObject:nil
218                                  waitUntilDone:YES];
219
220     [autoreleasePool release];
221     return VLC_SUCCESS;
222
223 bailout:
224     [autoreleasePool release];
225     Close(this);
226     return VLC_EGENERIC;
227 }
228
229 void Close (vlc_object_t *this)
230 {
231     vout_display_t *vd = (vout_display_t *)this;
232     vout_display_sys_t *sys = vd->sys;
233
234     if (sys->tapRecognizer) {
235         [sys->tapRecognizer.view removeGestureRecognizer:sys->tapRecognizer];
236         [sys->tapRecognizer release];
237     }
238
239     [sys->glESView setVoutDisplay:nil];
240
241     var_Destroy (vd, "drawable-nsobject");
242     [sys->viewContainer performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:NO];
243     [sys->glESView performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:NO];
244
245     if (sys->gl.sys != NULL) {
246         msg_Dbg(this, "deleting display");
247         vout_display_opengl_Delete(sys->vgl);
248     }
249
250     [sys->glESView release];
251
252     free(sys);
253 }
254
255 /*****************************************************************************
256  * vout display callbacks
257  *****************************************************************************/
258
259 static int Control(vout_display_t *vd, int query, va_list ap)
260 {
261     vout_display_sys_t *sys = vd->sys;
262
263     switch (query) {
264         case VOUT_DISPLAY_HIDE_MOUSE:
265             return VLC_EGENERIC;
266
267         case VOUT_DISPLAY_CHANGE_DISPLAY_FILLED:
268         case VOUT_DISPLAY_CHANGE_ZOOM:
269         case VOUT_DISPLAY_CHANGE_SOURCE_ASPECT:
270         case VOUT_DISPLAY_CHANGE_SOURCE_CROP:
271         case VOUT_DISPLAY_CHANGE_DISPLAY_SIZE:
272         {
273             if (!vd->sys)
274                 return VLC_EGENERIC;
275
276             NSAutoreleasePool * autoreleasePool = [[NSAutoreleasePool alloc] init];
277
278             const vout_display_cfg_t *cfg;
279             const video_format_t *source;
280
281             if (query == VOUT_DISPLAY_CHANGE_SOURCE_ASPECT || query == VOUT_DISPLAY_CHANGE_SOURCE_CROP) {
282                 source = (const video_format_t *)va_arg(ap, const video_format_t *);
283                 cfg = vd->cfg;
284             } else {
285                 source = &vd->source;
286                 cfg = (const vout_display_cfg_t*)va_arg(ap, const vout_display_cfg_t *);
287             }
288
289             /* we don't adapt anything here regardless of what the vout core
290              * wants since we are not in a traditional desktop window */
291             if (!cfg)
292                 return VLC_EGENERIC;
293
294             vout_display_cfg_t cfg_tmp = *cfg;
295             CGSize viewSize;
296             viewSize = [sys->glESView bounds].size;
297
298             /* on HiDPI displays, the point bounds don't equal the actual pixels */
299             CGFloat scaleFactor = sys->glESView.contentScaleFactor;
300             cfg_tmp.display.width = viewSize.width * scaleFactor;
301             cfg_tmp.display.height = viewSize.height * scaleFactor;
302
303             vout_display_place_t place;
304             vout_display_PlacePicture(&place, source, &cfg_tmp, false);
305             @synchronized (sys->glESView) {
306                 sys->place = place;
307             }
308
309             // x / y are top left corner, but we need the lower left one
310             if (query != VOUT_DISPLAY_CHANGE_DISPLAY_SIZE)
311                 glViewport(place.x, cfg_tmp.display.height - (place.y + place.height), place.width, place.height);
312
313             [autoreleasePool release];
314             return VLC_SUCCESS;
315         }
316
317         case VOUT_DISPLAY_RESET_PICTURES:
318             vlc_assert_unreachable ();
319         default:
320             msg_Err(vd, "Unknown request %d", query);
321         case VOUT_DISPLAY_CHANGE_FULLSCREEN:
322             return VLC_EGENERIC;
323     }
324 }
325
326 static void PictureDisplay(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
327 {
328     vout_display_sys_t *sys = vd->sys;
329     sys->has_first_frame = true;
330     if (likely([sys->glESView isAppActive]))
331         vout_display_opengl_Display(sys->vgl, &vd->source);
332
333     picture_Release(pic);
334
335     if (subpicture)
336         subpicture_Delete(subpicture);
337 }
338
339 static void PictureRender(vout_display_t *vd, picture_t *pic, subpicture_t *subpicture)
340 {
341     vout_display_sys_t *sys = vd->sys;
342
343     if (likely([sys->glESView isAppActive]))
344         vout_display_opengl_Prepare(sys->vgl, pic, subpicture);
345 }
346
347 static picture_pool_t *PicturePool(vout_display_t *vd, unsigned requested_count)
348 {
349     vout_display_sys_t *sys = vd->sys;
350
351     if (!sys->picturePool)
352         sys->picturePool = vout_display_opengl_GetPool(sys->vgl, requested_count);
353     assert(sys->picturePool);
354     return sys->picturePool;
355 }
356
357 /*****************************************************************************
358  * vout opengl callbacks
359  *****************************************************************************/
360 static int OpenglESClean(vlc_gl_t *gl)
361 {
362     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
363     if (likely([sys->glESView isAppActive]))
364         [sys->glESView resetBuffers];
365     return 0;
366 }
367
368 static void OpenglESSwap(vlc_gl_t *gl)
369 {
370     vout_display_sys_t *sys = (vout_display_sys_t *)gl->sys;
371     if (likely([sys->glESView isAppActive]))
372         [[sys->glESView eaglContext] presentRenderbuffer:GL_RENDERBUFFER];
373 }
374
375 /*****************************************************************************
376  * Our UIView object
377  *****************************************************************************/
378 @implementation VLCOpenGLES2VideoView
379 @synthesize voutDisplay = _voutDisplay, eaglContext = _eaglContext, isAppActive = _appActive;
380
381 + (Class)layerClass
382 {
383     return [CAEAGLLayer class];
384 }
385
386 - (id)initWithFrame:(CGRect)frame
387 {
388     self = [super initWithFrame:frame];
389
390     if (!self)
391         return nil;
392
393     CAEAGLLayer * layer = (CAEAGLLayer *)self.layer;
394     layer.drawableProperties = [NSDictionary dictionaryWithObject:kEAGLColorFormatRGBA8 forKey: kEAGLDrawablePropertyColorFormat];
395     layer.opaque = YES;
396
397     _eaglContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
398     if (!_eaglContext)
399         return nil;
400     [EAGLContext setCurrentContext:_eaglContext];
401
402     [self performSelectorOnMainThread:@selector(createBuffers) withObject:nil waitUntilDone:YES];
403     [self performSelectorOnMainThread:@selector(reshape) withObject:nil waitUntilDone:NO];
404     [self setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
405
406     _appActive = ([UIApplication sharedApplication].applicationState == UIApplicationStateActive);
407
408     return self;
409 }
410
411 - (void)dealloc
412 {
413     [[NSNotificationCenter defaultCenter] removeObserver:self];
414     [_eaglContext release];
415     [super dealloc];
416 }
417
418 - (void)didMoveToWindow
419 {
420     self.contentScaleFactor = self.window.screen.scale;
421     _bufferNeedReset = YES;
422 }
423
424 - (void)createBuffers
425 {
426     /* make sure the current context is us */
427     [EAGLContext setCurrentContext:_eaglContext];
428
429     /* create render buffer */
430     glGenRenderbuffers(1, &_renderBuffer);
431     glBindRenderbuffer(GL_RENDERBUFFER, _renderBuffer);
432
433     /* create frame buffer */
434     glGenFramebuffers(1, &_frameBuffer);
435     glBindFramebuffer(GL_FRAMEBUFFER, _frameBuffer);
436
437     /* allocate storage for the pixels we are going to to draw to */
438     [_eaglContext renderbufferStorage:GL_RENDERBUFFER fromDrawable:(id<EAGLDrawable>)self.layer];
439
440     /* bind render buffer to frame buffer */
441     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, _renderBuffer);
442
443     /* make sure that our shape is ok */
444     [self performSelectorOnMainThread:@selector(reshape) withObject:nil waitUntilDone:NO];
445 }
446
447 - (void)destroyBuffers
448 {
449     /* re-set current context */
450     [EAGLContext setCurrentContext:_eaglContext];
451
452     /* clear frame buffer */
453     glDeleteFramebuffers(1, &_frameBuffer);
454     _frameBuffer = 0;
455
456     /* clear render buffer */
457     glDeleteRenderbuffers(1, &_renderBuffer);
458     _renderBuffer = 0;
459 }
460
461 - (void)resetBuffers
462 {
463     if (_bufferNeedReset) {
464         [self destroyBuffers];
465         [self createBuffers];
466         _bufferNeedReset = NO;
467     }
468 }
469
470 - (void)layoutSubviews
471 {
472     /* this method is called as soon as we are resized.
473      * so set a variable to re-create our buffers on the next clean event */
474     _bufferNeedReset = YES;
475 }
476
477 - (void)reshape
478 {
479     assert([[NSThread currentThread] isMainThread]);
480
481     [EAGLContext setCurrentContext:_eaglContext];
482
483     CGSize viewSize = [self bounds].size;
484
485     vout_display_place_t place;
486
487     @synchronized(self) {
488         if (_voutDisplay) {
489             vout_display_cfg_t cfg_tmp = *(_voutDisplay->cfg);
490             CGFloat scaleFactor = self.contentScaleFactor;
491
492             cfg_tmp.display.width  = viewSize.width * scaleFactor;
493             cfg_tmp.display.height = viewSize.height * scaleFactor;
494
495             vout_display_PlacePicture(&place, &_voutDisplay->source, &cfg_tmp, false);
496             _voutDisplay->sys->place = place;
497             vout_display_SendEventDisplaySize(_voutDisplay, viewSize.width * scaleFactor,
498                                               viewSize.height * scaleFactor);
499         }
500     }
501
502     // x / y are top left corner, but we need the lower left one
503     glViewport(place.x, place.y, place.width, place.height);
504 }
505
506 - (void)tapRecognized:(UITapGestureRecognizer *)tapRecognizer
507 {
508     UIGestureRecognizerState state = [tapRecognizer state];
509     CGPoint touchPoint = [tapRecognizer locationInView:self];
510     CGFloat scaleFactor = self.contentScaleFactor;
511     vout_display_SendMouseMovedDisplayCoordinates(_voutDisplay, ORIENT_NORMAL,
512                                                   (int)touchPoint.x * scaleFactor, (int)touchPoint.y * scaleFactor,
513                                                   &_voutDisplay->sys->place);
514
515     vout_display_SendEventMousePressed(_voutDisplay, MOUSE_BUTTON_LEFT);
516     vout_display_SendEventMouseReleased(_voutDisplay, MOUSE_BUTTON_LEFT);
517 }
518
519 - (void)applicationStateChanged:(NSNotification *)notification
520 {
521     if ([[notification name] isEqualToString:UIApplicationWillResignActiveNotification]
522         || [[notification name] isEqualToString:UIApplicationDidEnterBackgroundNotification]
523         || [[notification name] isEqualToString:UIApplicationWillTerminateNotification])
524         _appActive = NO;
525     else
526         _appActive = YES;
527 }
528
529 - (void)updateConstraints
530 {
531     [self reshape];
532     [super updateConstraints];
533 }
534
535 - (BOOL)isOpaque
536 {
537     return YES;
538 }
539
540 - (BOOL)acceptsFirstResponder
541 {
542     return YES;
543 }
544
545 @end