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

fix(darwin): Start observing AVPlayerItem.status before being assigned to AVPlayer #1549

Merged
merged 27 commits into from
Sep 26, 2023

Conversation

blaugold
Copy link
Contributor

@blaugold blaugold commented Jun 17, 2023

Description

Beginning with 4.1.0 we are seeing a lot of TimeoutException after 0:00:30.000000: Future not completed exceptions on iOS.

I think I have tracked it down to when the KV-Observer for AVPlayerItem.status is registered. Before this change this was done after setting the AVPlayerItem on the AVPlayer. The problem with this is that AVPlayerItem starts loading immediately after setting it on the AVPlayer and can finish loading before the KV-Observer is even registered.

Now the KV-Observer is registered just after creating the AVPlayerItem.

The fix could have been done with less changes, but I though WrappedMediaPlayer could be clean up a bit.

Checklist

  • The title of my PR starts with a Conventional Commit prefix (fix:, feat:, docs:, chore: etc).
  • I have read the Contributor Guide and followed the process outlined for submitting PRs.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation and added dartdoc comments with ///, where necessary.
  • I have updated/added relevant examples in example.

Breaking Change

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

@spydon spydon requested a review from Gustl22 June 26, 2023 08:53
onPreparedSubscription.cancel();
try {
await fun();
await preparedCompleter.future.timeout(const Duration(seconds: 30));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is not the right way here: we should avoid catching any error and instead try to not emit the prepared more than once or reinitialize the onPreparedCompleter.

@Gustl22
Copy link
Collaborator

Gustl22 commented Jun 26, 2023

Hi @blaugold thank you very much for contributing! We love that :)
Would you mind to split the refactor part (in a new PR) from the actual fix, otherwise the changes are hard to track, and then maybe we could add a test to avoid reverting the fix? Thank you for your help!

@Gustl22
Copy link
Collaborator

Gustl22 commented Jul 18, 2023

@blaugold little reminder, if you still working on this :)

@blaugold
Copy link
Contributor Author

@Gustl22 Thanks for reviewing this PR and the ping. I'm still planning on addressing the comments, just haven't had the time yet :)

@Gustl22 Gustl22 mentioned this pull request Jul 28, 2023
7 tasks
@Gustl22 Gustl22 changed the base branch from main to gustl22/darwin-refactor July 28, 2023 22:05
# Conflicts:
#	packages/audioplayers_darwin/darwin/Classes/SwiftAudioplayersDarwinPlugin.swift
#	packages/audioplayers_darwin/darwin/Classes/WrappedMediaPlayer.swift
Copy link
Collaborator

@Gustl22 Gustl22 left a comment

Choose a reason for hiding this comment

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

I added another Pull request to better distinguish the refactor from the actual logical changes, for a better review. Hope that is ok for you.

setUpPlayerItemStatusObservation(playerItem, completer, completerError)
let player = updatePlayer(playerItem) ?? createPlayer(playerItem)
setUpPositionObserver(player)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Setting up position observer for both creating and updating the player means that previously the position was not updated, when setting a new url? Is that right? So this is a fix then maybe? Or if not, why did it work in the first place?

Copy link
Collaborator

@Gustl22 Gustl22 Aug 27, 2023

Choose a reason for hiding this comment

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

Now I get it: the position update is bound to the player instance, where as the completedObserver is bound to the playerItem instance.
I would propose the following:

  • We move the createPlayer part to the init method or/and just use player = AVPlayer.init(), as this is only called once, during its live time.
  • We only use updatePlayer in setSourceUrl, but can be sure, that player is never nil.
  • Split observers in a positionObserver (also initialized at the beginning) and a completedObserver, reset every time on reset. The positionObserver is reset on dispose.
  • Replacing the positonObserver on every source change doesn't really make sense, as the position updates keep on immediately, except url is nil. We should either remove them on every stop or leave them always running, I guess.

self.reset()
completerError?()
default:
break
}
}

playerItemStatusObservation?.invalidate()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just as note for myself: Invalidate is now called in reset() for both creating and updating the player

@Gustl22 Gustl22 deleted the branch bluefireteam:main August 1, 2023 10:35
Gustl22 added a commit that referenced this pull request Aug 1, 2023
# Description

Pick refactor code of #1549 

---------

Co-authored-by: Gabriel Terwesten <gabriel@terwesten.net>
@Gustl22 Gustl22 closed this Aug 1, 2023
@Gustl22 Gustl22 reopened this Aug 1, 2023
@Gustl22 Gustl22 changed the base branch from gustl22/darwin-refactor to main August 1, 2023 13:00
# Conflicts:
#	packages/audioplayers_darwin/darwin/Classes/WrappedMediaPlayer.swift
@Gustl22
Copy link
Collaborator

Gustl22 commented Aug 18, 2023

@blaugold small reminder, if you still plan to work on this. Thank you ;D

@Gustl22 Gustl22 changed the title fix: start observing AVPlayerItem.status before setting it on AVPlayer fix(darwin): Start observing AVPlayerItem.status before being assigned to AVPlayer Sep 23, 2023
@Gustl22
Copy link
Collaborator

Gustl22 commented Sep 25, 2023

@blaugold do you agree with the changes? Feel free to comment the concerns. I'll merge in the next few days :)

@Gustl22 Gustl22 merged commit 8c3a213 into bluefireteam:main Sep 26, 2023
7 checks passed
Gustl22 pushed a commit that referenced this pull request Sep 26, 2023
…ned to `AVPlayer` (#1549)

# Description

* KV-Observer is now registered just after creating the `AVPlayerItem`.
* Make player object non-nullable

---------

Co-authored-by: Gustl22 <git@reb0.org>
(cherry picked from commit 8c3a213)
Gustl22 added a commit that referenced this pull request Nov 14, 2023
…ned to `AVPlayer` (#1549)

# Description

* KV-Observer is now registered just after creating the `AVPlayerItem`.
* Make player object non-nullable

---------

Co-authored-by: Gustl22 <git@reb0.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants