This repository has been archived by the owner on Jan 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #330 from clappr/feature/playback_bitrate
Move playback bitrate logic
- Loading branch information
Showing
5 changed files
with
88 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
52 changes: 52 additions & 0 deletions
52
Sources/Clappr/Classes/Extension/AVFoundationPlayback+DVR.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import AVFoundation | ||
|
||
extension AVFoundationPlayback { | ||
open override var minDvrSize: Double { | ||
return options[kMinDvrSize] as? Double ?? 60.0 | ||
} | ||
|
||
open override var isDvrInUse: Bool { | ||
if state == .paused && isDvrAvailable { return true } | ||
guard let end = dvrWindowEnd, playbackType == .live else { return false } | ||
guard let currentTime = player?.currentTime().seconds else { return false } | ||
return end - liveHeadTolerance > currentTime | ||
} | ||
|
||
open override var isDvrAvailable: Bool { | ||
guard playbackType == .live else { return false } | ||
return duration >= minDvrSize | ||
} | ||
|
||
open override var currentDate: Date? { | ||
return player?.currentItem?.currentDate() | ||
} | ||
|
||
open override var seekableTimeRanges: [NSValue] { | ||
guard let ranges = player?.currentItem?.seekableTimeRanges else { return [] } | ||
return ranges | ||
} | ||
|
||
open override var loadedTimeRanges: [NSValue] { | ||
guard let ranges = player?.currentItem?.loadedTimeRanges else { return [] } | ||
return ranges | ||
} | ||
|
||
open override var epochDvrWindowStart: TimeInterval { | ||
guard let currentDate = currentDate else { return 0 } | ||
return currentDate.timeIntervalSince1970 - position | ||
} | ||
|
||
var dvrWindowStart: Double? { | ||
guard let end = dvrWindowEnd, isDvrAvailable, playbackType == .live else { return nil } | ||
return end - duration | ||
} | ||
|
||
var dvrWindowEnd: Double? { | ||
guard isDvrAvailable, playbackType == .live else { return nil } | ||
return seekableTimeRanges.max { rangeA, rangeB in rangeA.timeRangeValue.end.seconds < rangeB.timeRangeValue.end.seconds }?.timeRangeValue.end.seconds | ||
} | ||
|
||
fileprivate var liveHeadTolerance: Double { | ||
return 5 | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Sources/Clappr/Classes/Extension/AVFoundationPlayback+LogEvent.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import AVFoundation | ||
|
||
extension AVFoundationPlayback { | ||
var lastLogEvent: AVPlayerItemAccessLogEvent? { | ||
return player?.currentItem?.accessLog()?.events.last | ||
} | ||
} | ||
|
||
extension AVPlayerItemAccessLogEvent { | ||
var bitrate: Double { | ||
if segmentsDownloadedDuration > 0 { | ||
return indicatedBitrate | ||
} | ||
return observedBitrate | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters