blob: f5d7195c21494cc0d80035cb489e17d890d90b8e [file] [log] [blame]
Jon Hjelle78234952016-01-11 09:47:07 -08001/*
2 * Copyright 2015 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
Anders Carlsson7e042812017-10-05 16:55:38 +020011#import "RTCVideoFrame+Private.h"
12
anderscf9f448b2017-08-17 02:31:55 -070013#import "WebRTC/RTCVideoFrame.h"
14#import "WebRTC/RTCVideoFrameBuffer.h"
magjedc3c46242017-02-21 05:28:48 -080015
Anders Carlsson7e042812017-10-05 16:55:38 +020016#include "api/video/video_frame.h"
17#include "rtc_base/timeutils.h"
Anders Carlsson3ff50fb2018-02-01 15:47:05 +010018#include "sdk/objc/Framework/Native/api/video_frame_buffer.h"
19#include "sdk/objc/Framework/Native/src/objc_frame_buffer.h"
Anders Carlsson7e042812017-10-05 16:55:38 +020020
21id<RTCVideoFrameBuffer> nativeToRtcFrameBuffer(
22 const rtc::scoped_refptr<webrtc::VideoFrameBuffer> &buffer) {
23 return buffer->type() == webrtc::VideoFrameBuffer::Type::kNative ?
24 static_cast<webrtc::ObjCFrameBuffer *>(buffer.get())->wrapped_frame_buffer() :
25 [[RTCI420Buffer alloc] initWithFrameBuffer:buffer->ToI420()];
26}
27
Jon Hjelle78234952016-01-11 09:47:07 -080028@implementation RTCVideoFrame {
magjed7ee51252017-02-21 04:19:46 -080029 RTCVideoRotation _rotation;
magjedfb372f02016-08-10 07:58:29 -070030 int64_t _timeStampNs;
Jon Hjelle78234952016-01-11 09:47:07 -080031}
32
Anders Carlssone5960ce2017-06-22 15:26:30 +020033@synthesize buffer = _buffer;
kthelgasonfb143122017-07-25 07:55:58 -070034@synthesize timeStamp;
Anders Carlssone5960ce2017-06-22 15:26:30 +020035
magjed7ee51252017-02-21 04:19:46 -080036- (int)width {
Anders Carlssone5960ce2017-06-22 15:26:30 +020037 return _buffer.width;
Jon Hjelle78234952016-01-11 09:47:07 -080038}
39
magjed7ee51252017-02-21 04:19:46 -080040- (int)height {
Anders Carlssone5960ce2017-06-22 15:26:30 +020041 return _buffer.height;
magjedfb372f02016-08-10 07:58:29 -070042}
43
magjed7ee51252017-02-21 04:19:46 -080044- (RTCVideoRotation)rotation {
45 return _rotation;
Jon Hjelle78234952016-01-11 09:47:07 -080046}
47
magjedfb372f02016-08-10 07:58:29 -070048- (int64_t)timeStampNs {
49 return _timeStampNs;
tkchin7d06a8c2016-04-04 14:10:43 -070050}
51
magjed7ee51252017-02-21 04:19:46 -080052- (RTCVideoFrame *)newI420VideoFrame {
Anders Carlssone5960ce2017-06-22 15:26:30 +020053 return [[RTCVideoFrame alloc] initWithBuffer:[_buffer toI420]
54 rotation:_rotation
55 timeStampNs:_timeStampNs];
Jon Hjelle78234952016-01-11 09:47:07 -080056}
57
magjedc3c46242017-02-21 05:28:48 -080058- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer
59 rotation:(RTCVideoRotation)rotation
60 timeStampNs:(int64_t)timeStampNs {
Anders Carlssone5960ce2017-06-22 15:26:30 +020061 return [self initWithBuffer:[[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer]
62 rotation:rotation
63 timeStampNs:timeStampNs];
magjedc3c46242017-02-21 05:28:48 -080064}
65
66- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer
67 scaledWidth:(int)scaledWidth
68 scaledHeight:(int)scaledHeight
69 cropWidth:(int)cropWidth
70 cropHeight:(int)cropHeight
71 cropX:(int)cropX
72 cropY:(int)cropY
73 rotation:(RTCVideoRotation)rotation
74 timeStampNs:(int64_t)timeStampNs {
Anders Carlssone5960ce2017-06-22 15:26:30 +020075 RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer
76 adaptedWidth:scaledWidth
77 adaptedHeight:scaledHeight
78 cropWidth:cropWidth
79 cropHeight:cropHeight
80 cropX:cropX
81 cropY:cropY];
82 return [self initWithBuffer:rtcPixelBuffer rotation:rotation timeStampNs:timeStampNs];
magjedc3c46242017-02-21 05:28:48 -080083}
84
Anders Carlssone5960ce2017-06-22 15:26:30 +020085- (instancetype)initWithBuffer:(id<RTCVideoFrameBuffer>)buffer
86 rotation:(RTCVideoRotation)rotation
87 timeStampNs:(int64_t)timeStampNs {
Jon Hjelle78234952016-01-11 09:47:07 -080088 if (self = [super init]) {
Anders Carlssone5960ce2017-06-22 15:26:30 +020089 _buffer = buffer;
magjedfb372f02016-08-10 07:58:29 -070090 _rotation = rotation;
91 _timeStampNs = timeStampNs;
Jon Hjelle78234952016-01-11 09:47:07 -080092 }
Anders Carlsson0789dab2017-06-21 08:41:26 +000093
Anders Carlssone5960ce2017-06-22 15:26:30 +020094 return self;
Anders Carlsson1cfeb432017-06-22 13:06:39 +000095}
96
Anders Carlsson7e042812017-10-05 16:55:38 +020097- (instancetype)initWithNativeVideoFrame:(const webrtc::VideoFrame &)frame {
98 if (self = [self initWithBuffer:nativeToRtcFrameBuffer(frame.video_frame_buffer())
99 rotation:RTCVideoRotation(frame.rotation())
100 timeStampNs:frame.timestamp_us() * rtc::kNumNanosecsPerMicrosec]) {
101 self.timeStamp = frame.timestamp();
102 }
103
104 return self;
105}
106
107- (webrtc::VideoFrame)nativeVideoFrame {
108 rtc::scoped_refptr<webrtc::VideoFrameBuffer> frameBuffer =
Anders Carlsson3ff50fb2018-02-01 15:47:05 +0100109 webrtc::ObjCToNativeVideoFrameBuffer(self.buffer);
Anders Carlsson7e042812017-10-05 16:55:38 +0200110 webrtc::VideoFrame videoFrame(frameBuffer,
111 (webrtc::VideoRotation)self.rotation,
112 self.timeStampNs / rtc::kNumNanosecsPerMicrosec);
113 videoFrame.set_timestamp(self.timeStamp);
114 return videoFrame;
115}
116
Jon Hjelle78234952016-01-11 09:47:07 -0800117@end