forked from einsteinx2/iSubMusicStreamer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamManager.swift
More file actions
471 lines (389 loc) · 18.2 KB
/
Copy pathStreamManager.swift
File metadata and controls
471 lines (389 loc) · 18.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//
// StreamManager.swift
// iSub Release
//
// Created by Benjamin Baron on 1/21/21.
// Copyright © 2021 Ben Baron. All rights reserved.
//
import Foundation
import Resolver
import CocoaLumberjackSwift
final class StreamManager {
@LazyInjected private var downloadQueue: DownloadQueue
@LazyInjected private var store: Store
@LazyInjected private var settings: SavedSettings
@LazyInjected private var playQueue: PlayQueue
@LazyInjected private var player: BassPlayer
private let defaultNumberOfStreamsToQueue = 2
private let maxNumberOfReconnects = 5
private var handlerStack = [StreamHandler]()
private(set) var lastCachedSong: Song?
private(set) var lastTempCachedSong: Song?
private var resumeHandlerWorkItem: DispatchWorkItem?
func setup() {
// Load the handler stack, it may have been full when iSub was closed
loadHandlerStack()
if let firstHandler = handlerStack.first {
if firstHandler.isTempCache {
removeAllStreams()
} else {
for handler in handlerStack {
// Resume any handlers that were downloading when iSub closed
if handler.isDownloading && !handler.isTempCache {
handler.start(resume: true)
}
}
}
}
NotificationCenter.addObserverOnMainThread(self, selector: #selector(songCachingToggled), name: Notifications.songCachingEnabled)
NotificationCenter.addObserverOnMainThread(self, selector: #selector(songCachingToggled), name: Notifications.songCachingDisabled)
NotificationCenter.addObserverOnMainThread(self, selector: #selector(currentPlaylistIndexChanged), name: Notifications.currentPlaylistIndexChanged)
NotificationCenter.addObserverOnMainThread(self, selector: #selector(currentPlaylistOrderChanged), name: Notifications.repeatModeChanged)
NotificationCenter.addObserverOnMainThread(self, selector: #selector(currentPlaylistOrderChanged), name: Notifications.currentPlaylistOrderChanged)
NotificationCenter.addObserverOnMainThread(self, selector: #selector(currentPlaylistOrderChanged), name: Notifications.currentPlaylistShuffleToggled)
NotificationCenter.addObserverOnMainThread(self, selector: #selector(songPlaybackEnded), name: Notifications.songPlaybackEnded)
}
deinit {
NotificationCenter.removeObserverOnMainThread(self)
}
var currentStreamingSong: Song? {
guard isDownloading else { return nil }
return handlerStack.first?.song
}
var firstHandlerInQueue: StreamHandler? {
return handlerStack.first
}
func handler(song: Song) -> StreamHandler? {
return handlerStack.first { $0.song == song }
}
func isFirstInQueue(song: Song) -> Bool {
guard let firstSong = handlerStack.first?.song else { return false }
return firstSong == song
}
func isInQueue(song: Song) -> Bool {
return handlerStack.contains { $0.song == song }
}
func isDownloading(song: Song) -> Bool {
return handler(song: song)?.isDownloading ?? false
}
var isDownloading: Bool {
return handlerStack.contains { $0.isDownloading }
}
func cancelAllStreams(except handlers: [StreamHandler]) {
for handler in handlerStack {
if handlers.contains(handler) { continue }
cancelResume(handler: handler)
handler.cancel()
}
saveHandlerStack()
}
func cancelAllStreams(except songs: [Song]) {
let handlers = songs.compactMap { handler(song: $0)}
cancelAllStreams(except: handlers)
}
func cancelAllStreams(except song: Song) {
guard let handler = handler(song: song) else { return }
cancelAllStreams(except: [handler])
}
func cancelAllStreams() {
cancelAllStreams(except: [] as [StreamHandler])
}
func cancelStream(handler: StreamHandler) {
cancelResume(handler: handler)
handler.cancel()
saveHandlerStack()
}
func cancelStream(index: Int) {
guard index >= 0 && index < handlerStack.count else { return }
cancelStream(handler: handlerStack[index])
}
func cancelStream(song: Song) {
guard let handler = handlerStack.first(where: { $0.song == song }) else { return }
cancelStream(handler: handler)
}
private func removeStreamWithoutSavingStack(handler: StreamHandler) {
// Cancel the handler
cancelResume(handler: handler)
handler.cancel()
// Remove the handler
handlerStack.removeAll { $0 == handler }
// Remove the song from downloads if necessary
let song = handler.song
guard let currentQueuedSong = downloadQueue.currentQueuedSong, currentQueuedSong == song else { return }
// TODO: Why is this checking if the DownloadQueue is downloading?
if currentQueuedSong != song && !song.isFullyCached && !song.isTempCached && downloadQueue.isDownloading {
if Debug.streamManager {
DDLogInfo("[StreamManager] Removing song from cached songs table: \(song)")
}
_ = store.deleteDownloadedSong(song: song)
}
}
func removeAllStreams(except handlers: [StreamHandler]) {
// Remove the handlers
let handlerStackCopy = handlerStack
for handler in handlerStackCopy {
if handlers.contains(handler) { continue }
removeStreamWithoutSavingStack(handler: handler)
}
// Start the next handler
if let handler = handlerStack.first, !handler.isDownloading {
handler.start()
}
saveHandlerStack()
}
func removeAllStreams(except songs: [Song]) {
let handlers = songs.compactMap { handler(song: $0)}
removeAllStreams(except: handlers)
}
func removeAllStreams(except song: Song) {
guard let handler = handler(song: song) else { return }
removeAllStreams(except: [handler])
}
func removeAllStreams() {
removeAllStreams(except: [] as [StreamHandler])
}
func removeStream(handler: StreamHandler) {
// Remove the handler
removeStreamWithoutSavingStack(handler: handler)
// Start the next handler
if let handler = handlerStack.first, !handler.isDownloading {
handler.start()
}
saveHandlerStack()
}
func removeStream(index: Int) {
guard index >= 0 && index < handlerStack.count else { return }
removeStream(handler: handlerStack[index])
}
func removeStream(song: Song) {
guard let handler = handlerStack.first(where: { $0.song == song }) else { return }
removeStream(handler: handler)
}
private func cancelResume(handler: StreamHandler) {
resumeHandlerWorkItem?.cancel()
resumeHandlerWorkItem = nil
}
@objc private func resume(handler: StreamHandler) {
// As an added check, verify that this handler is still in the stack
guard isInQueue(song: handler.song) else { return }
if downloadQueue.isDownloading, let currentQueuedSong = downloadQueue.currentQueuedSong, currentQueuedSong == handler.song {
// This song is already being downloaded by the download queue, so just start the player
streamHandlerStartPlayback(handler: handler)
// Remove the handler from the stack
removeStream(handler: handler)
// Start the next handler which is now the first object
if let handler = handlerStack.first, !handler.isDownloading {
handler.start()
}
} else {
handler.start(resume: true)
}
}
func resumeQueue() {
guard let handler = handlerStack.first else { return }
resume(handler: handler)
}
func start(handler: StreamHandler, resume: Bool) {
// As an added check, verify that this handler is still in the stack
guard isInQueue(song: handler.song) else { return }
if Debug.streamManager {
DDLogInfo("[StreamManager] starting handler \(handler) resume: \(resume), handlerStack: \(handlerStack)")
}
if downloadQueue.isDownloading, let currentQueuedSong = downloadQueue.currentQueuedSong, currentQueuedSong == handler.song {
// This song is already being downloaded by the download queue, so just start the player
streamHandlerStartPlayback(handler: handler)
// Remove the handler from the stack
removeStream(handler: handler)
// Start the next handler which is now the first object
if let handler = handlerStack.first, !handler.isDownloading {
handler.start()
}
} else {
handler.start(resume: resume)
}
}
func start(handler: StreamHandler) {
start(handler: handler, resume: false)
}
private let handlerStackKey = "handlerStack"
func saveHandlerStack() {
do {
UserDefaults.standard.set(try JSONEncoder().encode(handlerStack), forKey: handlerStackKey)
UserDefaults.standard.synchronize()
} catch {
DDLogError("[StreamManager] saveHandlerStack: failed to archive handler stack \(error)")
}
}
func loadHandlerStack() {
do {
guard let data = UserDefaults.standard.object(forKey: handlerStackKey) as? Data else { return }
handlerStack = try JSONDecoder().decode(from: data)
handlerStack.forEach { $0.delegate = self }
if Debug.streamManager {
DDLogInfo("[StreamManager] loaded handler stack \(handlerStack)")
}
} catch {
DDLogError("[StreamManager] saveHandlerStack: failed to unarchive handler stack \(error)")
}
}
// MARK: Handler Stealing
func stealForDownloadQueue(handler: StreamHandler) {
if Debug.streamManager {
DDLogInfo("[StreamManager] download queue manager stole handler for song \(handler.song)")
}
handlerStack.removeAll { $0 == handler }
saveHandlerStack()
fillStreamQueue()
}
// MARK: Download
func queueStream(song: Song, byteOffset: Int = 0, secondsOffset: Double = 0.0, index: Int, tempCache: Bool, startDownload: Bool) {
guard index >= 0 && index <= handlerStack.count, !isInQueue(song: song) else { return }
let handler = StreamHandler(song: song, byteOffset: byteOffset, secondsOffset: secondsOffset, tempCache: tempCache, delegate: self)
handlerStack.insert(handler, at: index)
if handlerStack.count == 1 && startDownload {
start(handler: handler)
}
saveHandlerStack()
SongsHelper.downloadMetadata(song: song)
}
func queueStream(song: Song, tempCache: Bool, startDownload: Bool) {
queueStream(song: song, index: handlerStack.count, tempCache: tempCache, startDownload: startDownload)
}
func fillStreamQueue(startDownload: Bool) {
guard !settings.isJukeboxEnabled, !settings.isOfflineMode else { return }
let numStreamsToQueue = settings.isSongCachingEnabled && settings.isNextSongCacheEnabled ? defaultNumberOfStreamsToQueue : 1
guard handlerStack.count < numStreamsToQueue else { return }
for i in 0..<numStreamsToQueue {
if let song = playQueue.song(index: playQueue.indexFromCurrentIndex(offset: i)), !song.isVideo, !song.isFullyCached, !isInQueue(song: song) {
var isLastTempCachedSong = false
if let lastTempCachedSong = lastTempCachedSong, lastTempCachedSong == song {
isLastTempCachedSong = true
}
var isCurrentQueuedSong = false
if let currentQueuedSong = downloadQueue.currentQueuedSong, currentQueuedSong == song {
isCurrentQueuedSong = true
}
if !isLastTempCachedSong && !isCurrentQueuedSong {
queueStream(song: song, tempCache: !settings.isSongCachingEnabled, startDownload: startDownload)
}
}
}
if Debug.streamManager {
DDLogInfo("[StreamManager] fillStreamQueue: handlerStack: \(handlerStack)")
}
}
@objc private func fillStreamQueue() {
fillStreamQueue(startDownload: true)
}
@objc private func songCachingToggled() {
if settings.isSongCachingEnabled {
NotificationCenter.addObserverOnMainThread(self, selector: #selector(fillStreamQueue as () -> Void), name: Notifications.songPlaybackEnded)
} else {
NotificationCenter.removeObserverOnMainThread(self, name: Notifications.songPlaybackEnded)
}
}
@objc private func currentPlaylistIndexChanged() {
// TODO: implement this
// TODO: Fix this logic, it's wrong
if let prevSong = playQueue.prevSong {
removeStream(song: prevSong)
}
}
@objc private func currentPlaylistOrderChanged() {
var songs = [Song]()
if let currentSong = playQueue.currentSong {
songs.append(currentSong)
}
if let nextSong = playQueue.nextSong {
songs.append(nextSong)
}
removeAllStreams(except: songs)
fillStreamQueue(startDownload: player.isStarted)
}
@objc private func songPlaybackEnded() {
fillStreamQueue()
}
}
extension StreamManager: StreamHandlerDelegate {
func streamHandlerStarted(handler: StreamHandler) {
if handler.isTempCache {
lastTempCachedSong = nil
player.startSong(handler.song, index: handlerStack.startIndex, offsetInBytes: handler.byteOffset, offsetInSeconds: handler.secondsOffset)
}
}
func streamHandlerStartPlayback(handler: StreamHandler) {
lastCachedSong = handler.song
player.streamReadyToStartPlayback(handler: handler)
// TODO: Is this needed? Are we actually changing the stack?
// I guess this is to save the isDelegateNotifiedToStartPlayback property?
saveHandlerStack()
}
func streamHandlerConnectionFinished(handler: StreamHandler) {
var success = true
if handler.totalBytesTransferred == 0 {
// Not a trial issue, but no data was returned at all
let message = "We asked for a song, but the server didn't send anything!\n\nIt's likely that Subsonic's transcoding failed."
let alert = UIAlertController(title: "Uh Oh!", message: message, preferredStyle: .alert)
alert.addOKAction()
UIApplication.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
// TODO: Do we care if this fails? Can the file potentially not be there at all?
try? FileManager.default.removeItem(at: URL(fileURLWithPath: handler.filePath))
success = false
} else if handler.totalBytesTransferred < 1000 {
// Verify that it's a license issue
if let data = try? Data(contentsOf: URL(fileURLWithPath: handler.filePath)) {
let root = RXMLElement(fromXMLData: data)
if root.isValid {
if let error = root.child("error"), error.isValid {
let subsonicError = SubsonicError(element: error)
if case .trialExpired = subsonicError {
let alert = UIAlertController(title: "Subsonic Error", message: subsonicError.localizedDescription, preferredStyle: .alert)
alert.addOKAction()
UIApplication.keyWindow?.rootViewController?.present(alert, animated: true, completion: nil)
// TODO: Do we care if this fails? Can the file potentially not be there at all?
try? FileManager.default.removeItem(at: URL(fileURLWithPath: handler.filePath))
success = false
}
}
}
}
}
guard success else { return }
// TODO: Should check store return values and do some extra error handling?
if !handler.isTempCache {
if downloadQueue.isInQueue(song: handler.song) {
_ = store.removeFromDownloadQueue(song: handler.song)
}
if Debug.streamManager {
DDLogInfo("[StreamManager] Marking download finished for \(handler.song)")
}
_ = store.update(downloadFinished: true, song: handler.song)
}
lastCachedSong = handler.song
if handler.isTempCache {
lastTempCachedSong = handler.song
}
removeStream(handler: handler)
if let handler = handlerStack.first {
start(handler: handler)
}
fillStreamQueue()
NotificationCenter.postOnMainThread(name: Notifications.streamHandlerSongDownloaded, userInfo: ["songId": handler.song.id])
}
func streamHandlerConnectionFailed(handler: StreamHandler, error: Error) {
if handler.numberOfReconnects < maxNumberOfReconnects {
// Less than max number of reconnections, so try again
handler.numberOfReconnects += 1
// Retry connection after a delay to prevent a tight loop
let resumeHandlerWorkItem = DispatchWorkItem { [weak self] in
self?.resume(handler: handler)
}
self.resumeHandlerWorkItem = resumeHandlerWorkItem
DispatchQueue.main.async(after: 1.5, execute: resumeHandlerWorkItem)
} else {
// Tried max number of times so remove
NotificationCenter.postOnMainThread(name: Notifications.streamHandlerSongFailed)
removeStream(handler: handler)
}
}
}