Skip to content

Commit

Permalink
this makes more sense here
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjl-mux committed Sep 6, 2024
1 parent 66f355e commit 7139cde
Showing 1 changed file with 59 additions and 26 deletions.
85 changes: 59 additions & 26 deletions Sources/MuxPlayerSwift/Monitoring/Monitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,25 @@ class Monitor: ErrorDispatcher {
bindings[objectIdentifier] = monitoredPlayer

if let player = playerViewController.player {
let observation = player.observe(
\.error,
options: [.new, .old]
) { player, observation in
if let error = (observation.newValue ?? nil) as? NSError,
((observation.oldValue ?? nil) == nil) {
// TODO: Add a better way to protect against
// dual registrations for the same player
keyValueObservation.unregister(
player
)

keyValueObservation.register(
player,
for: \.error,
options: [.new, .old]
) { player, change in
if let error = (change.newValue ?? nil) as? NSError,
((change.oldValue ?? nil) == nil) {
binding.dispatchError(
"\(error.code)",
withMessage: error.localizedFailureReason
)
}
}

if let existingObservation = playersToObservations[ObjectIdentifier(player)] {
existingObservation.invalidate()
playersToObservations.removeValue(forKey: ObjectIdentifier(player))
}

playersToObservations[ObjectIdentifier(player)] = observation
}
}

Expand Down Expand Up @@ -209,25 +209,25 @@ class Monitor: ErrorDispatcher {
bindings[objectIdentifier] = monitoredPlayer

if let player = playerLayer.player {
let observation = player.observe(
\.error,
options: [.new, .old]
) { player, observation in
if let error = (observation.newValue ?? nil) as? NSError,
((observation.oldValue ?? nil) == nil) {
// TODO: Add a better way to protect against
// dual registrations for the same player
keyValueObservation.unregister(
player
)

keyValueObservation.register(
player,
for: \.error,
options: [.new, .old]
) { player, change in
if let error = (change.newValue ?? nil) as? NSError,
((change.oldValue ?? nil) == nil) {
binding.dispatchError(
"\(error.code)",
withMessage: error.localizedFailureReason
)
}
}

if let existingObservation = playersToObservations[ObjectIdentifier(player)] {
existingObservation.invalidate()
playersToObservations.removeValue(forKey: ObjectIdentifier(player))
}

playersToObservations[ObjectIdentifier(player)] = observation
}
}

Expand Down Expand Up @@ -276,5 +276,38 @@ class Monitor: ErrorDispatcher {
playbackID: String
) {

class KeyValueObservation {
var observations: [ObjectIdentifier: Set<NSKeyValueObservation>] = [:]

func register<Value>(
_ player: AVPlayer,
for keyPath: KeyPath<AVPlayer, Value>,
options: NSKeyValueObservingOptions,
changeHandler: @escaping (AVPlayer, NSKeyValueObservedChange<Value>) -> Void
) {
let observation = player.observe(
keyPath,
options: options,
changeHandler: changeHandler
)

if var o = observations[ObjectIdentifier(player)] {
o.insert(observation)
observations[ObjectIdentifier(player)] = o
} else {
observations[ObjectIdentifier(player)] = Set(arrayLiteral: observation)
}
}

func unregister(
_ player: AVPlayer
) {
if let o = observations[ObjectIdentifier(player)] {
o.forEach { observation in
observation.invalidate()
}
observations.removeValue(forKey: ObjectIdentifier(player))
}
}
}
}

0 comments on commit 7139cde

Please sign in to comment.