| 7 |
| #import "base/RTCVideoFrameBuffer.h" |
| 8 | #import "TGRTCCVPixelBuffer.h" |
| 9 | #include "sdk/objc/native/api/video_frame.h" |
| 10 | #include "sdk/objc/native/src/objc_frame_buffer.h" |
| 11 | #include "sdk/objc/base/RTCI420Buffer.h" |
| 12 |
| 13 | #import "api/video/video_sink_interface.h" |
| 14 | #import "api/media_stream_interface.h" |
| 15 | #import "rtc_base/time_utils.h" |
| 16 |
| 17 | @interface VideoCaptureContentView : UIView |
| 18 |
| 19 | @end |
| 20 |
| 21 | @implementation VideoCaptureContentView |
| 22 |
| 23 | + (Class)layerClass { |
| 24 | return [AVCaptureVideoPreviewLayer class]; |
| 25 | } |
| 26 |
| 27 | - (AVCaptureVideoPreviewLayer * _Nonnull)videoLayer { |
| 28 | return (AVCaptureVideoPreviewLayer *)self.layer; |
| 29 | } |
| 30 |
| 31 | @end |
| 32 |
| 33 | @interface VideoCaptureFrame : NSObject |
| 34 |
| 35 | @property (nonatomic, readonly) CGSize size; |
| 36 | @property (nonatomic, readonly) bool isMirrored; |
| 37 | @property (nonatomic, readonly) int rotation; |
| 38 |
| 39 | @end |
| 40 |
| 41 | @implementation VideoCaptureFrame |
| 42 |
| 43 | - (instancetype)initWithSize:(CGSize)size isMirrored:(bool)isMirrored rotation:(int)rotation { |
| 44 | self = [super init]; |
| 45 | if (self != nil) { |
| 46 | _size = size; |
| 47 | _isMirrored = isMirrored; |
| 48 | _rotation = rotation; |
| 49 | } |
| 50 | return self; |
| 51 | } |
| 52 |
| 53 | @end |
| 54 |
| 55 | @interface VideoCaptureView () { |
| 56 | VideoCaptureContentView *_captureView; |
| 57 |
| 58 | VideoCaptureFrame *_videoFrame; |
| 59 | VideoCaptureFrame *_stashedVideoFrame; |
| 60 |
| 61 | int _isWaitingForLayoutFrameCount; |
| 62 | bool _didStartWaitingForLayout; |
| 63 | CGSize _videoFrameSize; |
| 64 | int64_t _lastFrameTimeNs; |
| 65 |
| 66 | CGSize _currentSize; |
| 67 |
| 68 | void (^_onFirstFrameReceived)(); |
| 69 | bool _firstFrameReceivedReported; |
| 70 |
| 71 | void (^_onOrientationUpdated)(int, CGFloat); |
| 72 |
| 73 | void (^_onIsMirroredUpdated)(bool); |
| 74 |
| 75 | bool _didSetShouldBeMirrored; |
| 76 | bool _shouldBeMirrored; |
| 77 | } |
| 78 |
| 79 | @end |
| 80 |
| 81 | @implementation VideoCaptureView |
| 82 |
| 83 | - (instancetype)initWithFrame:(CGRect)frameRect { |
| 84 | self = [super initWithFrame:frameRect]; |
| 85 | if (self) { |
| 86 | [self configure]; |
| 87 |
| 88 | _enabled = true; |
| 89 |
| 90 | _currentSize = CGSizeZero; |
| 91 | _rotationOverride = @(RTCVideoRotation_0); |
| 92 | } |
| 93 | return self; |
| 94 | } |
| 95 |
| 96 | - (void)dealloc { |
| 97 | } |
| 98 |
| 99 | - (AVCaptureVideoPreviewLayer * _Nonnull)previewLayer { |
| 100 | return _captureView.videoLayer; |
| 101 | } |
| 102 |
| 103 | - (void)setEnabled:(BOOL)enabled { |
| 104 | if (_enabled != enabled) { |
| 105 | _enabled = enabled; |
| 106 | } |
| 107 | } |
| 108 |
| 109 | - (void)setVideoContentMode:(UIViewContentMode)mode { |
| 110 | _videoContentMode = mode; |
| 111 | } |
| 112 |
| 113 | - (void)configure { |
| 114 | _captureView = [[VideoCaptureContentView alloc] init]; |
| 115 | [_captureView videoLayer].videoGravity = AVLayerVideoGravityResizeAspectFill; |
| 116 | [self addSubview:_captureView]; |
| 117 |
| 118 | _videoFrameSize = CGSizeZero; |
| 119 | } |
| 120 |
| 121 | - (void)layoutSubviews { |
| 122 | [super layoutSubviews]; |
| 123 |
| 124 | CGRect bounds = self.bounds; |
| 125 |
| 126 | _captureView.frame = bounds; |
| 127 |
| 128 | if (_didStartWaitingForLayout) { |
| 129 | _didStartWaitingForLayout = false; |
| 130 | _isWaitingForLayoutFrameCount = 0; |
| 131 | if (_stashedVideoFrame != nil) { |
| 132 | _videoFrame = _stashedVideoFrame; |
| 133 | _stashedVideoFrame = nil; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 |
| 138 | #pragma mark - |
| 139 |
| 140 | - (void)setRotationOverride:(NSValue *)rotationOverride { |
| 141 | _rotationOverride = rotationOverride; |
| 142 |
| 143 | [self setNeedsLayout]; |
| 144 | } |
| 145 |
| 146 | - (RTCVideoRotation)frameRotation { |
| 147 | if (_rotationOverride) { |
| 148 | RTCVideoRotation rotation; |
| 149 | if (@available(iOS 11, *)) { |
| 150 | [_rotationOverride getValue:&rotation size:sizeof(rotation)]; |
| 151 | } else { |
| 152 | [_rotationOverride getValue:&rotation]; |
| 153 | } |
| 154 | return rotation; |
| 155 | } |
| 156 |
| 157 | if (_videoFrame) { |
| 158 | switch (_videoFrame.rotation) { |
| 159 | case webrtc::kVideoRotation_0: |
| 160 | return RTCVideoRotation_0; |
| 161 | case webrtc::kVideoRotation_90: |
| 162 | return RTCVideoRotation_90; |
| 163 | case webrtc::kVideoRotation_180: |
| 164 | return RTCVideoRotation_180; |
| 165 | case webrtc::kVideoRotation_270: |
| 166 | return RTCVideoRotation_270; |
| 167 | default: |
| 168 | return RTCVideoRotation_0; |
| 169 | } |
| 170 | } else { |
| 171 | return RTCVideoRotation_0; |
| 172 | } |
| 173 | } |
| 174 |
| 175 | - (void)onFrameGenerated:(CGSize)size isMirrored:(bool)isMirrored rotation:(int)rotation { |
| 176 | assert([NSThread isMainThread]); |
| 177 |
| 178 | if (!CGSizeEqualToSize(size, _currentSize)) { |
| 179 | _currentSize = size; |
| 180 | } |
| 181 |
| 182 | int mappedValue = 0; |
| 183 | switch (RTCVideoRotation(rotation)) { |
| 184 | case RTCVideoRotation_90: { |
| 185 | mappedValue = 1; |
| 186 | break; |
| 187 | } |
| 188 | case RTCVideoRotation_180: { |
| 189 | mappedValue = 2; |
| 190 | break; |
| 191 | } |
| 192 | case RTCVideoRotation_270: { |
| 193 | mappedValue = 3; |
| 194 | break; |
| 195 | } |
| 196 | default: { |
| 197 | mappedValue = 0; |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | [self setInternalOrientationAndSize:mappedValue size:size]; |
| 202 |
| 203 | if (!_firstFrameReceivedReported && _onFirstFrameReceived) { |
| 204 | _firstFrameReceivedReported = true; |
| 205 | _onFirstFrameReceived(); |
| 206 | } |
| 207 |
| 208 | if (!self.isEnabled) { |
| 209 | return; |
| 210 | } |
| 211 |
| 212 | VideoCaptureFrame *frame = [[VideoCaptureFrame alloc] initWithSize:size isMirrored:isMirrored rotation:rotation]; |
| 213 |
| 214 | if (_isWaitingForLayoutFrameCount > 0) { |
| 215 | _stashedVideoFrame = frame; |
| 216 | _isWaitingForLayoutFrameCount--; |
| 217 | return; |
| 218 | } |
| 219 | if (!_didStartWaitingForLayout) { |
| 220 | if (_videoFrame && _videoFrame.size.width > 0 && _videoFrame.size.height > 0 && frame.size.width > 0 && frame.size.height > 0) { |
| 221 | float previousAspect = ((float)_videoFrame.size.width) / ((float)_videoFrame.size.height); |
| 222 | float updatedAspect = ((float)frame.size.width) / ((float)frame.size.height); |
| 223 | if ((previousAspect < 1.0f) != (updatedAspect < 1.0f)) { |
| 224 | _stashedVideoFrame = frame; |
| 225 | _didStartWaitingForLayout = true; |
| 226 | _isWaitingForLayoutFrameCount = 5; |
| 227 | return; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 |
| 232 | _videoFrame = frame; |
| 233 | } |
| 234 |
| 235 | - (void)setOnFirstFrameReceived:(void (^ _Nullable)())onFirstFrameReceived { |
| 236 | _onFirstFrameReceived = [onFirstFrameReceived copy]; |
| 237 | _firstFrameReceivedReported = false; |
| 238 | } |
| 239 |
| 240 | - (void)setInternalOrientationAndSize:(int)internalOrientation size:(CGSize)size { |
| 241 | CGFloat aspect = 1.0f; |
| 242 | if (size.width > 1.0f && size.height > 1.0f) { |
| 243 | aspect = size.width / size.height; |
| 244 | } |
| 245 | if (_internalOrientation != internalOrientation || ABS(_internalAspect - aspect) > 0.001) { |
| 246 | RTCLogInfo(@"VideoCaptureView@%lx orientation: %d, aspect: %f", (intptr_t)self, internalOrientation, (float)aspect); |
| 247 |
| 248 | _internalOrientation = internalOrientation; |
| 249 | _internalAspect = aspect; |
| 250 | if (_onOrientationUpdated) { |
| 251 | _onOrientationUpdated(internalOrientation, aspect); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 |
| 256 | - (void)internalSetOnOrientationUpdated:(void (^ _Nullable)(int, CGFloat))onOrientationUpdated { |
| 257 | _onOrientationUpdated = [onOrientationUpdated copy]; |
| 258 | } |
| 259 |
| 260 | - (void)internalSetOnIsMirroredUpdated:(void (^ _Nullable)(bool))onIsMirroredUpdated { |
| 261 | _onIsMirroredUpdated = [onIsMirroredUpdated copy]; |
| 262 | } |
| 263 |
| 264 | @end |
| 265 |