blob: 261b945386427007824b8f1a12ff7a9c4739a013 [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"
25
26#if defined(WEBRTC_IOS)
27#import "Common/RTCUIApplicationStatusObserver.h"
JT Teha6368d12017-09-28 11:00:39 -070028#import "WebRTC/UIDevice+RTCDevice.h"
magjed73c0eb52017-08-07 06:55:28 -070029#endif
30
31// Struct that we pass to the decoder per frame to decode. We receive it again
32// in the decoder callback.
33struct RTCFrameDecodeParams {
34 RTCFrameDecodeParams(RTCVideoDecoderCallback cb, int64_t ts) : callback(cb), timestamp(ts) {}
35 RTCVideoDecoderCallback callback;
36 int64_t timestamp;
37};
38
39// This is the callback function that VideoToolbox calls when decode is
40// complete.
41void decompressionOutputCallback(void *decoder,
42 void *params,
43 OSStatus status,
44 VTDecodeInfoFlags infoFlags,
45 CVImageBufferRef imageBuffer,
46 CMTime timestamp,
47 CMTime duration) {
48 std::unique_ptr<RTCFrameDecodeParams> decodeParams(
49 reinterpret_cast<RTCFrameDecodeParams *>(params));
50 if (status != noErr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010051 RTC_LOG(LS_ERROR) << "Failed to decode frame. Status: " << status;
magjed73c0eb52017-08-07 06:55:28 -070052 return;
53 }
54 // TODO(tkchin): Handle CVO properly.
55 RTCCVPixelBuffer *frameBuffer = [[RTCCVPixelBuffer alloc] initWithPixelBuffer:imageBuffer];
56 RTCVideoFrame *decodedFrame =
57 [[RTCVideoFrame alloc] initWithBuffer:frameBuffer
58 rotation:RTCVideoRotation_0
59 timeStampNs:CMTimeGetSeconds(timestamp) * rtc::kNumNanosecsPerSec];
60 decodedFrame.timeStamp = decodeParams->timestamp;
61 decodeParams->callback(decodedFrame);
62}
63
64// Decoder.
65@implementation RTCVideoDecoderH264 {
66 CMVideoFormatDescriptionRef _videoFormat;
67 VTDecompressionSessionRef _decompressionSession;
68 RTCVideoDecoderCallback _callback;
69}
70
andersc9a85f072017-09-13 07:31:46 -070071- (instancetype)init {
72 if (self = [super init]) {
73#if defined(WEBRTC_IOS)
74 [RTCUIApplicationStatusObserver prepareForUse];
75#endif
76 }
77
78 return self;
79}
80
magjed73c0eb52017-08-07 06:55:28 -070081- (void)dealloc {
82 [self destroyDecompressionSession];
83 [self setVideoFormat:nullptr];
84}
85
86- (NSInteger)startDecodeWithSettings:(RTCVideoEncoderSettings *)settings
87 numberOfCores:(int)numberOfCores {
88 return WEBRTC_VIDEO_CODEC_OK;
89}
90
91- (NSInteger)decode:(RTCEncodedImage *)inputImage
92 missingFrames:(BOOL)missingFrames
93 fragmentationHeader:(RTCRtpFragmentationHeader *)fragmentationHeader
94 codecSpecificInfo:(__nullable id<RTCCodecSpecificInfo>)info
95 renderTimeMs:(int64_t)renderTimeMs {
96 RTC_DCHECK(inputImage.buffer);
97
98#if defined(WEBRTC_IOS)
99 if (![[RTCUIApplicationStatusObserver sharedInstance] isApplicationActive]) {
100 // Ignore all decode requests when app isn't active. In this state, the
101 // hardware decoder has been invalidated by the OS.
102 // Reset video format so that we won't process frames until the next
103 // keyframe.
104 [self setVideoFormat:nullptr];
105 return WEBRTC_VIDEO_CODEC_NO_OUTPUT;
106 }
107#endif
108 CMVideoFormatDescriptionRef inputFormat = nullptr;
109 if (webrtc::H264AnnexBBufferHasVideoFormatDescription((uint8_t *)inputImage.buffer.bytes,
110 inputImage.buffer.length)) {
111 inputFormat = webrtc::CreateVideoFormatDescription((uint8_t *)inputImage.buffer.bytes,
112 inputImage.buffer.length);
113 if (inputFormat) {
114 // Check if the video format has changed, and reinitialize decoder if
115 // needed.
116 if (!CMFormatDescriptionEqual(inputFormat, _videoFormat)) {
117 [self setVideoFormat:inputFormat];
118 [self resetDecompressionSession];
119 }
120 CFRelease(inputFormat);
121 }
122 }
123 if (!_videoFormat) {
124 // We received a frame but we don't have format information so we can't
125 // decode it.
126 // This can happen after backgrounding. We need to wait for the next
127 // sps/pps before we can resume so we request a keyframe by returning an
128 // error.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100129 RTC_LOG(LS_WARNING) << "Missing video format. Frame with sps/pps required.";
magjed73c0eb52017-08-07 06:55:28 -0700130 return WEBRTC_VIDEO_CODEC_ERROR;
131 }
132 CMSampleBufferRef sampleBuffer = nullptr;
133 if (!webrtc::H264AnnexBBufferToCMSampleBuffer((uint8_t *)inputImage.buffer.bytes,
134 inputImage.buffer.length,
135 _videoFormat,
136 &sampleBuffer)) {
137 return WEBRTC_VIDEO_CODEC_ERROR;
138 }
139 RTC_DCHECK(sampleBuffer);
140 VTDecodeFrameFlags decodeFlags = kVTDecodeFrame_EnableAsynchronousDecompression;
141 std::unique_ptr<RTCFrameDecodeParams> frameDecodeParams;
142 frameDecodeParams.reset(new RTCFrameDecodeParams(_callback, inputImage.timeStamp));
143 OSStatus status = VTDecompressionSessionDecodeFrame(
144 _decompressionSession, sampleBuffer, decodeFlags, frameDecodeParams.release(), nullptr);
145#if defined(WEBRTC_IOS)
146 // Re-initialize the decoder if we have an invalid session while the app is
147 // active and retry the decode request.
148 if (status == kVTInvalidSessionErr && [self resetDecompressionSession] == WEBRTC_VIDEO_CODEC_OK) {
149 frameDecodeParams.reset(new RTCFrameDecodeParams(_callback, inputImage.timeStamp));
150 status = VTDecompressionSessionDecodeFrame(
151 _decompressionSession, sampleBuffer, decodeFlags, frameDecodeParams.release(), nullptr);
152 }
153#endif
154 CFRelease(sampleBuffer);
155 if (status != noErr) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100156 RTC_LOG(LS_ERROR) << "Failed to decode frame with code: " << status;
magjed73c0eb52017-08-07 06:55:28 -0700157 return WEBRTC_VIDEO_CODEC_ERROR;
158 }
159 return WEBRTC_VIDEO_CODEC_OK;
160}
161
162- (void)setCallback:(RTCVideoDecoderCallback)callback {
163 _callback = callback;
164}
165
166- (NSInteger)releaseDecoder {
167 // Need to invalidate the session so that callbacks no longer occur and it
168 // is safe to null out the callback.
169 [self destroyDecompressionSession];
170 [self setVideoFormat:nullptr];
171 _callback = nullptr;
172 return WEBRTC_VIDEO_CODEC_OK;
173}
174
175#pragma mark - Private
176
177- (int)resetDecompressionSession {
178 [self destroyDecompressionSession];
179
180 // Need to wait for the first SPS to initialize decoder.
181 if (!_videoFormat) {
182 return WEBRTC_VIDEO_CODEC_OK;
183 }
184
185 // Set keys for OpenGL and IOSurface compatibilty, which makes the encoder
186 // create pixel buffers with GPU backed memory. The intent here is to pass
187 // the pixel buffers directly so we avoid a texture upload later during
188 // rendering. This currently is moot because we are converting back to an
189 // I420 frame after decode, but eventually we will be able to plumb
190 // CVPixelBuffers directly to the renderer.
191 // TODO(tkchin): Maybe only set OpenGL/IOSurface keys if we know that that
192 // we can pass CVPixelBuffers as native handles in decoder output.
193 static size_t const attributesSize = 3;
194 CFTypeRef keys[attributesSize] = {
195#if defined(WEBRTC_IOS)
196 kCVPixelBufferOpenGLESCompatibilityKey,
197#elif defined(WEBRTC_MAC)
198 kCVPixelBufferOpenGLCompatibilityKey,
199#endif
200 kCVPixelBufferIOSurfacePropertiesKey,
201 kCVPixelBufferPixelFormatTypeKey
202 };
203 CFDictionaryRef ioSurfaceValue = CreateCFTypeDictionary(nullptr, nullptr, 0);
204 int64_t nv12type = kCVPixelFormatType_420YpCbCr8BiPlanarFullRange;
205 CFNumberRef pixelFormat = CFNumberCreate(nullptr, kCFNumberLongType, &nv12type);
206 CFTypeRef values[attributesSize] = {kCFBooleanTrue, ioSurfaceValue, pixelFormat};
207 CFDictionaryRef attributes = CreateCFTypeDictionary(keys, values, attributesSize);
208 if (ioSurfaceValue) {
209 CFRelease(ioSurfaceValue);
210 ioSurfaceValue = nullptr;
211 }
212 if (pixelFormat) {
213 CFRelease(pixelFormat);
214 pixelFormat = nullptr;
215 }
216 VTDecompressionOutputCallbackRecord record = {
217 decompressionOutputCallback, nullptr,
218 };
219 OSStatus status = VTDecompressionSessionCreate(
220 nullptr, _videoFormat, nullptr, attributes, &record, &_decompressionSession);
221 CFRelease(attributes);
222 if (status != noErr) {
223 [self destroyDecompressionSession];
224 return WEBRTC_VIDEO_CODEC_ERROR;
225 }
226 [self configureDecompressionSession];
227
228 return WEBRTC_VIDEO_CODEC_OK;
229}
230
231- (void)configureDecompressionSession {
232 RTC_DCHECK(_decompressionSession);
233#if defined(WEBRTC_IOS)
234 VTSessionSetProperty(_decompressionSession, kVTDecompressionPropertyKey_RealTime, kCFBooleanTrue);
235#endif
236}
237
238- (void)destroyDecompressionSession {
239 if (_decompressionSession) {
JT Teha6368d12017-09-28 11:00:39 -0700240#if defined(WEBRTC_IOS)
241 if ([UIDevice isIOS11OrLater]) {
242 VTDecompressionSessionWaitForAsynchronousFrames(_decompressionSession);
243 }
244#endif
magjed73c0eb52017-08-07 06:55:28 -0700245 VTDecompressionSessionInvalidate(_decompressionSession);
246 CFRelease(_decompressionSession);
247 _decompressionSession = nullptr;
248 }
249}
250
251- (void)setVideoFormat:(CMVideoFormatDescriptionRef)videoFormat {
252 if (_videoFormat == videoFormat) {
253 return;
254 }
255 if (_videoFormat) {
256 CFRelease(_videoFormat);
257 }
258 _videoFormat = videoFormat;
259 if (_videoFormat) {
260 CFRetain(_videoFormat);
261 }
262}
263
264- (NSString *)implementationName {
265 return @"VideoToolbox";
266}
267
268@end