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 | |
magjed | 3149e09 | 2017-05-08 05:32:05 -0700 | [diff] [blame^] | 13 | #include "webrtc/sdk/objc/Framework/Classes/corevideo_frame_buffer.h" |
magjed | c3c4624 | 2017-02-21 05:28:48 -0800 | [diff] [blame] | 14 | |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 15 | @implementation RTCVideoFrame { |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 16 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> _videoBuffer; |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 17 | RTCVideoRotation _rotation; |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 18 | int64_t _timeStampNs; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 19 | } |
| 20 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 21 | - (int)width { |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 22 | return _videoBuffer->width(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 23 | } |
| 24 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 25 | - (int)height { |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 26 | return _videoBuffer->height(); |
| 27 | } |
| 28 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 29 | - (RTCVideoRotation)rotation { |
| 30 | return _rotation; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 31 | } |
| 32 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 33 | - (const uint8_t *)dataY { |
| 34 | return _videoBuffer->DataY(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 35 | } |
| 36 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 37 | - (const uint8_t *)dataU { |
| 38 | return _videoBuffer->DataU(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 39 | } |
| 40 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 41 | - (const uint8_t *)dataV { |
| 42 | return _videoBuffer->DataV(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 43 | } |
| 44 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 45 | - (int)strideY { |
| 46 | return _videoBuffer->StrideY(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 47 | } |
| 48 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 49 | - (int)strideU { |
| 50 | return _videoBuffer->StrideU(); |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 51 | } |
| 52 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 53 | - (int)strideV { |
| 54 | return _videoBuffer->StrideV(); |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 55 | } |
| 56 | |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 57 | - (int64_t)timeStampNs { |
| 58 | return _timeStampNs; |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | - (CVPixelBufferRef)nativeHandle { |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 62 | return static_cast<CVPixelBufferRef>(_videoBuffer->native_handle()); |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 63 | } |
| 64 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 65 | - (RTCVideoFrame *)newI420VideoFrame { |
| 66 | return [[RTCVideoFrame alloc] |
| 67 | initWithVideoBuffer:_videoBuffer->NativeToI420Buffer() |
| 68 | rotation:_rotation |
| 69 | timeStampNs:_timeStampNs]; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 70 | } |
| 71 | |
magjed | c3c4624 | 2017-02-21 05:28:48 -0800 | [diff] [blame] | 72 | - (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer |
| 73 | rotation:(RTCVideoRotation)rotation |
| 74 | timeStampNs:(int64_t)timeStampNs { |
| 75 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> videoBuffer( |
| 76 | new rtc::RefCountedObject<webrtc::CoreVideoFrameBuffer>(pixelBuffer)); |
| 77 | return [self initWithVideoBuffer:videoBuffer |
| 78 | rotation:rotation |
| 79 | timeStampNs:timeStampNs]; |
| 80 | } |
| 81 | |
| 82 | - (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer |
| 83 | scaledWidth:(int)scaledWidth |
| 84 | scaledHeight:(int)scaledHeight |
| 85 | cropWidth:(int)cropWidth |
| 86 | cropHeight:(int)cropHeight |
| 87 | cropX:(int)cropX |
| 88 | cropY:(int)cropY |
| 89 | rotation:(RTCVideoRotation)rotation |
| 90 | timeStampNs:(int64_t)timeStampNs { |
| 91 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> videoBuffer( |
| 92 | new rtc::RefCountedObject<webrtc::CoreVideoFrameBuffer>( |
| 93 | pixelBuffer, |
| 94 | scaledWidth, scaledHeight, |
| 95 | cropWidth, cropHeight, |
| 96 | cropX, cropY)); |
| 97 | return [self initWithVideoBuffer:videoBuffer |
| 98 | rotation:rotation |
| 99 | timeStampNs:timeStampNs]; |
| 100 | } |
| 101 | |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 102 | #pragma mark - Private |
| 103 | |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 104 | - (instancetype)initWithVideoBuffer: |
| 105 | (rtc::scoped_refptr<webrtc::VideoFrameBuffer>)videoBuffer |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 106 | rotation:(RTCVideoRotation)rotation |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 107 | timeStampNs:(int64_t)timeStampNs { |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 108 | if (self = [super init]) { |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 109 | _videoBuffer = videoBuffer; |
| 110 | _rotation = rotation; |
| 111 | _timeStampNs = timeStampNs; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 112 | } |
| 113 | return self; |
| 114 | } |
| 115 | |
magjed | abb84b8 | 2017-03-28 01:56:41 -0700 | [diff] [blame] | 116 | - (rtc::scoped_refptr<webrtc::VideoFrameBuffer>)videoBuffer { |
| 117 | return _videoBuffer; |
| 118 | } |
| 119 | |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 120 | @end |