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