Update:iOS stream and download URLs to use new API endpoints

This commit is contained in:
advplyr
2023-06-04 12:21:40 -05:00
parent a8ac4970cc
commit fbcb8620f9
2 changed files with 34 additions and 13 deletions
+15 -4
View File
@@ -347,10 +347,21 @@ public class AbsDownloader: CAPPlugin, URLSessionDownloadDelegate {
} }
private func urlForTrack(item: LibraryItem, track: AudioTrack) -> URL { private func urlForTrack(item: LibraryItem, track: AudioTrack) -> URL {
// filename needs to be encoded otherwise would just use contentUrl // TODO: Future server release should include ino with AudioFile or FileMetadata
let relPath = track.metadata?.relPath ?? "" let trackPath = track.metadata?.path ?? ""
let filepathEncoded = relPath.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let urlstr = "\(Store.serverConfig!.address)/s/item/\(item.id)/\(filepathEncoded ?? "")?token=\(Store.serverConfig!.token)" var audioFileIno = ""
if (item.mediaType == "podcast") {
let podcastEpisodes = item.media?.episodes ?? List<PodcastEpisode>()
let matchingEpisode = podcastEpisodes.first(where: { $0.audioFile?.metadata?.path == trackPath })
audioFileIno = matchingEpisode?.audioFile?.ino ?? ""
} else {
let audioFiles = item.media?.audioFiles ?? List<AudioFile>()
let matchingAudioFile = audioFiles.first(where: { $0.metadata?.path == trackPath })
audioFileIno = matchingAudioFile?.ino ?? ""
}
let urlstr = "\(Store.serverConfig!.address)/api/items/\(item.id)/file/\(audioFileIno)/download?token=\(Store.serverConfig!.token)"
return URL(string: urlstr)! return URL(string: urlstr)!
} }
+19 -9
View File
@@ -9,6 +9,7 @@ import Foundation
import AVFoundation import AVFoundation
import UIKit import UIKit
import MediaPlayer import MediaPlayer
import RealmSwift
enum PlayMethod: Int { enum PlayMethod: Int {
case directplay = 0 case directplay = 0
@@ -86,7 +87,21 @@ class AudioPlayer: NSObject {
self.audioPlayer.addObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem), options: .new, context: &playerContext) self.audioPlayer.addObserver(self, forKeyPath: #keyPath(AVPlayer.currentItem), options: .new, context: &playerContext)
for track in playbackSession.audioTracks { for track in playbackSession.audioTracks {
if let playerAsset = createAsset(itemId: playbackSession.libraryItemId!, track: track) {
// TODO: All of this to get the ino of the file on the server. Future server release will include the ino with the session tracks
var audioFileIno = ""
let trackPath = track.metadata?.path ?? ""
if (!playbackSession.isLocal && playbackSession.episodeId != nil) {
let episodes = playbackSession.libraryItem?.media?.episodes ?? List<PodcastEpisode>()
let matchingEpisode:PodcastEpisode? = episodes.first(where: { $0.audioFile?.metadata?.path == trackPath })
audioFileIno = matchingEpisode?.audioFile?.ino ?? ""
} else if (!playbackSession.isLocal) {
let audioFiles = playbackSession.libraryItem?.media?.audioFiles ?? List<AudioFile>()
let matchingAudioFile = audioFiles.first(where: { $0.metadata?.path == trackPath })
audioFileIno = matchingAudioFile?.ino ?? ""
}
if let playerAsset = createAsset(itemId: playbackSession.libraryItemId!, track: track, ino: audioFileIno) {
let playerItem = AVPlayerItem(asset: playerAsset) let playerItem = AVPlayerItem(asset: playerAsset)
if (playbackSession.playMethod == PlayMethod.transcode.rawValue) { if (playbackSession.playMethod == PlayMethod.transcode.rawValue) {
playerItem.preferredForwardBufferDuration = 50 playerItem.preferredForwardBufferDuration = 50
@@ -483,23 +498,18 @@ class AudioPlayer: NSObject {
} }
// MARK: - Private // MARK: - Private
private func createAsset(itemId:String, track:AudioTrack) -> AVAsset? { private func createAsset(itemId:String, track:AudioTrack, ino:String) -> AVAsset? {
guard let playbackSession = self.getPlaybackSession() else { return nil } guard let playbackSession = self.getPlaybackSession() else { return nil }
if (playbackSession.playMethod == PlayMethod.directplay.rawValue) { if (playbackSession.playMethod == PlayMethod.directplay.rawValue) {
// The only reason this is separate is because the filename needs to be encoded let urlstr = "\(Store.serverConfig!.address)/api/items/\(itemId)/file/\(ino)?token=\(Store.serverConfig!.token)"
let relPath = track.metadata?.relPath ?? ""
let filepathEncoded = relPath.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let urlstr = "\(Store.serverConfig!.address)/s/item/\(itemId)/\(filepathEncoded ?? "")?token=\(Store.serverConfig!.token)"
let url = URL(string: urlstr)! let url = URL(string: urlstr)!
return AVURLAsset(url: url) return AVURLAsset(url: url)
} else if (playbackSession.playMethod == PlayMethod.local.rawValue) { } else if (playbackSession.playMethod == PlayMethod.local.rawValue) {
guard let localFile = track.getLocalFile() else { guard let localFile = track.getLocalFile() else {
// Worst case we can stream the file // Worst case we can stream the file
logger.log("Unable to play local file. Resulting to streaming \(track.localFileId ?? "Unknown")") logger.log("Unable to play local file. Resulting to streaming \(track.localFileId ?? "Unknown")")
let relPath = track.metadata?.relPath ?? "" let urlstr = "\(Store.serverConfig!.address)/api/items/\(itemId)/file/\(ino)?token=\(Store.serverConfig!.token)"
let filepathEncoded = relPath.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let urlstr = "\(Store.serverConfig!.address)/s/item/\(itemId)/\(filepathEncoded ?? "")?token=\(Store.serverConfig!.token)"
let url = URL(string: urlstr)! let url = URL(string: urlstr)!
return AVURLAsset(url: url) return AVURLAsset(url: url)
} }