-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathAudioDataManager.swift
More file actions
255 lines (203 loc) · 9.2 KB
/
AudioDataManager.swift
File metadata and controls
255 lines (203 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// AudioDataManager.swift
// SwiftAudioPlayer
//
// Created by Tanha Kabir on 2019-01-29.
// Copyright © 2019 Tanha Kabir, Jon Mercer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
protocol AudioDataManagable {
var currentStreamFinished: Bool { get }
var currentStreamFinishedWithDuration: Duration { get }
var numberOfQueued: Int { get }
var numberOfActive: Int { get }
var allowCellular: Bool { get set }
var downloadDirectory: FileManager.SearchPathDirectory { get }
func setHTTPHeaderFields(_ fields: [String: String]?)
func setBackgroundCompletionHandler(_ completionHandler: @escaping () -> ())
func setAllowCellularDownloadPreference(_ preference: Bool)
func setDownloadDirectory(_ dir: FileManager.SearchPathDirectory)
func clear()
func updateDuration(d: Duration)
//Director pattern
func attach(callback: @escaping (_ id: ID, _ progress: Double)->())
func startStream(withRemoteURL url: AudioURL, callback: @escaping (StreamProgressPTO) -> ()) //called by throttler
func pauseStream(withRemoteURL url: AudioURL)
func resumeStream(withRemoteURL url: AudioURL)
func seekStream(withRemoteURL url: AudioURL, toByteOffset offset: UInt64)
func deleteStream(withRemoteURL url: AudioURL)
func getPersistedUrl(withRemoteURL url: AudioURL) -> URL?
func startDownload(withRemoteURL url: AudioURL, completion: @escaping (URL) -> ())
func cancelDownload(withRemoteURL url: AudioURL)
func deleteDownload(withLocalURL url: URL)
}
class AudioDataManager: AudioDataManagable {
var currentStreamFinishedWithDuration: Duration = 0
var allowCellular: Bool = true
var downloadDirectory: FileManager.SearchPathDirectory = .documentDirectory
public var currentStreamFinished = false
public var totalStreamedDuration = 0
static let shared: AudioDataManagable = AudioDataManager()
// When we're streaming we want to stagger the size of data push up from disk to prevent the phone from freezing. We push up data of this chunk size every couple milliseconds.
private let MAXIMUM_DATA_SIZE_TO_PUSH = 37744
private let TIME_IN_BETWEEN_STREAM_DATA_PUSH = 198
var backgroundCompletion: ()-> Void = {} // set by AppDelegate
//This is the first case where a DAO passes a closure to a singleon that receives delegate calls from the OS. When the delegate from the OS is called, this class calls the DAO's closure. We pretty much set up a stream from the delegate call to the director (and all the items subscribed to that director)
private var globalDownloadProgressCallback: (String, Double)-> Void = {_,_ in }
private var downloadWorker: AudioDataDownloadable!
private var streamWorker: AudioDataStreamable!
private var streamingCallbacks = [(ID, (StreamProgressPTO)->())]()
private var originalDataCountForDownloadedAudio = 0
var numberOfQueued: Int {
return downloadWorker.numberOfQueued
}
var numberOfActive: Int {
return downloadWorker.numberOfActive
}
private init() {
downloadWorker = AudioDownloadWorker(
allowCellular: allowCellular,
progressCallback: downloadProgressListener,
doneCallback: downloadDoneListener,
backgroundDownloadCallback: backgroundCompletion)
streamWorker = AudioStreamWorker(
progressCallback: streamProgressListener,
doneCallback: streamDoneListener)
}
func updateDuration(d: Duration) {
currentStreamFinishedWithDuration = d
}
func clear() {
streamingCallbacks = []
}
func setHTTPHeaderFields(_ fields: [String: String]?) {
streamWorker.HTTPHeaderFields = fields
downloadWorker.HTTPHeaderFields = fields
}
func setBackgroundCompletionHandler(_ completionHandler: @escaping () -> ()) {
backgroundCompletion = completionHandler
}
func setAllowCellularDownloadPreference(_ preference: Bool) {
allowCellular = preference
}
func setDownloadDirectory(_ dir: FileManager.SearchPathDirectory) {
downloadDirectory = dir
}
func attach(callback: @escaping (_ id: ID, _ progress: Double)->()) {
globalDownloadProgressCallback = callback
}
}
// MARK:- Streaming
extension AudioDataManager {
func startStream(withRemoteURL url: AudioURL, callback: @escaping (StreamProgressPTO) -> ()) {
currentStreamFinished = false
if let data = FileStorage.Audio.read(url.key) {
let dto = StreamProgressDTO.init(progress: 1.0, data: data, totalBytesExpected: Int64(data.count))
callback(StreamProgressPTO(dto: dto))
return
}
let exists = streamingCallbacks.contains { (cb: (ID, (StreamProgressPTO) -> ())) -> Bool in
return cb.0 == url.key
}
if !exists {
streamingCallbacks.append((url.key, callback))
}
downloadWorker.stop(withID: url.key) { [weak self] (fetchedData: Data?, totalBytesExpected: Int64?) in
self?.downloadWorker.pauseAllActive()
self?.streamWorker.start(withID: url.key, withRemoteURL: url, withInitialData: fetchedData, andTotalBytesExpectedPreviously: totalBytesExpected)
}
}
func pauseStream(withRemoteURL url: AudioURL) {
guard streamWorker.getRunningID() == url.key else { return }
streamWorker.pause(withId: url.key)
}
func resumeStream(withRemoteURL url: AudioURL) {
streamWorker.resume(withId: url.key)
}
func seekStream(withRemoteURL url: AudioURL, toByteOffset offset: UInt64) {
currentStreamFinished = false
streamWorker.seek(withId: url.key, withByteOffset: offset)
}
func deleteStream(withRemoteURL url: AudioURL) {
currentStreamFinished = false
streamWorker.stop(withId: url.key)
streamingCallbacks.removeAll { (cb: (ID, (StreamProgressPTO) -> ())) -> Bool in
return cb.0 == url.key
}
}
}
// MARK:- Download
extension AudioDataManager {
func getPersistedUrl(withRemoteURL url: AudioURL) -> URL? {
return FileStorage.Audio.locate(url.key)
}
func startDownload(withRemoteURL url: AudioURL, completion: @escaping (URL) -> ()) {
let key = url.key
if let savedUrl = FileStorage.Audio.locate(key), FileStorage.Audio.isStored(key) {
globalDownloadProgressCallback(key, 1.0)
completion(savedUrl)
return
}
if let currentProgress = downloadWorker.getProgressOfDownload(withID: key) {
globalDownloadProgressCallback(key, currentProgress)
return
}
// TODO: check if we already streaming and convert streaming to download when we have persistent play button
guard streamWorker.getRunningID() != key else {
Log.debug("already streaming audio, don't need to download key: \(key)")
return
}
downloadWorker.start(withID: key, withRemoteUrl: url, completion: completion)
}
func cancelDownload(withRemoteURL url: AudioURL) {
downloadWorker.stop(withID: url.key, callback: nil)
FileStorage.Audio.delete(url.key)
}
func deleteDownload(withLocalURL url: URL) {
FileStorage.delete(url)
}
}
// MARK:- Listeners
extension AudioDataManager {
private func downloadProgressListener(id: ID, progress: Double) {
globalDownloadProgressCallback(id, progress)
}
private func streamProgressListener(id: ID, dto: StreamProgressDTO) {
for c in streamingCallbacks {
if c.0 == id {
c.1(StreamProgressPTO(dto: dto))
}
}
}
private func downloadDoneListener(id: ID, error: Error?) {
if error != nil {
return
}
globalDownloadProgressCallback(id, 1.0)
}
private func streamDoneListener(id: ID, error: Error?) -> Bool {
if error != nil {
return false
}
currentStreamFinished = true
downloadWorker.resumeAllActive()
return false
}
}