blob: 9d93ea1e6c566d5b53bcf35320255ace97a90f1c [file] [log] [blame]
Anders Carlsson358f2e02018-06-04 10:24:37 +02001/*
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
Anders Carlsson7bca8ca2018-08-30 09:30:29 +020013#import <WebRTC/RTCCVPixelBuffer.h>
14#import <WebRTC/RTCVideoFrameBuffer.h>
Anders Carlsson358f2e02018-06-04 10:24:37 +020015
16@implementation ARDExternalSampleCapturer
17
18- (instancetype)initWithDelegate:(__weak id<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 RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer];
36 int64_t timeStampNs =
37 CMTimeGetSeconds(CMSampleBufferGetPresentationTimeStamp(sampleBuffer)) * NSEC_PER_SEC;
38 RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc] initWithBuffer:rtcPixelBuffer
39 rotation:RTCVideoRotation_0
40 timeStampNs:timeStampNs];
41 [self.delegate capturer:self didCaptureVideoFrame:videoFrame];
42}
43
44@end