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

FEC-12060 AVTimedMetadata handeling via AVPlayerItemMetadataOutput #468

Merged
merged 1 commit into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion Classes/Player/AVPlayerEngine/AVPlayerEngine+AssetLoading.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ extension AVPlayerEngine {
*/
let playerItem = AVPlayerItem(asset: newAsset.avAsset)
playerItem.preferredPeakBitRate = newAsset.playerSettings.network.preferredPeakBitRate


let metadataOutput = AVPlayerItemMetadataOutput(identifiers: nil)
metadataOutput.setDelegate(self, queue: DispatchQueue.main)
playerItem.add(metadataOutput)

if #available(iOS 13.0, tvOS 13.0, *) {
playerItem.automaticallyPreservesTimeOffsetFromLive = newAsset.playerSettings.lowLatency.automaticallyPreservesTimeOffsetFromLive

Expand Down Expand Up @@ -117,3 +121,14 @@ extension AVPlayerEngine {
}
}
}

extension AVPlayerEngine: AVPlayerItemMetadataOutputPushDelegate {

public func metadataOutput(_ output: AVPlayerItemMetadataOutput,
didOutputTimedMetadataGroups groups: [AVTimedMetadataGroup],
from track: AVPlayerItemTrack?) {
groups.forEach { group in
self.post(event: PlayerEvent.TimedMetadata(metadata: group.items))
}
}
}
11 changes: 1 addition & 10 deletions Classes/Player/AVPlayerEngine/AVPlayerEngine+Observation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ extension AVPlayerEngine {
#keyPath(currentItem.isPlaybackBufferEmpty),
#keyPath(currentItem.isPlaybackBufferFull),
#keyPath(currentItem.loadedTimeRanges),
#keyPath(currentItem.timedMetadata),
#keyPath(currentItem.duration)
#keyPath(currentItem.duration),
]
}

Expand Down Expand Up @@ -177,8 +176,6 @@ extension AVPlayerEngine {
return
}
self.handle(playerItemStatus: newPlayerItemStatus)
case #keyPath(currentItem.timedMetadata):
self.handleTimedMedia()
case #keyPath(currentItem.duration):
self.handleDurationChanged()
default: super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
Expand Down Expand Up @@ -307,12 +304,6 @@ extension AVPlayerEngine {
}
}

private func handleTimedMedia() {
guard let currentItem = self.currentItem else { return }
guard let metadata = currentItem.timedMetadata else { return }
self.post(event: PlayerEvent.TimedMetadata(metadata: metadata))
}

private func handleTracksSelection(_ tracks: PKTracks) {

func checkLanguageCode(current currentLanguageCode: String?, against languageCodeToCompare: String?) -> Bool {
Expand Down
50 changes: 24 additions & 26 deletions Classes/Player/Ads/AdsDAIPlayerEngineWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public protocol AdsDAIPlayerEngineWrapperDelegate {
func adPaused()
func adResumed()
func adCompleted()
func receivedTimedMetadata(_ metadata: [String : String])
func receivedTimedMetadata(_ metadata: [String: String])
}

public class AdsDAIPlayerEngineWrapper: PlayerEngineWrapper, AdsPluginDelegate, AdsPluginDataSource {
Expand All @@ -31,7 +31,9 @@ public class AdsDAIPlayerEngineWrapper: PlayerEngineWrapper, AdsPluginDelegate,
didSet {
// Add timedMetadata observer
if let avPlayerWrapper = playerEngine as? AVPlayerWrapper {
avPlayerWrapper.currentPlayer.addObserver(self, forKeyPath: "currentItem.timedMetadata", options: NSKeyValueObservingOptions.new, context: nil)
let metadataOutput = AVPlayerItemMetadataOutput(identifiers: nil)
metadataOutput.setDelegate(self, queue: DispatchQueue.main)
avPlayerWrapper.currentPlayer.currentItem?.add(metadataOutput)
}
}
}
Expand Down Expand Up @@ -122,30 +124,6 @@ public class AdsDAIPlayerEngineWrapper: PlayerEngineWrapper, AdsPluginDelegate,
self.adsPlugin.dataSource = self
}

public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
switch keyPath {
case "currentItem.timedMetadata":
if let avPlayerEngine = object as? AVPlayerEngine {
if let timedMetadata = avPlayerEngine.currentItem?.timedMetadata {
for avMetadataItem in timedMetadata {

let value = avMetadataItem.value as? String ?? ""
if value.contains("google") {

if let key = avMetadataItem.key, let value = avMetadataItem.stringValue {
delegate?.receivedTimedMetadata([key.description : value])
}

return
}
}
}
}
default:
break
}
}

/************************************************************/
// MARK: - Private
/************************************************************/
Expand Down Expand Up @@ -496,3 +474,23 @@ extension AdsDAIPlayerEngineWrapper: AppStateObservable {
]
}
}

extension AdsDAIPlayerEngineWrapper: AVPlayerItemMetadataOutputPushDelegate {

public func metadataOutput(_ output: AVPlayerItemMetadataOutput,
didOutputTimedMetadataGroups groups: [AVTimedMetadataGroup],
from track: AVPlayerItemTrack?) {
groups.forEach { group in
group.items.forEach { avMetadataItem in

let value = avMetadataItem.value as? String ?? ""
if value.contains("google") {
if let key = avMetadataItem.key, let value = avMetadataItem.stringValue {
delegate?.receivedTimedMetadata([key.description: value])
}
return
}
}
}
}
}