-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy path0004-samplebuffer-pip-safety-geometry.patch
More file actions
2568 lines (2422 loc) · 97 KB
/
Copy path0004-samplebuffer-pip-safety-geometry.patch
File metadata and controls
2568 lines (2422 loc) · 97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/include/meson.build b/include/meson.build
index a28298687a..7f7b951758 100644
--- a/include/meson.build
+++ b/include/meson.build
@@ -9,6 +9,7 @@ install_headers(
'vlc/libvlc_media_list.h',
'vlc/libvlc_media_list_player.h',
'vlc/libvlc_media_player.h',
+ 'vlc/swiftvlc_vmem.h',
'vlc/libvlc_media_track.h',
'vlc/libvlc_picture.h',
'vlc/libvlc_renderer_discoverer.h',
diff --git a/include/vlc/libvlc_media_player.h b/include/vlc/libvlc_media_player.h
index 7e28aeda59..c8e6b0fc45 100644
--- a/include/vlc/libvlc_media_player.h
+++ b/include/vlc/libvlc_media_player.h
@@ -29,6 +29,7 @@
/* Definitions of enum properties for video */
#include "libvlc_video.h"
+#include "swiftvlc_vmem.h"
# ifdef __cplusplus
extern "C" {
@@ -258,6 +259,37 @@ LIBVLC_API void libvlc_media_player_set_media( libvlc_media_player_t *p_mi,
*/
LIBVLC_API libvlc_media_t * libvlc_media_player_get_media( libvlc_media_player_t *p_mi );
+/**
+ * Atomic retained-media and playback-length snapshot.
+ *
+ * Both fields are captured while holding the media player's internal player
+ * lock, so the media identity cannot change between the retained reference and
+ * its associated playback length. The caller owns one reference to @a media
+ * on success and must release it with libvlc_media_release().
+ *
+ * \param p_mi the media player
+ * \param[out] snapshot destination for the retained media and length
+ * \retval true a media was captured; both output fields are initialized
+ * \retval false no media is associated; both output fields are initialized
+ */
+typedef struct swiftvlc_media_player_media_length_snapshot_t
+{
+ libvlc_media_t *media;
+ libvlc_time_t length;
+} swiftvlc_media_player_media_length_snapshot_t;
+
+/**
+ * Version of SwiftVLC's additive pinned-libVLC extensions.
+ *
+ * Version 1 provides the geometry-aware vmem callback and atomic retained
+ * media/length snapshot.
+ */
+LIBVLC_API unsigned swiftvlc_libvlc_pip_extensions_version( void );
+
+LIBVLC_API bool swiftvlc_libvlc_media_player_get_media_length_snapshot(
+ libvlc_media_player_t *p_mi,
+ swiftvlc_media_player_media_length_snapshot_t *snapshot );
+
/**
* Get the Event Manager from which the media player send event.
*
@@ -447,6 +479,31 @@ typedef unsigned (*libvlc_video_format_cb)(void **opaque, char *chroma,
unsigned *pitches,
unsigned *lines);
+/**
+ * Atomic source geometry supplied to @ref swiftvlc_video_format_ex_cb.
+ *
+ * The size, crop and sample-aspect fields describe one post-rotation
+ * video_format_ApplyRotation() snapshot. @a source_orientation records the
+ * original pre-rotation source orientation for diagnostics only.
+ */
+/**
+ * Extended callback to configure custom-memory picture buffers.
+ *
+ * Unlike @ref libvlc_video_format_cb, @a source_geometry preserves the exact
+ * coded, visible, crop, sample-aspect and original-orientation state seen by
+ * the vmem output. @a output_width and @a output_height select the delivered
+ * full-visible, zero-offset surface. The extended vmem path accepts only an
+ * exact square-pixel output aspect; an incompatible result fails setup.
+ *
+ * \param[in,out] opaque callback private pointer
+ * \param[in,out] chroma four-byte video format identifier
+ * \param[in] source_geometry atomic post-rotation source geometry
+ * \param[in,out] output_width delivered full-visible width
+ * \param[in,out] output_height delivered full-visible height
+ * \param[out] pitches scanline pitches for every plane
+ * \param[out] lines scanline counts for every plane
+ * \return a positive setup success count, or zero on failure
+ */
/**
* Callback prototype to configure picture buffers format.
*
@@ -535,6 +592,23 @@ void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
libvlc_video_format_cb setup,
libvlc_video_cleanup_cb cleanup );
+/**
+ * Set the additive extended decoded-video format callback.
+ *
+ * This is mutually exclusive with libvlc_video_set_format_callbacks(). Calling
+ * either setter clears the other setup callback. Legacy callback semantics are
+ * otherwise unchanged.
+ *
+ * \param mp the media player
+ * \param setup extended setup callback, or NULL to clear it
+ * \param cleanup callback to release setup resources, or NULL
+ */
+LIBVLC_API
+void swiftvlc_libvlc_video_set_format_callbacks_ex(
+ libvlc_media_player_t *mp,
+ swiftvlc_video_format_ex_cb setup,
+ libvlc_video_cleanup_cb cleanup );
+
typedef struct libvlc_video_setup_device_cfg_t
{
diff --git a/include/vlc/swiftvlc_vmem.h b/include/vlc/swiftvlc_vmem.h
new file mode 100644
index 0000000000..1e141bf17b
--- /dev/null
+++ b/include/vlc/swiftvlc_vmem.h
@@ -0,0 +1,69 @@
+/*****************************************************************************
+ * swiftvlc_vmem.h: SwiftVLC geometry-aware vmem callback ABI
+ *****************************************************************************
+ * This header intentionally depends only on fixed-width C types so the vmem
+ * core module can consume the ABI without importing LibVLC's public API.
+ *****************************************************************************/
+
+#ifndef VLC_SWIFTVLC_VMEM_H
+#define VLC_SWIFTVLC_VMEM_H 1
+
+#include <stddef.h>
+#include <stdint.h>
+
+/**
+ * One atomic post-rotation source-geometry snapshot.
+ *
+ * source_orientation is the numeric value of libvlc_video_orient_t before
+ * rotation was applied. It is fixed-width here so this ABI is independent of
+ * compiler enum representation.
+ */
+typedef struct swiftvlc_video_format_geometry_t
+{
+ uint32_t coded_width;
+ uint32_t coded_height;
+ uint32_t visible_width;
+ uint32_t visible_height;
+ uint32_t x_offset;
+ uint32_t y_offset;
+ uint32_t sar_num;
+ uint32_t sar_den;
+ uint32_t source_orientation;
+} swiftvlc_video_format_geometry_t;
+
+/** Geometry-aware custom-memory format callback. */
+typedef unsigned (*swiftvlc_video_format_ex_cb)(
+ void **opaque, char *chroma,
+ const swiftvlc_video_format_geometry_t *source_geometry,
+ unsigned *output_width, unsigned *output_height,
+ unsigned *pitches, unsigned *lines);
+
+#if defined(__cplusplus)
+# define SWIFTVLC_VMEM_STATIC_ASSERT(condition, message) \
+ static_assert(condition, message)
+#else
+# define SWIFTVLC_VMEM_STATIC_ASSERT(condition, message) \
+ _Static_assert(condition, message)
+#endif
+
+SWIFTVLC_VMEM_STATIC_ASSERT(sizeof(swiftvlc_video_format_geometry_t) == 36,
+ "SwiftVLC vmem geometry ABI size changed");
+SWIFTVLC_VMEM_STATIC_ASSERT(
+ offsetof(swiftvlc_video_format_geometry_t, coded_width) == 0,
+ "SwiftVLC vmem coded_width offset changed");
+SWIFTVLC_VMEM_STATIC_ASSERT(
+ offsetof(swiftvlc_video_format_geometry_t, visible_width) == 8,
+ "SwiftVLC vmem visible_width offset changed");
+SWIFTVLC_VMEM_STATIC_ASSERT(
+ offsetof(swiftvlc_video_format_geometry_t, x_offset) == 16,
+ "SwiftVLC vmem x_offset offset changed");
+SWIFTVLC_VMEM_STATIC_ASSERT(
+ offsetof(swiftvlc_video_format_geometry_t, sar_num) == 24,
+ "SwiftVLC vmem sar_num offset changed");
+SWIFTVLC_VMEM_STATIC_ASSERT(
+ offsetof(swiftvlc_video_format_geometry_t, source_orientation) == 32,
+ "SwiftVLC vmem source_orientation offset changed");
+
+#undef SWIFTVLC_VMEM_STATIC_ASSERT
+
+#endif /* VLC_SWIFTVLC_VMEM_H */
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 4fe7af932f..ed985db86b 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -21,6 +21,7 @@ pkginclude_HEADERS = \
../include/vlc/libvlc_picture.h \
../include/vlc/libvlc_version.h \
../include/vlc/libvlc_video.h \
+ ../include/vlc/swiftvlc_vmem.h \
../include/vlc/vlc.h
lib_LTLIBRARIES = libvlc.la
diff --git a/lib/libvlc.sym b/lib/libvlc.sym
index f92df6b013..d2d1374b80 100644
--- a/lib/libvlc.sym
+++ b/lib/libvlc.sym
@@ -137,6 +137,8 @@ libvlc_media_player_get_full_title_descriptions
libvlc_media_player_get_hwnd
libvlc_media_player_get_length
libvlc_media_player_get_media
+swiftvlc_libvlc_media_player_get_media_length_snapshot
+swiftvlc_libvlc_pip_extensions_version
libvlc_media_player_get_nsobject
libvlc_media_player_get_position
libvlc_media_player_get_rate
@@ -264,6 +266,7 @@ libvlc_video_set_display_fit
libvlc_video_set_deinterlace
libvlc_video_set_format
libvlc_video_set_format_callbacks
+swiftvlc_libvlc_video_set_format_callbacks_ex
libvlc_video_set_output_callbacks
libvlc_video_set_key_input
libvlc_video_set_logo_int
diff --git a/lib/media_player.c b/lib/media_player.c
index 73c0b54d71..2fe615d2e4 100644
--- a/lib/media_player.c
+++ b/lib/media_player.c
@@ -654,6 +654,7 @@ libvlc_media_player_new( libvlc_instance_t *instance )
var_Create (mp, "vmem-display", VLC_VAR_ADDRESS);
var_Create (mp, "vmem-data", VLC_VAR_ADDRESS);
var_Create (mp, "vmem-setup", VLC_VAR_ADDRESS);
+ var_Create (mp, "vmem-setup-ex", VLC_VAR_ADDRESS);
var_Create (mp, "vmem-cleanup", VLC_VAR_ADDRESS);
var_Create (mp, "vmem-chroma", VLC_VAR_STRING);
var_Create (mp, "vmem-width", VLC_VAR_INTEGER);
@@ -967,6 +968,35 @@ libvlc_media_player_get_media( libvlc_media_player_t *p_mi )
return p_m;
}
+unsigned swiftvlc_libvlc_pip_extensions_version( void )
+{
+ return 1;
+}
+
+bool
+swiftvlc_libvlc_media_player_get_media_length_snapshot(
+ libvlc_media_player_t *p_mi,
+ swiftvlc_media_player_media_length_snapshot_t *snapshot )
+{
+ if (snapshot == NULL)
+ return false;
+
+ vlc_player_t *player = p_mi->player;
+ vlc_player_Lock(player);
+
+ libvlc_media_t *media = p_mi->p_md;
+ if (media != NULL)
+ libvlc_media_retain(media);
+
+ snapshot->media = media;
+ snapshot->length = media != NULL
+ ? libvlc_time_from_vlc_tick(vlc_player_GetLength(player))
+ : -1;
+
+ vlc_player_Unlock(player);
+ return media != NULL;
+}
+
/**************************************************************************
* Get the event Manager.
**************************************************************************/
@@ -1090,6 +1120,17 @@ void libvlc_video_set_format_callbacks( libvlc_media_player_t *mp,
libvlc_video_cleanup_cb cleanup )
{
var_SetAddress( mp, "vmem-setup", setup );
+ var_SetAddress( mp, "vmem-setup-ex", NULL );
+ var_SetAddress( mp, "vmem-cleanup", cleanup );
+}
+
+void swiftvlc_libvlc_video_set_format_callbacks_ex(
+ libvlc_media_player_t *mp,
+ swiftvlc_video_format_ex_cb setup,
+ libvlc_video_cleanup_cb cleanup )
+{
+ var_SetAddress( mp, "vmem-setup", NULL );
+ var_SetAddress( mp, "vmem-setup-ex", setup );
var_SetAddress( mp, "vmem-cleanup", cleanup );
}
diff --git a/modules/video_output/Makefile.am b/modules/video_output/Makefile.am
index 62808377ed..bca97ac800 100644
--- a/modules/video_output/Makefile.am
+++ b/modules/video_output/Makefile.am
@@ -81,7 +81,10 @@ endif
if HAVE_DARWIN
if !HAVE_WATCHOS
-libsamplebufferdisplay_plugin_la_SOURCES = video_output/apple/VLCSampleBufferDisplay.m codec/vt_utils.c codec/vt_utils.h
+libsamplebufferdisplay_plugin_la_SOURCES = \
+ video_output/apple/VLCSampleBufferDisplay.m \
+ video_output/apple/VLCSampleBufferGeometry.h \
+ codec/vt_utils.c codec/vt_utils.h
libsamplebufferdisplay_plugin_la_OBJCFLAGS = $(AM_OBJCFLAGS) -fobjc-arc
if HAVE_OSX
libsamplebufferdisplay_plugin_la_LDFLAGS = $(AM_LDFLAGS) -rpath '$(voutdir)' \
@@ -422,3 +425,14 @@ vout_LTLIBRARIES += \
libwextern_plugin.la \
libvgl_plugin.la \
libyuv_plugin.la
+
+# libtool 2.5+ cannot infer the tag for .m compiles; force CC.
+libcaeagl_ios_plugin_la_LIBTOOLFLAGS = --tag=CC
+libcaopengllayer_plugin_la_LIBTOOLFLAGS = --tag=CC
+libcvpx_gl_plugin_la_LIBTOOLFLAGS = --tag=CC
+libglinterop_cvpx_plugin_la_LIBTOOLFLAGS = --tag=CC
+libpictureinpicturecontroller_plugin_la_LIBTOOLFLAGS = --tag=CC
+libsamplebufferdisplay_plugin_la_LIBTOOLFLAGS = --tag=CC
+libuiview_window_plugin_la_LIBTOOLFLAGS = --tag=CC
+libvout_macosx_plugin_la_LIBTOOLFLAGS = --tag=CC
+libwindow_macosx_plugin_la_LIBTOOLFLAGS = --tag=CC
diff --git a/modules/video_output/apple/VLCPictureInPictureController.m b/modules/video_output/apple/VLCPictureInPictureController.m
index ff3b7f9985..3926844c81 100644
--- a/modules/video_output/apple/VLCPictureInPictureController.m
+++ b/modules/video_output/apple/VLCPictureInPictureController.m
@@ -33,7 +33,9 @@
#import <TargetConditionals.h>
#import <Foundation/Foundation.h>
#import <AVKit/AVKit.h>
+#include <math.h>
#import <AVFoundation/AVFoundation.h>
+#import <stdatomic.h>
#import "VLCDrawable.h"
#include "vlc_pip_controller.h"
@@ -42,15 +44,21 @@
@interface VLCPictureInPictureController: NSObject
<AVPictureInPictureSampleBufferPlaybackDelegate,
AVPictureInPictureControllerDelegate,
- VLCPictureInPictureWindowControlling>
+ VLCPictureInPictureWindowControlling> {
+ atomic_bool _closed;
+ BOOL _observingPictureInPicturePossible;
+ id<VLCPictureInPictureMediaControlling> _mediaController;
+ void (^_pictureInPictureReady)(id<VLCPictureInPictureWindowControlling>);
+ BOOL _canStartAutomaticallyFromInline;
+ AVSampleBufferDisplayLayer *_displayLayer;
+}
@property (nonatomic, readonly) NSObject *avPipController;
-@property (nonatomic, readonly) pip_controller_t *pipcontroller;
-@property (nonatomic, readonly, weak) id<VLCPictureInPictureDrawable> drawable;
@property (nonatomic) void(^stateChangeEventHandler)(BOOL isStarted);
- (instancetype)initWithPipController:(pip_controller_t *)pipcontroller;
- (void)invalidatePlaybackState;
+- (void)close;
@end
@@ -60,25 +68,36 @@ - (instancetype)initWithPipController:(pip_controller_t *)pipcontroller {
self = [super init];
if (!self)
return nil;
- _pipcontroller = pipcontroller;
+ atomic_init(&_closed, false);
- id drawable = (__bridge id)var_InheritAddress (pipcontroller, "drawable-nsobject");
+ id<VLCPictureInPictureDrawable> drawable =
+ (__bridge id)var_InheritAddress (pipcontroller, "drawable-nsobject");
if (![drawable conformsToProtocol:@protocol(VLCPictureInPictureDrawable)])
return nil;
- _drawable = drawable;
+ _mediaController = drawable.mediaController;
+ _pictureInPictureReady = [drawable.pictureInPictureReady copy];
+ if ([drawable respondsToSelector:@selector(canStartPictureInPictureAutomaticallyFromInline)])
+ _canStartAutomaticallyFromInline =
+ [drawable canStartPictureInPictureAutomaticallyFromInline];
+ else
+ _canStartAutomaticallyFromInline = YES;
return self;
}
- (void)prepare:(AVSampleBufferDisplayLayer *)layer {
- if (![AVPictureInPictureController isPictureInPictureSupported]) {
- msg_Err(_pipcontroller, "Picture In Picture isn't supported");
+ NSAssert([NSThread isMainThread], @"PiP preparation must run on the main thread");
+ if (atomic_load_explicit(&_closed, memory_order_acquire))
+ return;
+ if (![AVPictureInPictureController isPictureInPictureSupported])
+ return;
+ if (_avPipController != nil)
return;
- }
assert(layer);
+ _displayLayer = layer;
AVPictureInPictureControllerContentSource *avPipSource;
avPipSource = [
[AVPictureInPictureControllerContentSource alloc]
@@ -89,20 +108,11 @@ - (void)prepare:(AVSampleBufferDisplayLayer *)layer {
[AVPictureInPictureController alloc]
initWithContentSource:avPipSource
];
- void *mediaController = (__bridge void*)_drawable.mediaController;
- BOOL isMediaSeekable = (BOOL)_pipcontroller->media_cbs->is_media_seekable(mediaController);
- avPipController.requiresLinearPlayback = !isMediaSeekable;
+ avPipController.requiresLinearPlayback = ![_mediaController isMediaSeekable];
avPipController.delegate = self;
#if TARGET_OS_IOS
- // Check if the drawable implements the new method to Controls whether PiP
- // can start automatically when video enters inline mode
- if ([_drawable respondsToSelector:@selector(canStartPictureInPictureAutomaticallyFromInline)]) {
- avPipController.canStartPictureInPictureAutomaticallyFromInline =
- [_drawable canStartPictureInPictureAutomaticallyFromInline];
- } else {
- // Use default value if method not implemented
- avPipController.canStartPictureInPictureAutomaticallyFromInline = YES;
- }
+ avPipController.canStartPictureInPictureAutomaticallyFromInline =
+ _canStartAutomaticallyFromInline;
#endif
_avPipController = avPipController;
@@ -111,29 +121,43 @@ - (void)prepare:(AVSampleBufferDisplayLayer *)layer {
forKeyPath:@"isPictureInPicturePossible"
options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew
context:NULL];
+ _observingPictureInPicturePossible = YES;
- _drawable.pictureInPictureReady(self);
+ if (!atomic_load_explicit(&_closed, memory_order_acquire) && _pictureInPictureReady)
+ _pictureInPictureReady(self);
}
- (void)close {
- AVPictureInPictureController *avPipController =
- (AVPictureInPictureController *)_avPipController;
- NSObject *observer = self;
- dispatch_async(dispatch_get_main_queue(),^{
+ if (atomic_exchange_explicit(&_closed, true, memory_order_acq_rel))
+ return;
+
+ dispatch_block_t cleanup = ^{
+ AVPictureInPictureController *avPipController =
+ (AVPictureInPictureController *)self->_avPipController;
+ if (avPipController.isPictureInPictureActive)
+ [avPipController stopPictureInPicture];
+ if (self->_observingPictureInPicturePossible) {
+ [avPipController removeObserver:self
+ forKeyPath:@"isPictureInPicturePossible"];
+ self->_observingPictureInPicturePossible = NO;
+ }
+ avPipController.delegate = nil;
avPipController.contentSource = nil;
- [avPipController removeObserver:observer forKeyPath:@"isPictureInPicturePossible"];
- });
- _avPipController = nil;
+ self.stateChangeEventHandler = nil;
+ self->_avPipController = nil;
+ };
+
+ if ([NSThread isMainThread])
+ cleanup();
+ else
+ dispatch_async(dispatch_get_main_queue(), cleanup);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
- if (object==_avPipController) {
- if ([keyPath isEqualToString:@"isPictureInPicturePossible"]) {
- msg_Dbg(_pipcontroller, "isPictureInPicturePossible:%d", [change[NSKeyValueChangeNewKey] boolValue]);
- }
- } else {
+ if (object != _avPipController ||
+ ![keyPath isEqualToString:@"isPictureInPicturePossible"]) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
@@ -147,49 +171,81 @@ - (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPict
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController
setPlaying:(BOOL)playing {
- assert(_pipcontroller->media_cbs);
- void *mediaController = (__bridge void*)_drawable.mediaController;
- bool isPlaying = _pipcontroller->media_cbs->is_media_playing(mediaController);
+ if (atomic_load_explicit(&_closed, memory_order_acquire))
+ return;
+ id<VLCPictureInPictureMediaControlling> mediaController = _mediaController;
+ if (!mediaController)
+ return;
+ BOOL isPlaying = mediaController.isMediaPlaying;
if(isPlaying && !playing)
- _pipcontroller->media_cbs->pause(mediaController);
+ [mediaController pause];
if(!isPlaying && playing)
- _pipcontroller->media_cbs->play(mediaController);
+ [mediaController play];
}
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController
skipByInterval:(CMTime)skipInterval
completionHandler:(void (^)(void))completionHandler {
- assert(_pipcontroller->media_cbs);
+ if (atomic_load_explicit(&_closed, memory_order_acquire)) {
+ completionHandler();
+ return;
+ }
+ id<VLCPictureInPictureMediaControlling> mediaController = _mediaController;
+ if (!mediaController) {
+ completionHandler();
+ return;
+ }
+ if (!CMTIME_IS_NUMERIC(skipInterval)) {
+ completionHandler();
+ return;
+ }
Float64 time_sec = CMTimeGetSeconds(skipInterval);
-
- void *mediaController = (__bridge void*)_drawable.mediaController;
- int64_t offset = time_sec * 1e3;
- _pipcontroller->media_cbs->seek_by(offset, completionHandler, mediaController);
+ if (!isfinite(time_sec)) {
+ completionHandler();
+ return;
+ }
+ int64_t offset;
+ if (time_sec >= (Float64)INT64_MAX / 1e3)
+ offset = INT64_MAX;
+ else if (time_sec <= (Float64)INT64_MIN / 1e3)
+ offset = INT64_MIN;
+ else
+ offset = (int64_t)(time_sec * 1e3);
+ [mediaController seekBy:offset completion:completionHandler];
}
- (BOOL)pictureInPictureControllerIsPlaybackPaused:(AVPictureInPictureController *)pictureInPictureController {
- assert(_pipcontroller->media_cbs);
- void *mediaController = (__bridge void*)_drawable.mediaController;
- return ! _pipcontroller->media_cbs->is_media_playing(mediaController);
+ if (atomic_load_explicit(&_closed, memory_order_acquire))
+ return YES;
+ id<VLCPictureInPictureMediaControlling> mediaController = _mediaController;
+ return mediaController ? !mediaController.isMediaPlaying : YES;
}
- (CMTimeRange)pictureInPictureControllerTimeRangeForPlayback:(AVPictureInPictureController *)pictureInPictureController {
- assert(_pipcontroller->media_cbs);
- //TODO: Handle media duration
- void *mediaController = (__bridge void*)_drawable.mediaController;
- const CMTimeRange live = CMTimeRangeMake(kCMTimeNegativeInfinity, kCMTimePositiveInfinity);
-
- int64_t length = _pipcontroller->media_cbs->media_length(mediaController);
- if (length == VLC_TICK_INVALID)
+ if (atomic_load_explicit(&_closed, memory_order_acquire))
+ return kCMTimeRangeInvalid;
+ id<VLCPictureInPictureMediaControlling> mediaController = _mediaController;
+ if (!mediaController)
+ return kCMTimeRangeInvalid;
+ const CMTimeRange live = CMTimeRangeMake(kCMTimeZero, kCMTimePositiveInfinity);
+
+ int64_t length = mediaController.mediaLength;
+ if (length <= 0)
return live;
- int64_t time = _pipcontroller->media_cbs->media_time(mediaController);
-
- CFTimeInterval ca_now = CACurrentMediaTime();
- CFTimeInterval time_sec = ((Float64)time) * 1e-3;
- CFTimeInterval start = ca_now - time_sec;
- CFTimeInterval duration = ((Float64)length) * 1e-3;
- return CMTimeRangeMake(CMTimeMakeWithSeconds(start, 1000000), CMTimeMakeWithSeconds(duration, 1000000));
+ int64_t time = mediaController.mediaTime;
+ if (time < 0)
+ time = 0;
+ else if (time >= length)
+ time = length - 1;
+
+ CMTime current =
+ CMTimebaseGetTime(_displayLayer.sampleBufferRenderer.timebase);
+ if (!CMTIME_IS_NUMERIC(current))
+ current = CMClockGetTime(CMClockGetHostTimeClock());
+ CMTime start = CMTimeSubtract(current, CMTimeMake(time, 1000));
+ CMTime duration = CMTimeMake(length, 1000);
+ return CMTimeRangeMake(start, duration);
}
- (BOOL)pictureInPictureControllerShouldProhibitBackgroundAudioPlayback:(AVPictureInPictureController *)pictureInPictureController {
@@ -204,34 +260,41 @@ - (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureC
* pictureInPictureControllerTimeRangeForPlayback: isn't automatically
* called each time PiP is activated
*/
- [self invalidatePlaybackState];
+ if (!atomic_load_explicit(&_closed, memory_order_acquire))
+ [self invalidatePlaybackState];
}
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
- if (_stateChangeEventHandler)
+ if (!atomic_load_explicit(&_closed, memory_order_acquire) && _stateChangeEventHandler)
_stateChangeEventHandler(YES);
}
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
- if(_stateChangeEventHandler)
+ if (!atomic_load_explicit(&_closed, memory_order_acquire) && _stateChangeEventHandler)
_stateChangeEventHandler(NO);
}
#pragma mark - VLCDisplayPictureInPictureControlling
- (void)startPictureInPicture {
+ if (atomic_load_explicit(&_closed, memory_order_acquire))
+ return;
AVPictureInPictureController *avPipController =
(AVPictureInPictureController *)_avPipController;
[avPipController startPictureInPicture];
}
- (void)stopPictureInPicture {
+ if (atomic_load_explicit(&_closed, memory_order_acquire))
+ return;
AVPictureInPictureController *avPipController =
(AVPictureInPictureController *)_avPipController;
[avPipController stopPictureInPicture];
}
- (void)invalidatePlaybackState {
+ if (atomic_load_explicit(&_closed, memory_order_acquire))
+ return;
AVPictureInPictureController *avPipController =
(AVPictureInPictureController *)_avPipController;
[avPipController invalidatePlaybackState];
@@ -239,58 +302,28 @@ - (void)invalidatePlaybackState {
@end
-static void SetDisplayLayer( pip_controller_t *pipcontroller, void *layer) {
+static void *HoldPresentationContext(pip_controller_t *pipcontroller) {
+ if (pipcontroller->p_sys == NULL)
+ return NULL;
+ id context = (__bridge id)pipcontroller->p_sys;
+ return (__bridge_retained void *)context;
+}
+
+static void SetDisplayLayerOnContext(void *opaque, void *layer) {
if (@available(macOS 12.0, iOS 15.0, tvos 15.0, *)) {
- VLCPictureInPictureController *sys =
- (__bridge VLCPictureInPictureController*)pipcontroller->p_sys;
-
+ VLCPictureInPictureController *sys =
+ (__bridge VLCPictureInPictureController *)opaque;
AVSampleBufferDisplayLayer *displayLayer =
(__bridge AVSampleBufferDisplayLayer *)layer;
-
[sys prepare:displayLayer];
}
}
-static void PipControllerMediaPlay(void *opaque) {
- id<VLCPictureInPictureMediaControlling> mediaController;
- mediaController = (__bridge id<VLCPictureInPictureMediaControlling>)opaque;
- [mediaController play];
-}
-
-static void PipControllerMediaPause(void *opaque) {
- id<VLCPictureInPictureMediaControlling> mediaController;
- mediaController = (__bridge id<VLCPictureInPictureMediaControlling>)opaque;
- [mediaController pause];
-}
-
-static void PipControllerMediaSeekBy(vlc_tick_t time, dispatch_block_t completion, void *opaque) {
- id<VLCPictureInPictureMediaControlling> mediaController;
- mediaController = (__bridge id<VLCPictureInPictureMediaControlling>)opaque;
- [mediaController seekBy:time completion:completion];
-}
-
-static vlc_tick_t PipControllerMediaGetLength(void *opaque) {
- id<VLCPictureInPictureMediaControlling> mediaController;
- mediaController = (__bridge id<VLCPictureInPictureMediaControlling>)opaque;
- return [mediaController mediaLength];
-}
-
-static vlc_tick_t PipControllerMediaGetTime(void *opaque) {
- id<VLCPictureInPictureMediaControlling> mediaController;
- mediaController = (__bridge id<VLCPictureInPictureMediaControlling>)opaque;
- return [mediaController mediaTime];
-}
-
-static bool PipControllerMediaIsSeekable(void *opaque) {
- id<VLCPictureInPictureMediaControlling> mediaController;
- mediaController = (__bridge id<VLCPictureInPictureMediaControlling>)opaque;
- return (bool)[mediaController isMediaSeekable];
-}
-
-static bool PipControllerMediaIsPlaying(void *opaque) {
- id<VLCPictureInPictureMediaControlling> mediaController;
- mediaController = (__bridge id<VLCPictureInPictureMediaControlling>)opaque;
- return [mediaController isMediaPlaying];
+static void ReleasePresentationContext(void *opaque) {
+ if (opaque != NULL) {
+ id context = (__bridge_transfer id)opaque;
+ (void)context;
+ }
}
static int CloseController( pip_controller_t *pipcontroller )
@@ -298,6 +331,7 @@ static int CloseController( pip_controller_t *pipcontroller )
if (@available(macOS 12.0, iOS 15.0, tvos 15.0, *)) {
VLCPictureInPictureController *sys =
(__bridge_transfer VLCPictureInPictureController*)pipcontroller->p_sys;
+ pipcontroller->p_sys = NULL;
[sys close];
}
@@ -308,24 +342,14 @@ static int CloseController( pip_controller_t *pipcontroller )
static int OpenController( pip_controller_t *pipcontroller )
{
static const struct pip_controller_operations ops = {
- SetDisplayLayer,
- CloseController
+ .hold_presentation_context = HoldPresentationContext,
+ .set_display_layer_on_context = SetDisplayLayerOnContext,
+ .release_presentation_context = ReleasePresentationContext,
+ .close = CloseController,
};
pipcontroller->ops = &ops;
- static const struct pip_controller_media_callbacks cbs = {
- PipControllerMediaPlay,
- PipControllerMediaPause,
- PipControllerMediaSeekBy,
- PipControllerMediaGetLength,
- PipControllerMediaGetTime,
- PipControllerMediaIsSeekable,
- PipControllerMediaIsPlaying
- };
-
- pipcontroller->media_cbs = &cbs;
-
if (@available(macOS 12.0, iOS 15.0, tvos 15.0, *)) {
VLCPictureInPictureController *sys =
[[VLCPictureInPictureController alloc]
diff --git a/modules/video_output/apple/VLCSampleBufferDisplay.m b/modules/video_output/apple/VLCSampleBufferDisplay.m
index 2d90143261..ebe5a3be95 100644
--- a/modules/video_output/apple/VLCSampleBufferDisplay.m
+++ b/modules/video_output/apple/VLCSampleBufferDisplay.m
@@ -33,6 +33,8 @@
#include <vlc_atomic.h>
#include <vlc_modules.h>
+#include <limits.h>
+
#import "VLCDrawable.h"
#import <AVFoundation/AVFoundation.h>
@@ -40,6 +42,7 @@
#include "../../codec/vt_utils.h"
#include "vlc_pip_controller.h"
+#include "VLCSampleBufferGeometry.h"
#import <VideoToolbox/VideoToolbox.h>
@@ -70,7 +73,8 @@ typedef NS_ENUM(NSUInteger, VLCSampleBufferPixelFlip) {
@interface VLCRotatedPixelBufferProvider : NSObject
- (CVPixelBufferRef)provideFromBuffer:(CVPixelBufferRef)pixelBuffer
- rotation:(VLCSampleBufferPixelRotation)rotation;
+ outputWidth:(size_t)outputWidth
+ outputHeight:(size_t)outputHeight;
@end
@implementation VLCRotatedPixelBufferProvider
@@ -78,40 +82,40 @@ @implementation VLCRotatedPixelBufferProvider
CVPixelBufferPoolRef _rotationPool;
}
-- (BOOL)_validateRotationPoolWithBuffer:(CVPixelBufferRef)pixelBuffer
- rotation:(VLCSampleBufferPixelRotation)rotation
+- (BOOL)_validatePoolWithBuffer:(CVPixelBufferRef)pixelBuffer
+ outputWidth:(size_t)outputWidth
+ outputHeight:(size_t)outputHeight
{
if (!_rotationPool)
return NO;
- uint32_t poolWidth, poolHeigth, bufferWidth, bufferHeight;
-
- bufferWidth = (uint32_t)CVPixelBufferGetWidth(pixelBuffer);
- bufferHeight = (uint32_t)CVPixelBufferGetHeight(pixelBuffer);
- if (rotation == kVLCSampleBufferPixelRotation_90CW || rotation == kVLCSampleBufferPixelRotation_90CCW)
- {
- uint32_t swap = bufferWidth;
- bufferWidth = bufferHeight;
- bufferHeight = swap;
- }
-
CFDictionaryRef poolAttr = CVPixelBufferPoolGetPixelBufferAttributes(_rotationPool);
if (!poolAttr) {
return NO;
}
CFTypeRef value;
+ int64_t poolValue;
value = CFDictionaryGetValue(poolAttr, kCVPixelBufferWidthKey);
if (!value || CFGetTypeID(value) != CFNumberGetTypeID()
- || !CFNumberGetValue(value, kCFNumberIntType, &poolWidth)
- || poolWidth != bufferWidth)
+ || !CFNumberGetValue(value, kCFNumberSInt64Type, &poolValue)
+ || poolValue < 0 || (uint64_t)poolValue != outputWidth)
{
return NO;
}
value = CFDictionaryGetValue(poolAttr, kCVPixelBufferHeightKey);
if (!value || CFGetTypeID(value) != CFNumberGetTypeID()
- || !CFNumberGetValue(value, kCFNumberIntType, &poolHeigth)
- || poolHeigth != bufferHeight)
+ || !CFNumberGetValue(value, kCFNumberSInt64Type, &poolValue)
+ || poolValue < 0 || (uint64_t)poolValue != outputHeight)
+ {
+ return NO;
+ }
+
+ value = CFDictionaryGetValue(poolAttr, kCVPixelBufferPixelFormatTypeKey);
+ if (!value || CFGetTypeID(value) != CFNumberGetTypeID()
+ || !CFNumberGetValue(value, kCFNumberSInt64Type, &poolValue)
+ || poolValue < 0
+ || (OSType)poolValue != CVPixelBufferGetPixelFormatType(pixelBuffer))
{
return NO;
}
@@ -120,18 +124,21 @@ - (BOOL)_validateRotationPoolWithBuffer:(CVPixelBufferRef)pixelBuffer
}
- (CVPixelBufferRef)provideFromBuffer:(CVPixelBufferRef)pixelBuffer
- rotation:(VLCSampleBufferPixelRotation)rotation
+ outputWidth:(size_t)outputWidth
+ outputHeight:(size_t)outputHeight
{
- if (![self _validateRotationPoolWithBuffer:pixelBuffer rotation:rotation])
+ if (outputWidth == 0 || outputHeight == 0 ||
+ outputWidth > INT64_MAX || outputHeight > INT64_MAX)
+ return NULL;
+
+ if (![self _validatePoolWithBuffer:pixelBuffer
+ outputWidth:outputWidth
+ outputHeight:outputHeight]) {
CVPixelBufferPoolRelease(_rotationPool);
+ _rotationPool = NULL;
+ }
if (!_rotationPool) {
- bool rotated = rotation == kVLCSampleBufferPixelRotation_90CW || rotation == kVLCSampleBufferPixelRotation_90CCW;
- uint32_t srcWidth = CVPixelBufferGetWidth(pixelBuffer);
- uint32_t srcHeight = CVPixelBufferGetHeight(pixelBuffer);
- uint32_t dstWidth = rotated ? srcHeight : srcWidth;
- uint32_t dstHeight = rotated ? srcWidth : srcHeight;
-
CFTypeRef keys[] = {
kCVPixelBufferPixelFormatTypeKey,
kCVPixelBufferWidthKey,
@@ -147,8 +154,8 @@ - (CVPixelBufferRef)provideFromBuffer:(CVPixelBufferRef)pixelBuffer
CFTypeRef values[] = {
(__bridge CFNumberRef)(@(CVPixelBufferGetPixelFormatType(pixelBuffer))),
- (__bridge CFNumberRef)(@(dstWidth)),
- (__bridge CFNumberRef)(@(dstHeight)),
+ (__bridge CFNumberRef)(@(outputWidth)),
+ (__bridge CFNumberRef)(@(outputHeight)),
(__bridge CFDictionaryRef)@{},
kCFBooleanTrue,
#if (!defined(TARGET_OS_VISION) || !TARGET_OS_VISION) && !TARGET_OS_MACCATALYST
@@ -171,16 +178,6 @@ - (CVPixelBufferRef)provideFromBuffer:(CVPixelBufferRef)pixelBuffer
if (status != noErr) {
return NULL;
}
- CFDictionaryRef attachments;
- if (@available(iOS 15.0, tvOS 15.0, macOS 12.0, *)) {
- attachments = CVBufferCopyAttachments(pixelBuffer, kCVAttachmentMode_ShouldPropagate);
- } else {
- attachments = CVBufferGetAttachments(pixelBuffer, kCVAttachmentMode_ShouldPropagate);
- }
- CVBufferSetAttachments(rotated, attachments, kCVAttachmentMode_ShouldPropagate);
- if (@available(iOS 15.0, tvOS 15.0, macOS 12.0, *)) {
- CFRelease(attachments);
- }
return rotated;
}
@@ -190,6 +187,113 @@ - (void)dealloc
}
@end
+#pragma mark - VLCPixelBufferCropContext
+
+@interface VLCPixelBufferCropContext : NSObject
+- (CVPixelBufferRef)crop:(CVPixelBufferRef)pixelBuffer
+ rect:(vlc_samplebuffer_crop)crop;
+@end
+
+@implementation VLCPixelBufferCropContext
+{
+ VLCRotatedPixelBufferProvider *_bufferProvider;
+ VTPixelTransferSessionRef _transferSession;
+}
+
+- (instancetype)init
+{
+ self = [super init];
+ if (!self)
+ return nil;
+
+ OSStatus status = VTPixelTransferSessionCreate(NULL, &_transferSession);
+ if (status != noErr)
+ return nil;
+
+ status = VTSessionSetProperty(_transferSession,
+ kVTPixelTransferPropertyKey_ScalingMode,
+ kVTScalingMode_CropSourceToCleanAperture);
+ if (status == noErr)
+ status = VTSessionSetProperty(
+ _transferSession, kVTPixelTransferPropertyKey_RealTime,
+ kCFBooleanTrue);
+ if (status != noErr)
+ {
+ VTPixelTransferSessionInvalidate(_transferSession);
+ CFRelease(_transferSession);
+ _transferSession = NULL;
+ return nil;
+ }
+
+ _bufferProvider = [VLCRotatedPixelBufferProvider new];
+ return self;
+}
+
+- (CVPixelBufferRef)crop:(CVPixelBufferRef)pixelBuffer
+ rect:(vlc_samplebuffer_crop)crop
+{
+ if (_transferSession == NULL)
+ return NULL;
+
+ CVPixelBufferRef cropped =
+ [_bufferProvider provideFromBuffer:pixelBuffer
+ outputWidth:crop.width
+ outputHeight:crop.height];
+ if (cropped == NULL)
+ return NULL;
+
+ size_t sourceWidth = CVPixelBufferGetWidth(pixelBuffer);
+ size_t sourceHeight = CVPixelBufferGetHeight(pixelBuffer);
+ NSDictionary *cleanAperture = @{
+ (__bridge NSString *)kCVImageBufferCleanApertureWidthKey:
+ @(crop.width),
+ (__bridge NSString *)kCVImageBufferCleanApertureHeightKey:
+ @(crop.height),
+ (__bridge NSString *)kCVImageBufferCleanApertureHorizontalOffsetKey:
+ @((double)crop.x + (double)crop.width / 2.0 -
+ (double)sourceWidth / 2.0),
+ (__bridge NSString *)kCVImageBufferCleanApertureVerticalOffsetKey:
+ @((double)crop.y + (double)crop.height / 2.0 -
+ (double)sourceHeight / 2.0),
+ };
+
+ CVAttachmentMode previousMode = kCVAttachmentMode_ShouldPropagate;
+ CFTypeRef previousAperture =
+ CVBufferCopyAttachment(pixelBuffer, kCVImageBufferCleanApertureKey,
+ &previousMode);
+ CVBufferSetAttachment(pixelBuffer, kCVImageBufferCleanApertureKey,
+ (__bridge CFDictionaryRef)cleanAperture,
+ kCVAttachmentMode_ShouldPropagate);
+ OSStatus status = VTPixelTransferSessionTransferImage(
+ _transferSession, pixelBuffer, cropped);
+ CVBufferRemoveAttachment(pixelBuffer, kCVImageBufferCleanApertureKey);
+ if (previousAperture != NULL)
+ {
+ CVBufferSetAttachment(pixelBuffer, kCVImageBufferCleanApertureKey,
+ previousAperture, previousMode);
+ CFRelease(previousAperture);
+ }
+
+ if (status != noErr)
+ {
+ CVPixelBufferRelease(cropped);
+ return NULL;
+ }
+
+ return cropped;
+}
+
+- (void)dealloc
+{