From a9c89472a6bf9ca473ffff29a0bc201cd35b704b Mon Sep 17 00:00:00 2001 From: Stuart Morgan Date: Wed, 25 Feb 2026 11:06:18 -0500 Subject: [PATCH] [video_player] Stop display link on dispose The video player and display link won't necessarily be deallocated when the player is disposed, so the display link should be stopped at that point to avoid having it drive frame requests. Fixes https://github.com/flutter/flutter/issues/181387 --- .../video_player_avfoundation/CHANGELOG.md | 4 ++++ .../darwin/RunnerTests/VideoPlayerTests.swift | 23 +++++++++++++++++++ .../FVPTextureBasedVideoPlayer.m | 1 + .../video_player_avfoundation/pubspec.yaml | 2 +- 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/video_player/video_player_avfoundation/CHANGELOG.md b/packages/video_player/video_player_avfoundation/CHANGELOG.md index b22e2b1231e9..1508c0bbfe22 100644 --- a/packages/video_player/video_player_avfoundation/CHANGELOG.md +++ b/packages/video_player/video_player_avfoundation/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.9.4 + +* Ensures that the display link does not continue requesting frames after a player is disposed. + ## 2.9.3 * Fixes a regression where HTTP headers were ignored. diff --git a/packages/video_player/video_player_avfoundation/darwin/RunnerTests/VideoPlayerTests.swift b/packages/video_player/video_player_avfoundation/darwin/RunnerTests/VideoPlayerTests.swift index 14d46abb2c5a..c965a70a8430 100644 --- a/packages/video_player/video_player_avfoundation/darwin/RunnerTests/VideoPlayerTests.swift +++ b/packages/video_player/video_player_avfoundation/darwin/RunnerTests/VideoPlayerTests.swift @@ -196,6 +196,29 @@ private let hlsAudioTestURI = #expect(stubDisplayLinkFactory.displayLink.running) } + @Test func disposeWhilePlayingStopsDisplayLink() async { + let stubDisplayLinkFactory = StubFVPDisplayLinkFactory() + let mockVideoOutput = TestPixelBufferSource() + let videoPlayerPlugin = createInitializedPlugin( + avFactory: StubFVPAVFactory(pixelBufferSource: mockVideoOutput), + displayLinkFactory: stubDisplayLinkFactory) + + var error: FlutterError? + let identifiers = videoPlayerPlugin.createTexturePlayer( + with: FVPCreationOptions.make(withUri: hlsTestURI, httpHeaders: [:]), + error: &error) + #expect(error == nil) + let player = + videoPlayerPlugin.playersByIdentifier[identifiers!.playerId] as! FVPTextureBasedVideoPlayer + + player.playWithError(&error) + #expect(error == nil) + player.disposeWithError(&error) + #expect(error == nil) + + #expect(!stubDisplayLinkFactory.displayLink.running) + } + @Test func deregistersFromPlayer() throws { let videoPlayerPlugin = createInitializedPlugin() diff --git a/packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation/FVPTextureBasedVideoPlayer.m b/packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation/FVPTextureBasedVideoPlayer.m index b474f8457e8d..1419da4d7743 100644 --- a/packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation/FVPTextureBasedVideoPlayer.m +++ b/packages/video_player/video_player_avfoundation/darwin/video_player_avfoundation/Sources/video_player_avfoundation/FVPTextureBasedVideoPlayer.m @@ -116,6 +116,7 @@ - (void)disposeWithError:(FlutterError *_Nullable *_Nonnull)error { [self.playerLayer removeFromSuperlayer]; + _displayLink.running = NO; _displayLink = nil; } diff --git a/packages/video_player/video_player_avfoundation/pubspec.yaml b/packages/video_player/video_player_avfoundation/pubspec.yaml index 853ce2760ae8..f14fefb73326 100644 --- a/packages/video_player/video_player_avfoundation/pubspec.yaml +++ b/packages/video_player/video_player_avfoundation/pubspec.yaml @@ -2,7 +2,7 @@ name: video_player_avfoundation description: iOS and macOS implementation of the video_player plugin. repository: https://github.com/flutter/packages/tree/main/packages/video_player/video_player_avfoundation issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+video_player%22 -version: 2.9.3 +version: 2.9.4 environment: sdk: ^3.10.0