Skip to content

Commit 4d9bb98

Browse files
authored
Support customizing HTTP method and HTTP body (#108)
1 parent 31368a5 commit 4d9bb98

3 files changed

Lines changed: 54 additions & 6 deletions

File tree

AudioStreaming/Streaming/Audio Entry/AudioEntryProvider.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import AVFoundation
77

88
protocol AudioEntryProviding {
9+
func provideAudioEntry(url: URL, httpMethod: String?, httpBody: Data?, headers: [String: String]) -> AudioEntry
910
func provideAudioEntry(url: URL, headers: [String: String]) -> AudioEntry
1011
func provideAudioEntry(url: URL) -> AudioEntry
1112
}
@@ -25,7 +26,14 @@ final class AudioEntryProvider: AudioEntryProviding {
2526
}
2627

2728
func provideAudioEntry(url: URL, headers: [String: String]) -> AudioEntry {
28-
let source = self.source(for: url, headers: headers)
29+
let source = self.source(for: url, httpMethod: nil, httpBody: nil, headers: headers)
30+
return AudioEntry(source: source,
31+
entryId: AudioEntryId(id: url.absoluteString),
32+
outputAudioFormat: outputAudioFormat)
33+
}
34+
35+
func provideAudioEntry(url: URL, httpMethod: String?, httpBody: Data?, headers: [String: String]) -> AudioEntry {
36+
let source = self.source(for: url, httpMethod: httpMethod, httpBody: httpBody, headers: headers)
2937
return AudioEntry(source: source,
3038
entryId: AudioEntryId(id: url.absoluteString),
3139
outputAudioFormat: outputAudioFormat)
@@ -34,10 +42,12 @@ final class AudioEntryProvider: AudioEntryProviding {
3442
func provideAudioEntry(url: URL) -> AudioEntry {
3543
provideAudioEntry(url: url, headers: [:])
3644
}
37-
38-
func provideAudioSource(url: URL, headers: [String: String]) -> AudioStreamSource {
45+
46+
func provideAudioSource(url: URL, httpMethod: String?, httpBody: Data?, headers: [String: String]) -> AudioStreamSource {
3947
RemoteAudioSource(networking: networkingClient,
4048
url: url,
49+
httpMethod: httpMethod,
50+
httpBody: httpBody,
4151
underlyingQueue: underlyingQueue,
4252
httpHeaders: headers)
4353
}
@@ -46,10 +56,10 @@ final class AudioEntryProvider: AudioEntryProviding {
4656
FileAudioSource(url: url, underlyingQueue: underlyingQueue)
4757
}
4858

49-
func source(for url: URL, headers: [String: String]) -> CoreAudioStreamSource {
59+
func source(for url: URL, httpMethod: String?, httpBody: Data?, headers: [String: String]) -> CoreAudioStreamSource {
5060
guard !url.isFileURL else {
5161
return provideFileAudioSource(url: url)
5262
}
53-
return provideAudioSource(url: url, headers: headers)
63+
return provideAudioSource(url: url, httpMethod: httpMethod, httpBody: httpBody, headers: headers)
5464
}
5565
}

AudioStreaming/Streaming/Audio Source/RemoteAudioSource.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class RemoteAudioSource: AudioStreamSource {
2525
}
2626

2727
private let url: URL
28+
private let httpMethod: String?
29+
private let httpBody: Data?
2830
private let networkingClient: NetworkingClient
2931
private var streamRequest: NetworkDataStream?
3032

@@ -61,12 +63,16 @@ public class RemoteAudioSource: AudioStreamSource {
6163
netStatusProvider: NetStatusProvider,
6264
retrier: Retrier,
6365
url: URL,
66+
httpMethod: String?,
67+
httpBody: Data?,
6468
underlyingQueue: DispatchQueue,
6569
httpHeaders: [String: String])
6670
{
6771
networkingClient = networking
6872
metadataStreamProcessor = metadataStreamSource
6973
self.url = url
74+
self.httpMethod = httpMethod
75+
self.httpBody = httpBody
7076
additionalRequestHeaders = httpHeaders
7177
relativePosition = 0
7278
seekOffset = 0
@@ -83,9 +89,11 @@ public class RemoteAudioSource: AudioStreamSource {
8389
mp4Restructure = RemoteMp4Restructure(url: url, networking: networkingClient)
8490
startNetworkService()
8591
}
86-
92+
8793
convenience init(networking: NetworkingClient,
8894
url: URL,
95+
httpMethod: String?,
96+
httpBody: Data?,
8997
underlyingQueue: DispatchQueue,
9098
httpHeaders: [String: String])
9199
{
@@ -100,6 +108,21 @@ public class RemoteAudioSource: AudioStreamSource {
100108
netStatusProvider: netStatusProvider,
101109
retrier: retrierTimeout,
102110
url: url,
111+
httpMethod: httpMethod,
112+
httpBody: httpBody,
113+
underlyingQueue: underlyingQueue,
114+
httpHeaders: httpHeaders)
115+
}
116+
117+
convenience init(networking: NetworkingClient,
118+
url: URL,
119+
underlyingQueue: DispatchQueue,
120+
httpHeaders: [String: String])
121+
{
122+
self.init(networking: networking,
123+
url: url,
124+
httpMethod: nil,
125+
httpBody: nil,
103126
underlyingQueue: underlyingQueue,
104127
httpHeaders: httpHeaders)
105128
}
@@ -347,6 +370,8 @@ public class RemoteAudioSource: AudioStreamSource {
347370
urlRequest.networkServiceType = .avStreaming
348371
urlRequest.cachePolicy = .reloadIgnoringLocalCacheData
349372
urlRequest.timeoutInterval = 60
373+
urlRequest.httpMethod = httpMethod
374+
urlRequest.httpBody = httpBody
350375

351376
for header in additionalRequestHeaders {
352377
urlRequest.addValue(header.value, forHTTPHeaderField: header.key)
@@ -366,6 +391,8 @@ public class RemoteAudioSource: AudioStreamSource {
366391
urlRequest.networkServiceType = .avStreaming
367392
urlRequest.cachePolicy = .reloadIgnoringLocalCacheData
368393
urlRequest.timeoutInterval = 60
394+
urlRequest.httpMethod = httpMethod
395+
urlRequest.httpBody = httpBody
369396

370397
for header in additionalRequestHeaders {
371398
urlRequest.addValue(header.value, forHTTPHeaderField: header.key)

AudioStreaming/Streaming/AudioPlayer/AudioPlayer.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,17 @@ open class AudioPlayer {
202202
let audioEntry = entryProvider.provideAudioEntry(url: url, headers: headers)
203203
play(audioEntry: audioEntry)
204204
}
205+
206+
/// Starts the audio playback for the given URL
207+
///
208+
/// - parameter url: A `URL` specifying the audio context to be played.
209+
/// - parameter httpMethod: A `String` specifying the HTTP method to use (e.g. "GET", "POST").
210+
/// - parameter httpBody: A "Data" specifying the HTTP request body, if any.
211+
/// - parameter headers: A `Dictionary` specifying any additional headers to be pass to the network request.
212+
public func play(url: URL, httpMethod: String?, httpBody: Data?, headers: [String: String]) {
213+
let audioEntry = entryProvider.provideAudioEntry(url: url, httpMethod: httpMethod, httpBody: httpBody, headers: headers)
214+
play(audioEntry: audioEntry)
215+
}
205216

206217
/// Starts the audio playback for the supplied stream
207218
///

0 commit comments

Comments
 (0)