blob: 8a99d4e5ef3e64d95702e31e58a501dd2d632849 [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
tkchin9eeb6242016-04-27 01:54:20 -070011#import "RTCVideoFrame+Private.h"
Jon Hjelle78234952016-01-11 09:47:07 -080012
13#include "webrtc/base/scoped_ptr.h"
14
Jon Hjelle78234952016-01-11 09:47:07 -080015@implementation RTCVideoFrame {
16 rtc::scoped_ptr<cricket::VideoFrame> _videoFrame;
tkchin7d06a8c2016-04-04 14:10:43 -070017 rtc::scoped_refptr<webrtc::VideoFrameBuffer> _i420Buffer;
Jon Hjelle78234952016-01-11 09:47:07 -080018}
19
20- (size_t)width {
nisse71a0c2f2016-04-04 00:57:29 -070021 return _videoFrame->width();
Jon Hjelle78234952016-01-11 09:47:07 -080022}
23
24- (size_t)height {
nisse71a0c2f2016-04-04 00:57:29 -070025 return _videoFrame->height();
Jon Hjelle78234952016-01-11 09:47:07 -080026}
27
nisse71a0c2f2016-04-04 00:57:29 -070028// TODO(nisse): chromaWidth and chromaHeight are used only in
29// RTCOpenGLVideoRenderer.mm. Update, and then delete these
30// properties.
Jon Hjelle78234952016-01-11 09:47:07 -080031- (size_t)chromaWidth {
nisse71a0c2f2016-04-04 00:57:29 -070032 return (self.width + 1) / 2;
Jon Hjelle78234952016-01-11 09:47:07 -080033}
34
35- (size_t)chromaHeight {
nisse71a0c2f2016-04-04 00:57:29 -070036 return (self.height + 1) / 2;
Jon Hjelle78234952016-01-11 09:47:07 -080037}
38
39- (const uint8_t *)yPlane {
tkchin7d06a8c2016-04-04 14:10:43 -070040 if (!self.i420Buffer) {
41 return nullptr;
42 }
43 return self.i420Buffer->data(webrtc::kYPlane);
Jon Hjelle78234952016-01-11 09:47:07 -080044}
45
46- (const uint8_t *)uPlane {
tkchin7d06a8c2016-04-04 14:10:43 -070047 if (!self.i420Buffer) {
48 return nullptr;
49 }
50 return self.i420Buffer->data(webrtc::kUPlane);
Jon Hjelle78234952016-01-11 09:47:07 -080051}
52
53- (const uint8_t *)vPlane {
tkchin7d06a8c2016-04-04 14:10:43 -070054 if (!self.i420Buffer) {
55 return nullptr;
56 }
57 return self.i420Buffer->data(webrtc::kVPlane);
Jon Hjelle78234952016-01-11 09:47:07 -080058}
59
60- (int32_t)yPitch {
tkchin7d06a8c2016-04-04 14:10:43 -070061 if (!self.i420Buffer) {
62 return 0;
63 }
64 return self.i420Buffer->stride(webrtc::kYPlane);
Jon Hjelle78234952016-01-11 09:47:07 -080065}
66
67- (int32_t)uPitch {
tkchin7d06a8c2016-04-04 14:10:43 -070068 if (!self.i420Buffer) {
69 return 0;
70 }
71 return self.i420Buffer->stride(webrtc::kUPlane);
Jon Hjelle78234952016-01-11 09:47:07 -080072}
73
74- (int32_t)vPitch {
tkchin7d06a8c2016-04-04 14:10:43 -070075 if (!self.i420Buffer) {
76 return 0;
77 }
78 return self.i420Buffer->stride(webrtc::kVPlane);
79}
80
81- (int64_t)timeStamp {
82 return _videoFrame->GetTimeStamp();
83}
84
85- (CVPixelBufferRef)nativeHandle {
86 return static_cast<CVPixelBufferRef>(_videoFrame->GetNativeHandle());
87}
88
89- (void)convertBufferIfNeeded {
90 if (!_i420Buffer) {
91 if (_videoFrame->GetNativeHandle()) {
92 // Convert to I420.
nissef3868762016-04-13 03:29:16 -070093 _i420Buffer = _videoFrame->video_frame_buffer()->NativeToI420Buffer();
tkchin7d06a8c2016-04-04 14:10:43 -070094 } else {
95 // Should already be I420.
nissef3868762016-04-13 03:29:16 -070096 _i420Buffer = _videoFrame->video_frame_buffer();
tkchin7d06a8c2016-04-04 14:10:43 -070097 }
98 }
Jon Hjelle78234952016-01-11 09:47:07 -080099}
100
101#pragma mark - Private
102
103- (instancetype)initWithNativeFrame:(const cricket::VideoFrame *)nativeFrame {
104 if (self = [super init]) {
105 // Keep a shallow copy of the video frame. The underlying frame buffer is
106 // not copied.
107 _videoFrame.reset(nativeFrame->Copy());
108 }
109 return self;
110}
111
tkchin7d06a8c2016-04-04 14:10:43 -0700112- (rtc::scoped_refptr<webrtc::VideoFrameBuffer>)i420Buffer {
113 [self convertBufferIfNeeded];
114 return _i420Buffer;
115}
116
Jon Hjelle78234952016-01-11 09:47:07 -0800117@end