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
+12

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
+10
View File
@@ -98,6 +98,7 @@ extern int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size,
_formatDescImageBuffer = nil; _formatDescImageBuffer = nil;
} }
@synchronized(self) {
if (_decompressionSession != nil){ if (_decompressionSession != nil){
VTDecompressionSessionWaitForAsynchronousFrames(_decompressionSession); VTDecompressionSessionWaitForAsynchronousFrames(_decompressionSession);
@@ -106,6 +107,7 @@ extern int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size,
_decompressionSession = nil; _decompressionSession = nil;
} }
} }
}
- (id)initWithView:(UIView* )view callbacks:(id<ConnectionCallbacks>)callbacks streamAspectRatio:(float)aspectRatio - (id)initWithView:(UIView* )view callbacks:(id<ConnectionCallbacks>)callbacks streamAspectRatio:(float)aspectRatio
{ {
@@ -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,12 +370,14 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
[_displayLink invalidate]; [_displayLink invalidate];
} }
@synchronized(self) {
if (_decompressionSession != NULL) { if (_decompressionSession != NULL) {
VTDecompressionSessionInvalidate(_decompressionSession); VTDecompressionSessionInvalidate(_decompressionSession);
CFRelease(_decompressionSession); CFRelease(_decompressionSession);
_decompressionSession = nil; _decompressionSession = nil;
} }
} }
}
#define NALU_START_PREFIX_SIZE 3 #define NALU_START_PREFIX_SIZE 3
#define NAL_LENGTH_PREFIX_SIZE 4 #define NAL_LENGTH_PREFIX_SIZE 4
@@ -845,6 +850,8 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
frameNumber:(int)frameNumber frameNumber:(int)frameNumber
frameType:(int)frameType frameType:(int)frameType
decodeStartTime:(CFTimeInterval)decodeStartTime { decodeStartTime:(CFTimeInterval)decodeStartTime {
// Synchronize access to decompression session to prevent race conditions during background/foreground transitions
@synchronized(self) {
// Check if we need to create/recreate the decompression session // Check if we need to create/recreate the decompression session
BOOL needsNewSession = (frameType == FRAME_TYPE_IDR || _decompressionSession == nil); BOOL needsNewSession = (frameType == FRAME_TYPE_IDR || _decompressionSession == nil);
@@ -878,12 +885,14 @@ 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.
@synchronized(self) {
if (self->_decompressionSession) { if (self->_decompressionSession) {
VTDecompressionSessionInvalidate(self->_decompressionSession); VTDecompressionSessionInvalidate(self->_decompressionSession);
CFRelease(self->_decompressionSession); CFRelease(self->_decompressionSession);
self->_decompressionSession = nil; self->_decompressionSession = nil;
} }
} }
}
LiRequestIdrFrame(); // Request an IDR to restart the stream LiRequestIdrFrame(); // Request an IDR to restart the stream
return; return;
} }
@@ -950,6 +959,7 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
return status; return status;
} }
}
- (void)setHdrMode:(BOOL)enabled { - (void)setHdrMode:(BOOL)enabled {
SS_HDR_METADATA hdrMetadata; SS_HDR_METADATA hdrMetadata;