fix: stream freeze when turning screen off and on with AVSB

This commit is contained in:
Acaki committed 2025-07-16 02:15:28 +08:00
1 parent bd9e2e3215
commit 5df330f608
1 file changed
+189 -151
+189 -151
View File
@@ -297,7 +297,7 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
- (void)renderFrame:(Frame *)frame atTime:(CMTime)targetTime { - (void)renderFrame:(Frame *)frame atTime:(CMTime)targetTime {
CMSampleBufferSetOutputPresentationTimeStamp(frame.sampleBuffer, targetTime); CMSampleBufferSetOutputPresentationTimeStamp(frame.sampleBuffer, targetTime);
if (frame.frameNumber == 1) { if ([self->_displayLayer controlTimebase] == NULL) {
// On first frame, set timebase to the initial presentation time. // On first frame, set timebase to the initial presentation time.
// This will let us present frames using the local clock (vsync pacing) or // This will let us present frames using the local clock (vsync pacing) or
// the pts timestamps from the host. // the pts timestamps from the host.
@@ -310,6 +310,9 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
CMTimebaseSetRate(timebase, 1.0); CMTimebaseSetRate(timebase, 1.0);
[self->_displayLayer setControlTimebase:timebase]; [self->_displayLayer setControlTimebase:timebase];
if (timebase) {
CFRelease(timebase);
}
Log(LOG_I, @"Setting timebase for stream to %d / %d", pts.value, pts.timescale); Log(LOG_I, @"Setting timebase for stream to %d / %d", pts.value, pts.timescale);
} }
@@ -821,162 +824,184 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
frameNumber:(int)frameNumber frameNumber:(int)frameNumber
frameType:(int)frameType frameType:(int)frameType
decodeStartTime:(CFTimeInterval)decodeStartTime { decodeStartTime:(CFTimeInterval)decodeStartTime {
if (frameType == FRAME_TYPE_IDR || _decompressionSession == nil) { @synchronized(self) {
[self setupDecompressionSession]; // If the decompression session is nil, we must decide what to do.
} if (_decompressionSession == nil) {
// We can only create a session with an IDR frame.
if (frameType == FRAME_TYPE_IDR && !_hasSentVideoContentShown) { if (frameType == FRAME_TYPE_IDR) {
// Tell our parent VC to hide the progress indicator and set up PiP // This is the startup path. Create the session now.
[_callbacks videoContentShown]; [self setupDecompressionSession];
_hasSentVideoContentShown = YES;
}
OSStatus status = VTDecompressionSessionDecodeFrameWithOutputHandler( // If session creation still fails, we cannot proceed.
_decompressionSession, if (_decompressionSession == nil) {
sampleBuffer, Log(LOG_E, @"Failed to create initial decompression session with IDR frame.");
0, return kVTInvalidSessionErr;
NULL,
^(OSStatus status, VTDecodeInfoFlags infoFlags, CVImageBufferRef _Nullable imageBuffer, CMTime presentationTimestamp, CMTime presentationDuration) {
if (status != noErr || !imageBuffer) {
NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
Log(LOG_E, @"Decompression session error: %@", error);
if (status == kVTInvalidSessionErr) {
// The session was invalidated by the OS. Destroy our reference
// so it gets recreated on the next IDR frame.
if (self->_decompressionSession) {
VTDecompressionSessionInvalidate(self->_decompressionSession);
CFRelease(self->_decompressionSession);
self->_decompressionSession = nil;
} }
} else {
// This happens when resuming from background; we've reset the session
// and are waiting for a new IDR frame. Drop all other frames until it arrives.
return noErr;
} }
LiRequestIdrFrame(); // Request an IDR to restart the stream }
return; // Also re-create the session if a new IDR frame arrives during an active stream,
} // as it may contain updated video parameters (e.g., HDR toggled).
else if (frameType == FRAME_TYPE_IDR) {
if (self.pipLayer) { [self setupDecompressionSession];
CVPixelBufferRetain(imageBuffer); }
dispatch_async(dispatch_get_main_queue(), ^{
@synchronized(self) { if (frameType == FRAME_TYPE_IDR && !_hasSentVideoContentShown) {
if (self.pipLayer.controlTimebase == NULL) { // Tell our parent VC to hide the progress indicator and set up PiP
NSLog(@"[Metal PiP Path] Timebase is NULL. Setting it now."); [_callbacks videoContentShown];
CMTimebaseRef timebase; _hasSentVideoContentShown = YES;
CMTimebaseCreateWithSourceClock(kCFAllocatorDefault, CMClockGetHostTimeClock(), &timebase); }
if (timebase) {
CMTimebaseSetTime(timebase, presentationTimestamp); OSStatus status = VTDecompressionSessionDecodeFrameWithOutputHandler(
CMTimebaseSetRate(timebase, 1.0); _decompressionSession,
[self.pipLayer setControlTimebase:timebase]; sampleBuffer,
CFRelease(timebase); 0,
} NULL,
} ^(OSStatus status, VTDecodeInfoFlags infoFlags, CVImageBufferRef _Nullable imageBuffer, CMTime presentationTimestamp, CMTime presentationDuration) {
if (status != noErr || !imageBuffer) {
if (self->_pipFormatDesc == NULL || !CMVideoFormatDescriptionMatchesImageBuffer(self->_pipFormatDesc, imageBuffer)) { NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
NSLog(@"[Metal PiP Path] Format description mismatch or NULL. Creating new one."); Log(LOG_E, @"Decompression session error: %@", error);
if (self->_pipFormatDesc) { CFRelease(self->_pipFormatDesc); } if (status == kVTInvalidSessionErr) {
OSStatus res = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, imageBuffer, &(self->_pipFormatDesc)); // The session was invalidated by the OS. Destroy our reference
if (res == noErr) { // so it gets recreated on the next IDR frame.
NSLog(@"[Metal PiP Path] New format description created: %@", self->_pipFormatDesc); if (self->_decompressionSession) {
} else { VTDecompressionSessionInvalidate(self->_decompressionSession);
NSLog(@"[Metal PiP Path] FAILED to create format description. Error: %d", (int)res); CFRelease(self->_decompressionSession);
} self->_decompressionSession = nil;
} }
}
if (self->_pipFormatDesc) { LiRequestIdrFrame(); // Request an IDR to restart the stream
CMSampleBufferRef sampleBufferForPip = NULL; return;
CMSampleTimingInfo sampleTiming = {kCMTimeInvalid, presentationTimestamp, presentationDuration};
OSStatus err = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, imageBuffer, self->_pipFormatDesc, &sampleTiming, &sampleBufferForPip);
if (err == noErr && sampleBufferForPip) {
[self.pipLayer enqueueSampleBuffer:sampleBufferForPip];
CFRelease(sampleBufferForPip);
} else {
NSLog(@"[Metal PiP Path] FAILED to create sample buffer. Error: %d", (int)err);
}
}
}
CVPixelBufferRelease(imageBuffer);
});
}
// --- MAIN RENDERER PATH ---
if (self->_renderingBackend == RENDER_AVSB) {
self.pipLayer.hidden = NO;
if (self->_avsbFormatDesc == NULL || !CMVideoFormatDescriptionMatchesImageBuffer(self->_avsbFormatDesc, imageBuffer)) {
if (self->_avsbFormatDesc) {
CFRelease(self->_avsbFormatDesc);
}
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, imageBuffer, &(self->_avsbFormatDesc));
} }
CMSampleBufferRef sampleBufferForAvsb = NULL; if (self.pipLayer) {
CMSampleTimingInfo sampleTiming = {kCMTimeInvalid, presentationTimestamp, presentationDuration}; CVPixelBufferRetain(imageBuffer);
OSStatus err = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, imageBuffer, self->_avsbFormatDesc, &sampleTiming, &sampleBufferForAvsb); dispatch_async(dispatch_get_main_queue(), ^{
@synchronized(self) {
if (err == noErr && sampleBufferForAvsb) { if (self.pipLayer.controlTimebase == NULL) {
NSLog(@"[Metal PiP Path] Timebase is NULL. Setting it now.");
CMTimebaseRef timebase;
CMTimebaseCreateWithSourceClock(kCFAllocatorDefault, CMClockGetHostTimeClock(), &timebase);
if (timebase) {
CMTimebaseSetTime(timebase, presentationTimestamp);
CMTimebaseSetRate(timebase, 1.0);
[self.pipLayer setControlTimebase:timebase];
CFRelease(timebase);
}
}
if (self->_pipFormatDesc == NULL || !CMVideoFormatDescriptionMatchesImageBuffer(self->_pipFormatDesc, imageBuffer)) {
NSLog(@"[Metal PiP Path] Format description mismatch or NULL. Creating new one.");
if (self->_pipFormatDesc) { CFRelease(self->_pipFormatDesc); }
OSStatus res = CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, imageBuffer, &(self->_pipFormatDesc));
if (res == noErr) {
NSLog(@"[Metal PiP Path] New format description created: %@", self->_pipFormatDesc);
} else {
NSLog(@"[Metal PiP Path] FAILED to create format description. Error: %d", (int)res);
}
}
if (self->_pipFormatDesc) {
CMSampleBufferRef sampleBufferForPip = NULL;
CMSampleTimingInfo sampleTiming = {kCMTimeInvalid, presentationTimestamp, presentationDuration};
OSStatus err = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, imageBuffer, self->_pipFormatDesc, &sampleTiming, &sampleBufferForPip);
if (err == noErr && sampleBufferForPip) {
[self.pipLayer enqueueSampleBuffer:sampleBufferForPip];
CFRelease(sampleBufferForPip);
} else {
NSLog(@"[Metal PiP Path] FAILED to create sample buffer. Error: %d", (int)err);
}
}
}
CVPixelBufferRelease(imageBuffer);
});
}
// --- MAIN RENDERER PATH ---
if (self->_renderingBackend == RENDER_AVSB) {
self.pipLayer.hidden = NO;
if (self->_avsbFormatDesc == NULL || !CMVideoFormatDescriptionMatchesImageBuffer(self->_avsbFormatDesc, imageBuffer)) {
if (self->_avsbFormatDesc) {
CFRelease(self->_avsbFormatDesc);
}
CMVideoFormatDescriptionCreateForImageBuffer(kCFAllocatorDefault, imageBuffer, &(self->_avsbFormatDesc));
}
CMSampleBufferRef sampleBufferForAvsb = NULL;
CMSampleTimingInfo sampleTiming = {kCMTimeInvalid, presentationTimestamp, presentationDuration};
OSStatus err = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, imageBuffer, self->_avsbFormatDesc, &sampleTiming, &sampleBufferForAvsb);
if (err == noErr && sampleBufferForAvsb) {
dispatch_async(self->_vtq, ^{
Frame *frame = [[Frame alloc] initWithSampleBuffer:sampleBufferForAvsb frameNumber:frameNumber frameType:frameType];
int framesDropped = [self->_frameQueue enqueue:frame withSlackSize:3];
static PlotMetrics frameQueueMetrics = {};
[[ImGuiPlots sharedInstance] observeFloatReturnMetrics:PLOT_QUEUED_FRAMES value:[self->_frameQueue count] plotMetrics:&frameQueueMetrics];
[self safeCopyMetricsTo:&self->_frameQueueMetrics from:&frameQueueMetrics];
[[ImGuiPlots sharedInstance] observeFloat:PLOT_DROPPED value:framesDropped];
static CFTimeInterval lastHostFrame = 0.0f;
if (lastHostFrame != 0) {
[[ImGuiPlots sharedInstance] observeFloat:PLOT_HOST_FRAMETIME value:(frame.pts - lastHostFrame) * 1000.0];
}
lastHostFrame = frame.pts;
static PlotMetrics decodeMetrics = {};
[[ImGuiPlots sharedInstance] observeFloatReturnMetrics:PLOT_DECODE value:(CACurrentMediaTime() - decodeStartTime) * 1000.0 plotMetrics:&decodeMetrics];
[self safeCopyMetricsTo:&self->_decodeMetrics from:&decodeMetrics];
});
} else {
if (sampleBufferForAvsb) {
CFRelease(sampleBufferForAvsb);
}
}
} else if (self->_renderingBackend == RENDER_METAL) {
self.pipLayer.hidden = NO;
// Safely capture the format description for the background block.
__block CMVideoFormatDescriptionRef capturedFormatDesc = NULL;
@synchronized(self) {
if (self->_formatDesc) {
capturedFormatDesc = (CMVideoFormatDescriptionRef)CFRetain(self->_formatDesc);
}
}
if (!capturedFormatDesc) {
Log(LOG_E, @"Failed to capture format description for Metal frame, dropping.");
return;
}
CVPixelBufferRef pixelBuffer = CVPixelBufferRetain((CVPixelBufferRef)imageBuffer);
dispatch_async(self->_vtq, ^{ dispatch_async(self->_vtq, ^{
Frame *frame = [[Frame alloc] initWithSampleBuffer:sampleBufferForAvsb frameNumber:frameNumber frameType:frameType]; Frame *frame = [[Frame alloc] initWithPixelBufffer:pixelBuffer frameNumber:frameNumber frameType:frameType pts:presentationTimestamp];
[frame setFormatDesc:capturedFormatDesc];
CFRelease(capturedFormatDesc);
int framesDropped = [self->_frameQueue enqueue:frame withSlackSize:3]; int framesDropped = [self->_frameQueue enqueue:frame withSlackSize:3];
static PlotMetrics frameQueueMetrics = {}; static PlotMetrics frameQueueMetrics = {};
[[ImGuiPlots sharedInstance] observeFloatReturnMetrics:PLOT_QUEUED_FRAMES value:[self->_frameQueue count] plotMetrics:&frameQueueMetrics]; [[ImGuiPlots sharedInstance] observeFloatReturnMetrics:PLOT_QUEUED_FRAMES value:[self->_frameQueue count] plotMetrics:&frameQueueMetrics];
[self safeCopyMetricsTo:&self->_frameQueueMetrics from:&frameQueueMetrics]; [self safeCopyMetricsTo:&self->_frameQueueMetrics from:&frameQueueMetrics];
[[ImGuiPlots sharedInstance] observeFloat:PLOT_DROPPED value:framesDropped]; [[ImGuiPlots sharedInstance] observeFloat:PLOT_DROPPED value:framesDropped];
static CFTimeInterval lastHostFrame = 0.0f; static CFTimeInterval lastHostFrame = 0.0f;
if (lastHostFrame != 0) { if (lastHostFrame != 0) {
[[ImGuiPlots sharedInstance] observeFloat:PLOT_HOST_FRAMETIME value:(frame.pts - lastHostFrame) * 1000.0]; [[ImGuiPlots sharedInstance] observeFloat:PLOT_HOST_FRAMETIME value:(frame.pts - lastHostFrame) * 1000.0];
} }
lastHostFrame = frame.pts; lastHostFrame = frame.pts;
static PlotMetrics decodeMetrics = {}; static PlotMetrics decodeMetrics = {};
[[ImGuiPlots sharedInstance] observeFloatReturnMetrics:PLOT_DECODE value:(CACurrentMediaTime() - decodeStartTime) * 1000.0 plotMetrics:&decodeMetrics]; [[ImGuiPlots sharedInstance] observeFloatReturnMetrics:PLOT_DECODE value:(CACurrentMediaTime() - decodeStartTime) * 1000.0 plotMetrics:&decodeMetrics];
[self safeCopyMetricsTo:&self->_decodeMetrics from:&decodeMetrics]; [self safeCopyMetricsTo:&self->_decodeMetrics from:&decodeMetrics];
}); });
} else {
if (sampleBufferForAvsb) {
CFRelease(sampleBufferForAvsb);
}
} }
} else if (self->_renderingBackend == RENDER_METAL) { });
self.pipLayer.hidden = NO;
// Safely capture the format description for the background block.
__block CMVideoFormatDescriptionRef capturedFormatDesc = NULL;
@synchronized(self) {
if (self->_formatDesc) {
capturedFormatDesc = (CMVideoFormatDescriptionRef)CFRetain(self->_formatDesc);
}
}
if (!capturedFormatDesc) {
Log(LOG_E, @"Failed to capture format description for Metal frame, dropping.");
return;
}
CVPixelBufferRef pixelBuffer = CVPixelBufferRetain((CVPixelBufferRef)imageBuffer);
dispatch_async(self->_vtq, ^{
Frame *frame = [[Frame alloc] initWithPixelBufffer:pixelBuffer frameNumber:frameNumber frameType:frameType pts:presentationTimestamp];
[frame setFormatDesc:capturedFormatDesc];
CFRelease(capturedFormatDesc);
int framesDropped = [self->_frameQueue enqueue:frame withSlackSize:3];
static PlotMetrics frameQueueMetrics = {};
[[ImGuiPlots sharedInstance] observeFloatReturnMetrics:PLOT_QUEUED_FRAMES value:[self->_frameQueue count] plotMetrics:&frameQueueMetrics];
[self safeCopyMetricsTo:&self->_frameQueueMetrics from:&frameQueueMetrics];
[[ImGuiPlots sharedInstance] observeFloat:PLOT_DROPPED value:framesDropped];
static CFTimeInterval lastHostFrame = 0.0f;
if (lastHostFrame != 0) {
[[ImGuiPlots sharedInstance] observeFloat:PLOT_HOST_FRAMETIME value:(frame.pts - lastHostFrame) * 1000.0];
}
lastHostFrame = frame.pts;
static PlotMetrics decodeMetrics = {};
[[ImGuiPlots sharedInstance] observeFloatReturnMetrics:PLOT_DECODE value:(CACurrentMediaTime() - decodeStartTime) * 1000.0 plotMetrics:&decodeMetrics];
[self safeCopyMetricsTo:&self->_decodeMetrics from:&decodeMetrics];
});
}
});
return status; return status;
}
} }
- (void)setHdrMode:(BOOL)enabled { - (void)setHdrMode:(BOOL)enabled {
@@ -1130,25 +1155,38 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
- (void)resetFramePacing { - (void)resetFramePacing {
// Ensure this only runs for the AVSampleBuffer rendering backend and that the display link exists. // Ensure this only runs for the AVSampleBuffer rendering backend and that the display link exists.
if (_renderingBackend == RENDER_AVSB && _displayLink) { if (_renderingBackend == RENDER_AVSB) {
Log(LOG_I, @"Frame pacing is being reset to %d FPS...", self->_frameRate); Log(LOG_I, @"Resetting video pipeline for AVSB renderer after resuming...");
// Toggling the paused state can help re-engage the display link with the // 1. Reset the receiver layers on the main thread
// run loop correctly after the app resumes from a background state like PiP. dispatch_async(dispatch_get_main_queue(), ^{
_displayLink.paused = YES; // Reset the main display layer
if (self->_displayLayer) {
// Re-apply the desired frame rate range. This is the critical hint for ProMotion [self->_displayLayer flushAndRemoveImage];
// that may have been lost or ignored during the PiP transition. [self->_displayLayer setControlTimebase:NULL];
if (@available(iOS 15.0, *)) { }
_displayLink.preferredFrameRateRange = CAFrameRateRangeMake(self->_frameRate, self->_frameRate, self->_frameRate);
} else { // FIX: Also reset the PiP layer if it exists
_displayLink.preferredFramesPerSecond = self->_frameRate; if (self.pipLayer) {
[self.pipLayer flushAndRemoveImage];
[self.pipLayer setControlTimebase:NULL];
}
});
// 2. Reset the sender (the Decompression Session)
@synchronized(self) {
if (_decompressionSession != nil) {
VTDecompressionSessionInvalidate(_decompressionSession);
CFRelease(_decompressionSession);
_decompressionSession = nil;
}
} }
// Resume the display link immediately. // 3. Request a new keyframe to restart the video stream cleanly
_displayLink.paused = NO; LiRequestIdrFrame();
} }
else if (_renderingBackend == RENDER_METAL) { else if (_renderingBackend == RENDER_METAL) {
// (The existing, correct logic for the Metal renderer remains here)
Log(LOG_I, @"Resetting video pipeline for Metal renderer after resuming..."); Log(LOG_I, @"Resetting video pipeline for Metal renderer after resuming...");
// 1. Reset the receiver (the PiP Layer) on the main thread // 1. Reset the receiver (the PiP Layer) on the main thread