Skip to content

Commit

Permalink
feat(ios): update timedmetadata handler
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMoch committed Dec 27, 2023
1 parent c8983f4 commit 382cd66
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
22 changes: 16 additions & 6 deletions ios/Video/Features/RCTPlayerObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol RCTPlayerObserverHandlerObjc {
protocol RCTPlayerObserverHandler: RCTPlayerObserverHandlerObjc {
func handleTimeUpdate(time: CMTime)
func handleReadyForDisplay(changeObject: Any, change: NSKeyValueObservedChange<Bool>)
func handleTimeMetadataChange(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<[AVMetadataItem]?>)
func handleTimeMetadataChange(timedMetadata: [AVMetadataItem])
func handlePlayerItemStatusChange(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<AVPlayerItem.Status>)
func handlePlaybackBufferKeyEmpty(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<Bool>)
func handlePlaybackLikelyToKeepUp(playerItem: AVPlayerItem, change: NSKeyValueObservedChange<Bool>)
Expand All @@ -29,7 +29,7 @@ protocol RCTPlayerObserverHandler: RCTPlayerObserverHandlerObjc {

// MARK: - RCTPlayerObserver

class RCTPlayerObserver: NSObject {
class RCTPlayerObserver: NSObject, AVPlayerItemMetadataOutputPushDelegate {
weak var _handlers: RCTPlayerObserverHandler?

var player: AVPlayer? {
Expand All @@ -50,9 +50,14 @@ class RCTPlayerObserver: NSObject {
removePlayerItemObservers()
}
didSet {
if playerItem != nil {
addPlayerItemObservers()
}
guard let playerItem = playerItem else { return }

addPlayerItemObservers()

// handle timedMetadata
let metadataOutput = AVPlayerItemMetadataOutput()
playerItem.add(metadataOutput)
metadataOutput.setDelegate(self, queue: .global())
}
}

Expand Down Expand Up @@ -98,6 +103,12 @@ class RCTPlayerObserver: NSObject {
}
}

func metadataOutput(_: AVPlayerItemMetadataOutput, didOutputTimedMetadataGroups groups: [AVTimedMetadataGroup], from _: AVPlayerItemTrack?) {
for metadataGroup in groups {
_handlers?.handleTimeMetadataChange(timedMetadata: metadataGroup.items)
}
}

func addPlayerObservers() {
guard let player = player, let _handlers = _handlers else {
return
Expand Down Expand Up @@ -126,7 +137,6 @@ class RCTPlayerObserver: NSObject {
options: [.new, .old],
changeHandler: _handlers.handlePlaybackLikelyToKeepUp
)
_playerTimedMetadataObserver = playerItem.observe(\.timedMetadata, options: [.new], changeHandler: _handlers.handleTimeMetadataChange)
}

func removePlayerItemObservers() {
Expand Down
8 changes: 2 additions & 6 deletions ios/Video/RCTVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1113,13 +1113,9 @@ class RCTVideo: UIView, RCTVideoPlayerViewControllerDelegate, RCTPlayerObserverH
}

// When timeMetadata is read the event onTimedMetadata is triggered
func handleTimeMetadataChange(playerItem _: AVPlayerItem, change: NSKeyValueObservedChange<[AVMetadataItem]?>) {
guard let newValue = change.newValue, let _items = newValue, !_items.isEmpty else {
return
}

func handleTimeMetadataChange(timedMetadata: [AVMetadataItem]) {
var metadata: [[String: String?]?] = []
for item in _items {
for item in timedMetadata {
let value = item.value as? String
let identifier = item.identifier?.rawValue

Expand Down

0 comments on commit 382cd66

Please sign in to comment.