Add RTCVideoFrame init function from CVPixelBufferRef
Adds a public init function in RTCVideoFrame that makes it possible to
create a frame from a CVPixelBufferRef.
BUG=webrtc:7177
NOTRY=True
Review-Url: https://codereview.webrtc.org/2700113003
Cr-Commit-Position: refs/heads/master@{#16746}
diff --git a/webrtc/sdk/objc/Framework/Classes/RTCVideoFrame.mm b/webrtc/sdk/objc/Framework/Classes/RTCVideoFrame.mm
index 0403557..ea603b9c 100644
--- a/webrtc/sdk/objc/Framework/Classes/RTCVideoFrame.mm
+++ b/webrtc/sdk/objc/Framework/Classes/RTCVideoFrame.mm
@@ -10,6 +10,8 @@
#import "RTCVideoFrame+Private.h"
+#include "webrtc/common_video/include/corevideo_frame_buffer.h"
+
@implementation RTCVideoFrame {
rtc::scoped_refptr<webrtc::VideoFrameBuffer> _videoBuffer;
RTCVideoRotation _rotation;
@@ -67,6 +69,36 @@
timeStampNs:_timeStampNs];
}
+- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer
+ rotation:(RTCVideoRotation)rotation
+ timeStampNs:(int64_t)timeStampNs {
+ rtc::scoped_refptr<webrtc::VideoFrameBuffer> videoBuffer(
+ new rtc::RefCountedObject<webrtc::CoreVideoFrameBuffer>(pixelBuffer));
+ return [self initWithVideoBuffer:videoBuffer
+ rotation:rotation
+ timeStampNs:timeStampNs];
+}
+
+- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer
+ scaledWidth:(int)scaledWidth
+ scaledHeight:(int)scaledHeight
+ cropWidth:(int)cropWidth
+ cropHeight:(int)cropHeight
+ cropX:(int)cropX
+ cropY:(int)cropY
+ rotation:(RTCVideoRotation)rotation
+ timeStampNs:(int64_t)timeStampNs {
+ rtc::scoped_refptr<webrtc::VideoFrameBuffer> videoBuffer(
+ new rtc::RefCountedObject<webrtc::CoreVideoFrameBuffer>(
+ pixelBuffer,
+ scaledWidth, scaledHeight,
+ cropWidth, cropHeight,
+ cropX, cropY));
+ return [self initWithVideoBuffer:videoBuffer
+ rotation:rotation
+ timeStampNs:timeStampNs];
+}
+
#pragma mark - Private
- (instancetype)initWithVideoBuffer:
diff --git a/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCVideoFrame.h b/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCVideoFrame.h
index cbd8cc7..ddf8a6b 100644
--- a/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCVideoFrame.h
+++ b/webrtc/sdk/objc/Framework/Headers/WebRTC/RTCVideoFrame.h
@@ -50,6 +50,27 @@
@property(nonatomic, readonly) CVPixelBufferRef nativeHandle;
- (instancetype)init NS_UNAVAILABLE;
+- (instancetype)new NS_UNAVAILABLE;
+
+/** Initialize an RTCVideoFrame from a pixel buffer, rotation, and timestamp.
+ */
+- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer
+ rotation:(RTCVideoRotation)rotation
+ timeStampNs:(int64_t)timeStampNs;
+
+/** Initialize an RTCVideoFrame from a pixel buffer combined with cropping and
+ * scaling. Cropping will be applied first on the pixel buffer, followed by
+ * scaling to the final resolution of scaledWidth x scaledHeight.
+ */
+- (instancetype)initWithPixelBuffer:(CVPixelBufferRef)pixelBuffer
+ scaledWidth:(int)scaledWidth
+ scaledHeight:(int)scaledHeight
+ cropWidth:(int)cropWidth
+ cropHeight:(int)cropHeight
+ cropX:(int)cropX
+ cropY:(int)cropY
+ rotation:(RTCVideoRotation)rotation
+ timeStampNs:(int64_t)timeStampNs;
/** Return a frame that is guaranteed to be I420, i.e. it is possible to access
* the YUV data on it.