From be2019b41b5bfe395e13c4dec94f53afb2e2a20f Mon Sep 17 00:00:00 2001 From: Stefano Russello Date: Thu, 21 Nov 2024 14:46:34 +0100 Subject: [PATCH] CORE-4594 Playlist API integration - Added entryId to the PlaybackResponseModel - Fixing entryId on sourceConfig metadata field - Fixing issue mixing Live with VOD --- .../PlayBack API/PlaybackAPIService.swift | 3 ++- .../PlayBack API/PlaybackResponseModel.swift | 1 + Sources/PlaybackSDK/PlaybackSDKManager.swift | 25 ++++++++++++------ .../BitMovinPlugin/BitmovinPlayerPlugin.swift | 26 ++++++++++++++----- 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/Sources/PlaybackSDK/PlayBack API/PlaybackAPIService.swift b/Sources/PlaybackSDK/PlayBack API/PlaybackAPIService.swift index c01f546..083d9d6 100644 --- a/Sources/PlaybackSDK/PlayBack API/PlaybackAPIService.swift +++ b/Sources/PlaybackSDK/PlayBack API/PlaybackAPIService.swift @@ -64,7 +64,8 @@ internal class PlaybackAPIService: PlaybackAPI { switch httpResponse.statusCode { case 200...299: - if let response = try? JSONDecoder().decode(PlaybackResponseModel.self, from: data) { + if var response = try? JSONDecoder().decode(PlaybackResponseModel.self, from: data) { + response.entryId = entryId return .success(response) } else { return .failure(PlaybackAPIError.invalidResponsePlaybackData) diff --git a/Sources/PlaybackSDK/PlayBack API/PlaybackResponseModel.swift b/Sources/PlaybackSDK/PlayBack API/PlaybackResponseModel.swift index 4b748b7..0a3ccd5 100644 --- a/Sources/PlaybackSDK/PlayBack API/PlaybackResponseModel.swift +++ b/Sources/PlaybackSDK/PlayBack API/PlaybackResponseModel.swift @@ -21,6 +21,7 @@ public struct PlaybackResponseModel: Decodable { public let playFrom: Int? public let adverts: [Advert]? public let coverImg: CoverImages? + public var entryId: String? public struct Media: Decodable { public let hls: String? diff --git a/Sources/PlaybackSDK/PlaybackSDKManager.swift b/Sources/PlaybackSDK/PlaybackSDKManager.swift index 32b70db..aab902a 100644 --- a/Sources/PlaybackSDK/PlaybackSDKManager.swift +++ b/Sources/PlaybackSDK/PlaybackSDKManager.swift @@ -377,16 +377,25 @@ public class PlaybackSDKManager { // sourceConfig.title = details.name // sourceConfig.posterSource = details.coverImg?._360 // sourceConfig.sourceDescription = details.description - let regex = try! NSRegularExpression(pattern: "/entryId/(.+?)/") - let range = NSRange(location: 0, length: hlsURLString.count) - if let match = regex.firstMatch(in: hlsURLString, options: [], range: range) { - if let swiftRange = Range(match.range(at: 1), in: hlsURLString) { - let entryId = hlsURLString[swiftRange] - sourceConfig.metadata["entryId"] = String(entryId) - sourceConfig.metadata["details"] = details - sourceConfig.metadata["authorizationToken"] = authorizationToken + + if details.entryId?.isEmpty == false { + sourceConfig.metadata["entryId"] = details.entryId + } else { + // Recover entryId from hls url (not working for live url) + let regex = try! NSRegularExpression(pattern: "/entryId/(.+?)/") + let range = NSRange(location: 0, length: hlsURLString.count) + if let match = regex.firstMatch(in: hlsURLString, options: [], range: range) { + if let swiftRange = Range(match.range(at: 1), in: hlsURLString) { + let entryId = hlsURLString[swiftRange] + sourceConfig.metadata["entryId"] = String(entryId) + + } } } + + sourceConfig.metadata["details"] = details + sourceConfig.metadata["authorizationToken"] = authorizationToken + return SourceFactory.createSource(from: sourceConfig) } } diff --git a/Sources/PlaybackSDK/Player Plugin/BitMovinPlugin/BitmovinPlayerPlugin.swift b/Sources/PlaybackSDK/Player Plugin/BitMovinPlugin/BitmovinPlayerPlugin.swift index cda08d9..a387a74 100644 --- a/Sources/PlaybackSDK/Player Plugin/BitMovinPlugin/BitmovinPlayerPlugin.swift +++ b/Sources/PlaybackSDK/Player Plugin/BitMovinPlugin/BitmovinPlayerPlugin.swift @@ -153,7 +153,7 @@ public class BitmovinPlayerPlugin: VideoPlayerPlugin, ObservableObject { public func seek(to entryId: String, completion: @escaping (Bool) -> Void) { if let sources = player?.playlist.sources { - if let index = sources.firstIndex(where: { $0.metadata?["entryId"] as? String == entryId }) { + if let index = sources.firstIndex(where: { $0.sourceConfig.metadata["entryId"] as? String == entryId }) { seekSource(to: sources[index]) { success in completion(success) } @@ -167,12 +167,26 @@ public class BitmovinPlayerPlugin: VideoPlayerPlugin, ObservableObject { private func seekSource(to source: Source, completion: ( (Bool) -> (Void))? = nil) { if let sources = player?.playlist.sources { - if let index = sources.firstIndex(where: { $0 === source }) { + if let index = sources.firstIndex(where: { $0.sourceConfig.metadata["entryId"] as? String == source.sourceConfig.metadata["entryId"] as? String }) { updateSource(for: sources[index]) { updatedSource in if let updatedSource = updatedSource { self.player?.playlist.remove(sourceAt: index) self.player?.playlist.add(source: updatedSource, at: index) - self.player?.playlist.seek(source: updatedSource, time: 0) + + if let sources = self.player?.playlist.sources { + DispatchQueue.main.asyncAfter(deadline: .now()) { [weak self] in + + let playlistOptions = PlaylistOptions(preloadAllSources: false) + let pConfig = PlaylistConfig(sources: sources, options: playlistOptions) + + self?.player?.load(playlistConfig: pConfig) + self?.player?.playlist.seek(source: updatedSource, time: .zero) + self?.player?.seek(time: .zero) + } + } else { + self.player?.playlist.seek(source: updatedSource, time: .zero) + } + completion?(true) } else { completion?(false) @@ -184,8 +198,8 @@ public class BitmovinPlayerPlugin: VideoPlayerPlugin, ObservableObject { private func updateSource(for source: Source, completion: @escaping (Source?) -> Void) { - let entryId = source.metadata?["entryId"] as? String - let authorizationToken = source.metadata?["authorizationToken"] as? String + let entryId = source.sourceConfig.metadata["entryId"] as? String + let authorizationToken = source.sourceConfig.metadata["authorizationToken"] as? String if let entryId = entryId { PlaybackSDKManager.shared.loadHLSStream(forEntryId: entryId, andAuthorizationToken: authorizationToken) { result in @@ -204,7 +218,7 @@ public class BitmovinPlayerPlugin: VideoPlayerPlugin, ObservableObject { public func activeEntryId() -> String? { if let sources = player?.playlist.sources { if let index = sources.firstIndex(where: { $0.isActive }) { - if let entryId = sources[index].metadata?["entryId"] as? String { + if let entryId = sources[index].sourceConfig.metadata["entryId"] as? String { return entryId } }