Selecting the download stream
As described in Starting the download, in options
you can set the:
Download bitrate with the
AVAssetDownloadTaskMinimumRequiredMediaBitrateKey
key.Download resolution with the
AVAssetDownloadTaskMinimumRequiredPresentationSizeKey
key. (Only available on iOS 14 and above.)
However, this cannot guarantee the actual download bitrate or resolution will equal the expected value; see the Apple documents AVAssetDownloadTaskMinimumRequiredMediaBitrateKey and AVAssetDownloadTaskMinimumRequiredPresentationSizeKey. To address this issue, the Player SDK provides the prepareDownload
API to return a preparing OTVPersistenceAsset
instance, from which the available bitrates and resolutions can be retrieved when the state changes to prepared
.
You will need to select one bitrate or resolution from the available bitrates array returned from OTVPersistenceAsset.mediaInfo.availableStreamInfo
, and set the chosen value back to OTVPersistenceAsset.mediaInfo.
selectedStreamInfo
.
To set up DRM, an OTVLicenseDelegate licenseDelegate
must be set by calling OTVPersistenceAsset.setupFPS(with: licenseDelegate);
ignore this for download of clear content.
When the OTVPersistenceAsset
instance has bitrate or resolution selected, and its DRM is set up, it can be passed to startDownload
to start the downloading.
Preparing for a download
// Observe the OTVAssetDownloadStateChanged before calling `prepareDownload`
NotificationCenter.default.addObserver(
self,
selector: #selector(self.stateChanged),
name: .OTVAssetDownloadStateChanged,
object: nil)
let assetURL = "https://d3bqrzf9w11pn3.cloudfront.net/basic_hls_bbb_encrypted/index.m3u8"
let assetName = "Big Buck Bunny"
// prepare for a download
// opyPersistenceAsset must be retained for later downloading
let opyPersistenceAsset = OTVPersistenceManager.shared.prepareDownload(url: assetURL, title: assetName)
// setup DRM for download
let licenseDelegate = OTVSSPLicenseDelegate(certificateURL: certificateURL, licenseURL: licenseURL)
licenseDelegate .setStream(token: token, with: assetURL)
opyPersistenceAsset.setupFPS(with: licenseDelegate )
// Handle state changed event
@objc func stateChanged(notification: NSNotification) {
let title = notification.userInfo![OTVPersistenceAsset.Keys.name] as! String
let stateString = notification.userInfo![OTVPersistenceAsset.Keys.downloadState] as! String
guard let state = OTVDownloadState(rawValue: stateString) else {
return
}
swicth(state) {
case .prepared:
handlePreparedDownload()
...
}
}
Selecting a stream to download
func handlePreparedDownload() {
let allStreamInfo = opyPersistenceAsset?.mediaInfo?.availableStreamInfo
var selectedStreamInfo: OTVStreamInfo?
for streamInfo in allStreamInfo {
let bitrate = streamInfo.bitrate
let resolution = streamInfo.resolution
// find the bitrate or resolution you want to download
selectedStreamInfo = streamInfo
break
}
opyPersistenceAsset?.mediaInfo?.selectedStreamInfo = selectedStreamInfo
//Download all available tracks (audio/subtitle) for the asset to be downloaded. Use option .preferred to download only the preferred tracks.
opyPersistenceAsset?.mediaSelections = .all
OTVPersistenceManager.shared.startDownload(asset: persistanceAsset, artwork: nil, options: nil)
}