UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
avfoundation.mm
Go to the documentation of this file.
1
5/*
6 * Copyright (c) 2014-2023 CESNET z.s.p.o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * 3. Neither the name of CESNET nor the names of its contributors may be
21 * used to endorse or promote products derived from this software without
22 * specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
26 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
27 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
28 * EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#import <AVFoundation/AVFoundation.h>
39#include <AppKit/NSApplication.h>
40#include <chrono>
41#include <condition_variable>
42#include <iostream>
43#include <mutex>
44#include <queue>
45#include <string>
46#include <unordered_map>
47
48#include "debug.h"
49#include "lib_common.h"
50#include "utils/color_out.h"
51#include "video.h"
52#include "video_capture.h"
53
54#define MOD_NAME "[AVFoundation] "
55
56#define NSAppKitVersionNumber10_8 1187
57#define NSAppKitVersionNumber10_9 1265
58
59#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 140000
60#define DESK_VIEW_IF_DEFINED ,AVCaptureDeviceTypeDeskViewCamera
61#else
62#define DESK_VIEW_IF_DEFINED
63#endif // mac 14
64
65#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 140000
66#define AVCaptureDeviceTypeExternal AVCaptureDeviceTypeExternalUnknown
67#endif // < mac 14
68
69using std::string;
70
72std::unordered_map<std::string, NSString *> preset_to_av = {
73 { "high", AVCaptureSessionPresetHigh },
74 { "medium", AVCaptureSessionPresetMedium },
75 { "low", AVCaptureSessionPresetLow },
76 { "VGA", AVCaptureSessionPreset640x480 },
77 { "HD", AVCaptureSessionPreset1280x720 },
78};
79
80std::unordered_map<CMPixelFormatType, std::tuple<codec_t, int, int, int>> av_to_uv = {
81 {kCVPixelFormatType_32ARGB, {RGBA, 8, 16, 24}},
82 {kCVPixelFormatType_32BGRA, {RGBA, 16, 8, 0}},
83 {kCVPixelFormatType_24RGB, {RGB, 0, 8, 16}},
84 //kCMPixelFormat_16BE555
85 //kCMPixelFormat_16BE565
86 //kCMPixelFormat_16LE555
87 //kCMPixelFormat_16LE565
88 //kCMPixelFormat_16LE5551
89 {kCVPixelFormatType_422YpCbCr8, {UYVY, 0, 0, 0}},
90 {kCVPixelFormatType_422YpCbCr8_yuvs, {YUYV, 0, 0, 0}},
91 //kCMPixelFormat_444YpCbCr8
92 //kCMPixelFormat_4444YpCbCrA8
93 //kCMPixelFormat_422YpCbCr16
94 {kCVPixelFormatType_422YpCbCr10, {v210, 0, 0, 0}},
95 //kCMPixelFormat_444YpCbCr10
96 //kCMPixelFormat_8IndexedGray_WhiteIsZero
97};
98
99constexpr int MAX_CAPTURE_QUEUE_SIZE = 2;
100} // namespace vidcap_avfoundation
101
102using namespace std;
103using namespace vidcap_avfoundation;
104using std::chrono::milliseconds;
105
106@interface vidcap_avfoundation_state : NSObject
107{
108 AVCaptureDevice *m_device;
109 AVCaptureSession *m_session;
110 mutex m_lock;
111 condition_variable m_frame_ready;
112 queue<struct video_frame *> m_queue;
113 chrono::steady_clock::time_point m_t0;
114 double m_fps_req;
116}
117
118- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
119fromConnection:(AVCaptureConnection *)connection;
120- (struct video_frame *) grab;
121@end
122
123@interface vidcap_avfoundation_state () <AVCaptureVideoDataOutputSampleBufferDelegate>
124@end
125
126@implementation vidcap_avfoundation_state
127
129+(NSArray *) devices
130{
131
132#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101500
133 AVCaptureDeviceDiscoverySession *discoverySession =
134 [AVCaptureDeviceDiscoverySession
135 discoverySessionWithDeviceTypes:@[
136 AVCaptureDeviceTypeBuiltInWideAngleCamera,
137 AVCaptureDeviceTypeExternal DESK_VIEW_IF_DEFINED
138 ]
139 mediaType:AVMediaTypeVideo
140 position:
141 AVCaptureDevicePositionUnspecified];
142 return
chrono::steady_clock::time_point m_t0
condition_variable m_frame_ready
queue< struct video_frame * > m_queue
AVCaptureSession * m_session
AVCaptureDevice * m_device
std::unordered_map< CMPixelFormatType, std::tuple< codec_t, int, int, int > > av_to_uv
constexpr int MAX_CAPTURE_QUEUE_SIZE
std::unordered_map< std::string, NSString * > preset_to_av
Struct video_frame represents a video frame and contains video description.
Definition types.h:305
@ YUYV
YCbCr 422 8-bit - Y0 Cb Y1 Cr.
Definition types.h:72
@ RGB
RGB 8-bit (packed into 24-bit word)
Definition types.h:80
@ UYVY
YCbCr 422 8-bit - Cb Y0 Cr Y1.
Definition types.h:71
@ RGBA
RGBA 8-bit, big-endian.
Definition types.h:70
@ v210
YCbCr 422 10-bit - xVY0U|xY2UY1|xUY3V|xY5VY4, little-endian (2 bit padding /'x'/),...
Definition types.h:75
This is an umbrella header for video functions.