| 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 | #import "RTCMTLI420Renderer.h" |
| 18 | #import "RTCMTLNV12Renderer.h" |
| 19 | #import "RTCMTLRGBRenderer.h" |
| 20 |
| 21 | #include "libyuv.h" |
| 22 |
| 23 | #define MTKViewClass NSClassFromString(@"MTKView") |
| 24 | #define RTCMTLNV12RendererClass NSClassFromString(@"RTCMTLNV12Renderer") |
| 25 | #define RTCMTLI420RendererClass NSClassFromString(@"RTCMTLI420Renderer") |
| 26 | #define RTCMTLRGBRendererClass NSClassFromString(@"RTCMTLRGBRenderer") |
| 27 |
| 28 | namespace { |
| 29 |
| 30 | class VideoRendererAdapterImpl : public rtc::VideoSinkInterface<webrtc::VideoFrame> { |
| 31 | public: |
| 32 | VideoRendererAdapterImpl(void (^frameReceived)(std::shared_ptr<webrtc::VideoFrame>)) { |
| 33 | _frameReceived = [frameReceived copy]; |
| 34 | } |
| 35 |
| 36 | void OnFrame(const webrtc::VideoFrame& nativeVideoFrame) override { |
| 37 | @autoreleasepool { |
| 38 | if (_frameReceived) { |
| 39 | std::shared_ptr<webrtc::VideoFrame> videoFrame = std::make_shared<webrtc::VideoFrame>(nativeVideoFrame); |
| 40 | _frameReceived(videoFrame); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 |
| 45 | private: |
| 46 | void (^_frameReceived)(std::shared_ptr<webrtc::VideoFrame>); |
| 47 | }; |
| 48 |
| 49 | } |
| 50 |
| 51 | @interface VideoSampleBufferContentView : UIView |
| 52 |
| 53 | @property (nonatomic) bool isPaused; |
| 54 |
| 55 | @end |
| 56 |
| 57 | @implementation VideoSampleBufferContentView |
| 58 |
| 59 | + (Class)layerClass { |
| 60 | return [AVSampleBufferDisplayLayer class]; |
| 61 | } |
| 62 |
| 63 | - (AVSampleBufferDisplayLayer * _Nonnull)videoLayer { |
| 64 | return (AVSampleBufferDisplayLayer *)self.layer; |
| 65 | } |
| 66 |
| 67 | @end |
| 68 |
| 69 | @interface VideoSampleBufferViewRenderingContext : NSObject { |
| 70 | __weak VideoSampleBufferContentView *_sampleBufferView; |
| 71 | __weak VideoSampleBufferContentView *_cloneTarget; |
| 72 |
| 73 | CVPixelBufferPoolRef _pixelBufferPool; |
| 74 | int _pixelBufferPoolWidth; |
| 75 | int _pixelBufferPoolHeight; |
| 76 |
| 77 | bool _isBusy; |
| 78 | } |
| 79 |
| 80 | @end |
| 81 |
| 82 | @implementation VideoSampleBufferViewRenderingContext |
| 83 |
| 84 | + (dispatch_queue_t)sharedQueue { |
| 85 | static dispatch_queue_t queue; |
| 86 | static dispatch_once_t onceToken; |
| 87 | dispatch_once(&onceToken, ^{ |
| 88 | queue = dispatch_queue_create("VideoSampleBufferViewRenderingContext", 0); |
| 89 | }); |
| 90 | return queue; |
| 91 | } |
| 92 |
| 93 | - (instancetype)initWithView:(VideoSampleBufferContentView *)view { |
| 94 | self = [super init]; |
| 95 | if (self != nil) { |
| 96 | _sampleBufferView = view; |
| 97 | } |
| 98 | return self; |
| 99 | } |
| 100 |
| 101 | - (void)dealloc { |
| 102 | _isBusy = true; |
| 103 | if (_pixelBufferPool) { |
| 104 | CFRelease(_pixelBufferPool); |
| 105 | } |
| 106 |
| 107 | void *opaqueReference = (__bridge_retained void *)_sampleBufferView; |
| 108 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 109 | __strong VideoSampleBufferContentView *object = (__bridge_transfer VideoSampleBufferContentView *)opaqueReference; |
| 110 | [object description]; |
| 111 | }); |
| 112 | } |
| 113 |
| 114 | static bool CopyVideoFrameToNV12PixelBuffer(const webrtc::I420BufferInterface *frameBuffer, CVPixelBufferRef pixelBuffer) { |
| 115 | if (!frameBuffer) { |
| 116 | return false; |
| 117 | } |
| 118 | RTC_DCHECK(pixelBuffer); |
| 119 | RTC_DCHECK_EQ(CVPixelBufferGetPixelFormatType(pixelBuffer), kCVPixelFormatType_420YpCbCr8BiPlanarFullRange); |
| 120 | RTC_DCHECK_EQ(CVPixelBufferGetHeightOfPlane(pixelBuffer, 0), frameBuffer->height()); |
| 121 | RTC_DCHECK_EQ(CVPixelBufferGetWidthOfPlane(pixelBuffer, 0), frameBuffer->width()); |
| 122 |
| 123 | CVReturn cvRet = CVPixelBufferLockBaseAddress(pixelBuffer, 0); |
| 124 | if (cvRet != kCVReturnSuccess) { |
| 125 | return false; |
| 126 | } |
| 127 | uint8_t *dstY = reinterpret_cast<uint8_t *>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0)); |
| 128 | int dstStrideY = (int)CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0); |
| 129 | uint8_t *dstUV = reinterpret_cast<uint8_t *>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1)); |
| 130 | int dstStrideUV = (int)CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1); |
| 131 | // Convert I420 to NV12. |
| 132 | int ret = libyuv::I420ToNV12(frameBuffer->DataY(), |
| 133 | frameBuffer->StrideY(), |
| 134 | frameBuffer->DataU(), |
| 135 | frameBuffer->StrideU(), |
| 136 | frameBuffer->DataV(), |
| 137 | frameBuffer->StrideV(), |
| 138 | dstY, |
| 139 | dstStrideY, |
| 140 | dstUV, |
| 141 | dstStrideUV, |
| 142 | frameBuffer->width(), |
| 143 | frameBuffer->height()); |
| 144 | CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); |
| 145 | if (ret) { |
| 146 | return false; |
| 147 | } |
| 148 | return true; |
| 149 | } |
| 150 |
| 151 | static bool CopyNV12VideoFrameToNV12PixelBuffer(const webrtc::NV12BufferInterface *frameBuffer, CVPixelBufferRef pixelBuffer) { |
| 152 | if (!frameBuffer) { |
| 153 | return false; |
| 154 | } |
| 155 | RTC_DCHECK(pixelBuffer); |
| 156 | RTC_DCHECK_EQ(CVPixelBufferGetPixelFormatType(pixelBuffer), kCVPixelFormatType_420YpCbCr8BiPlanarFullRange); |
| 157 | RTC_DCHECK_EQ(CVPixelBufferGetHeightOfPlane(pixelBuffer, 0), frameBuffer->height()); |
| 158 | RTC_DCHECK_EQ(CVPixelBufferGetWidthOfPlane(pixelBuffer, 0), frameBuffer->width()); |
| 159 |
| 160 | CVReturn cvRet = CVPixelBufferLockBaseAddress(pixelBuffer, 0); |
| 161 | if (cvRet != kCVReturnSuccess) { |
| 162 | return false; |
| 163 | } |
| 164 | uint8_t *dstY = reinterpret_cast<uint8_t *>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 0)); |
| 165 | int dstStrideY = (int)CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 0); |
| 166 | uint8_t *dstUV = reinterpret_cast<uint8_t *>(CVPixelBufferGetBaseAddressOfPlane(pixelBuffer, 1)); |
| 167 | int dstStrideUV = (int)CVPixelBufferGetBytesPerRowOfPlane(pixelBuffer, 1); |
| 168 | // Convert I420 to NV12. |
| 169 | int ret = libyuv::NV12Copy(frameBuffer->DataY(), frameBuffer->StrideY(), frameBuffer->DataUV(), frameBuffer->StrideUV(), dstY, dstStrideY, dstUV, dstStrideUV, frameBuffer->width(), frameBuffer->height()); |
| 170 | CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); |
| 171 | if (ret) { |
| 172 | return false; |
| 173 | } |
| 174 | return true; |
| 175 | } |
| 176 |
| 177 | - (CVPixelBufferPoolRef)createPixelBufferPoolWithWidth:(int32_t)width height:(int32_t)height pixelFormat:(FourCharCode)pixelFormat maxBufferCount:(int32_t) maxBufferCount { |
| 178 | CVPixelBufferPoolRef outputPool = NULL; |
| 179 |
| 180 | NSDictionary *sourcePixelBufferOptions = @{ |
| 181 | (id)kCVPixelBufferPixelFormatTypeKey : @(pixelFormat), |
| 182 | (id)kCVPixelBufferWidthKey : @(width), |
| 183 | (id)kCVPixelBufferHeightKey : @(height), |
| 184 | (id)kCVPixelBufferIOSurfacePropertiesKey : @{} |
| 185 | }; |
| 186 |
| 187 | NSDictionary *pixelBufferPoolOptions = @{ (id)kCVPixelBufferPoolMinimumBufferCountKey : @(maxBufferCount) }; |
| 188 | CVPixelBufferPoolCreate(kCFAllocatorDefault, (__bridge CFDictionaryRef)pixelBufferPoolOptions, (__bridge CFDictionaryRef)sourcePixelBufferOptions, &outputPool); |
| 189 |
| 190 | return outputPool; |
| 191 | } |
| 192 |
| 193 | - (CMSampleBufferRef)createSampleBufferFromI420Buffer:(const webrtc::I420BufferInterface *)buffer { |
| 194 | if (!buffer) { |
| 195 | return nil; |
| 196 | } |
| 197 |
| 198 | NSMutableDictionary *ioSurfaceProperties = [[NSMutableDictionary alloc] init]; |
| 199 | //ioSurfaceProperties[@"IOSurfaceIsGlobal"] = @(true); |
| 200 |
| 201 | NSMutableDictionary *options = [[NSMutableDictionary alloc] init]; |
| 202 | //options[(__bridge NSString *)kCVPixelBufferBytesPerRowAlignmentKey] = @(buffer.strideY); |
| 203 | options[(__bridge NSString *)kCVPixelBufferIOSurfacePropertiesKey] = ioSurfaceProperties; |
| 204 |
| 205 | CVPixelBufferRef pixelBufferRef = nil; |
| 206 |
| 207 | if (!(_pixelBufferPool != nil && _pixelBufferPoolWidth == buffer->width() && _pixelBufferPoolHeight == buffer->height())) { |
| 208 | if (_pixelBufferPool) { |
| 209 | CFRelease(_pixelBufferPool); |
| 210 | _pixelBufferPool = nil; |
| 211 | } |
| 212 | _pixelBufferPool = [self createPixelBufferPoolWithWidth:buffer->width() height:buffer->height() pixelFormat:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange maxBufferCount:10]; |
| 213 | _pixelBufferPoolWidth = buffer->width(); |
| 214 | _pixelBufferPoolHeight = buffer->height(); |
| 215 | } |
| 216 |
| 217 | if (_pixelBufferPool != nil && _pixelBufferPoolWidth == buffer->width() && _pixelBufferPoolHeight == buffer->height()) { |
| 218 | CVPixelBufferPoolCreatePixelBufferWithAuxAttributes(kCFAllocatorDefault, _pixelBufferPool, nil, &pixelBufferRef); |
| 219 | } else { |
| 220 | CVPixelBufferCreate( |
| 221 | kCFAllocatorDefault, |
| 222 | buffer->width(), |
| 223 | buffer->height(), |
| 224 | kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, |
| 225 | (__bridge CFDictionaryRef)options, |
| 226 | &pixelBufferRef |
| 227 | ); |
| 228 | } |
| 229 |
| 230 | if (pixelBufferRef == nil) { |
| 231 | return nil; |
| 232 | } |
| 233 |
| 234 | CopyVideoFrameToNV12PixelBuffer(buffer, pixelBufferRef); |
| 235 |
| 236 | CMVideoFormatDescriptionRef formatRef = nil; |
| 237 | OSStatus status = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBufferRef, &formatRef); |
| 238 | if (status != 0) { |
| 239 | return nil; |
| 240 | } |
| 241 | if (formatRef == nil) { |
| 242 | return nil; |
| 243 | } |
| 244 |
| 245 | CMSampleTimingInfo timingInfo; |
| 246 | timingInfo.duration = CMTimeMake(1, 30); |
| 247 | timingInfo.presentationTimeStamp = CMTimeMake(0, 30); |
| 248 | timingInfo.decodeTimeStamp = CMTimeMake(0, 30); |
| 249 |
| 250 | CMSampleBufferRef sampleBuffer = nil; |
| 251 | OSStatus bufferStatus = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, pixelBufferRef, formatRef, &timingInfo, &sampleBuffer); |
| 252 |
| 253 | CFRelease(formatRef); |
| 254 | CFRelease(pixelBufferRef); |
| 255 |
| 256 | if (bufferStatus != noErr) { |
| 257 | return nil; |
| 258 | } |
| 259 | if (sampleBuffer == nil) { |
| 260 | return nil; |
| 261 | } |
| 262 |
| 263 | NSArray *attachments = (__bridge NSArray *)CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true); |
| 264 | NSMutableDictionary *dict = (NSMutableDictionary *)attachments[0]; |
| 265 |
| 266 | dict[(__bridge NSString *)kCMSampleAttachmentKey_DisplayImmediately] = @(true); |
| 267 |
| 268 | return sampleBuffer; |
| 269 | } |
| 270 |
| 271 | - (CMSampleBufferRef)createSampleBufferFromNV12Buffer:(const webrtc::NV12BufferInterface *)buffer { |
| 272 | if (!buffer) { |
| 273 | return nil; |
| 274 | } |
| 275 |
| 276 | NSMutableDictionary *ioSurfaceProperties = [[NSMutableDictionary alloc] init]; |
| 277 | //ioSurfaceProperties[@"IOSurfaceIsGlobal"] = @(true); |
| 278 |
| 279 | NSMutableDictionary *options = [[NSMutableDictionary alloc] init]; |
| 280 | //options[(__bridge NSString *)kCVPixelBufferBytesPerRowAlignmentKey] = @(buffer.strideY); |
| 281 | options[(__bridge NSString *)kCVPixelBufferIOSurfacePropertiesKey] = ioSurfaceProperties; |
| 282 |
| 283 | CVPixelBufferRef pixelBufferRef = nil; |
| 284 |
| 285 | if (!(_pixelBufferPool != nil && _pixelBufferPoolWidth == buffer->width() && _pixelBufferPoolHeight == buffer->height())) { |
| 286 | if (_pixelBufferPool) { |
| 287 | CFRelease(_pixelBufferPool); |
| 288 | _pixelBufferPool = nil; |
| 289 | } |
| 290 | _pixelBufferPool = [self createPixelBufferPoolWithWidth:buffer->width() height:buffer->height() pixelFormat:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange maxBufferCount:10]; |
| 291 | _pixelBufferPoolWidth = buffer->width(); |
| 292 | _pixelBufferPoolHeight = buffer->height(); |
| 293 | } |
| 294 |
| 295 | if (_pixelBufferPool != nil && _pixelBufferPoolWidth == buffer->width() && _pixelBufferPoolHeight == buffer->height()) { |
| 296 | CVPixelBufferPoolCreatePixelBufferWithAuxAttributes(kCFAllocatorDefault, _pixelBufferPool, nil, &pixelBufferRef); |
| 297 | } else { |
| 298 | CVPixelBufferCreate( |
| 299 | kCFAllocatorDefault, |
| 300 | buffer->width(), |
| 301 | buffer->height(), |
| 302 | kCVPixelFormatType_420YpCbCr8BiPlanarFullRange, |
| 303 | (__bridge CFDictionaryRef)options, |
| 304 | &pixelBufferRef |
| 305 | ); |
| 306 | } |
| 307 |
| 308 | if (pixelBufferRef == nil) { |
| 309 | return nil; |
| 310 | } |
| 311 |
| 312 | CopyNV12VideoFrameToNV12PixelBuffer(buffer, pixelBufferRef); |
| 313 |
| 314 | CMVideoFormatDescriptionRef formatRef = nil; |
| 315 | OSStatus status = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBufferRef, &formatRef); |
| 316 | if (status != 0) { |
| 317 | return nil; |
| 318 | } |
| 319 | if (formatRef == nil) { |
| 320 | return nil; |
| 321 | } |
| 322 |
| 323 | CMSampleTimingInfo timingInfo; |
| 324 | timingInfo.duration = CMTimeMake(1, 30); |
| 325 | timingInfo.presentationTimeStamp = CMTimeMake(0, 30); |
| 326 | timingInfo.decodeTimeStamp = CMTimeMake(0, 30); |
| 327 |
| 328 | CMSampleBufferRef sampleBuffer = nil; |
| 329 | OSStatus bufferStatus = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, pixelBufferRef, formatRef, &timingInfo, &sampleBuffer); |
| 330 |
| 331 | CFRelease(formatRef); |
| 332 | CFRelease(pixelBufferRef); |
| 333 |
| 334 | if (bufferStatus != noErr) { |
| 335 | return nil; |
| 336 | } |
| 337 | if (sampleBuffer == nil) { |
| 338 | return nil; |
| 339 | } |
| 340 |
| 341 | NSArray *attachments = (__bridge NSArray *)CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true); |
| 342 | NSMutableDictionary *dict = (NSMutableDictionary *)attachments[0]; |
| 343 |
| 344 | dict[(__bridge NSString *)kCMSampleAttachmentKey_DisplayImmediately] = @(true); |
| 345 |
| 346 | return sampleBuffer; |
| 347 | } |
| 348 |
| 349 | - (CMSampleBufferRef)createSampleBufferFromPixelBuffer:(CVPixelBufferRef)pixelBufferRef { |
| 350 | CMVideoFormatDescriptionRef formatRef = nil; |
| 351 | OSStatus status = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, pixelBufferRef, &formatRef); |
| 352 | if (status != 0) { |
| 353 | return nil; |
| 354 | } |
| 355 | if (formatRef == nil) { |
| 356 | return nil; |
| 357 | } |
| 358 |
| 359 | CMSampleTimingInfo timingInfo; |
| 360 | timingInfo.duration = CMTimeMake(1, 30); |
| 361 | timingInfo.presentationTimeStamp = CMTimeMake(0, 30); |
| 362 | timingInfo.decodeTimeStamp = CMTimeMake(0, 30); |
| 363 |
| 364 | CMSampleBufferRef sampleBuffer = nil; |
| 365 | OSStatus bufferStatus = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, pixelBufferRef, formatRef, &timingInfo, &sampleBuffer); |
| 366 |
| 367 | CFRelease(formatRef); |
| 368 |
| 369 | if (bufferStatus != noErr) { |
| 370 | return nil; |
| 371 | } |
| 372 | if (sampleBuffer == nil) { |
| 373 | return nil; |
| 374 | } |
| 375 |
| 376 | NSArray *attachments = (__bridge NSArray *)CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true); |
| 377 | NSMutableDictionary *dict = (NSMutableDictionary *)attachments[0]; |
| 378 |
| 379 | dict[(__bridge NSString *)kCMSampleAttachmentKey_DisplayImmediately] = @(true); |
| 380 |
| 381 | return sampleBuffer; |
| 382 | } |
| 383 |
| 384 | - (void)renderFrameIfReady:(std::shared_ptr<webrtc::VideoFrame>)frame { |
| 385 | if (_isBusy) { |
| 386 | return; |
| 387 | } |
| 388 | VideoSampleBufferContentView *sampleBufferView = _sampleBufferView; |
| 389 | if (!sampleBufferView) { |
| 390 | return; |
| 391 | } |
| 392 |
| 393 | AVSampleBufferDisplayLayer *layer = [sampleBufferView videoLayer]; |
| 394 |
| 395 | VideoSampleBufferContentView *cloneTarget = _cloneTarget; |
| 396 | __weak AVSampleBufferDisplayLayer *cloneLayer = nil; |
| 397 | if (cloneTarget) { |
| 398 | cloneLayer = [cloneTarget videoLayer]; |
| 399 | } |
| 400 |
| 401 | _isBusy = true; |
| 402 | dispatch_async([VideoSampleBufferViewRenderingContext sharedQueue], ^{ |
| 403 | __strong AVSampleBufferDisplayLayer *strongCloneLayer = cloneLayer; |
| 404 |
| 405 | switch (frame->video_frame_buffer()->type()) { |
| 406 | case webrtc::VideoFrameBuffer::Type::kI420: |
| 407 | case webrtc::VideoFrameBuffer::Type::kI420A: { |
| 408 | CMSampleBufferRef sampleBuffer = [self createSampleBufferFromI420Buffer:frame->video_frame_buffer()->GetI420()]; |
| 409 | if (sampleBuffer) { |
| 410 | [layer enqueueSampleBuffer:sampleBuffer]; |
| 411 | [cloneLayer enqueueSampleBuffer:sampleBuffer]; |
| 412 |
| 413 | if ([layer status] == AVQueuedSampleBufferRenderingStatusFailed) { |
| 414 | [layer flush]; |
| 415 | } |
| 416 | if ([cloneLayer status] == AVQueuedSampleBufferRenderingStatusFailed) { |
| 417 | [cloneLayer flush]; |
| 418 | } |
| 419 |
| 420 | CFRelease(sampleBuffer); |
| 421 | } |
| 422 |
| 423 | break; |
| 424 | } |
| 425 | case webrtc::VideoFrameBuffer::Type::kNV12: { |
| 426 | CMSampleBufferRef sampleBuffer = [self createSampleBufferFromNV12Buffer:(webrtc::NV12BufferInterface *)frame->video_frame_buffer().get()]; |
| 427 | if (sampleBuffer) { |
| 428 | [layer enqueueSampleBuffer:sampleBuffer]; |
| 429 | [cloneLayer enqueueSampleBuffer:sampleBuffer]; |
| 430 |
| 431 | CFRelease(sampleBuffer); |
| 432 | } |
| 433 | break; |
| 434 | } |
| 435 | case webrtc::VideoFrameBuffer::Type::kNative: { |
| 436 | id<RTC_OBJC_TYPE(RTCVideoFrameBuffer)> nativeBuffer = static_cast<webrtc::ObjCFrameBuffer *>(frame->video_frame_buffer().get())->wrapped_frame_buffer(); |
| 437 | if ([nativeBuffer isKindOfClass:[RTC_OBJC_TYPE(RTCCVPixelBuffer) class]]) { |
| 438 | RTCCVPixelBuffer *pixelBuffer = (RTCCVPixelBuffer *)nativeBuffer; |
| 439 | CMSampleBufferRef sampleBuffer = [self createSampleBufferFromPixelBuffer:pixelBuffer.pixelBuffer]; |
| 440 | if (sampleBuffer) { |
| 441 | [layer enqueueSampleBuffer:sampleBuffer]; |
| 442 | [cloneLayer enqueueSampleBuffer:sampleBuffer]; |
| 443 |
| 444 | CFRelease(sampleBuffer); |
| 445 | } |
| 446 | } |
| 447 | break; |
| 448 | } |
| 449 | default: { |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 |
| 454 | _isBusy = false; |
| 455 |
| 456 | void *opaqueReference = (__bridge_retained void *)layer; |
| 457 | void *cloneLayerReference = (__bridge_retained void *)strongCloneLayer; |
| 458 | strongCloneLayer = nil; |
| 459 |
| 460 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 461 | __strong AVSampleBufferDisplayLayer *object = (__bridge_transfer AVSampleBufferDisplayLayer *)opaqueReference; |
| 462 | object = nil; |
| 463 |
| 464 | __strong AVSampleBufferDisplayLayer *cloneObject = (__bridge_transfer AVSampleBufferDisplayLayer *)cloneLayerReference; |
| 465 | cloneObject = nil; |
| 466 | }); |
| 467 | }); |
| 468 | } |
| 469 |
| 470 | - (void)setCloneTarget:(VideoSampleBufferContentView * _Nullable)cloneTarget { |
| 471 | _cloneTarget = cloneTarget; |
| 472 | } |
| 473 |
| 474 | @end |
| 475 |
| 476 | @protocol ClonePortalView |
| 477 |
| 478 | - (void)setSourceView:(UIView * _Nullable)sourceView; |
| 479 | - (void)setHidesSourceView:(bool)arg1; |
| 480 | - (void)setMatchesAlpha:(bool)arg1; |
| 481 | - (void)setMatchesPosition:(bool)arg1; |
| 482 | - (void)setMatchesTransform:(bool)arg1; |
| 483 |
| 484 | @end |
| 485 |
| 486 | @interface VideoSampleBufferView () { |
| 487 | VideoSampleBufferContentView *_sampleBufferView; |
| 488 | VideoSampleBufferViewRenderingContext *_renderingContext; |
| 489 |
| 490 | std::shared_ptr<webrtc::VideoFrame> _videoFrame; |
| 491 | std::shared_ptr<webrtc::VideoFrame> _stashedVideoFrame; |
| 492 |
| 493 | int _isWaitingForLayoutFrameCount; |
| 494 | bool _didStartWaitingForLayout; |
| 495 | CGSize _videoFrameSize; |
| 496 | int64_t _lastFrameTimeNs; |
| 497 |
| 498 | CGSize _currentSize; |
| 499 | std::shared_ptr<VideoRendererAdapterImpl> _sink; |
| 500 |
| 501 | void (^_onFirstFrameReceived)(); |
| 502 | bool _firstFrameReceivedReported; |
| 503 |
| 504 | void (^_onOrientationUpdated)(int, CGFloat); |
| 505 |
| 506 | void (^_onIsMirroredUpdated)(bool); |
| 507 |
| 508 | bool _didSetShouldBeMirrored; |
| 509 | bool _shouldBeMirrored; |
| 510 |
| 511 | __weak VideoSampleBufferView *_cloneTarget; |
| 512 | UIView<ClonePortalView> *_portalView; |
| 513 | } |
| 514 |
| 515 | @end |
| 516 |
| 517 | @implementation VideoSampleBufferView |
| 518 |
| 519 | - (instancetype)initWithFrame:(CGRect)frameRect { |
| 520 | self = [super initWithFrame:frameRect]; |
| 521 | if (self) { |
| 522 | [self configure]; |
| 523 |
| 524 | _enabled = true; |
| 525 |
| 526 | _currentSize = CGSizeZero; |
| 527 | _rotationOverride = @(RTCVideoRotation_0); |
| 528 |
| 529 | __weak VideoSampleBufferView *weakSelf = self; |
| 530 | _sink.reset(new VideoRendererAdapterImpl(^(std::shared_ptr<webrtc::VideoFrame> videoFrame) { |
| 531 | if (!videoFrame) { |
| 532 | return; |
| 533 | } |
| 534 |
| 535 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 536 | __strong VideoSampleBufferView *strongSelf = weakSelf; |
| 537 | if (strongSelf == nil) { |
| 538 | return; |
| 539 | } |
| 540 |
| 541 | [strongSelf renderFrame:videoFrame]; |
| 542 | }); |
| 543 | })); |
| 544 | } |
| 545 | return self; |
| 546 | } |
| 547 |
| 548 | - (void)dealloc { |
| 549 | } |
| 550 |
| 551 | - (void)setEnabled:(BOOL)enabled { |
| 552 | if (_enabled != enabled) { |
| 553 | _enabled = enabled; |
| 554 | _sampleBufferView.isPaused = !enabled; |
| 555 | } |
| 556 | } |
| 557 |
| 558 | - (void)setVideoContentMode:(UIViewContentMode)mode { |
| 559 | _videoContentMode = mode; |
| 560 | } |
| 561 |
| 562 | #pragma mark - Private |
| 563 |
| 564 | - (void)configure { |
| 565 | _sampleBufferView = [[VideoSampleBufferContentView alloc] init]; |
| 566 | [self addSubview:_sampleBufferView]; |
| 567 |
| 568 | _renderingContext = [[VideoSampleBufferViewRenderingContext alloc] initWithView:_sampleBufferView]; |
| 569 |
| 570 | _videoFrameSize = CGSizeZero; |
| 571 | } |
| 572 |
| 573 | - (void)layoutSubviews { |
| 574 | [super layoutSubviews]; |
| 575 |
| 576 | CGRect bounds = self.bounds; |
| 577 |
| 578 | if (!CGRectEqualToRect(_sampleBufferView.frame, bounds)) { |
| 579 | _sampleBufferView.frame = bounds; |
| 580 | _portalView.frame = bounds; |
| 581 | } |
| 582 |
| 583 | if (_didStartWaitingForLayout) { |
| 584 | _didStartWaitingForLayout = false; |
| 585 | _isWaitingForLayoutFrameCount = 0; |
| 586 | if (_stashedVideoFrame != nil) { |
| 587 | _videoFrame = _stashedVideoFrame; |
| 588 | _stashedVideoFrame = nil; |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 |
| 593 | #pragma mark - |
| 594 |
| 595 | - (void)setRotationOverride:(NSValue *)rotationOverride { |
| 596 | _rotationOverride = rotationOverride; |
| 597 |
| 598 | [self setNeedsLayout]; |
| 599 | } |
| 600 |
| 601 | - (RTCVideoRotation)frameRotation { |
| 602 | if (_rotationOverride) { |
| 603 | RTCVideoRotation rotation; |
| 604 | if (@available(iOS 11, *)) { |
| 605 | [_rotationOverride getValue:&rotation size:sizeof(rotation)]; |
| 606 | } else { |
| 607 | [_rotationOverride getValue:&rotation]; |
| 608 | } |
| 609 | return rotation; |
| 610 | } |
| 611 |
| 612 | if (_videoFrame) { |
| 613 | switch (_videoFrame->rotation()) { |
| 614 | case webrtc::kVideoRotation_0: |
| 615 | return RTCVideoRotation_0; |
| 616 | case webrtc::kVideoRotation_90: |
| 617 | return RTCVideoRotation_90; |
| 618 | case webrtc::kVideoRotation_180: |
| 619 | return RTCVideoRotation_180; |
| 620 | case webrtc::kVideoRotation_270: |
| 621 | return RTCVideoRotation_270; |
| 622 | default: |
| 623 | return RTCVideoRotation_0; |
| 624 | } |
| 625 | } else { |
| 626 | return RTCVideoRotation_0; |
| 627 | } |
| 628 | } |
| 629 |
| 630 | - (void)setSize:(CGSize)size { |
| 631 | assert([NSThread isMainThread]); |
| 632 | } |
| 633 |
| 634 | - (void)renderFrame:(std::shared_ptr<webrtc::VideoFrame>)frame { |
| 635 | [self renderFrameInternal:frame skipRendering:false]; |
| 636 | VideoSampleBufferView *cloneTarget = _cloneTarget; |
| 637 | if (cloneTarget) { |
| 638 | [cloneTarget renderFrameInternal:frame skipRendering:true]; |
| 639 | } |
| 640 | } |
| 641 |
| 642 | - (void)renderFrameInternal:(std::shared_ptr<webrtc::VideoFrame>)frame skipRendering:(bool)skipRendering { |
| 643 | assert([NSThread isMainThread]); |
| 644 |
| 645 | CGSize size = CGSizeMake(frame->width(), frame->height()); |
| 646 | if (!CGSizeEqualToSize(size, _currentSize)) { |
| 647 | _currentSize = size; |
| 648 | [self setSize:size]; |
| 649 | } |
| 650 |
| 651 | int mappedValue = 0; |
| 652 | switch (RTCVideoRotation(frame->rotation())) { |
| 653 | case RTCVideoRotation_90: { |
| 654 | mappedValue = 1; |
| 655 | break; |
| 656 | } |
| 657 | case RTCVideoRotation_180: { |
| 658 | mappedValue = 2; |
| 659 | break; |
| 660 | } |
| 661 | case RTCVideoRotation_270: { |
| 662 | mappedValue = 3; |
| 663 | break; |
| 664 | } |
| 665 | default: { |
| 666 | mappedValue = 0; |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | [self setInternalOrientationAndSize:mappedValue size:size]; |
| 671 |
| 672 | if (!_firstFrameReceivedReported && _onFirstFrameReceived) { |
| 673 | _firstFrameReceivedReported = true; |
| 674 | _onFirstFrameReceived(); |
| 675 | } |
| 676 |
| 677 | if (!self.isEnabled) { |
| 678 | return; |
| 679 | } |
| 680 |
| 681 | if (!frame) { |
| 682 | return; |
| 683 | } |
| 684 |
| 685 | if (_isWaitingForLayoutFrameCount > 0) { |
| 686 | _stashedVideoFrame = frame; |
| 687 | _isWaitingForLayoutFrameCount--; |
| 688 | return; |
| 689 | } |
| 690 | if (!_didStartWaitingForLayout) { |
| 691 | if (_videoFrame && _videoFrame->width() > 0 && _videoFrame->height() > 0 && frame->width() > 0 && frame->height() > 0) { |
| 692 | float previousAspect = ((float)_videoFrame->width()) / ((float)_videoFrame->height()); |
| 693 | float updatedAspect = ((float)frame->width()) / ((float)frame->height()); |
| 694 | if ((previousAspect < 1.0f) != (updatedAspect < 1.0f)) { |
| 695 | _stashedVideoFrame = frame; |
| 696 | _didStartWaitingForLayout = true; |
| 697 | _isWaitingForLayoutFrameCount = 5; |
| 698 | return; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 |
| 703 | _videoFrame = frame; |
| 704 |
| 705 | if (!skipRendering) { |
| 706 | [_renderingContext renderFrameIfReady:frame]; |
| 707 | } |
| 708 | } |
| 709 |
| 710 | - (std::shared_ptr<rtc::VideoSinkInterface<webrtc::VideoFrame>>)getSink { |
| 711 | assert([NSThread isMainThread]); |
| 712 |
| 713 | return _sink; |
| 714 | } |
| 715 |
| 716 | - (void)addFrame:(const webrtc::VideoFrame&)frame { |
| 717 | std::shared_ptr<webrtc::VideoFrame> videoFrame = std::make_shared<webrtc::VideoFrame>(frame); |
| 718 | [self renderFrame:videoFrame]; |
| 719 | } |
| 720 |
| 721 | static NSString * _Nonnull shiftString(NSString *string, int key) { |
| 722 | NSMutableString *result = [[NSMutableString alloc] init]; |
| 723 |
| 724 | for (int i = 0; i < (int)[string length]; i++) { |
| 725 | unichar c = [string characterAtIndex:i]; |
| 726 | c += key; |
| 727 | [result appendString:[NSString stringWithCharacters:&c length:1]]; |
| 728 | } |
| 729 |
| 730 | return result; |
| 731 | } |
| 732 |
| 733 | /*- (void)addAsCloneTarget:(VideoSampleBufferView *)sourceView { |
| 734 | if (_portalView) { |
| 735 | [_portalView setSourceView:nil]; |
| 736 | [_portalView removeFromSuperview]; |
| 737 | } |
| 738 |
| 739 | if (!sourceView) { |
| 740 | return; |
| 741 | } |
| 742 | static Class portalViewClass = nil; |
| 743 | static dispatch_once_t onceToken; |
| 744 | dispatch_once(&onceToken, ^{ |
| 745 | portalViewClass = NSClassFromString(@"_UIPortalView"); |
| 746 | }); |
| 747 | if (portalViewClass) { |
| 748 | _portalView = (UIView<ClonePortalView> *)[[portalViewClass alloc] init]; |
| 749 | _portalView.frame = sourceView->_sampleBufferView.frame; |
| 750 | _portalView.backgroundColor = [UIColor redColor]; |
| 751 | [_portalView setSourceView:sourceView->_sampleBufferView]; |
| 752 | [_portalView setHidesSourceView:true]; |
| 753 | [_portalView setMatchesAlpha:false]; |
| 754 | [_portalView setMatchesPosition:false]; |
| 755 | [_portalView setMatchesTransform:false]; |
| 756 |
| 757 | [self addSubview:_portalView]; |
| 758 | } |
| 759 | }*/ |
| 760 |
| 761 | - (void)setCloneTarget:(VideoSampleBufferView * _Nullable)cloneTarget { |
| 762 | _cloneTarget = cloneTarget; |
| 763 | if (cloneTarget) { |
| 764 | [_renderingContext setCloneTarget:cloneTarget->_sampleBufferView]; |
| 765 | //[cloneTarget addAsCloneTarget:self]; |
| 766 | } |
| 767 | } |
| 768 |
| 769 | - (void)setOnFirstFrameReceived:(void (^ _Nullable)())onFirstFrameReceived { |
| 770 | _onFirstFrameReceived = [onFirstFrameReceived copy]; |
| 771 | _firstFrameReceivedReported = false; |
| 772 | } |
| 773 |
| 774 | - (void)setInternalOrientationAndSize:(int)internalOrientation size:(CGSize)size { |
| 775 | CGFloat aspect = 1.0f; |
| 776 | if (size.width > 1.0f && size.height > 1.0f) { |
| 777 | aspect = size.width / size.height; |
| 778 | } |
| 779 | if (_internalOrientation != internalOrientation || ABS(_internalAspect - aspect) > 0.001) { |
| 780 | RTCLogInfo(@"VideoSampleBufferView@%lx orientation: %d, aspect: %f", (intptr_t)self, internalOrientation, (float)aspect); |
| 781 |
| 782 | _internalOrientation = internalOrientation; |
| 783 | _internalAspect = aspect; |
| 784 | if (_onOrientationUpdated) { |
| 785 | _onOrientationUpdated(internalOrientation, aspect); |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 |
| 790 | - (void)internalSetOnOrientationUpdated:(void (^ _Nullable)(int, CGFloat))onOrientationUpdated { |
| 791 | _onOrientationUpdated = [onOrientationUpdated copy]; |
| 792 | } |
| 793 |
| 794 | - (void)internalSetOnIsMirroredUpdated:(void (^ _Nullable)(bool))onIsMirroredUpdated { |
| 795 | _onIsMirroredUpdated = [onIsMirroredUpdated copy]; |
| 796 | } |
| 797 |
| 798 | @end |
| 799 |