Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[video_player] Improve macOS frame management #5078

Merged

Conversation

stuartmorgan
Copy link
Contributor

@stuartmorgan stuartmorgan commented Oct 5, 2023

Fixes some logic around getting new frames on macOS:

  • The logic to copy a frame had a race condition; the macOS code checks that a frame is available for the current playback time before informing the engine that a frame is available (it's not clear why the iOS code doesn't do this; apparently the engine tolerates returning NULL frames on iOS?), but failed to account for the fact that the current playback time when the (async) request from the engine comes in can be different. This fixes it to remember the last time that was known to be available, and uses that when the current time isn't available. This fixes flickering on playback (since returning NULL on macOS causes the video to vanish until a new frame is available).
  • Fixes seek to temporarily restart the display link if the video is paused, rather than telling the engine that a frame is available, because it might not be. This is changed for both macOS and iOS since I don't see any reason this bug couldn't affect iOS as well (although in practice I'm only aware of it being reproducible on macOS).

This extracts the display link code for macOS and iOS into an abstraction, eliminating most of the ifdefing, in order to support the latter (since more code needs to be able to play/pause the display link), which also resolves a TODO from the initial implementation.

There is also some non-trivial addition of factory injection in order to make the code more testable. This is definitely not complete, but it incrementally moves the code toward being more testable than it was before, and allows for testing the display link behavior.

Lastly, this moves some code used by tests to the existing _Test.h header, removing redeclarations from unit test files, since we already have a test header and that's our preferred approach for accessing private details in ObjC tests. (Longer term the multi-class mega-file should be broken up more to reduce the need for that.)

Fixes flutter/flutter#136027
Improves flutter/flutter#135999

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

While the display-link-driven frame updater will only inform the engine
that a frame is available when the underlying player has a frame for the
current time, there's no guarantee that the player will have a frame for
the time that is current when the engine actually requests the frame.
To avoid flickering from returning no frame, if a frame isn't available
for the current time when it is requested, provide the frame that was
known to be available when the request was triggered in the first place.

Fixes flutter/flutter#135999
@stuartmorgan
Copy link
Contributor Author

I can pull a1d46cb out into a prequel PR if it would help with review, but hopefully just having it as a separate commit here makes it easy enough.

@stuartmorgan
Copy link
Contributor Author

(I can also do some of the testability refactoring, like moving things to the Test header and adjusting the AV* factory injection, in a prequel PR if desired.)

@hellohuanlin
Copy link
Contributor

CC: @hellohuanlin

@stuartmorgan stuartmorgan force-pushed the video-player-macos-fallback-frame branch from 64d2898 to 8295980 Compare November 9, 2023 16:09
if ([self.videoOutput hasNewPixelBufferForItemTime:outputItemTime]) {
_lastKnownAvailableTime = outputItemTime;
reportFrame = YES;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reset in else?

else {
  _lastKnownAvailableTime = kCMTimeInvalid;
  reportFrame = NO;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reportFrame is a local initialized to NO, so there's no need to set it to NO.

Why would we reset _lastKnownAvailableTime? If we don't have a new time, then the last known time is still the last known time.

}
}

if (self.waitingForFrame && buffer) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will buffer be nil (seeking to the end?)

should this be just if (self.waitingForFrame) so that displayLink is guaranteed to shutdown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will buffer be nil (seeking to the end?)

Seeking to the end should work. It could be nil on iOS since iOS just fires the display link all the time and relies on the engine (apparently) allowing null frames. There may also be edge cases on macOS where we still can get nulls.

should this be just if (self.waitingForFrame) so that displayLink is guaranteed to shutdown?

But if we're waiting for a frame and we didn't get one yet, then we need to wait longer.

Consider iOS seeking while paused, for instance, and then firing the display link indiscriminately before the video frame is actually ready. In that case, we don't want to stop the display link or the frame will be stuck on the old location. Instead we need to wait until the video decoding catches up and there's a frame to show.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be worth documenting the macOS edge cases here in a comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, I don't actually know what the macOS edge cases are. Recent comments in flutter/flutter#135999 suggest they still exist, but I don't yet know when/why.

Copy link
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Member

@cbracken cbracken left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm modulo a couple tiny doc nits.

@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 14, 2023
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 14, 2023
Copy link
Contributor

auto-submit bot commented Nov 14, 2023

auto label is removed for flutter/packages/5078, due to - The status or check suite Mac_arm64 macos_platform_tests master - packages has failed. Please fix the issues identified (or deflake) before re-applying this label.

@stuartmorgan
Copy link
Contributor Author

Hm, looks like one of the new tests may be flaky. I'll investigate that.

@stuartmorgan
Copy link
Contributor Author

I couldn't get it to flake locally in many attempts, and there's no info about what failed on the bot, so I'm going to land it and see if it was a fluke, or if it shows up again (hopefully with details about what actually failed).

@stuartmorgan stuartmorgan added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 16, 2023
@auto-submit auto-submit bot merged commit 25574f9 into flutter:main Nov 16, 2023
79 checks passed
@stuartmorgan stuartmorgan deleted the video-player-macos-fallback-frame branch November 16, 2023 16:19
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Nov 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Nov 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Nov 20, 2023
fluttermirroringbot pushed a commit to flutter/flutter that referenced this pull request Nov 20, 2023
flutter/packages@07b4b29...c5443ad

2023-11-20 JeroenWeener@users.noreply.github.com [webview_flutter] Support for handling basic authentication requests (Platform Interface) (flutter/packages#5362)
2023-11-18 engine-flutter-autoroll@skia.org Roll Flutter from 53a57ad to 6cf9ab0 (31 revisions) (flutter/packages#5426)
2023-11-18 kevmoo@users.noreply.github.com [shared_preferences_web] migrate to pkg:web (flutter/packages#5414)
2023-11-18 stuartmorgan@google.com [ci] Roll minimum allowable Flutter to 3.10 (flutter/packages#5425)
2023-11-18 43054281+camsim99@users.noreply.github.com [path_provider_android]  Run tests on AVDs running Android 34 (flutter/packages#5222)
2023-11-17 tarrinneal@gmail.com [pigeon] isEnum, isClass, fix swift casting, default values, optional method arguments, named method arguments (flutter/packages#5355)
2023-11-17 stuartmorgan@google.com [plugin_platform_interface] Switch mixin to `mixin class` (flutter/packages#5420)
2023-11-17 stuartmorgan@google.com [go_router] Fixes use of `Iterable` (flutter/packages#5421)
2023-11-17 stuartmorgan@google.com [pigeon] Adds `analyzer` 6.x compatibility (flutter/packages#5418)
2023-11-17 stuartmorgan@google.com Update release step to 3.16 (flutter/packages#5416)
2023-11-17 kevmoo@users.noreply.github.com [file_selector_web] migrate to pkg:web (flutter/packages#5413)
2023-11-16 stuartmorgan@google.com [video_player] Improve macOS frame management (flutter/packages#5078)
2023-11-16 engine-flutter-autoroll@skia.org Roll Flutter from e8c2bb1 to 53a57ad (39 revisions) (flutter/packages#5412)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
HugoOlthof pushed a commit to moneybird/packages that referenced this pull request Dec 13, 2023
Fixes some logic around getting new frames on macOS:
- The logic to copy a frame had a race condition; the macOS code checks that a frame is available for the current playback time before informing the engine that a frame is available (it's not clear why the iOS code doesn't do this; apparently the engine tolerates returning NULL frames on iOS?), but failed to account for the fact that the current playback time when the (async) request from the engine comes in can be different. This fixes it to remember the last time that was known to be available, and uses that when the current time isn't available. This fixes flickering on playback (since returning NULL on macOS causes the video to vanish until a new frame is available).
- Fixes seek to temporarily restart the display link if the video is paused, rather than telling the engine that a frame is available, because it might not be. This is changed for both macOS and iOS since I don't see any reason this bug couldn't affect iOS as well (although in practice I'm only aware of it being reproducible on macOS).

This extracts the display link code for macOS and iOS into an abstraction, eliminating most of the ifdefing, in order to support the latter (since more code needs to be able to play/pause the display link), which also resolves a TODO from the initial implementation.

There is also some non-trivial addition of factory injection in order to make the code more testable. This is definitely not complete, but it incrementally moves the code toward being more testable than it was before, and allows for testing the display link behavior.

Lastly, this moves some code used by tests to the existing `_Test.h` header, removing redeclarations from unit test files, since we already have a test header and that's our preferred approach for accessing private details in ObjC tests. (Longer term the multi-class mega-file should be broken up more to reduce the need for that.)

Fixes flutter/flutter#136027
Improves flutter/flutter#135999
@reidbaker reidbaker mentioned this pull request Jan 12, 2024
arc-yong pushed a commit to Arctuition/packages-arc that referenced this pull request Jun 14, 2024
Fixes some logic around getting new frames on macOS:
- The logic to copy a frame had a race condition; the macOS code checks that a frame is available for the current playback time before informing the engine that a frame is available (it's not clear why the iOS code doesn't do this; apparently the engine tolerates returning NULL frames on iOS?), but failed to account for the fact that the current playback time when the (async) request from the engine comes in can be different. This fixes it to remember the last time that was known to be available, and uses that when the current time isn't available. This fixes flickering on playback (since returning NULL on macOS causes the video to vanish until a new frame is available).
- Fixes seek to temporarily restart the display link if the video is paused, rather than telling the engine that a frame is available, because it might not be. This is changed for both macOS and iOS since I don't see any reason this bug couldn't affect iOS as well (although in practice I'm only aware of it being reproducible on macOS).

This extracts the display link code for macOS and iOS into an abstraction, eliminating most of the ifdefing, in order to support the latter (since more code needs to be able to play/pause the display link), which also resolves a TODO from the initial implementation.

There is also some non-trivial addition of factory injection in order to make the code more testable. This is definitely not complete, but it incrementally moves the code toward being more testable than it was before, and allows for testing the display link behavior.

Lastly, this moves some code used by tests to the existing `_Test.h` header, removing redeclarations from unit test files, since we already have a test header and that's our preferred approach for accessing private details in ObjC tests. (Longer term the multi-class mega-file should be broken up more to reduce the need for that.)

Fixes flutter/flutter#136027
Improves flutter/flutter#135999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autosubmit Merge PR when tree becomes green via auto submit App p: video_player platform-ios platform-macos
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[video_player_avfoundation] Only updates every other seek on macOS
3 participants