iOS H264 encoder: Make initial compression session respect pixel format.

Bug: webrtc:9150
Change-Id: Ib331391f585c3d94190bb67c38e2d59b22834b25
Reviewed-on: https://webrtc-review.googlesource.com/69812
Reviewed-by: Kári Helgason <kthelgason@webrtc.org>
Commit-Queue: Anders Carlsson <andersc@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#22872}
diff --git a/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm b/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm
index 39620d2..a0b6d78 100644
--- a/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm
+++ b/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm
@@ -488,6 +488,16 @@
                                      withFrame:(RTCVideoFrame *)frame {
   BOOL resetCompressionSession = NO;
 
+  // If we're capturing native frames in another pixel format than the compression session is
+  // configured with, make sure the compression session is reset using the correct pixel format.
+  // If we're capturing non-native frames and the compression session is configured with a non-NV12
+  // format, reset it to NV12.
+  OSType framePixelFormat = kNV12PixelFormat;
+  if ([frame.buffer isKindOfClass:[RTCCVPixelBuffer class]]) {
+    RTCCVPixelBuffer *rtcPixelBuffer = (RTCCVPixelBuffer *)frame.buffer;
+    framePixelFormat = CVPixelBufferGetPixelFormatType(rtcPixelBuffer.pixelBuffer);
+  }
+
 #if defined(WEBRTC_IOS)
   if (!pixelBufferPool) {
     // Kind of a hack. On backgrounding, the compression session seems to get
@@ -500,11 +510,6 @@
   }
 #endif
 
-  // If we're capturing native frames in another pixel format than the compression session is
-  // configured with, make sure the compression session is reset using the correct pixel format.
-  // If we're capturing non-native frames and the compression session is configured with a non-NV12
-  // format, reset it to NV12.
-  OSType framePixelFormat = kNV12PixelFormat;
   if (pixelBufferPool) {
     // The pool attribute `kCVPixelBufferPixelFormatTypeKey` can contain either an array of pixel
     // formats or a single pixel format.
@@ -519,11 +524,6 @@
       compressionSessionPixelFormats = @[ (NSNumber *)pixelFormats ];
     }
 
-    if ([frame.buffer isKindOfClass:[RTCCVPixelBuffer class]]) {
-      RTCCVPixelBuffer *rtcPixelBuffer = (RTCCVPixelBuffer *)frame.buffer;
-      framePixelFormat = CVPixelBufferGetPixelFormatType(rtcPixelBuffer.pixelBuffer);
-    }
-
     if (![compressionSessionPixelFormats
             containsObject:[NSNumber numberWithLong:framePixelFormat]]) {
       resetCompressionSession = YES;