Anders Carlsson | 358f2e0 | 2018-06-04 10:24:37 +0200 | [diff] [blame] | 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 | |
Anders Carlsson | 7bca8ca | 2018-08-30 09:30:29 +0200 | [diff] [blame] | 13 | #import <WebRTC/RTCCVPixelBuffer.h> |
| 14 | #import <WebRTC/RTCVideoFrameBuffer.h> |
Anders Carlsson | 358f2e0 | 2018-06-04 10:24:37 +0200 | [diff] [blame] | 15 | |
| 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 |