2022-02-17 01:53:11 +05:30
import CoreMedia
import Defaults
import Foundation
2024-08-25 20:53:04 +05:30
import Logging
2022-08-28 22:48:49 +05:30
#if ! os ( macOS )
import UIKit
#endif
2022-02-17 01:53:11 +05:30
protocol PlayerBackend {
2022-11-11 03:49:34 +05:30
var suggestedPlaybackRates : [ Double ] { get }
2022-11-25 02:06:05 +05:30
var model : PlayerModel { get }
var controls : PlayerControlsModel { get }
var playerTime : PlayerTimeModel { get }
var networkState : NetworkStateModel { get }
2022-02-17 01:53:11 +05:30
var stream : Stream ? { get set }
var video : Video ? { get set }
var currentTime : CMTime ? { get }
var loadedVideo : Bool { get }
var isLoadingVideo : Bool { get }
Conditional proxying
I added a new feature. When instances are not proxied, Yattee first checks the URL to make sure it is not a restricted video. Usually, music videos and sports content can only be played back by the same IP address that requested the URL in the first place. That is why some videos do not play when the proxy is disabled.
This approach has multiple advantages. First and foremost, It reduced the load on Invidious/Piped instances, since users can now directly access the videos without going through the instance, which might be severely bandwidth limited. Secondly, users don't need to manually turn on the proxy when they want to watch IP address bound content, since Yattee automatically proxies such content.
Furthermore, adding the proxy option allows mitigating some severe playback issues with invidious instances. Invidious by default returns proxied URLs for videos, and due to some bug in the Invidious proxy, scrubbing or continuing playback at a random timestamp can lead to severe wait times for the users.
This should fix numerous playback issues: #666, #626, #590, #585, #498, #457, #400
2024-05-09 23:37:55 +05:30
var hasStarted : Bool { get }
var isPaused : Bool { get }
2022-02-17 01:53:11 +05:30
var isPlaying : Bool { get }
2022-06-18 18:09:49 +05:30
var isSeeking : Bool { get }
2022-02-17 01:53:11 +05:30
var playerItemDuration : CMTime ? { get }
2022-07-09 05:51:04 +05:30
var aspectRatio : Double { get }
2022-08-24 02:59:50 +05:30
var controlsUpdates : Bool { get }
2022-07-09 05:51:04 +05:30
2022-11-10 22:41:28 +05:30
var videoWidth : Double ? { get }
var videoHeight : Double ? { get }
2022-02-17 01:53:11 +05:30
func canPlay ( _ stream : Stream ) -> Bool
2022-11-11 03:49:34 +05:30
func canPlayAtRate ( _ rate : Double ) -> Bool
2022-02-17 01:53:11 +05:30
func playStream (
_ stream : Stream ,
of video : Video ,
preservingTime : Bool ,
upgrading : Bool
)
func play ( )
func pause ( )
func togglePlay ( )
func stop ( )
2022-08-29 17:25:23 +05:30
func seek ( to time : CMTime , seekType : SeekType , completionHandler : ( ( Bool ) -> Void ) ? )
func seek ( to seconds : Double , seekType : SeekType , completionHandler : ( ( Bool ) -> Void ) ? )
2022-02-17 01:53:11 +05:30
2022-11-11 03:30:17 +05:30
func setRate ( _ rate : Double )
2022-02-17 01:53:11 +05:30
func closeItem ( )
2022-08-19 04:10:46 +05:30
func closePiP ( )
2022-02-17 01:53:11 +05:30
2022-08-21 02:01:03 +05:30
func startMusicMode ( )
func stopMusicMode ( )
2022-08-28 22:48:49 +05:30
func getTimeUpdates ( )
func updateControls ( completionHandler : ( ( ) -> Void ) ? )
2022-02-17 01:53:11 +05:30
func startControlsUpdates ( )
func stopControlsUpdates ( )
2022-08-21 02:01:03 +05:30
func didChangeTo ( )
2022-06-25 05:09:29 +05:30
func setNeedsNetworkStateUpdates ( _ needsUpdates : Bool )
2022-06-18 18:09:49 +05:30
2022-02-17 01:53:11 +05:30
func setNeedsDrawing ( _ needsDrawing : Bool )
2022-03-27 17:12:20 +05:30
func setSize ( _ width : Double , _ height : Double )
2022-11-13 17:58:25 +05:30
func cancelLoads ( )
2022-02-17 01:53:11 +05:30
}
extension PlayerBackend {
2024-08-25 20:53:04 +05:30
var logger : Logger {
return Logger ( label : " stream.yattee.player.backend " )
}
2022-08-29 17:25:23 +05:30
func seek ( to time : CMTime , seekType : SeekType , completionHandler : ( ( Bool ) -> Void ) ? = nil ) {
2022-09-02 04:35:31 +05:30
model . seek . registerSeek ( at : time , type : seekType , restore : currentTime )
2022-08-28 22:48:49 +05:30
seek ( to : time , seekType : seekType , completionHandler : completionHandler )
2022-02-17 01:53:11 +05:30
}
2022-08-29 17:25:23 +05:30
func seek ( to seconds : Double , seekType : SeekType , completionHandler : ( ( Bool ) -> Void ) ? = nil ) {
2022-08-28 22:48:49 +05:30
let seconds = CMTime . secondsInDefaultTimescale ( seconds )
2022-09-02 04:35:31 +05:30
model . seek . registerSeek ( at : seconds , type : seekType , restore : currentTime )
2022-08-28 22:48:49 +05:30
seek ( to : seconds , seekType : seekType , completionHandler : completionHandler )
2022-02-17 01:53:11 +05:30
}
2022-08-29 17:25:23 +05:30
func seek ( relative time : CMTime , seekType : SeekType , completionHandler : ( ( Bool ) -> Void ) ? = nil ) {
2022-09-28 19:57:01 +05:30
if let currentTime , let duration = playerItemDuration {
2022-08-28 22:48:49 +05:30
let seekTime = min ( max ( 0 , currentTime . seconds + time . seconds ) , duration . seconds )
2022-09-02 04:35:31 +05:30
model . seek . registerSeek ( at : . secondsInDefaultTimescale ( seekTime ) , type : seekType , restore : currentTime )
2022-08-28 22:48:49 +05:30
seek ( to : seekTime , seekType : seekType , completionHandler : completionHandler )
}
2022-02-17 01:53:11 +05:30
}
2022-07-11 03:54:56 +05:30
func eofPlaybackModeAction ( ) {
2022-12-19 00:09:03 +05:30
let loopAction = {
model . backend . seek ( to : . zero , seekType : . loopRestart ) { _ in
self . model . play ( )
}
}
guard model . playbackMode != . loopOne else {
loopAction ( )
return
}
2023-04-22 17:26:25 +05:30
switch model . playbackMode {
case . queue , . shuffle :
model . prepareCurrentItemForHistory ( finished : true )
if model . queue . isEmpty {
2023-11-28 04:19:18 +05:30
#if os ( tvOS )
if Defaults [ . closeVideoOnEOF ] {
2023-05-21 16:03:59 +05:30
if model . activeBackend = = . appleAVPlayer {
model . avPlayerBackend . controller ? . dismiss ( animated : false )
}
2023-11-28 04:19:18 +05:30
model . resetQueue ( )
model . hide ( )
}
#else
if Defaults [ . closeVideoOnEOF ] {
model . resetQueue ( )
model . hide ( )
2024-08-21 02:26:55 +05:30
} else if Defaults [ . exitFullscreenOnEOF ] , model . playingFullScreen {
2023-11-28 04:19:18 +05:30
model . exitFullScreen ( )
}
#endif
2022-12-19 00:09:03 +05:30
} else {
2023-04-22 17:26:25 +05:30
model . advanceToNextItem ( )
2022-12-19 00:09:03 +05:30
}
2023-04-22 17:26:25 +05:30
case . loopOne :
loopAction ( )
case . related :
guard let item = model . autoplayItem else { return }
model . resetAutoplay ( )
model . advanceToItem ( item )
2022-12-19 00:09:03 +05:30
}
2022-07-11 03:54:56 +05:30
}
2022-08-28 22:48:49 +05:30
2024-04-26 15:57:25 +05:30
func bestPlayable ( _ streams : [ Stream ] , maxResolution : ResolutionSetting , formatOrder : [ QualityProfile . Format ] ) -> Stream ? {
2024-08-25 20:53:04 +05:30
logger . info ( " Starting bestPlayable function " )
logger . info ( " Total streams received: \( streams . count ) " )
logger . info ( " Max resolution allowed: \( String ( describing : maxResolution . value ) ) " )
logger . info ( " Format order: \( formatOrder ) " )
// F i l t e r o u t n o n - H L S s t r e a m s a n d s t r e a m s w i t h r e s o l u t i o n m o r e t h a n m a x R e s o l u t i o n
2024-05-19 16:09:47 +05:30
let nonHLSStreams = streams . filter {
2024-08-25 20:53:04 +05:30
let isHLS = $0 . kind = = . hls
2024-08-31 17:52:28 +05:30
// S a f e l y u n w r a p r e s o l u t i o n a n d m a x R e s o l u t i o n . v a l u e t o a v o i d c r a s h e s
let isWithinResolution = ( $0 . resolution != nil && maxResolution . value != nil ) ? $0 . resolution ! <= maxResolution . value ! : false
2024-08-25 20:53:04 +05:30
logger . info ( " Stream ID: \( $0 . id ) - Kind: \( String ( describing : $0 . kind ) ) - Resolution: \( String ( describing : $0 . resolution ) ) - Bitrate: \( $0 . bitrate ? ? 0 ) " )
logger . info ( " Is HLS: \( isHLS ) , Is within resolution: \( isWithinResolution ) " )
return ! isHLS && isWithinResolution
2024-05-19 16:09:47 +05:30
}
2024-08-25 20:53:04 +05:30
logger . info ( " Non-HLS streams after filtering: \( nonHLSStreams . count ) " )
2024-05-11 17:38:40 +05:30
2024-08-25 20:53:04 +05:30
// F i n d m a x r e s o l u t i o n a n d b i t r a t e f r o m n o n - H L S s t r e a m s
2024-05-19 16:09:47 +05:30
let bestResolutionStream = nonHLSStreams . max { $0 . resolution < $1 . resolution }
let bestBitrateStream = nonHLSStreams . max { $0 . bitrate ? ? 0 < $1 . bitrate ? ? 0 }
2024-05-13 11:24:24 +05:30
2024-08-25 20:53:04 +05:30
logger . info ( " Best resolution stream: \( String ( describing : bestResolutionStream ? . id ) ) with resolution: \( String ( describing : bestResolutionStream ? . resolution ) ) " )
logger . info ( " Best bitrate stream: \( String ( describing : bestBitrateStream ? . id ) ) with bitrate: \( String ( describing : bestBitrateStream ? . bitrate ) ) " )
2024-05-19 16:09:47 +05:30
let bestResolution = bestResolutionStream ? . resolution ? ? maxResolution . value
let bestBitrate = bestBitrateStream ? . bitrate ? ? bestResolutionStream ? . resolution . bitrate ? ? maxResolution . value . bitrate
2024-05-11 17:38:40 +05:30
2024-08-25 20:53:04 +05:30
logger . info ( " Final best resolution selected: \( String ( describing : bestResolution ) ) " )
logger . info ( " Final best bitrate selected: \( bestBitrate ) " )
let adjustedStreams = streams . map { stream in
2024-04-26 15:57:25 +05:30
if stream . kind = = . hls {
2024-08-25 20:53:04 +05:30
logger . info ( " Adjusting HLS stream ID: \( stream . id ) " )
2024-05-19 16:09:47 +05:30
stream . resolution = bestResolution
stream . bitrate = bestBitrate
2024-04-26 15:57:25 +05:30
stream . format = . hls
} else if stream . kind = = . stream {
2024-08-25 20:53:04 +05:30
logger . info ( " Adjusting non-HLS stream ID: \( stream . id ) " )
2024-04-26 15:57:25 +05:30
stream . format = . stream
}
return stream
}
2024-08-25 20:53:04 +05:30
let filteredStreams = adjustedStreams . filter { stream in
2024-08-31 17:52:28 +05:30
// S a f e l y u n w r a p r e s o l u t i o n a n d m a x R e s o l u t i o n . v a l u e t o a v o i d c r a s h e s
let isWithinResolution = ( stream . resolution != nil && maxResolution . value != nil ) ? stream . resolution ! <= maxResolution . value ! : false
2024-08-25 20:53:04 +05:30
logger . info ( " Filtered stream ID: \( stream . id ) - Is within max resolution: \( isWithinResolution ) " )
return isWithinResolution
2024-04-26 15:57:25 +05:30
}
2024-08-25 20:53:04 +05:30
logger . info ( " Filtered streams count after adjustments: \( filteredStreams . count ) " )
let bestStream = filteredStreams . max { lhs , rhs in
2024-04-26 15:57:25 +05:30
if lhs . resolution = = rhs . resolution {
guard let lhsFormat = QualityProfile . Format ( rawValue : lhs . format . rawValue ) ,
let rhsFormat = QualityProfile . Format ( rawValue : rhs . format . rawValue )
else {
2024-08-25 20:53:04 +05:30
logger . info ( " Failed to extract lhsFormat or rhsFormat for streams \( lhs . id ) and \( rhs . id ) " )
2024-04-26 15:57:25 +05:30
return false
}
let lhsFormatIndex = formatOrder . firstIndex ( of : lhsFormat ) ? ? Int . max
let rhsFormatIndex = formatOrder . firstIndex ( of : rhsFormat ) ? ? Int . max
2024-08-25 20:53:04 +05:30
logger . info ( " Comparing formats for streams \( lhs . id ) and \( rhs . id ) - LHS Format Index: \( lhsFormatIndex ) , RHS Format Index: \( rhsFormatIndex ) " )
2024-04-26 15:57:25 +05:30
return lhsFormatIndex > rhsFormatIndex
}
2024-08-25 20:53:04 +05:30
logger . info ( " Comparing resolutions for streams \( lhs . id ) and \( rhs . id ) - LHS Resolution: \( String ( describing : lhs . resolution ) ) , RHS Resolution: \( String ( describing : rhs . resolution ) ) " )
2024-04-26 15:57:25 +05:30
return lhs . resolution < rhs . resolution
}
2024-08-25 20:53:04 +05:30
logger . info ( " Best stream selected: \( String ( describing : bestStream ? . id ) ) with resolution: \( String ( describing : bestStream ? . resolution ) ) and format: \( String ( describing : bestStream ? . format ) ) " )
return bestStream
2024-04-26 15:57:25 +05:30
}
2022-08-28 22:48:49 +05:30
func updateControls ( completionHandler : ( ( ) -> Void ) ? = nil ) {
2024-08-25 20:53:04 +05:30
logger . info ( " updating controls " )
2022-08-28 22:48:49 +05:30
guard model . presentingPlayer , ! model . controls . presentingOverlays else {
2024-08-25 20:53:04 +05:30
logger . info ( " ignored controls update " )
2022-08-28 22:48:49 +05:30
completionHandler ? ( )
return
}
DispatchQueue . main . async ( qos : . userInteractive ) {
#if ! os ( macOS )
guard UIApplication . shared . applicationState != . background else {
2024-08-25 20:53:04 +05:30
logger . info ( " not performing controls updates in background " )
2022-08-28 22:48:49 +05:30
completionHandler ? ( )
return
}
#endif
2022-09-01 00:54:46 +05:30
PlayerTimeModel . shared . currentTime = self . currentTime ? ? . zero
PlayerTimeModel . shared . duration = self . playerItemDuration ? ? . zero
2022-08-28 22:48:49 +05:30
completionHandler ? ( )
}
}
2022-02-17 01:53:11 +05:30
}