Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(158)

Side by Side Diff: webrtc/sdk/objc/Framework/Classes/PeerConnection/RTCVideoCodecH264.mm

Issue 2966023002: Injectable Obj-C video codecs (Closed)
Patch Set: Fix some issues from comments. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2017 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
11 #import <Foundation/Foundation.h>
12 #import <WebRTC/RTCRtpFragmentationHeader.h>
13 #import <WebRTC/RTCVideoCodec.h>
14 #import <WebRTC/RTCVideoCodecH264.h>
magjed_webrtc 2017/07/05 13:22:32 This should be the first import.
andersc 2017/07/06 12:39:41 Done.
15 #import <WebRTC/RTCVideoFrame.h>
16 #import <WebRTC/RTCVideoFrameBuffer.h>
17
18 #include <vector>
magjed_webrtc 2017/07/05 13:22:32 This include should be before webrtc imports/inclu
andersc 2017/07/06 12:39:41 Done.
19
20 #import "RTCVideoCodec+Private.h"
21 #include "webrtc/base/timeutils.h"
22 #include "webrtc/sdk/objc/Framework/Classes/Video/objc_frame_buffer.h"
23 #include "webrtc/sdk/objc/Framework/Classes/VideoToolbox/decoder.h"
24 #include "webrtc/sdk/objc/Framework/Classes/VideoToolbox/encoder.h"
25 #include "webrtc/system_wrappers/include/field_trial.h"
26
27 const size_t kDefaultPayloadSize = 1440;
28
29 const char kHighProfileExperiment[] = "WebRTC-H264HighProfile";
30
31 bool IsHighProfileEnabled() {
32 return webrtc::field_trial::IsEnabled(kHighProfileExperiment);
33 }
34
35 // H264 specific settings
36 @implementation RTCCodecSpecificInfoH264
37
38 @synthesize packetizationMode = _packetizationMode;
39
40 - (webrtc::CodecSpecificInfo)toCpp {
41 webrtc::CodecSpecificInfo codecSpecificInfo;
42 codecSpecificInfo.codecType = webrtc::kVideoCodecH264;
43 codecSpecificInfo.codec_name = "H264";
44 codecSpecificInfo.codecSpecific.H264.packetization_mode =
45 (webrtc::H264PacketizationMode)_packetizationMode;
46
47 return codecSpecificInfo;
48 }
49
50 @end
51
52 namespace {
53
54 class H264VideoToolboxEncodeCompleteCallback : public webrtc::EncodedImageCallba ck {
55 public:
56 Result OnEncodedImage(const webrtc::EncodedImage &encoded_image,
57 const webrtc::CodecSpecificInfo *codec_specific_info,
58 const webrtc::RTPFragmentationHeader *fragmentation) {
59 RTCEncodedImage *image = [[RTCEncodedImage alloc] initWithEncodedImage:encod ed_image];
60
61 RTCCodecSpecificInfoH264 *info = [[RTCCodecSpecificInfoH264 alloc] init];
62 info.packetizationMode =
63 (RTCH264PacketizationMode)codec_specific_info->codecSpecific.H264.packet ization_mode;
64
65 RTCRtpFragmentationHeader *header =
66 [[RTCRtpFragmentationHeader alloc] initWithFragmentationHeader:fragmenta tion];
67
68 callback(image, info, header);
69 return Result(Result::OK, 0);
70 }
71
72 RTCVideoEncoderCallback callback;
73 };
74
75 class H264VideoToolboxDecodeCompleteCallback : public webrtc::DecodedImageCallba ck {
76 public:
77 int32_t Decoded(webrtc::VideoFrame &decodedImage) {
78 rtc::scoped_refptr<webrtc::VideoFrameBuffer> video_frame_buffer =
79 decodedImage.video_frame_buffer();
80 id<RTCVideoFrameBuffer> rtcFrameBuffer;
81 rtc::scoped_refptr<webrtc::ObjCFrameBuffer> objc_frame_buffer(
82 static_cast<webrtc::ObjCFrameBuffer *>(video_frame_buffer.get()));
83 rtcFrameBuffer = (id<RTCVideoFrameBuffer>)objc_frame_buffer->wrapped_frame_b uffer();
84
85 RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc]
86 initWithBuffer:rtcFrameBuffer
87 rotation:static_cast<RTCVideoRotation>(decodedImage.rotation())
88 timeStampNs:decodedImage.timestamp_us() * rtc::kNumNanosecsPerMicrose c];
89 videoFrame.timeStamp = decodedImage.timestamp();
90
91 callback(videoFrame);
92
93 return 0;
94 }
95
96 RTCVideoDecoderCallback callback;
97 };
98
99 } // namespace
100
101 // Encoder
102 @implementation RTCVideoEncoderH264 {
103 webrtc::H264VideoToolboxEncoder *_videoToolboxEncoder;
104 H264VideoToolboxEncodeCompleteCallback *_toolboxCallback;
105 }
106
107 - (instancetype)initWithCodecInfo:(RTCVideoCodecInfo *)codecInfo {
108 if (self = [super init]) {
109 cricket::VideoCodec codec = [codecInfo toCpp];
110 _videoToolboxEncoder = new webrtc::H264VideoToolboxEncoder(codec);
111 }
112 return self;
113 }
114
115 - (void)setCallback:(RTCVideoEncoderCallback)callback {
116 _toolboxCallback = new H264VideoToolboxEncodeCompleteCallback();
117 _toolboxCallback->callback = callback;
118 _videoToolboxEncoder->RegisterEncodeCompleteCallback(_toolboxCallback);
119 }
120
121 - (void)initEncodeWithSettings:(RTCVideoEncoderSettings *)settings
122 numberOfCores:(int)numberOfCores {
123 webrtc::VideoCodec *codecSettings = [settings toCpp];
124 _videoToolboxEncoder->InitEncode(codecSettings, numberOfCores, kDefaultPayload Size);
125 }
126
127 - (void)releaseEncode {
128 _videoToolboxEncoder->Release();
129 }
130
131 - (void)encode:(RTCVideoFrame *)frame
132 codecSpecificInfo:(id<RTCCodecSpecificInfo>)info
133 frameTypes:(NSArray<NSNumber *> *)frameTypes {
134 rtc::scoped_refptr<webrtc::VideoFrameBuffer> frameBuffer =
135 new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>(frame.buffer);
136 webrtc::VideoFrame videoFrame(frameBuffer,
137 (webrtc::VideoRotation)frame.rotation,
138 frame.timeStampNs / rtc::kNumNanosecsPerMicrosec );
139 videoFrame.set_timestamp(frame.timeStamp);
140
141 // Handle types than can be converted into one of webrtc::CodecSpecificInfo's hard coded cases.
142 webrtc::CodecSpecificInfo codecSpecificInfo;
143 if ([info isKindOfClass:[RTCCodecSpecificInfoH264 class]]) {
144 codecSpecificInfo = [(RTCCodecSpecificInfoH264 *)info toCpp];
145 }
146
147 std::vector<webrtc::FrameType> nativeFrameTypes;
148 for (NSNumber *frameType in frameTypes) {
149 RTCFrameType rtcFrameType = (RTCFrameType)frameType.unsignedIntegerValue;
150 nativeFrameTypes.push_back((webrtc::FrameType)rtcFrameType);
151 }
152
153 _videoToolboxEncoder->Encode(videoFrame, &codecSpecificInfo, &nativeFrameTypes );
154 }
155
156 - (BOOL)setBitrate:(uint32_t)bitrateKbit framerate:(uint32_t)framerate {
157 return _videoToolboxEncoder->SetRates(bitrateKbit, framerate) == WEBRTC_VIDEO_ CODEC_OK;
158 }
159
160 @end
161
162 // Decoder
163 @implementation RTCVideoDecoderH264 {
164 webrtc::H264VideoToolboxDecoder *_videoToolboxDecoder;
165 H264VideoToolboxDecodeCompleteCallback *_toolboxCallback;
166 }
167
168 - (instancetype)init {
169 if (self = [super init]) {
170 cricket::VideoCodec codec(cricket::kH264CodecName);
171 _videoToolboxDecoder = new webrtc::H264VideoToolboxDecoder();
172 }
173 return self;
174 }
175
176 - (int)initDecodeWithSettings:(RTCVideoEncoderSettings *)settings numberOfCores: (int)numberOfCores {
177 webrtc::VideoCodec *codecSettings = [settings toCpp];
178 return _videoToolboxDecoder->InitDecode(codecSettings, numberOfCores);
179 }
180
181 - (void)setCallback:(RTCVideoDecoderCallback)callback {
182 _toolboxCallback = new H264VideoToolboxDecodeCompleteCallback();
183 _toolboxCallback->callback = callback;
184 _videoToolboxDecoder->RegisterDecodeCompleteCallback(_toolboxCallback);
185 }
186
187 - (int32_t)releaseDecode {
188 return _videoToolboxDecoder->Release();
189 }
190
191 - (int)decode:(RTCEncodedImage *)encodedImage
192 missingFrames:(BOOL)missingFrames
193 fragmentationHeader:(RTCRtpFragmentationHeader *)fragmentationHeader
194 codecSpecificInfo:(__nullable id<RTCCodecSpecificInfo>)info
195 renderTimeMs:(int64_t)renderTimeMs {
196 webrtc::EncodedImage image = [encodedImage toCpp];
197
198 // Handle types than can be converted into one of webrtc::CodecSpecificInfo's hard coded cases.
199 webrtc::CodecSpecificInfo codecSpecificInfo;
200 if ([info isKindOfClass:[RTCCodecSpecificInfoH264 class]]) {
201 codecSpecificInfo = [(RTCCodecSpecificInfoH264 *)info toCpp];
202 }
203
204 webrtc::RTPFragmentationHeader *header = [fragmentationHeader toCpp];
205
206 return _videoToolboxDecoder->Decode(
207 image, missingFrames, header, &codecSpecificInfo, renderTimeMs);
208 }
209
210 @end
211
212 // Encoder factory
213 @implementation RTCVideoEncoderFactoryH264
214
215 - (NSArray<RTCVideoCodecInfo *> *)supportedCodecs {
216 NSMutableArray<RTCVideoCodecInfo *> *codecs = [NSMutableArray array];
217 NSString *codecName = [NSString stringWithUTF8String:cricket::kH264CodecName];
218
219 if (IsHighProfileEnabled()) {
220 NSDictionary<NSString *, NSString *> *constrainedHighParams = @{
221 @"profile-level-id" : @"640c1f", // Level 3.1 Constrained High
222 @"level-asymmetry-allowed" : @"1",
223 @"packetization-mode" : @"1",
224 };
225 RTCVideoCodecInfo *constrainedHighInfo =
226 [[RTCVideoCodecInfo alloc] initWithPayload:0
227 name:codecName
228 parameters:constrainedHighParams];
229 [codecs addObject:constrainedHighInfo];
230 }
231
232 NSDictionary<NSString *, NSString *> *constrainedBaselineParams = @{
233 @"profile-level-id" : @"42e01f", // Level 3.1 Constrained Baseline
234 @"level-asymmetry-allowed" : @"1",
235 @"packetization-mode" : @"1",
236 };
237 RTCVideoCodecInfo *constrainedBaselineInfo =
238 [[RTCVideoCodecInfo alloc] initWithPayload:0
239 name:codecName
240 parameters:constrainedBaselineParams];
241 [codecs addObject:constrainedBaselineInfo];
242
243 return [codecs copy];
244 }
245
246 - (id<RTCVideoEncoder>)createEncoder:(RTCVideoCodecInfo *)info {
247 return [[RTCVideoEncoderH264 alloc] initWithCodecInfo:info];
248 }
249
250 @end
251
252 // Decoder factory
253 @implementation RTCVideoDecoderFactoryH264
254
255 - (id<RTCVideoDecoder>)createDecoder:(RTCVideoCodecInfo *)info {
256 return [[RTCVideoDecoderH264 alloc] init];
257 }
258
259 - (NSArray<RTCVideoCodecInfo *> *)supportedCodecs {
260 NSString *codecName = [NSString stringWithUTF8String:cricket::kH264CodecName];
261 return @[ [[RTCVideoCodecInfo alloc] initWithPayload:0 name:codecName paramete rs:@{}] ];
262 }
263
264 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698