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 crash on attaching metadata output #74

Merged
merged 3 commits into from
Mar 8, 2024
Merged

Conversation

Fonos-development
Copy link
Contributor

The issue: doublesymmetry/react-native-track-player#1818
The previous fix: #68

The previous attempt to fix it was not ideal cause we don't know the exact observation removal time.

My approach:

  • Deferred Addition of Metadata Output
  • Each observed item now gets a fresh instance of metadata output for cleaner management.
  • Added a check to ensure safe removal of metadata output from the observing item.

Copy link
Collaborator

@dcvz dcvz left a comment

Choose a reason for hiding this comment

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

I've left some comments / thoughts. Overall looks good to me though, thanks for this @Fonos-development

@@ -47,7 +47,6 @@ class AVPlayerItemObserver: NSObject {

override init() {
super.init()
metadataOutput.setDelegate(self, queue: .main)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can also remove the private let metadataOutput = AVPlayerItemMetadataOutput() above right?

// Create and add a new metadata output to the item.
let metadataOutput = AVPlayerItemMetadataOutput()
metadataOutput.setDelegate(self, queue: .main)
item.add(metadataOutput)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder whether we need to add a check in the delegate method func metadataOutput() to make sure we're receiving metadata from the "currently focused' item and output before we broadcast it. We could still do it by keeping a reference to the latest created output currentMetadataOutput and comparing the output given in the delegate to it.

I think once we reassign currentMetadataOutput any old metadata outputs will keep hanging in memory until we call remove(metadataOutput) in the stopObservingCurrentItem. We should probably do a small check on that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, thanks for your advice. Kindly have a look at the new commit.

observingItem.remove(metadataOutput)

// Remove metadata output if it's attached.
if let metadataOutput = observingItem.outputs.first(where: { $0 is AVPlayerItemMetadataOutput }) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is clever. We could probably put this in an extension and remove all outputs for safety.

extension AVPlayerItem {
    func removeAllMetadataOutputs() {
        for output in self.outputs.filter({ $0 is AVPlayerItemMetadataOutput }) {
            self.remove(output)
        }
    }
}

Copy link
Collaborator

@dcvz dcvz left a comment

Choose a reason for hiding this comment

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

LGTM 👍 thanks for this @Fonos-development

@dcvz dcvz merged commit 2424550 into doublesymmetry:main Mar 8, 2024
1 check failed
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.

2 participants