diff --git a/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm b/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm index 39620d2012..a0b6d78179 100644 --- a/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm +++ b/sdk/objc/Framework/Classes/VideoToolbox/RTCVideoEncoderH264.mm @@ -488,6 +488,16 @@ CFStringRef ExtractProfile(webrtc::SdpVideoFormat videoFormat) { 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 @@ CFStringRef ExtractProfile(webrtc::SdpVideoFormat videoFormat) { } #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 @@ CFStringRef ExtractProfile(webrtc::SdpVideoFormat videoFormat) { 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;