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 | |
Anders Carlsson | 7e04281 | 2017-10-05 16:55:38 +0200 | [diff] [blame] | 11 | #import "RTCVideoFrame+Private.h" |
| 12 | |
andersc | f9f448b | 2017-08-17 02:31:55 -0700 | [diff] [blame] | 13 | #import "WebRTC/RTCVideoFrame.h" |
| 14 | #import "WebRTC/RTCVideoFrameBuffer.h" |
magjed | c3c4624 | 2017-02-21 05:28:48 -0800 | [diff] [blame] | 15 | |
Anders Carlsson | 7e04281 | 2017-10-05 16:55:38 +0200 | [diff] [blame] | 16 | #include "api/video/video_frame.h" |
| 17 | #include "rtc_base/timeutils.h" |
| 18 | |
| 19 | id<RTCVideoFrameBuffer> nativeToRtcFrameBuffer( |
| 20 | const rtc::scoped_refptr<webrtc::VideoFrameBuffer> &buffer) { |
| 21 | return buffer->type() == webrtc::VideoFrameBuffer::Type::kNative ? |
| 22 | static_cast<webrtc::ObjCFrameBuffer *>(buffer.get())->wrapped_frame_buffer() : |
| 23 | [[RTCI420Buffer alloc] initWithFrameBuffer:buffer->ToI420()]; |
| 24 | } |
| 25 | |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 26 | @implementation RTCVideoFrame { |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 27 | RTCVideoRotation _rotation; |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 28 | int64_t _timeStampNs; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 31 | @synthesize buffer = _buffer; |
kthelgason | fb14312 | 2017-07-25 07:55:58 -0700 | [diff] [blame] | 32 | @synthesize timeStamp; |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 33 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 34 | - (int)width { |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 35 | return _buffer.width; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 36 | } |
| 37 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 38 | - (int)height { |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 39 | return _buffer.height; |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 40 | } |
| 41 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 42 | - (RTCVideoRotation)rotation { |
| 43 | return _rotation; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 44 | } |
| 45 | |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 46 | - (int64_t)timeStampNs { |
| 47 | return _timeStampNs; |
tkchin | 7d06a8c | 2016-04-04 14:10:43 -0700 | [diff] [blame] | 48 | } |
| 49 | |
magjed | 7ee5125 | 2017-02-21 04:19:46 -0800 | [diff] [blame] | 50 | - (RTCVideoFrame *)newI420VideoFrame { |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 51 | return [[RTCVideoFrame alloc] initWithBuffer:[_buffer toI420] |
| 52 | rotation:_rotation |
| 53 | timeStampNs:_timeStampNs]; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 54 | } |
| 55 | |
magjed | c3c4624 | 2017-02-21 05:28:48 -0800 | [diff] [blame] | 56 | - (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer |
| 57 | rotation:(RTCVideoRotation)rotation |
| 58 | timeStampNs:(int64_t)timeStampNs { |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 59 | return [self initWithBuffer:[[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer] |
| 60 | rotation:rotation |
| 61 | timeStampNs:timeStampNs]; |
magjed | c3c4624 | 2017-02-21 05:28:48 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | - (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer |
| 65 | scaledWidth:(int)scaledWidth |
| 66 | scaledHeight:(int)scaledHeight |
| 67 | cropWidth:(int)cropWidth |
| 68 | cropHeight:(int)cropHeight |
| 69 | cropX:(int)cropX |
| 70 | cropY:(int)cropY |
| 71 | rotation:(RTCVideoRotation)rotation |
| 72 | timeStampNs:(int64_t)timeStampNs { |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 73 | RTCCVPixelBuffer *rtcPixelBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer |
| 74 | adaptedWidth:scaledWidth |
| 75 | adaptedHeight:scaledHeight |
| 76 | cropWidth:cropWidth |
| 77 | cropHeight:cropHeight |
| 78 | cropX:cropX |
| 79 | cropY:cropY]; |
| 80 | return [self initWithBuffer:rtcPixelBuffer rotation:rotation timeStampNs:timeStampNs]; |
magjed | c3c4624 | 2017-02-21 05:28:48 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 83 | - (instancetype)initWithBuffer:(id<RTCVideoFrameBuffer>)buffer |
| 84 | rotation:(RTCVideoRotation)rotation |
| 85 | timeStampNs:(int64_t)timeStampNs { |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 86 | if (self = [super init]) { |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 87 | _buffer = buffer; |
magjed | fb372f0 | 2016-08-10 07:58:29 -0700 | [diff] [blame] | 88 | _rotation = rotation; |
| 89 | _timeStampNs = timeStampNs; |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 90 | } |
Anders Carlsson | 0789dab | 2017-06-21 08:41:26 +0000 | [diff] [blame] | 91 | |
Anders Carlsson | e5960ce | 2017-06-22 15:26:30 +0200 | [diff] [blame] | 92 | return self; |
Anders Carlsson | 1cfeb43 | 2017-06-22 13:06:39 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Anders Carlsson | 7e04281 | 2017-10-05 16:55:38 +0200 | [diff] [blame] | 95 | - (instancetype)initWithNativeVideoFrame:(const webrtc::VideoFrame &)frame { |
| 96 | if (self = [self initWithBuffer:nativeToRtcFrameBuffer(frame.video_frame_buffer()) |
| 97 | rotation:RTCVideoRotation(frame.rotation()) |
| 98 | timeStampNs:frame.timestamp_us() * rtc::kNumNanosecsPerMicrosec]) { |
| 99 | self.timeStamp = frame.timestamp(); |
| 100 | } |
| 101 | |
| 102 | return self; |
| 103 | } |
| 104 | |
| 105 | - (webrtc::VideoFrame)nativeVideoFrame { |
| 106 | rtc::scoped_refptr<webrtc::VideoFrameBuffer> frameBuffer = |
| 107 | new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>(self.buffer); |
| 108 | webrtc::VideoFrame videoFrame(frameBuffer, |
| 109 | (webrtc::VideoRotation)self.rotation, |
| 110 | self.timeStampNs / rtc::kNumNanosecsPerMicrosec); |
| 111 | videoFrame.set_timestamp(self.timeStamp); |
| 112 | return videoFrame; |
| 113 | } |
| 114 | |
Jon Hjelle | 7823495 | 2016-01-11 09:47:07 -0800 | [diff] [blame] | 115 | @end |