Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #import "RTCVideoFrame.h" |
| 12 | |
| 13 | #include "webrtc/base/scoped_ptr.h" |
| 14 | |
| 15 | #import "webrtc/api/objc/RTCVideoFrame+Private.h" |
| 16 | |
| 17 | @implementation RTCVideoFrame { |
| 18 | rtc::scoped_ptr<cricket::VideoFrame> _videoFrame; |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 19 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> _i420Buffer; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | - (size_t)width { |
nisse | 71a0c2f | 2016-04-04 00:57:29 -0700 | [diff] [blame] | 23 | return _videoFrame->width(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | - (size_t)height { |
nisse | 71a0c2f | 2016-04-04 00:57:29 -0700 | [diff] [blame] | 27 | return _videoFrame->height(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 28 | } |
| 29 | |
nisse | 71a0c2f | 2016-04-04 00:57:29 -0700 | [diff] [blame] | 30 | // TODO(nisse): chromaWidth and chromaHeight are used only in |
| 31 | // RTCOpenGLVideoRenderer.mm. Update, and then delete these |
| 32 | // properties. |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 33 | - (size_t)chromaWidth { |
nisse | 71a0c2f | 2016-04-04 00:57:29 -0700 | [diff] [blame] | 34 | return (self.width + 1) / 2; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | - (size_t)chromaHeight { |
nisse | 71a0c2f | 2016-04-04 00:57:29 -0700 | [diff] [blame] | 38 | return (self.height + 1) / 2; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | - (const uint8_t *)yPlane { |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 42 | if (!self.i420Buffer) { |
| 43 | return nullptr; |
| 44 | } |
| 45 | return self.i420Buffer->data(webrtc::kYPlane); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | - (const uint8_t *)uPlane { |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 49 | if (!self.i420Buffer) { |
| 50 | return nullptr; |
| 51 | } |
| 52 | return self.i420Buffer->data(webrtc::kUPlane); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | - (const uint8_t *)vPlane { |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 56 | if (!self.i420Buffer) { |
| 57 | return nullptr; |
| 58 | } |
| 59 | return self.i420Buffer->data(webrtc::kVPlane); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | - (int32_t)yPitch { |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 63 | if (!self.i420Buffer) { |
| 64 | return 0; |
| 65 | } |
| 66 | return self.i420Buffer->stride(webrtc::kYPlane); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | - (int32_t)uPitch { |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 70 | if (!self.i420Buffer) { |
| 71 | return 0; |
| 72 | } |
| 73 | return self.i420Buffer->stride(webrtc::kUPlane); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | - (int32_t)vPitch { |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 77 | if (!self.i420Buffer) { |
| 78 | return 0; |
| 79 | } |
| 80 | return self.i420Buffer->stride(webrtc::kVPlane); |
| 81 | } |
| 82 | |
| 83 | - (int64_t)timeStamp { |
| 84 | return _videoFrame->GetTimeStamp(); |
| 85 | } |
| 86 | |
| 87 | - (CVPixelBufferRef)nativeHandle { |
| 88 | return static_cast<CVPixelBufferRef>(_videoFrame->GetNativeHandle()); |
| 89 | } |
| 90 | |
| 91 | - (void)convertBufferIfNeeded { |
| 92 | if (!_i420Buffer) { |
| 93 | if (_videoFrame->GetNativeHandle()) { |
| 94 | // Convert to I420. |
| 95 | _i420Buffer = _videoFrame->GetVideoFrameBuffer()->NativeToI420Buffer(); |
| 96 | } else { |
| 97 | // Should already be I420. |
| 98 | _i420Buffer = _videoFrame->GetVideoFrameBuffer(); |
| 99 | } |
| 100 | } |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | #pragma mark - Private |
| 104 | |
| 105 | - (instancetype)initWithNativeFrame:(const cricket::VideoFrame *)nativeFrame { |
| 106 | if (self = [super init]) { |
| 107 | // Keep a shallow copy of the video frame. The underlying frame buffer is |
| 108 | // not copied. |
| 109 | _videoFrame.reset(nativeFrame->Copy()); |
| 110 | } |
| 111 | return self; |
| 112 | } |
| 113 | |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 114 | - (rtc::scoped_refptr<webrtc::VideoFrameBuffer>)i420Buffer { |
| 115 | [self convertBufferIfNeeded]; |
| 116 | return _i420Buffer; |
| 117 | } |
| 118 | |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 119 | @end |