1/* 2 * Copyright 2018 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11#import "ARDExternalSampleCapturer.h" 12 13#import <WebRTC/RTCCVPixelBuffer.h> 14#import <WebRTC/RTCVideoFrameBuffer.h> 15 16@implementation ARDExternalSampleCapturer 17 18- (instancetype)initWithDelegate:(__weak id<RTC_OBJC_TYPE(RTCVideoCapturerDelegate)>)delegate { 19 return [super initWithDelegate:delegate]; 20} 21 22#pragma mark - ARDExternalSampleDelegate 23 24- (void)didCaptureSampleBuffer:(CMSampleBufferRef)sampleBuffer { 25 if (CMSampleBufferGetNumSamples(sampleBuffer) != 1 || !CMSampleBufferIsValid(sampleBuffer) || 26 !CMSampleBufferDataIsReady(sampleBuffer)) { 27 return; 28 } 29 30 CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer); 31 if (pixelBuffer == nil) { 32 return; 33 } 34 35 RTC_OBJC_TYPE(RTCCVPixelBuffer) *rtcPixelBuffer = 36 [[RTC_OBJC_TYPE(RTCCVPixelBuffer) alloc] initWithPixelBuffer:pixelBuffer]; 37 int64_t timeStampNs = 38 CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) * NSEC_PER_SEC; 39 RTC_OBJC_TYPE(RTCVideoFrame) *videoFrame = 40 [[RTC_OBJC_TYPE(RTCVideoFrame) alloc] initWithBuffer:rtcPixelBuffer 41 rotation:RTCVideoRotation_0 42 timeStampNs:timeStampNs]; 43 [self.delegate capturer:self didCaptureVideoFrame:videoFrame]; 44} 45 46@end 47