Skip to content

Commit

Permalink
feat: add headers to ContentInfo and Data requests
Browse files Browse the repository at this point in the history
  • Loading branch information
eroscai committed Aug 14, 2021
1 parent 786b533 commit bac3d4d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 6 deletions.
10 changes: 9 additions & 1 deletion Example/SZAVPlayer/AudioView/AudioViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,15 @@ extension AudioViewController {
audioPlayer.play()
} else {
audioPlayer.pause()
let config = SZAVPlayerConfig(urlStr: audio.url, uniqueID: nil)
var config = SZAVPlayerConfig(urlStr: audio.url, uniqueID: nil)
config.headersForContentInfoRequest = [
"header1": "111",
"header2": "222"
]
config.headersForDataRequest = [
"header3": "333",
"header4": "444"
]
audioPlayer.setupPlayer(config: config)
}
playerControllerEvent = .playing
Expand Down
10 changes: 9 additions & 1 deletion Example/SZAVPlayer/VideoView/VideoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,15 @@ extension VideoViewController {
videoPlayer.play()
} else {
videoPlayer.pause()
let config = SZAVPlayerConfig(urlStr: video.url, uniqueID: nil, isVideo: true, isVideoOutputEnabled: enableVideoOutput)
var config = SZAVPlayerConfig(urlStr: video.url, uniqueID: nil, isVideo: true, isVideoOutputEnabled: enableVideoOutput)
config.headersForContentInfoRequest = [
"header1": "111",
"header2": "222"
]
config.headersForDataRequest = [
"header3": "333",
"header4": "444"
]
videoPlayer.setupPlayer(config: config)
}
playerControllerEvent = .playing
Expand Down
2 changes: 1 addition & 1 deletion SZAVPlayer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SZAVPlayer'
s.version = '1.3.0'
s.version = '1.3.1'
s.summary = 'Swift AVPlayer, based on AVAssetResourceLoaderDelegate, support cache.'

# This description is used to generate tags and improve search results.
Expand Down
3 changes: 3 additions & 0 deletions Sources/Classes/Model/SZAVPlayerConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public struct SZAVPlayerConfig {
/// local files or you don't needed custom loading.
public var disableCustomLoading: Bool = false

public var headersForContentInfoRequest: [String: String]?
public var headersForDataRequest: [String: String]?

public init(urlStr: String, uniqueID: String?, isVideo: Bool = false, isVideoOutputEnabled: Bool = false) {
self.urlStr = urlStr
self.uniqueID = uniqueID
Expand Down
5 changes: 4 additions & 1 deletion Sources/Classes/Request/SZAVPlayerDataLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ class SZAVPlayerDataLoader: NSObject {
private lazy var dataLoaderOperationQueue = createOperationQueue(name: "dataLoaderOperationQueue")
private let uniqueID: String
private let url: URL
private let config: SZAVPlayerConfig
private var mediaData: Data?

init(uniqueID: String, url: URL, callbackQueue: DispatchQueue) {
init(uniqueID: String, url: URL, config: SZAVPlayerConfig, callbackQueue: DispatchQueue) {
self.uniqueID = uniqueID
self.url = url
self.config = config
self.callbackQueue = callbackQueue
super.init()
}
Expand All @@ -41,6 +43,7 @@ class SZAVPlayerDataLoader: NSObject {
public func append(requestedRange: SZAVPlayerRange, dataRequest: SZAVPlayerDataRequest) {
let dataLoaderOperation = SZAVPlayerDataLoaderOperation(uniqueID: uniqueID,
url: url,
config: config,
requestedRange: requestedRange,
dataRequest: dataRequest)
dataLoaderOperation.delegate = self
Expand Down
7 changes: 6 additions & 1 deletion Sources/Classes/Request/SZAVPlayerDataLoaderOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SZAVPlayerDataLoaderOperation: Operation {
private var operationCompletion: CompletionHandler?
private let uniqueID: String
private let url: URL
private let config: SZAVPlayerConfig
private let requestedRange: SZAVPlayerRange
private let dataRequest: SZAVPlayerDataRequest
private var mediaData: Data?
Expand All @@ -39,12 +40,14 @@ class SZAVPlayerDataLoaderOperation: Operation {

init(uniqueID: String,
url: URL,
config: SZAVPlayerConfig,
requestedRange: SZAVPlayerRange,
dataRequest: SZAVPlayerDataRequest)
{
self.performQueue = DispatchQueue(label: "com.SZAVPlayer.DataLoaderOperation", qos: .background)
self.uniqueID = uniqueID
self.url = url
self.config = config
self.requestedRange = requestedRange
self.dataRequest = dataRequest
super.init()
Expand Down Expand Up @@ -195,7 +198,9 @@ extension SZAVPlayerDataLoaderOperation {
}

private func remoteRequestOperation(range: SZAVPlayerRange) -> SZAVPlayerRequestOperation {
let operation = SZAVPlayerRequestOperation(url: url, range: range)
let operation = SZAVPlayerRequestOperation(url: url,
range: range,
config: config)
operation.delegate = self

return operation
Expand Down
10 changes: 9 additions & 1 deletion Sources/Classes/Request/SZAVPlayerRequestOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class SZAVPlayerRequestOperation: Operation {
private var requestCompletion: CompletionHandler?
private lazy var session: URLSession = createSession()
private var task: URLSessionDataTask?
private let config: SZAVPlayerConfig

private var _finished: Bool = false
private var _executing: Bool = false
Expand All @@ -32,7 +33,8 @@ public class SZAVPlayerRequestOperation: Operation {
SZLogInfo("deinit")
}

public init(url: URL, range: SZAVPlayerRange?) {
public init(url: URL, range: SZAVPlayerRange?, config: SZAVPlayerConfig) {
self.config = config
self.performQueue = DispatchQueue(label: "com.SZAVPlayer.RequestOperation", qos: .background)
super.init()

Expand Down Expand Up @@ -148,6 +150,12 @@ extension SZAVPlayerRequestOperation {
startOffset = range.lowerBound
}

if let headers = config.headersForDataRequest {
for (key, value) in headers {
request.setValue(value, forHTTPHeaderField: key)
}
}

return session.dataTask(with: request)
}

Expand Down
7 changes: 7 additions & 0 deletions Sources/Classes/SZAVPlayerAssetLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,19 @@ extension SZAVPlayerAssetLoader {
request.setValue(rangeHeader, forHTTPHeaderField: "Range")
}

if let headers = config.headersForContentInfoRequest {
for (key, value) in headers {
request.setValue(value, forHTTPHeaderField: key)
}
}

return request
}

private func createDataLoader() -> SZAVPlayerDataLoader {
let loader = SZAVPlayerDataLoader(uniqueID: uniqueID,
url: url,
config: config,
callbackQueue: loaderQueue)
loader.delegate = self

Expand Down

0 comments on commit bac3d4d

Please sign in to comment.