Live streams fix (fix #174, #175)

This commit is contained in:
Arkadiusz Fal
2022-07-22 00:44:21 +02:00
parent 827c64719c
commit e6baaa519a
8 changed files with 81 additions and 20 deletions

View File

@@ -488,8 +488,14 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
}
private func extractStreams(from json: JSON) -> [Stream] {
extractFormatStreams(from: json["formatStreams"].arrayValue) +
extractAdaptiveFormats(from: json["adaptiveFormats"].arrayValue)
let hls = extractHLSStreams(from: json)
if json["liveNow"].boolValue {
return hls
}
return extractFormatStreams(from: json["formatStreams"].arrayValue) +
extractAdaptiveFormats(from: json["adaptiveFormats"].arrayValue) +
hls
}
private func extractFormatStreams(from streams: [JSON]) -> [Stream] {
@@ -538,6 +544,14 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
}
}
private func extractHLSStreams(from content: JSON) -> [Stream] {
if let hlsURL = content.dictionaryValue["hlsUrl"]?.url {
return [Stream(hlsURL: hlsURL)]
}
return []
}
private func extractRelated(from content: JSON) -> [Video] {
content
.dictionaryValue["recommendedVideos"]?
@@ -576,8 +590,8 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI {
private func extractCaptions(from content: JSON) -> [Captions] {
content["captions"].arrayValue.compactMap { details in
guard let baseURL = account.url,
let url = URL(string: baseURL + details["url"].stringValue) else { return nil }
let baseURL = account.url
guard let url = URL(string: baseURL + details["url"].stringValue) else { return nil }
return Captions(
label: details["label"].stringValue,

View File

@@ -132,6 +132,8 @@ final class AVPlayerBackend: PlayerBackend {
}
func seek(to time: CMTime, completionHandler: ((Bool) -> Void)?) {
guard !model.live else { return }
avPlayer.seek(
to: time,
toleranceBefore: .secondsInDefaultTimescale(1),

View File

@@ -140,6 +140,8 @@ final class MPVClient: ObservableObject {
options.append("sub-files-append=\"\(subURL)\"")
}
options.append("force-seekable=yes")
args.append(options.joined(separator: ","))
command("loadfile", args: args, returnValueCallback: completionHandler)

View File

@@ -273,7 +273,7 @@ final class PlayerModel: ObservableObject {
}
var videoDuration: TimeInterval? {
currentItem?.duration ?? currentVideo?.length ?? playerItemDuration?.seconds
playerItemDuration?.seconds ?? currentItem?.duration ?? currentVideo?.length
}
var time: CMTime? {
@@ -284,6 +284,18 @@ final class PlayerModel: ObservableObject {
currentVideo?.live ?? false
}
var playingLive: Bool {
guard live,
let videoDuration = videoDuration,
let time = backend.currentTime?.seconds else { return false }
return videoDuration - time < 30
}
var liveStreamInAVPlayer: Bool {
live && activeBackend == .appleAVPlayer
}
func togglePlay() {
backend.togglePlay()
}
@@ -751,7 +763,7 @@ final class PlayerModel: ObservableObject {
var nowPlayingInfo: [String: AnyObject] = [
MPMediaItemPropertyTitle: video.title as AnyObject,
MPMediaItemPropertyArtist: video.author as AnyObject,
MPNowPlayingInfoPropertyIsLiveStream: video.live as AnyObject,
MPNowPlayingInfoPropertyIsLiveStream: live as AnyObject,
MPNowPlayingInfoPropertyElapsedPlaybackTime: currentTime as AnyObject,
MPNowPlayingInfoPropertyPlaybackQueueCount: queue.count as AnyObject,
MPNowPlayingInfoPropertyPlaybackQueueIndex: 1 as AnyObject,