Revert "refactor: use separate piplayer for AVSB pip function"

This reverts commit bd9e2e3215.
This commit is contained in:
Acaki committed 2025-07-19 17:43:46 +08:00
1 parent 61c5b958f2
commit f368076aaf
2 files changed
+33 -81

No files matched your search

+21 -70
View File
@@ -824,32 +824,8 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
frameNumber:(int)frameNumber frameNumber:(int)frameNumber
frameType:(int)frameType frameType:(int)frameType
decodeStartTime:(CFTimeInterval)decodeStartTime { decodeStartTime:(CFTimeInterval)decodeStartTime {
VTDecompressionSessionRef sessionForDecode = NULL; if (frameType == FRAME_TYPE_IDR || _decompressionSession == nil) {
@synchronized(self) {
// If the session is nil, it can only be created by an IDR frame.
if (_decompressionSession == nil) {
if (frameType == FRAME_TYPE_IDR) {
[self setupDecompressionSession]; [self setupDecompressionSession];
} else {
// Not an IDR, so we can't create a session. Drop the frame.
return noErr;
}
}
// If an IDR arrives during a valid session, re-create the session
// in case video parameters have changed.
else if (frameType == FRAME_TYPE_IDR) {
[self setupDecompressionSession];
}
// If, after trying, the session is still nil, we cannot proceed.
if (_decompressionSession == nil) {
Log(LOG_E, @"Decompression session is nil after setup attempt. Dropping frame.");
return kVTInvalidSessionErr;
}
// Capture a strong reference to the session that we can use outside the lock.
// This prevents another thread from deallocating it while we're using it.
sessionForDecode = (VTDecompressionSessionRef)CFRetain(_decompressionSession);
} }
if (frameType == FRAME_TYPE_IDR && !_hasSentVideoContentShown) { if (frameType == FRAME_TYPE_IDR && !_hasSentVideoContentShown) {
@@ -879,10 +855,11 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
LiRequestIdrFrame(); // Request an IDR to restart the stream LiRequestIdrFrame(); // Request an IDR to restart the stream
return; return;
} }
if (self->_renderingBackend == RENDER_METAL && self.pipLayer) {
if (self.pipLayer) { // NSLog(@"[Metal PiP Path] Entered for frame number %d.", frameNumber);
CVPixelBufferRetain(imageBuffer); CVPixelBufferRetain(imageBuffer);
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
// NSLog(@"[Metal PiP Path] Executing on main thread for frame %d.", frameNumber);
@synchronized(self) { @synchronized(self) {
if (self.pipLayer.controlTimebase == NULL) { if (self.pipLayer.controlTimebase == NULL) {
NSLog(@"[Metal PiP Path] Timebase is NULL. Setting it now."); NSLog(@"[Metal PiP Path] Timebase is NULL. Setting it now.");
@@ -912,6 +889,7 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
CMSampleTimingInfo sampleTiming = {kCMTimeInvalid, presentationTimestamp, presentationDuration}; CMSampleTimingInfo sampleTiming = {kCMTimeInvalid, presentationTimestamp, presentationDuration};
OSStatus err = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, imageBuffer, self->_pipFormatDesc, &sampleTiming, &sampleBufferForPip); OSStatus err = CMSampleBufferCreateReadyWithImageBuffer(kCFAllocatorDefault, imageBuffer, self->_pipFormatDesc, &sampleTiming, &sampleBufferForPip);
if (err == noErr && sampleBufferForPip) { if (err == noErr && sampleBufferForPip) {
// NSLog(@"[Metal PiP Path] Sample buffer created. Enqueuing to _pipLayer.");
[self.pipLayer enqueueSampleBuffer:sampleBufferForPip]; [self.pipLayer enqueueSampleBuffer:sampleBufferForPip];
CFRelease(sampleBufferForPip); CFRelease(sampleBufferForPip);
} else { } else {
@@ -924,7 +902,7 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
} }
// --- MAIN RENDERER PATH --- // --- MAIN RENDERER PATH ---
if (self->_renderingBackend == RENDER_AVSB) { if (self->_renderingBackend == RENDER_AVSB) {
self.pipLayer.hidden = NO; // self.pipLayer.hidden = YES;
if (self->_avsbFormatDesc == NULL || !CMVideoFormatDescriptionMatchesImageBuffer(self->_avsbFormatDesc, imageBuffer)) { if (self->_avsbFormatDesc == NULL || !CMVideoFormatDescriptionMatchesImageBuffer(self->_avsbFormatDesc, imageBuffer)) {
if (self->_avsbFormatDesc) { if (self->_avsbFormatDesc) {
CFRelease(self->_avsbFormatDesc); CFRelease(self->_avsbFormatDesc);
@@ -940,21 +918,7 @@ int DrSubmitDecodeUnit(PDECODE_UNIT decodeUnit);
dispatch_async(self->_vtq, ^{ dispatch_async(self->_vtq, ^{
Frame *frame = [[Frame alloc] initWithSampleBuffer:sampleBufferForAvsb frameNumber:frameNumber frameType:frameType]; Frame *frame = [[Frame alloc] initWithSampleBuffer:sampleBufferForAvsb frameNumber:frameNumber frameType:frameType];
int framesDropped = [self->_frameQueue enqueue:frame withSlackSize:3]; int framesDropped = [self->_frameQueue enqueue:frame withSlackSize:3];
static PlotMetrics frameQueueMetrics = {}; // ... stats and metrics updates
[[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 { } else {
if (sampleBufferForAvsb) { if (sampleBufferForAvsb) {
@@ -1157,38 +1121,25 @@ 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) { if (_renderingBackend == RENDER_AVSB && _displayLink) {
Log(LOG_I, @"Resetting video pipeline for AVSB renderer after resuming..."); Log(LOG_I, @"Frame pacing is being reset to %d FPS...", self->_frameRate);
// 1. Reset the receiver layers on the main thread // Toggling the paused state can help re-engage the display link with the
dispatch_async(dispatch_get_main_queue(), ^{ // run loop correctly after the app resumes from a background state like PiP.
// Reset the main display layer _displayLink.paused = YES;
if (self->_displayLayer) {
[self->_displayLayer flushAndRemoveImage]; // Re-apply the desired frame rate range. This is the critical hint for ProMotion
[self->_displayLayer setControlTimebase:NULL]; // that may have been lost or ignored during the PiP transition.
if (@available(iOS 15.0, *)) {
_displayLink.preferredFrameRateRange = CAFrameRateRangeMake(self->_frameRate, self->_frameRate, self->_frameRate);
} else {
_displayLink.preferredFramesPerSecond = self->_frameRate;
} }
// FIX: Also reset the PiP layer if it exists // Resume the display link immediately.
if (self.pipLayer) { _displayLink.paused = NO;
[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;
}
}
// 3. Request a new keyframe to restart the video stream cleanly
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
@@ -97,7 +97,6 @@
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController { - (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
_streamView.hidden = YES; _streamView.hidden = YES;
_pipLayer.hidden = YES;
if (self.imguiView) { if (self.imguiView) {
self.imguiView.mtkView.hidden = YES; self.imguiView.mtkView.hidden = YES;
Log(LOG_I, @"Hiding ImGui view for PiP start."); Log(LOG_I, @"Hiding ImGui view for PiP start.");
@@ -117,7 +116,6 @@
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController { - (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
_streamView.hidden = NO; _streamView.hidden = NO;
_pipLayer.hidden = NO;
if (self.imguiView) { if (self.imguiView) {
self.imguiView.mtkView.hidden = NO; self.imguiView.mtkView.hidden = NO;
Log(LOG_I, @"Showing ImGui view after PiP stop."); Log(LOG_I, @"Showing ImGui view after PiP stop.");
@@ -131,7 +129,6 @@
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler { - (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {
_isRestoringFromPiP = YES; _isRestoringFromPiP = YES;
_streamView.hidden = NO; _streamView.hidden = NO;
_pipLayer.hidden = NO;
if (self.imguiView) { if (self.imguiView) {
self.imguiView.mtkView.hidden = NO; self.imguiView.mtkView.hidden = NO;
Log(LOG_I, @"Showing ImGui view for PiP restore."); Log(LOG_I, @"Showing ImGui view for PiP restore.");
@@ -181,23 +178,27 @@
return (_settings.touchMode.intValue == RelativeTouch || _settings.touchMode.intValue == NativeTouch || _settings.touchMode.intValue == AbsoluteTouch) && _settings.onscreenControls.intValue == OnScreenControlsLevelCustom; return (_settings.touchMode.intValue == RelativeTouch || _settings.touchMode.intValue == NativeTouch || _settings.touchMode.intValue == AbsoluteTouch) && _settings.onscreenControls.intValue == OnScreenControlsLevelCustom;
} }
- (void)setupPiPControllerWithRenderer:(VideoDecoderRenderer *)videoRenderer { - (void)setupPiPControllerWithRenderer:(VideoDecoderRenderer *)videoRenderer { // Ensure we have the renderer and its layer
if (self.pipController) { if (self.pipController) {
return; return;
} }
Log(LOG_I, @"Setting up PiP controller..."); Log(LOG_I, @"Setting up PiP controller...");
// Both Metal and AVSB will use the dedicated _pipLayer for PiP. AVSampleBufferDisplayLayer* sourceLayer;
AVSampleBufferDisplayLayer* sourceLayer = _pipLayer;
if ([_settings.renderingBackend intValue] == RENDER_METAL) { if ([_settings.renderingBackend intValue] == RENDER_METAL) {
NSLog(@"[PiP LOG] Backend: Metal. Using dedicated _pipLayer: %p", sourceLayer); sourceLayer = _pipLayer;
NSLog(@"[PiP LOG] Backend: Metal. Using _pipLayer: %p", sourceLayer);
if (!sourceLayer) {
NSLog(@"[PiP LOG] ERROR: _pipLayer is nil during setup.");
return;
}
} else { // RENDER_AVSB } else { // RENDER_AVSB
NSLog(@"[PiP LOG] Backend: AVSB. Using dedicated _pipLayer: %p", sourceLayer); sourceLayer = videoRenderer.displayLayer;
NSLog(@"[PiP LOG] Backend: AVSB. Using _displayLayer: %p", sourceLayer);
} }
if (!sourceLayer) { if (!_pipLayer) {
Log(LOG_E, @"PiP setup failed: Dedicated PiP layer is nil."); Log(LOG_E, @"PiP setup failed: Dedicated PiP layer not ready.");
return; return;
} }