fix: occasional crashes when put in foreground

This commit is contained in:
Acaki committed 2025-08-25 02:05:00 +08:00
1 parent c8f61b0b87
commit 0582655954
2 files changed
+43 -31

No files matched your search

+2
View File
@@ -43,4 +43,6 @@
frameType:(int)frameType frameType:(int)frameType
decodeStartTime:(CFTimeInterval)decodeStartTime; decodeStartTime:(CFTimeInterval)decodeStartTime;
- (void)invalidateDecompressionSession;
@end @end
+41 -31
View File
@@ -98,12 +98,14 @@ extern int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size,
_formatDescImageBuffer = nil; _formatDescImageBuffer = nil;
} }
if (_decompressionSession != nil){ @synchronized(self) {
VTDecompressionSessionWaitForAsynchronousFrames(_decompressionSession); if (_decompressionSession != nil){
VTDecompressionSessionWaitForAsynchronousFrames(_decompressionSession);
VTDecompressionSessionInvalidate(_decompressionSession); VTDecompressionSessionInvalidate(_decompressionSession);
CFRelease(_decompressionSession); CFRelease(_decompressionSession);
_decompressionSession = nil; _decompressionSession = nil;
}
} }
} }
@@ -174,6 +176,7 @@ extern int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size,
- (void)setupDecompressionSessionWithAttributes:(NSDictionary *)destinationPixelBufferAttributes { - (void)setupDecompressionSessionWithAttributes:(NSDictionary *)destinationPixelBufferAttributes {
// This method is called from within synchronized block, so no additional sync needed here
if (_decompressionSession != NULL) { if (_decompressionSession != NULL) {
VTDecompressionSessionInvalidate(_decompressionSession); VTDecompressionSessionInvalidate(_decompressionSession);
CFRelease(_decompressionSession); CFRelease(_decompressionSession);
@@ -367,10 +370,12 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
[_displayLink invalidate]; [_displayLink invalidate];
} }
if (_decompressionSession != NULL) { @synchronized(self) {
VTDecompressionSessionInvalidate(_decompressionSession); if (_decompressionSession != NULL) {
CFRelease(_decompressionSession); VTDecompressionSessionInvalidate(_decompressionSession);
_decompressionSession = nil; CFRelease(_decompressionSession);
_decompressionSession = nil;
}
} }
} }
@@ -845,28 +850,30 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
frameNumber:(int)frameNumber frameNumber:(int)frameNumber
frameType:(int)frameType frameType:(int)frameType
decodeStartTime:(CFTimeInterval)decodeStartTime { decodeStartTime:(CFTimeInterval)decodeStartTime {
// Check if we need to create/recreate the decompression session // Synchronize access to decompression session to prevent race conditions during background/foreground transitions
BOOL needsNewSession = (frameType == FRAME_TYPE_IDR || _decompressionSession == nil); @synchronized(self) {
// Check if we need to create/recreate the decompression session
BOOL needsNewSession = (frameType == FRAME_TYPE_IDR || _decompressionSession == nil);
// Also check if the session might have been invalidated by iOS during background // Also check if the session might have been invalidated by iOS during background
if (!needsNewSession && _decompressionSession != nil) { if (!needsNewSession && _decompressionSession != nil) {
Boolean isValid = VTDecompressionSessionCanAcceptFormatDescription(_decompressionSession, _formatDesc); Boolean isValid = VTDecompressionSessionCanAcceptFormatDescription(_decompressionSession, _formatDesc);
if (!isValid) { if (!isValid) {
Log(LOG_W, @"Decompression session is invalid, needs recreation"); Log(LOG_W, @"Decompression session is invalid, needs recreation");
needsNewSession = YES; needsNewSession = YES;
}
} }
}
if (needsNewSession) { if (needsNewSession) {
[self setupDecompressionSession]; [self setupDecompressionSession];
} }
if (_decompressionSession == nil) { if (_decompressionSession == nil) {
Log(LOG_E, @"Failed to create decompression session"); Log(LOG_E, @"Failed to create decompression session");
return kVTInvalidSessionErr; return kVTInvalidSessionErr;
} }
OSStatus status = VTDecompressionSessionDecodeFrameWithOutputHandler( OSStatus status = VTDecompressionSessionDecodeFrameWithOutputHandler(
_decompressionSession, _decompressionSession,
sampleBuffer, sampleBuffer,
0, 0,
@@ -878,10 +885,12 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
if (status == kVTInvalidSessionErr) { if (status == kVTInvalidSessionErr) {
// The session was invalidated by the OS. Destroy our reference // The session was invalidated by the OS. Destroy our reference
// so it gets recreated on the next IDR frame. // so it gets recreated on the next IDR frame.
if (self->_decompressionSession) { @synchronized(self) {
VTDecompressionSessionInvalidate(self->_decompressionSession); if (self->_decompressionSession) {
CFRelease(self->_decompressionSession); VTDecompressionSessionInvalidate(self->_decompressionSession);
self->_decompressionSession = nil; CFRelease(self->_decompressionSession);
self->_decompressionSession = nil;
}
} }
} }
LiRequestIdrFrame(); // Request an IDR to restart the stream LiRequestIdrFrame(); // Request an IDR to restart the stream
@@ -948,7 +957,8 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
}); });
}); });
return status; return status;
}
} }
- (void)setHdrMode:(BOOL)enabled { - (void)setHdrMode:(BOOL)enabled {