blob: c39a309ca80234fb07189d9f2b0abb95adfbe533 [file] [log] [blame]
magjed73c0eb52017-08-07 06:55:28 -07001/*
2 * Copyright (c) 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 */
11
12#import "WebRTC/RTCVideoCodecH264.h"
13
14#import <VideoToolbox/VideoToolbox.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "modules/video_coding/include/video_error_codes.h"
17#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
19#include "rtc_base/timeutils.h"
20#include "sdk/objc/Framework/Classes/VideoToolbox/nalu_rewriter.h"
magjed73c0eb52017-08-07 06:55:28 -070021
22#import "WebRTC/RTCVideoFrame.h"
23#import "WebRTC/RTCVideoFrameBuffer.h"
24#import "helpers.h"
Peter Hanspersd9b64cd2018-01-12 16:16:18 +010025#import "scoped_cftyperef.h"
magjed73c0eb52017-08-07 06:55:28 -070026
27#if defined(WEBRTC_IOS)
28#import "Common/RTCUIApplicationStatusObserver.h"
JT Teha6368d12017-09-28 11:00:39 -070029#import "WebRTC/UIDevice+RTCDevice.h"
magjed73c0eb52017-08-07 06:55:28 -070030#endif
31
32// Struct that we pass to the decoder per frame to decode. We receive it again
33// in the decoder callback.
34struct RTCFrameDecodeParams {
35 RTCFrameDecodeParams(RTCVideoDecoderCallback cb, int64_t ts) : callback(cb), timestamp(ts) {}
36 RTCVideoDecoderCallback callback;
37 int64_t timestamp;
38};
39
Kári Tristan Helgason86de7e82017-12-01 13:48:48 +010040@interface RTCVideoDecoderH264 ()
41- (void)setError:(OSStatus)error;
42@end
43
magjed73c0eb52017-08-07 06:55:28 -070044// This is the callback function that VideoToolbox calls when decode is
45// complete.
Kári Tristan Helgason86de7e82017-12-01 13:48:48 +010046void decompressionOutputCallback(void *decoderRef,
magjed73c0eb52017-08-07 06:55:28 -070047 void *params,
48 OSStatus status,
49 VTDecodeInfoFlags infoFlags,
50 CVImageBufferRef imageBuffer,
51 CMTime timestamp,
52 CMTime duration) {
53 std::unique_ptr<RTCFrameDecodeParams> decodeParams(
54 reinterpret_cast<RTCFrameDecodeParams *>(params));
55 if (status != noErr) {
Kári Tristan Helgason86de7e82017-12-01 13:48:48 +010056 RTCVideoDecoderH264 *decoder = (__bridge RTCVideoDecoderH264 *)decoderRef;
57 [decoder setError:status];
Mirko Bonadei675513b2017-11-09 11:09:25 +010058 RTC_LOG(LS_ERROR) << "Failed to decode frame. Status: " << status;
magjed73c0eb52017-08-07 06:55:28 -070059 return;
60 }
61 // TODO(tkchin): Handle CVO properly.
62 RTCCVPixelBuffer *frameBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:imageBuffer];
63 RTCVideoFrame *decodedFrame =
64 [[RTCVideoFrame alloc] initWithBuffer:frameBuffer
65 rotation:RTCVideoRotation_0
66 timeStampNs:CMTimeGetSeconds(timestamp) * rtc::kNumNanosecsPerSec];
67 decodedFrame.timeStamp = decodeParams->timestamp;
68 decodeParams->callback(decodedFrame);
69}
70
71// Decoder.
72@implementation RTCVideoDecoderH264 {
73 CMVideoFormatDescriptionRef _videoFormat;
74 VTDecompressionSessionRef _decompressionSession;
75 RTCVideoDecoderCallback _callback;
Kári Tristan Helgason86de7e82017-12-01 13:48:48 +010076 OSStatus _error;
magjed73c0eb52017-08-07 06:55:28 -070077}
78
andersc9a85f072017-09-13 07:31:46 -070079- (instancetype)init {
80 if (self = [super init]) {
81#if defined(WEBRTC_IOS)
82 [RTCUIApplicationStatusObserver prepareForUse];
Kári Tristan Helgason86de7e82017-12-01 13:48:48 +010083 _error = noErr;
andersc9a85f072017-09-13 07:31:46 -070084#endif
85 }
86
87 return self;
88}
89
magjed73c0eb52017-08-07 06:55:28 -070090- (void)dealloc {
91 [self destroyDecompressionSession];
92 [self setVideoFormat:nullptr];
93}
94
Anders Carlsson2a1bbc32018-04-04 12:49:43 +020095- (NSInteger)startDecodeWithNumberOfCores:(int)numberOfCores {
96 return WEBRTC_VIDEO_CODEC_OK;
97}
98
magjed73c0eb52017-08-07 06:55:28 -070099- (NSInteger)startDecodeWithSettings:(RTCVideoEncoderSettings *)settings
100 numberOfCores:(int)numberOfCores {
101 return WEBRTC_VIDEO_CODEC_OK;
102}
103
Niels Möllerc199fae2018-04-26 09:54:25 +0200104- (NSInteger)decode:(RTCEncodedImage *)encodedImage
magjed73c0eb52017-08-07 06:55:28 -0700105 missingFrames:(BOOL)missingFrames
106 fragmentationHeader:(RTCRtpFragmentationHeader *)fragmentationHeader
Peter Hanspersd9b64cd2018-01-12 16:16:18 +0100107 codecSpecificInfo:(nullable id<RTCCodecSpecificInfo>)info
magjed73c0eb52017-08-07 06:55:28 -0700108 renderTimeMs:(int64_t)renderTimeMs {
Niels Möllerc199fae2018-04-26 09:54:25 +0200109 return [self decode:encodedImage
110 missingFrames:missingFrames
111 codecSpecificInfo:info
112 renderTimeMs:renderTimeMs];
113}
114
115- (NSInteger)decode:(RTCEncodedImage *)inputImage
116 missingFrames:(BOOL)missingFrames
117 codecSpecificInfo:(nullable id<RTCCodecSpecificInfo>)info
118 renderTimeMs:(int64_t)renderTimeMs {
magjed73c0eb52017-08-07 06:55:28 -0700119 RTC_DCHECK(inputImage.buffer);
120
Kári Tristan Helgason86de7e82017-12-01 13:48:48 +0100121 if (_error != noErr) {
122 RTC_LOG(LS_WARNING) << "Last frame decode failed.";
123 _error = noErr;
124 return WEBRTC_VIDEO_CODEC_ERROR;
125 }
126
magjed73c0eb52017-08-07 06:55:28 -0700127#if defined(WEBRTC_IOS)
128 if (![[RTCUIApplicationStatusObserver sharedInstance] isApplicationActive]) {
129 // Ignore all decode requests when app isn't active. In this state, the
130 // hardware decoder has been invalidated by the OS.
131 // Reset video format so that we won't process frames until the next
132 // keyframe.
133 [self setVideoFormat:nullptr];
134 return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
135 }
136#endif
Guy Hershenbaum2fcb8342018-02-20 21:33:36 -0800137 rtc::ScopedCFTypeRef<CMVideoFormatDescriptionRef> inputFormat =
138 rtc::ScopedCF(webrtc::CreateVideoFormatDescription((uint8_t *)inputImage.buffer.bytes,
139 inputImage.buffer.length));
140 if (inputFormat) {
141 // Check if the video format has changed, and reinitialize decoder if
142 // needed.
143 if (!CMFormatDescriptionEqual(inputFormat.get(), _videoFormat)) {
144 [self setVideoFormat:inputFormat.get()];
145 int resetDecompressionSessionError = [self resetDecompressionSession];
146 if (resetDecompressionSessionError != WEBRTC_VIDEO_CODEC_OK) {
147 return resetDecompressionSessionError;
magjed73c0eb52017-08-07 06:55:28 -0700148 }
magjed73c0eb52017-08-07 06:55:28 -0700149 }
150 }
151 if (!_videoFormat) {
152 // We received a frame but we don't have format information so we can't
153 // decode it.
154 // This can happen after backgrounding. We need to wait for the next
155 // sps/pps before we can resume so we request a keyframe by returning an
156 // error.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100157 RTC_LOG(LS_WARNING) << "Missing video format. Frame with sps/pps required.";
magjed73c0eb52017-08-07 06:55:28 -0700158 return WEBRTC_VIDEO_CODEC_ERROR;
159 }
160 CMSampleBufferRef sampleBuffer = nullptr;
161 if (!webrtc::H264AnnexBBufferToCMSampleBuffer((uint8_t *)inputImage.buffer.bytes,
162 inputImage.buffer.length,
163 _videoFormat,
164 &sampleBuffer)) {
165 return WEBRTC_VIDEO_CODEC_ERROR;
166 }
167 RTC_DCHECK(sampleBuffer);
168 VTDecodeFrameFlags decodeFlags = kVTDecodeFrame_EnableAsynchronousDecompression;
169 std::unique_ptr<RTCFrameDecodeParams> frameDecodeParams;
170 frameDecodeParams.reset(new RTCFrameDecodeParams(_callback, inputImage.timeStamp));
171 OSStatus status = VTDecompressionSessionDecodeFrame(
172 _decompressionSession, sampleBuffer, decodeFlags, frameDecodeParams.release(), nullptr);
173#if defined(WEBRTC_IOS)
174 // Re-initialize the decoder if we have an invalid session while the app is
175 // active and retry the decode request.
176 if (status == kVTInvalidSessionErr && [self resetDecompressionSession] == WEBRTC_VIDEO_CODEC_OK) {
177 frameDecodeParams.reset(new RTCFrameDecodeParams(_callback, inputImage.timeStamp));
178 status = VTDecompressionSessionDecodeFrame(
179 _decompressionSession, sampleBuffer, decodeFlags, frameDecodeParams.release(), nullptr);
180 }
181#endif
182 CFRelease(sampleBuffer);
183 if (status != noErr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100184 RTC_LOG(LS_ERROR) << "Failed to decode frame with code: " << status;
magjed73c0eb52017-08-07 06:55:28 -0700185 return WEBRTC_VIDEO_CODEC_ERROR;
186 }
187 return WEBRTC_VIDEO_CODEC_OK;
188}
189
190- (void)setCallback:(RTCVideoDecoderCallback)callback {
191 _callback = callback;
192}
193
Kári Tristan Helgason86de7e82017-12-01 13:48:48 +0100194- (void)setError:(OSStatus)error {
195 _error = error;
196}
197
magjed73c0eb52017-08-07 06:55:28 -0700198- (NSInteger)releaseDecoder {
199 // Need to invalidate the session so that callbacks no longer occur and it
200 // is safe to null out the callback.
201 [self destroyDecompressionSession];
202 [self setVideoFormat:nullptr];
203 _callback = nullptr;
204 return WEBRTC_VIDEO_CODEC_OK;
205}
206
207#pragma mark - Private
208
209- (int)resetDecompressionSession {
210 [self destroyDecompressionSession];
211
212 // Need to wait for the first SPS to initialize decoder.
213 if (!_videoFormat) {
214 return WEBRTC_VIDEO_CODEC_OK;
215 }
216
217 // Set keys for OpenGL and IOSurface compatibilty, which makes the encoder
218 // create pixel buffers with GPU backed memory. The intent here is to pass
219 // the pixel buffers directly so we avoid a texture upload later during
220 // rendering. This currently is moot because we are converting back to an
221 // I420 frame after decode, but eventually we will be able to plumb
222 // CVPixelBuffers directly to the renderer.
223 // TODO(tkchin): Maybe only set OpenGL/IOSurface keys if we know that that
224 // we can pass CVPixelBuffers as native handles in decoder output.
225 static size_t const attributesSize = 3;
226 CFTypeRef keys[attributesSize] = {
227#if defined(WEBRTC_IOS)
228 kCVPixelBufferOpenGLESCompatibilityKey,
229#elif defined(WEBRTC_MAC)
230 kCVPixelBufferOpenGLCompatibilityKey,
231#endif
232 kCVPixelBufferIOSurfacePropertiesKey,
233 kCVPixelBufferPixelFormatTypeKey
234 };
235 CFDictionaryRef ioSurfaceValue = CreateCFTypeDictionary(nullptr, nullptr, 0);
236 int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
237 CFNumberRef pixelFormat = CFNumberCreate(nullptr, kCFNumberLongType, &nv12type);
238 CFTypeRef values[attributesSize] = {kCFBooleanTrue, ioSurfaceValue, pixelFormat};
239 CFDictionaryRef attributes = CreateCFTypeDictionary(keys, values, attributesSize);
240 if (ioSurfaceValue) {
241 CFRelease(ioSurfaceValue);
242 ioSurfaceValue = nullptr;
243 }
244 if (pixelFormat) {
245 CFRelease(pixelFormat);
246 pixelFormat = nullptr;
247 }
248 VTDecompressionOutputCallbackRecord record = {
Kári Tristan Helgasondb6145f2018-02-13 13:58:10 +0100249 decompressionOutputCallback, (__bridge void *)self,
magjed73c0eb52017-08-07 06:55:28 -0700250 };
251 OSStatus status = VTDecompressionSessionCreate(
252 nullptr, _videoFormat, nullptr, attributes, &record, &_decompressionSession);
253 CFRelease(attributes);
254 if (status != noErr) {
Yura Yaroshevich27af5db2018-04-10 19:43:20 +0300255 RTC_LOG(LS_ERROR) << "Failed to create decompression session: " << status;
magjed73c0eb52017-08-07 06:55:28 -0700256 [self destroyDecompressionSession];
257 return WEBRTC_VIDEO_CODEC_ERROR;
258 }
259 [self configureDecompressionSession];
260
261 return WEBRTC_VIDEO_CODEC_OK;
262}
263
264- (void)configureDecompressionSession {
265 RTC_DCHECK(_decompressionSession);
266#if defined(WEBRTC_IOS)
267 VTSessionSetProperty(_decompressionSession, kVTDecompressionPropertyKey_RealTime, kCFBooleanTrue);
268#endif
269}
270
271- (void)destroyDecompressionSession {
272 if (_decompressionSession) {
JT Teha6368d12017-09-28 11:00:39 -0700273#if defined(WEBRTC_IOS)
274 if ([UIDevice isIOS11OrLater]) {
275 VTDecompressionSessionWaitForAsynchronousFrames(_decompressionSession);
276 }
277#endif
magjed73c0eb52017-08-07 06:55:28 -0700278 VTDecompressionSessionInvalidate(_decompressionSession);
279 CFRelease(_decompressionSession);
280 _decompressionSession = nullptr;
281 }
282}
283
284- (void)setVideoFormat:(CMVideoFormatDescriptionRef)videoFormat {
285 if (_videoFormat == videoFormat) {
286 return;
287 }
288 if (_videoFormat) {
289 CFRelease(_videoFormat);
290 }
291 _videoFormat = videoFormat;
292 if (_videoFormat) {
293 CFRetain(_videoFormat);
294 }
295}
296
297- (NSString *)implementationName {
298 return @"VideoToolbox";
299}
300
301@end