Fix occassional hang in iOS 11 when calling VTDecompressionSessionInvalidate.

BUG=webrtc:8302

Change-Id: I426116c621c53a0300f87a2a5dc147578b559ed6
Reviewed-on: https://webrtc-review.googlesource.com/4520
Reviewed-by: Zeke Chin <tkchin@webrtc.org>
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Commit-Queue: JT Teh <jtteh@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20026}
diff --git a/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoDecoderH264.mm b/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoDecoderH264.mm
index 6e000ab..4cf48bd 100644
--- a/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoDecoderH264.mm
+++ b/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoDecoderH264.mm
@@ -25,6 +25,7 @@
 
 #if defined(WEBRTC_IOS)
 #import "Common/RTCUIApplicationStatusObserver.h"
+#import "WebRTC/UIDevice+RTCDevice.h"
 #endif
 
 // Struct that we pass to the decoder per frame to decode. We receive it again
@@ -236,6 +237,11 @@
 
 - (void)destroyDecompressionSession {
   if (_decompressionSession) {
+#if defined(WEBRTC_IOS)
+    if ([UIDevice isIOS11OrLater]) {
+      VTDecompressionSessionWaitForAsynchronousFrames(_decompressionSession);
+    }
+#endif
     VTDecompressionSessionInvalidate(_decompressionSession);
     CFRelease(_decompressionSession);
     _decompressionSession = nullptr;