Skip to content

Commit

Permalink
Added support for adaptive episode streams
Browse files Browse the repository at this point in the history
  • Loading branch information
4np committed Mar 21, 2017
1 parent c1b94ba commit 570bf43
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
32 changes: 18 additions & 14 deletions NPOKit/NPOKit/NPOManager+Stream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,28 @@ extension NPOManager {
return
}

// https://ida.omroep.nl/app.php/BNN_101382709?adaptive=yes&token=62i5nn3ci2vphfb8or665jqgv6
let url = "http://ida.omroep.nl/app.php/\(mid)?adaptive=yes&token=\(token)"
//DDLogDebug("episode url -> \(url)")

self?.getVideoStream(forURL: url, andLiveChannel: nil, withCompletion: completed)
}
}

public func getVideoStream(forLiveChannel channel: NPOLive, withCompletion completed: @escaping (_ url: URL?, _ error: NPOError?) -> Void = { url, error in }) {
self.getToken { [weak self] token, error in
guard let token = token else {
completed(nil, error)
return
}

let channelMID = channel.rawValue
let url = "http://ida.omroep.nl/app.php/\(channelMID)?adaptive=yes&token=\(token)"
//DDLogDebug("live url: \(url)")
self?.getVideoStream(forURL: url, andLiveChannel: channel, withCompletion: completed)
}
}

private func getVideoStream(forURL url: String, andLiveChannel liveChannel: NPOLive?, withCompletion completed: @escaping (_ url: URL?, _ error: NPOError?) -> Void = { url, error in }) {
let _ = self.fetchModel(ofType: NPOVideo.self, fromURL: url) { video, error in
guard let video = video else {
Expand All @@ -106,26 +121,15 @@ extension NPOManager {
// set live channel, if we know it
video.channel = liveChannel

guard let stream = video.highestQualityStream else {
guard let stream = video.preferredQualityStream else {
let error = error ?? NPOError.networkError("Could not fetch stream for video model (url: \(url))")
completed(nil, error)
return
}

stream.getVideoStreamURL(withCompletion: completed)
}
}

public func getVideoStream(forLiveChannel channel: NPOLive, withCompletion completed: @escaping (_ url: URL?, _ error: NPOError?) -> Void = { url, error in }) {
self.getToken { [weak self] token, error in
guard let token = token else {
completed(nil, error)
return
}
//DDLogDebug("Stream quality \(stream.type)")

let url = "http://ida.omroep.nl/app.php/\(channel.rawValue)?adaptive=yes&token=\(token)"
//DDLogDebug("live url: \(url)")
self?.getVideoStream(forURL: url, andLiveChannel: channel, withCompletion: completed)
stream.getVideoStreamURL(withCompletion: completed)
}
}
}
7 changes: 4 additions & 3 deletions NPOKit/NPOKit/NPOStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import ObjectMapper
import CocoaLumberjack

public enum NPOStreamType: String {
case live = "Live"
case adaptive = "Adaptive"
case high = "Hoog"
case normal = "Normaal"
case low = "Laag"
case live = "Live"

internal static let preferredOrder: [NPOStreamType] = [.high, .normal, .low]
internal static let preferredOrder: [NPOStreamType] = [.adaptive, .high, .normal, .low]
}

open class NPOStream: Mappable, CustomDebugStringConvertible {
Expand Down Expand Up @@ -65,7 +66,7 @@ open class NPOStream: Mappable, CustomDebugStringConvertible {
}

switch type {
case .high, .normal, .low:
case .adaptive, .high, .normal, .low:
self.getVideoStreamURL(forURL: url, withCompletion: completed)
case .live:
completed(url, nil)
Expand Down
2 changes: 1 addition & 1 deletion NPOKit/NPOKit/NPOVideo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ open class NPOVideo: Mappable, CustomDebugStringConvertible {

// MARK: Convenience

public var highestQualityStream: NPOStream? {
public var preferredQualityStream: NPOStream? {
var preferredEpisodeQualityOrder = NPOManager.sharedInstance.preferredEpisodeQualityOrder

// not really a quality, but the same back end returns live streams as well
Expand Down
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,6 @@ If you have set up a firewall to filter _outbound_ network requests, make sure t

_Note: as the video and image content URLs come from the APIs, although unlikely, these URLs *might* change_

# Limited bandwidth

While the live streams are adaptive (and hence will optimise for the available bandwidth), the episode streams are actually based on order of quality which defaults to _Hoog_ (high), _Normaal_ (normal), _Laag_ (low) (in Dutch as the NPO uses these values as well). This basically means that when an episode has a high quality stream available, that is what will get played. Falling back to normal quality or lastly to low quality.

If your internet connection is limited and you experience issues playing episodes you can change this order to (for example) _Laag,Normaal,Hoog_ and see if the episode stream perform more reliably:

![videoqualityorderofpreference](https://cloud.githubusercontent.com/assets/1049693/24160516/5d113fe4-0e62-11e7-801f-885021617333.jpg)

# Donate

![donations-banner](https://cloud.githubusercontent.com/assets/1049693/23892645/c292896c-089b-11e7-9c9a-2c8f60d64c0a.jpg)
Expand Down
2 changes: 1 addition & 1 deletion UitzendingGemist/Settings.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>UGPreferedEpisodeQualityOrder</key>
<string>Hoog,Normaal,Laag</string>
<string>Adaptive,Hoog,Normaal,Laag</string>
<key>UGEnablePlayerDebugging</key>
<string>NO</string>
</dict>
Expand Down

0 comments on commit 570bf43

Please sign in to comment.