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