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 | |
tkchin | 9eeb624 | 2016-04-27 01:54:20 -0700 | [diff] [blame] | 11 | #import "RTCVideoFrame+Private.h" |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 12 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 13 | #include <memory> |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 14 | |
nisse | 7e4e00d | 2016-06-21 12:53:48 -0700 | [diff] [blame] | 15 | #include "webrtc/media/engine/webrtcvideoframe.h" |
| 16 | |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 17 | @implementation RTCVideoFrame { |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 18 | std::unique_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 | } |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 45 | return self.i420Buffer->DataY(); |
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 | } |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 52 | return self.i420Buffer->DataU(); |
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 | } |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 59 | return self.i420Buffer->DataV(); |
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 | } |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 66 | return self.i420Buffer->StrideY(); |
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 | } |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 73 | return self.i420Buffer->StrideU(); |
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 | } |
nisse | ca6d5d1 | 2016-06-17 05:03:04 -0700 | [diff] [blame] | 80 | return self.i420Buffer->StrideV(); |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | - (int64_t)timeStamp { |
| 84 | return _videoFrame->GetTimeStamp(); |
| 85 | } |
| 86 | |
| 87 | - (CVPixelBufferRef)nativeHandle { |
nisse | 04ebea3 | 2016-05-20 01:48:45 -0700 | [diff] [blame] | 88 | return static_cast<CVPixelBufferRef>( |
| 89 | _videoFrame->video_frame_buffer()->native_handle()); |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | - (void)convertBufferIfNeeded { |
| 93 | if (!_i420Buffer) { |
nisse | 04ebea3 | 2016-05-20 01:48:45 -0700 | [diff] [blame] | 94 | if (_videoFrame->video_frame_buffer()->native_handle()) { |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 95 | // Convert to I420. |
nisse | f386876 | 2016-04-13 03:29:16 -0700 | [diff] [blame] | 96 | _i420Buffer = _videoFrame->video_frame_buffer()->NativeToI420Buffer(); |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 97 | } else { |
| 98 | // Should already be I420. |
nisse | f386876 | 2016-04-13 03:29:16 -0700 | [diff] [blame] | 99 | _i420Buffer = _videoFrame->video_frame_buffer(); |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | #pragma mark - Private |
| 105 | |
| 106 | - (instancetype)initWithNativeFrame:(const cricket::VideoFrame *)nativeFrame { |
| 107 | if (self = [super init]) { |
| 108 | // Keep a shallow copy of the video frame. The underlying frame buffer is |
| 109 | // not copied. |
nisse | 7e4e00d | 2016-06-21 12:53:48 -0700 | [diff] [blame] | 110 | _videoFrame.reset(new cricket::WebRtcVideoFrame( |
| 111 | nativeFrame->video_frame_buffer(), |
| 112 | nativeFrame->rotation(), |
| 113 | nativeFrame->timestamp_us())); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 114 | } |
| 115 | return self; |
| 116 | } |
| 117 | |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 118 | - (rtc::scoped_refptr<webrtc::VideoFrameBuffer>)i420Buffer { |
| 119 | [self convertBufferIfNeeded]; |
| 120 | return _i420Buffer; |
| 121 | } |
| 122 | |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 123 | @end |