Add a way for RTCVideoDecoderH264 to signal decode errors.

This means we will properly request a new keyframe if decoding fails.

Bug: webrtc:8600
Change-Id: Id213686f016c5418bf04b2ee68bd19dbbe1ea954
Reviewed-on: https://webrtc-review.googlesource.com/28101
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Commit-Queue: Kári Helgason <kthelgason@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21036}
diff --git a/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoDecoderH264.mm b/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoDecoderH264.mm
index 261b945..debd504 100644
--- a/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoDecoderH264.mm
+++ b/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoDecoderH264.mm
@@ -36,9 +36,13 @@
   int64_t timestamp;
 };
 
+@interface RTCVideoDecoderH264 ()
+- (void)setError:(OSStatus)error;
+@end
+
 // This is the callback function that VideoToolbox calls when decode is
 // complete.
-void decompressionOutputCallback(void *decoder,
+void decompressionOutputCallback(void *decoderRef,
                                  void *params,
                                  OSStatus status,
                                  VTDecodeInfoFlags infoFlags,
@@ -48,6 +52,8 @@
   std::unique_ptr<RTCFrameDecodeParams> decodeParams(
       reinterpret_cast<RTCFrameDecodeParams *>(params));
   if (status != noErr) {
+    RTCVideoDecoderH264 *decoder = (__bridge RTCVideoDecoderH264 *)decoderRef;
+    [decoder setError:status];
     RTC_LOG(LS_ERROR) << "Failed to decode frame. Status: " << status;
     return;
   }
@@ -66,12 +72,14 @@
   CMVideoFormatDescriptionRef _videoFormat;
   VTDecompressionSessionRef _decompressionSession;
   RTCVideoDecoderCallback _callback;
+  OSStatus _error;
 }
 
 - (instancetype)init {
   if (self = [super init]) {
 #if defined(WEBRTC_IOS)
     [RTCUIApplicationStatusObserver prepareForUse];
+    _error = noErr;
 #endif
   }
 
@@ -95,6 +103,12 @@
            renderTimeMs:(int64_t)renderTimeMs {
   RTC_DCHECK(inputImage.buffer);
 
+  if (_error != noErr) {
+    RTC_LOG(LS_WARNING) << "Last frame decode failed.";
+    _error = noErr;
+    return WEBRTC_VIDEO_CODEC_ERROR;
+  }
+
 #if defined(WEBRTC_IOS)
   if (![[RTCUIApplicationStatusObserver sharedInstance] isApplicationActive]) {
     // Ignore all decode requests when app isn't active. In this state, the
@@ -163,6 +177,10 @@
   _callback = callback;
 }
 
+- (void)setError:(OSStatus)error {
+  _error = error;
+}
+
 - (NSInteger)releaseDecoder {
   // Need to invalidate the session so that callbacks no longer occur and it
   // is safe to null out the callback.