fix: background stream issues

This commit is contained in:
Acaki
2025-07-12 15:33:14 +08:00
parent 4b8691b943
commit e61e96b352
2 changed files with 13 additions and 14 deletions
+12 -13
View File
@@ -39,10 +39,12 @@ extern int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size,
- (void)reinitializeDisplayLayer
{
CALayer *oldLayer = _displayLayer;
_displayLayer = [[AVSampleBufferDisplayLayer alloc] init];
_displayLayer.backgroundColor = [UIColor blackColor].CGColor;
if (_displayLayer == nil) {
_displayLayer = [[AVSampleBufferDisplayLayer alloc] init];
_displayLayer.backgroundColor = [UIColor blackColor].CGColor;
_displayLayer.videoGravity = AVLayerVideoGravityResize;
[_view.layer addSublayer:_displayLayer];
}
// Ensure the AVSampleBufferDisplayLayer is sized to preserve the aspect ratio
// of the video stream. We used to use AVLayerVideoGravityResizeAspect, but that
@@ -55,22 +57,17 @@ extern int ff_isom_write_av1c(AVIOContext *pb, const uint8_t *buf, int size,
} else {
videoSize = CGSizeMake(_view.bounds.size.width, _view.bounds.size.width / _streamAspectRatio);
}
[CATransaction begin];
[CATransaction setDisableActions:YES];
_displayLayer.position = CGPointMake(CGRectGetMidX(_view.bounds), CGRectGetMidY(_view.bounds));
_displayLayer.bounds = CGRectMake(0, 0, videoSize.width, videoSize.height);
_displayLayer.videoGravity = AVLayerVideoGravityResize;
[CATransaction commit];
// Hide the layer until we get an IDR frame. This ensures we
// can see the loading progress label as the stream is starting.
_displayLayer.hidden = YES;
if (oldLayer != nil) {
// Switch out the old display layer with the new one
[_view.layer replaceSublayer:oldLayer with:_displayLayer];
}
else {
[_view.layer addSublayer:_displayLayer];
}
if (formatDesc != nil) {
CFRelease(formatDesc);
formatDesc = nil;
@@ -525,6 +522,8 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
if (_displayLayer.status == AVQueuedSampleBufferRenderingStatusFailed) {
Log(LOG_E, @"Display layer rendering failed: %@", _displayLayer.error);
[self->_displayLayer flushAndRemoveImage];
// Recreate the display layer. We are already on the main thread,
// so this is safe to do right here.
[self reinitializeDisplayLayer];
@@ -889,7 +889,7 @@
#if !TARGET_OS_TV
// Terminate the stream if the app is inactive for 60 seconds
Log(LOG_I, @"Starting inactivity termination timer");
_inactivityTimer = [NSTimer scheduledTimerWithTimeInterval:60
_inactivityTimer = [NSTimer scheduledTimerWithTimeInterval:1800
target:self
selector:@selector(inactiveTimerExpired:)
userInfo:nil