-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayolaStationPlayer.swift
More file actions
1025 lines (914 loc) Β· 38.8 KB
/
Copy pathPlayolaStationPlayer.swift
File metadata and controls
1025 lines (914 loc) Β· 38.8 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// PlayolaPlayer.swift
// PlayolaPlayer
//
// Created by Brian D Keane on 12/29/24.
//
// swiftlint:disable file_length
import AVFAudio
import Combine
import Foundation
@_exported import PlayolaCore
import os.log
/// Errors specific to the station player
public enum StationPlayerError: Error, LocalizedError, Sendable {
case networkError(String)
case scheduleError(String)
case playbackError(String)
case invalidStationId(String)
case fileDownloadError(String)
public var errorDescription: String? {
switch self {
case .networkError(let message):
return "Network error: \(message)"
case .scheduleError(let message):
return "Schedule error: \(message)"
case .playbackError(let message):
return "Playback error: \(message)"
case .invalidStationId(let id):
return "Invalid station ID: \(id)"
case .fileDownloadError(let message):
return "File download error: \(message)"
}
}
}
/// A player for Playola stations that manages audio playback, scheduling, and stream management.
///
/// `PlayolaStationPlayer` is the main entry point for apps integrating with the Playola platform.
/// It handles:
/// - Loading and playing audio from Playola stations
/// - Scheduling upcoming audio content
/// - Managing the audio session and playback state
/// - Reporting listening sessions to Playola analytics
///
/// ## Usage Example:
/// ```
/// // Play a specific station
/// try await PlayolaStationPlayer.shared.play(stationId: "station-id-here")
///
/// // Stop playback
/// PlayolaStationPlayer.shared.stop()
///
/// // Observe state changes
/// player.$state.sink { state in
/// // Handle state changes
/// }
/// ```
@MainActor
final public class PlayolaStationPlayer: ObservableObject {
var baseUrl = URL(string: "https://admin-api.playola.fm")!
@Published public var stationId: String?
private var interruptedStationId: String?
var currentSchedule: Schedule?
let fileDownloadManager: FileDownloadManaging
var listeningSessionReporter: ListeningSessionReporter?
private let errorReporter = PlayolaErrorReporter.shared
private var authProvider: PlayolaAuthenticationProvider?
private let urlSession: URLSessionProtocol
/// Resolved lazily so merely constructing a station player (e.g. in headless
/// macOS test runs) does not build the shared CoreAudio graph. The SDK touches
/// the mixer only when playback actually starts (or resumes).
var mainMixer: PlayolaMainMixer { .shared }
/// Base delay (seconds) for the initial schedule-fetch exponential backoff.
/// Internal so tests can disable the wait; not part of the public API.
var scheduleRetryBaseDelay: TimeInterval = 0.5
/// Time offset for playing station from a different point in time
private var scheduleOffset: TimeInterval?
private var activeDownloadIds: [String: UUID] = [:]
// Internal (not private) so tests can observe scheduling-task lifecycle.
var schedulingTask: Task<Void, Never>?
// Monotonic token identifying the current play() attempt. Bumped on every
// play() and stop(). Work started by an older generation β a prior session's
// scheduling loop, or a spin whose audio starts after a newer attempt began β
// must not publish state into the current generation. Internal for tests.
var playGeneration: Int = 0
/// Whether work tagged with `generation` belongs to the current play attempt.
/// The gate that stops a superseded session from publishing state. Internal
/// so tests can assert it without constructing a CoreAudio-backed SpinPlayer.
func isCurrentGeneration(_ generation: Int) -> Bool {
generation == playGeneration
}
// Audio interruption state
private var isSuspended = false
private var wasPlayingBeforeInterruption = false
public weak var delegate: PlayolaStationPlayerDelegate?
// Thread-safe access to spin players - all access must be on main actor
private var _spinPlayers: [SpinPlayer] = []
/// Thread-safe access to spin players array
private var spinPlayers: [SpinPlayer] {
get {
MainActor.assumeIsolated {
return _spinPlayers
}
}
set {
MainActor.assumeIsolated {
_spinPlayers = newValue
}
}
}
public static let shared = PlayolaStationPlayer()
/// Configure this instance with authentication provider
/// - Parameters:
/// - authProvider: Provider for JWT tokens
/// - baseURL: Base URL for API endpoints. Defaults to production URL.
///
/// The SDK does not own the `AVAudioSession`. The host app must configure and
/// activate it (category `.playback`) before calling `play(stationId:)` or
/// `resumeAfterInterruption()`, and owns all interruption/route-change policy.
/// See the README "Audio session" section.
public func configure(
authProvider: PlayolaAuthenticationProvider,
baseURL: URL = URL(string: "https://admin-api.playola.fm")!
) {
self.authProvider = authProvider
self.listeningSessionReporter = ListeningSessionReporter(
stationPlayer: self, authProvider: authProvider, baseURL: baseURL)
self.baseUrl = baseURL
}
public enum State: Sendable {
case loading(Float)
case playing(Spin)
case idle
/// Emitted when a playback attempt fails terminally (e.g. the schedule
/// fetch exhausted its retries). Lets consumers render a recoverable
/// error instead of being stuck in a prior state.
case error(StationPlayerError)
/// Playback suspended by an interruption (host- or legacy-driven). The spin
/// is the one that was playing at pause time β display metadata only; it
/// will be re-fetched and re-synced on resume.
case paused(Spin)
}
@Published public var state: PlayolaStationPlayer.State = .idle {
didSet {
delegate?.player(self, playerStateDidChange: state)
}
}
public var isPlaying: Bool {
switch state {
case .playing:
return true
default:
return false
}
}
@MainActor
internal init(
fileDownloadManager: FileDownloadManaging? = nil,
urlSession: URLSessionProtocol = PlayolaNetworkLoggingSession(wrapping: tls12Session)
) {
self.fileDownloadManager = fileDownloadManager ?? FileDownloadManagerAsync.shared
self.urlSession = urlSession
self.authProvider = nil
self.listeningSessionReporter = ListeningSessionReporter(stationPlayer: self, authProvider: nil)
// The SDK does NOT observe AVAudioSession interruptions or route changes β
// the host owns that policy and drives pauseForInterruption() /
// resumeAfterInterruption() explicitly. It DOES self-recover its own engine
// graph; that observer is registered lazily once playback begins (see
// registerEngineConfigObserverIfNeeded()) so construction stays cheap.
}
/// True once the engine-config-change observer has been registered (lazily,
/// on first playback) so we register it exactly once.
private var engineConfigObserverRegistered = false
/// Registers the AVAudioEngineConfigurationChange observer, scoped to the
/// SDK's OWN engine. AVAudioEngine stops itself on a hardware/format/route
/// reconfiguration and posts this notification; only the SDK can observe it
/// for its own engine, so it must self-heal. This is engine ownership, not
/// session ownership β it touches no AVAudioSession API.
///
/// Registered lazily (from getAvailableSpinPlayer, at the moment a SpinPlayer
/// resolves the shared mixer anyway) rather than at init, so merely
/// constructing a player never builds the CoreAudio graph. Scoping to
/// `mainMixer.engine` (not `object: nil`) means a host running its own
/// AVAudioEngine does NOT trigger spurious SDK restarts.
private func registerEngineConfigObserverIfNeeded() {
guard !engineConfigObserverRegistered else { return }
engineConfigObserverRegistered = true
NotificationCenter.default.addObserver(
self,
selector: #selector(handleAudioEngineConfigurationChange(_:)),
name: .AVAudioEngineConfigurationChange,
object: mainMixer.engine
)
}
private static let logger = OSLog(
subsystem: "PlayolaPlayer",
category: "PlayolaStationPlayer")
private func getAvailableSpinPlayer() -> SpinPlayer {
let availablePlayers = _spinPlayers.filter({ $0.state == .available })
if let available = availablePlayers.first { return available }
// First real playback: the SpinPlayer below resolves the shared mixer, so
// it's safe to scope the engine-recovery observer to that engine now.
registerEngineConfigObserverIfNeeded()
// Note: SpinPlayer currently couples to PlayolaMainMixer.shared internally
// (it ignores an injected mixer). Fine in production where everything uses
// .shared; threading injection through SpinPlayer is a known follow-up.
let newPlayer = SpinPlayer(delegate: self)
_spinPlayers.append(newPlayer)
return newPlayer
}
@MainActor
private func scheduleSpin(
spin: Spin, generation: Int, showProgress: Bool = false, retryCount: Int = 0
)
async throws
{
os_log(
"π
Scheduling spin: %@ - Retry: %d", log: PlayolaStationPlayer.logger, type: .info, spin.id,
retryCount)
try validateSpinForScheduling(spin)
// The generation is threaded in from the caller (play() or the scheduling
// loop), not re-read from the mutable global, so a caller that has already
// been superseded can't tag a player as the current attempt.
let spinPlayer = getAvailableSpinPlayer()
spinPlayer.playGeneration = generation
cancelExistingDownload(for: spin.id)
do {
let result = await loadSpinWithProgress(spin, spinPlayer, generation, showProgress)
try await handleLoadResult(
result, spin: spin, generation: generation, showProgress: showProgress,
retryCount: retryCount)
} catch {
try await handleSchedulingError(
error, spin: spin, generation: generation, showProgress: showProgress,
retryCount: retryCount)
}
}
private func validateSpinForScheduling(_ spin: Spin) throws {
guard spin.audioBlock.downloadUrl != nil else {
let error = StationPlayerError.playbackError("Invalid audio file URL in spin")
let spinDetails = createSpinDetailsString(spin)
Task {
await errorReporter.reportError(
error,
context: "Invalid or missing download URL for spin | \(spinDetails)",
level: .error)
}
throw error
}
}
private func createSpinDetailsString(_ spin: Spin) -> String {
return """
Spin ID: \(spin.id)
Audio Block ID: \(spin.audioBlock.id)
Audio Block Title: \(spin.audioBlock.title)
Audio Block Artist: \(spin.audioBlock.artist)
Download URL: \(spin.audioBlock.downloadUrl?.absoluteString ?? "nil")
"""
}
private func cancelExistingDownload(for spinId: String) {
if let existingDownloadId = activeDownloadIds[spinId] {
_ = fileDownloadManager.cancelDownload(id: existingDownloadId)
activeDownloadIds.removeValue(forKey: spinId)
}
}
private func loadSpinWithProgress(
_ spin: Spin, _ spinPlayer: SpinPlayer, _ generation: Int, _ showProgress: Bool
) async -> Result<URL, Error> {
return await spinPlayer.load(
spin,
onDownloadProgress: { [weak self] progress in
guard let self = self, showProgress, self.isCurrentGeneration(generation) else { return }
self.state = .loading(progress)
}
)
}
private func handleLoadResult(
_ result: Result<URL, Error>, spin: Spin, generation: Int, showProgress: Bool,
retryCount: Int
) async throws {
switch result {
case .success:
if showProgress, isCurrentGeneration(generation) {
self.state = .playing(spin)
}
case .failure(let error):
try await handleLoadFailure(
error, spin: spin, generation: generation, showProgress: showProgress,
retryCount: retryCount)
}
}
private func handleLoadFailure(
_ error: Error, spin: Spin, generation: Int, showProgress: Bool, retryCount: Int
)
async throws
{
if shouldSkipRetry(for: error) {
throw error
}
let stationError = convertToStationError(error)
Task {
await self.errorReporter.reportError(stationError, level: .error)
}
try await retryIfPossible(
spin: spin, generation: generation, showProgress: showProgress, retryCount: retryCount,
fallbackError: error)
}
private func handleSchedulingError(
_ error: Error, spin: Spin, generation: Int, showProgress: Bool, retryCount: Int
) async throws {
if shouldSkipRetry(for: error) {
throw error
}
try await retryIfPossible(
spin: spin, generation: generation, showProgress: showProgress, retryCount: retryCount,
fallbackError: error)
}
private func shouldSkipRetry(for error: Error) -> Bool {
if let urlError = error as? URLError, urlError.code == .cancelled {
return true
}
if let fileDownloadError = error as? FileDownloadError,
case .downloadCancelled = fileDownloadError
{
return true
}
return false
}
private func convertToStationError(_ error: Error) -> Error {
return error is FileDownloadError
? error
: StationPlayerError.fileDownloadError(error.localizedDescription)
}
private func retryIfPossible(
spin: Spin, generation: Int, showProgress: Bool, retryCount: Int, fallbackError: Error
) async throws {
let maxRetries = 3
if retryCount < maxRetries {
let delay = TimeInterval(0.5 * pow(2.0, Double(retryCount)))
try await Task.sleep(for: .seconds(delay))
// A newer play()/stop() superseded us during backoff β abandon the retry
// rather than reviving a stale session's spin.
guard isCurrentGeneration(generation) else { return }
try await self.scheduleSpin(
spin: spin, generation: generation, showProgress: showProgress, retryCount: retryCount + 1)
} else {
let finalError =
fallbackError is StationPlayerError
? fallbackError
: StationPlayerError.fileDownloadError(fallbackError.localizedDescription)
Task {
await errorReporter.reportError(finalError, level: .error)
}
throw fallbackError
}
}
@MainActor
private func isScheduled(spin: Spin) -> Bool {
return _spinPlayers.contains { $0.spin?.id == spin.id }
}
private let schedulePollingInterval: UInt64 = 20_000_000_000 // 20 seconds in nanoseconds
@MainActor
private func scheduleUpcomingSpins(generation: Int) async {
guard let stationId else {
let error = StationPlayerError.invalidStationId("No station ID available")
Task { await errorReporter.reportError(error, level: .warning) }
return
}
// Stop as soon as a newer play()/stop() supersedes this loop, even if this
// task hasn't hit a cooperative cancellation point yet.
while !Task.isCancelled && generation == playGeneration {
do {
try await performScheduleUpdate(stationId: stationId, generation: generation)
} catch is CancellationError {
os_log("π Schedule update cancelled", log: PlayolaStationPlayer.logger, type: .info)
return
} catch {
Task {
await errorReporter.reportError(
error, context: "Failed to schedule upcoming spins", level: .error)
}
os_log(
"Schedule update failed: %@", log: PlayolaStationPlayer.logger, type: .error,
error.localizedDescription)
}
do {
try await Task.sleep(nanoseconds: schedulePollingInterval)
} catch {
return
}
}
}
@MainActor
private func performScheduleUpdate(stationId: String, generation: Int) async throws {
let updatedSchedule = try await getUpdatedSchedule(stationId: stationId)
// A newer play()/stop() superseded this loop while the fetch was in flight β
// don't schedule the old session's spins into the new attempt.
guard isCurrentGeneration(generation) else { return }
os_log(
"Retrieved schedule: %d total, %d current", log: PlayolaStationPlayer.logger, type: .info,
updatedSchedule.spins.count,
updatedSchedule.current(offsetTimeInterval: scheduleOffset).count)
// Server returns only locked-in spins via lockedIn=true param.
// This filter is a client-side safety net to prevent scheduling spins too far out.
let spinsToLoad = updatedSchedule.current(offsetTimeInterval: scheduleOffset).filter {
$0.airtime < .now + TimeInterval(600)
}
os_log(
"Loading %d upcoming spins", log: PlayolaStationPlayer.logger, type: .info,
spinsToLoad.count)
for spin in spinsToLoad {
try Task.checkCancellation()
// Re-check between spins: scheduleSpin awaits a download, during which a
// newer attempt can supersede us.
guard isCurrentGeneration(generation) else { return }
if !isScheduled(spin: spin) {
os_log(
"Scheduling new spin: %@ at %@", log: PlayolaStationPlayer.logger, type: .info,
spin.id, ISO8601DateFormatter().string(from: spin.airtime))
try await scheduleSpin(spin: spin, generation: generation)
}
}
let scheduledSpinsCount = _spinPlayers.filter { $0.spin != nil }.count
os_log(
"Total scheduled spins: %d", log: PlayolaStationPlayer.logger, type: .info,
scheduledSpinsCount)
}
/// Internal classification so the retry layer can tell transient failures
/// (5xx, connectivity) apart from permanent ones (4xx, decode errors)
/// without widening the public error surface. `publicError` is what callers
/// ultimately receive β the public error API is unchanged.
private struct ClassifiedScheduleError: Error {
let isTransient: Bool
let publicError: Error
}
/// Fetches the schedule with bounded exponential backoff, retrying only
/// transient failures (server 5xx and connectivity errors). Permanent
/// failures (404, decode errors, empty schedules) fail fast. The error
/// thrown to callers is the same public error a single fetch would throw.
///
/// Intentionally `internal` (not `private`) for test access, matching
/// `scheduleRetryBaseDelay`; not part of the public API.
func getUpdatedScheduleWithRetry(stationId: String) async throws -> Schedule {
let maxRetries = 3
var attempt = 0
while true {
do {
return try await fetchScheduleOnce(stationId: stationId)
} catch let classified as ClassifiedScheduleError {
guard classified.isTransient, attempt < maxRetries else {
throw classified.publicError
}
let delay = scheduleRetryBaseDelay * pow(2.0, Double(attempt))
if delay > 0 {
try await Task.sleep(for: .seconds(delay))
}
attempt += 1
}
}
}
/// Single, non-retrying schedule fetch. Used directly by the polling loop;
/// re-throws the public error so existing callers see an unchanged surface.
private func getUpdatedSchedule(stationId: String) async throws -> Schedule {
do {
return try await fetchScheduleOnce(stationId: stationId)
} catch let classified as ClassifiedScheduleError {
throw classified.publicError
}
}
private func fetchScheduleOnce(stationId: String) async throws -> Schedule {
let url = createScheduleURL(for: stationId)
do {
let (data, response) = try await urlSession.data(for: URLRequest(url: url))
let httpResponse = try validateHTTPResponse(response, url: url)
try await validateStatusCode(httpResponse, data: data, stationId: stationId)
return try await decodeSchedule(from: data, stationId: stationId)
} catch let classified as ClassifiedScheduleError {
// HTTP-status failures are already reported once by validateStatusCode.
throw classified
} catch let stationError as StationPlayerError {
// validateHTTPResponse and decodeSchedule already reported these once.
throw classifyScheduleFetchError(stationError)
} catch {
// A cancellation (a routine stop() during an in-flight fetch) is not a
// reportable failure β skip the production error event but still
// propagate so the caller treats it as a cancellation, not an error.
if !isCancellation(error) {
// Raw transport error (e.g. URLError) β not reported by any inner step,
// so report it here exactly once.
await reportScheduleFetchError(error, stationId: stationId)
}
throw classifyScheduleFetchError(error)
}
}
/// Connectivity-related `URLError`s that a retry can plausibly recover from.
/// Non-connectivity failures (e.g. `.badURL`, `.unsupportedURL`) are permanent
/// and must fail fast instead of burning the backoff budget.
private static let retryableURLErrorCodes: Set<URLError.Code> = [
.timedOut, .cannotConnectToHost, .cannotFindHost, .networkConnectionLost,
.notConnectedToInternet, .dnsLookupFailed, .resourceUnavailable,
.secureConnectionFailed,
]
private func classifyScheduleFetchError(_ error: Error) -> ClassifiedScheduleError {
if error is CancellationError {
return ClassifiedScheduleError(isTransient: false, publicError: error)
}
if let urlError = error as? URLError {
let isTransient = Self.retryableURLErrorCodes.contains(urlError.code)
return ClassifiedScheduleError(isTransient: isTransient, publicError: error)
}
return ClassifiedScheduleError(isTransient: false, publicError: error)
}
private func createScheduleURL(for stationId: String) -> URL {
return baseUrl.appending(path: "/v1/stations/\(stationId)/schedule")
.appending(queryItems: [
URLQueryItem(name: "includeRelatedTexts", value: "true"),
URLQueryItem(name: "lockedIn", value: "true"),
])
}
private func validateHTTPResponse(_ response: URLResponse, url: URL) throws -> HTTPURLResponse {
guard let httpResponse = response as? HTTPURLResponse else {
let error = StationPlayerError.networkError("Invalid response type")
Task {
await errorReporter.reportError(
error,
context: "Non-HTTP response received from schedule endpoint: \(url.absoluteString)",
level: .error)
}
throw error
}
return httpResponse
}
private func validateStatusCode(_ httpResponse: HTTPURLResponse, data: Data, stationId: String)
async throws
{
guard (200...299).contains(httpResponse.statusCode) else {
let responseText = String(data: data, encoding: .utf8) ?? "Unable to decode response"
let error = StationPlayerError.networkError("HTTP error: \(httpResponse.statusCode)")
await reportHTTPError(
error, statusCode: httpResponse.statusCode, stationId: stationId, responseText: responseText
)
throw ClassifiedScheduleError(
isTransient: (500...599).contains(httpResponse.statusCode), publicError: error)
}
}
private func reportHTTPError(
_ error: StationPlayerError, statusCode: Int, stationId: String, responseText: String
) async {
if statusCode == 404 {
Task {
await errorReporter.reportError(
error,
context: "Station not found: \(stationId) | Response: \(responseText.prefix(100))",
level: .error)
}
} else {
Task {
await errorReporter.reportError(
error,
context:
"HTTP \(statusCode) error getting schedule for station: \(stationId) | "
+ "Response: \(responseText.prefix(100))",
level: .error)
}
}
}
private func decodeSchedule(from data: Data, stationId: String) async throws -> Schedule {
let decoder = JSONDecoderWithIsoFull()
do {
let spins = try decoder.decode([Spin].self, from: data)
guard !spins.isEmpty else {
let error = StationPlayerError.scheduleError(
"No spins returned in schedule for station ID: \(stationId)")
Task {
await errorReporter.reportError(error, level: .error)
}
throw error
}
return Schedule(stationId: spins[0].stationId, spins: spins)
} catch let decodingError as DecodingError {
let context = await reportDecodingError(decodingError)
throw StationPlayerError.scheduleError("Invalid schedule data: \(context)")
}
}
private func reportDecodingError(_ decodingError: DecodingError) async -> String {
let context: String
switch decodingError {
case .dataCorrupted(let reportedContext):
context = "Corrupted data: \(reportedContext.debugDescription)"
case .keyNotFound(let key, _):
context = "Missing key: \(key)"
case .typeMismatch(let type, _):
context = "Type mismatch for: \(type)"
default:
context = "Unknown decoding error"
}
Task {
await errorReporter.reportError(
decodingError, context: "Failed to decode schedule: \(context)", level: .error)
}
return context
}
private func reportScheduleFetchError(_ error: Error, stationId: String) async {
Task {
await errorReporter.reportError(
error, context: "Failed to fetch schedule for station: \(stationId)", level: .error)
}
}
/// Begins playback of the specified Playola station.
///
/// This method:
/// 1. Fetches the station's schedule from the Playola API
/// 2. Loads and prepares the current audio block for playback
/// 3. Schedules upcoming audio blocks to ensure smooth transitions
/// 4. Reports the listening session to Playola analytics
///
/// - Parameters:
/// - stationId: The unique identifier of the station to play
/// - atDate: Optional date to play from. If provided, the station will play as if it were that date/time.
/// For example, passing a date 1 hour ago will play the station from 1 hour ago.
/// - Throws: `StationPlayerError` if playback cannot be started due to:
/// - Network errors when fetching the schedule
/// - Invalid station ID
/// - Missing audio content in the schedule
/// - File download failures
public func play(stationId: String, atDate: Date? = nil) async throws {
// Reset any stale interruption state when explicitly starting playback.
// This ensures CarPlay and other external callers always get a clean start.
isSuspended = false
wasPlayingBeforeInterruption = false
interruptedStationId = nil
// Calculate schedule offset if atDate is provided
self.scheduleOffset = atDate?.timeIntervalSinceNow
self.stationId = stationId
// Take over as the current play attempt. Any in-flight work from a prior
// attempt (its scheduling loop, late spin callbacks) belongs to an older
// generation and will no longer publish into state.
playGeneration += 1
let generation = playGeneration
// Emit a terminal-trackable state from the start so consumers observing
// $state always see the attempt begin and (below) its success or failure.
self.state = .loading(0)
do {
// Get the schedule (with bounded backoff for transient outages)
let schedule = try await getUpdatedScheduleWithRetry(stationId: stationId)
// A newer play()/stop() superseded us mid-fetch β abandon quietly without
// writing the stale schedule into the shared field we no longer own.
guard generation == playGeneration else { return }
self.currentSchedule = schedule
guard
let spinToPlay = currentSchedule?.current(offsetTimeInterval: scheduleOffset).first
else {
let error = StationPlayerError.scheduleError("No available spins to play")
Task {
await errorReporter.reportError(
error,
context:
"Schedule for station \(stationId) contains no current spins | "
+ "Total spins: \(currentSchedule?.spins.count ?? 0)",
level: .error)
}
throw error
}
// Log success with context
os_log(
"Starting playback for station: %@", log: PlayolaStationPlayer.logger, type: .info,
stationId)
// Schedule the first spin with progress shown
try await scheduleSpin(spin: spinToPlay, generation: generation, showProgress: true)
// Superseded while loading the first spin β don't start a scheduling loop.
guard generation == playGeneration else { return }
// Take over the scheduling loop from any prior session and start our own.
schedulingTask?.cancel()
schedulingTask = Task { [generation] in
await scheduleUpcomingSpins(generation: generation)
}
} catch {
// Only publish the failure if we're still the current attempt. A newer
// play()/stop() owns state now, and a prior session's still-running loop
// is gated by generation rather than force-cancelled here (it would
// otherwise tear down a live session this failed attempt never started).
guard generation == playGeneration else { throw error }
// Emit a terminal error state so consumers can render a recoverable
// error instead of being stuck in .loading. Cancellation is not an error.
if !isCancellation(error) {
let stationError =
(error as? StationPlayerError) ?? .scheduleError(error.localizedDescription)
self.state = .error(stationError)
}
throw error
}
}
// Internal for testability. Kept consistent with `shouldSkipRetry`: a
// cancelled in-flight download (thrown by scheduleSpin when stop() runs) is a
// cancellation, not a terminal error, and must not flip state to .error.
func isCancellation(_ error: Error) -> Bool {
if error is CancellationError { return true }
if let urlError = error as? URLError, urlError.code == .cancelled { return true }
if let fileDownloadError = error as? FileDownloadError,
case .downloadCancelled = fileDownloadError
{
return true
}
return false
}
// MARK: - Internal test seams
func setStateForTesting(_ state: State, stationId: String?) {
self.state = state
self.stationId = stationId
}
var isSuspendedForTesting: Bool { isSuspended }
var interruptedStationIdForTesting: String? { interruptedStationId }
var wasPlayingBeforeInterruptionForTesting: Bool { wasPlayingBeforeInterruption }
/// Stops the current playback and releases associated resources.
///
/// This method:
/// 1. Stops all playing audio
/// 2. Cancels pending downloads
/// 3. Clears the current schedule
/// 4. Reports the end of the listening session
public func stop() {
os_log("π STOP called", log: PlayolaStationPlayer.logger, type: .info)
// Supersede any in-flight play()/scheduling work so it can't publish state
// after we go idle.
playGeneration += 1
// Log current state before stopping
os_log(
"Current state: %d players, %d downloads", log: PlayolaStationPlayer.logger, type: .info,
_spinPlayers.count, activeDownloadIds.count)
// Cancel any active async tasks first
if schedulingTask != nil {
os_log("Cancelling scheduling task", log: PlayolaStationPlayer.logger, type: .info)
schedulingTask?.cancel()
schedulingTask = nil
}
// Stop all players (this will cancel their individual downloads)
os_log("Stopping %d players", log: PlayolaStationPlayer.logger, type: .info, _spinPlayers.count)
for (index, player) in _spinPlayers.enumerated() {
os_log(
"Stopping player %d/%d", log: PlayolaStationPlayer.logger, type: .info, index + 1,
_spinPlayers.count)
player.stop()
}
// Cancel any downloads tracked at this level
os_log(
"Cancelling %d downloads", log: PlayolaStationPlayer.logger, type: .info,
activeDownloadIds.count)
for (spinId, downloadId) in activeDownloadIds {
os_log("Cancelling download: %@", log: PlayolaStationPlayer.logger, type: .info, spinId)
_ = fileDownloadManager.cancelDownload(id: downloadId)
}
activeDownloadIds.removeAll()
self.stationId = nil
self.currentSchedule = nil
self.state = .idle
// stop() means "forget everything", including any armed interruption. Without
// this, a later resumeAfterInterruption() (or a resume retry) could revive a
// station the host explicitly stopped.
self.isSuspended = false
self.wasPlayingBeforeInterruption = false
self.interruptedStationId = nil
os_log("β
STOP completed", log: PlayolaStationPlayer.logger, type: .info)
}
/// Host-driven interruption pause. Silences playback and cancels scheduling,
/// preserving ONLY the station id; the schedule is wall-clock-stale by resume
/// time, so resume re-fetches. Bumps playGeneration so in-flight work from
/// before the pause cannot publish state after it.
public func pauseForInterruption() {
playGeneration += 1
// Loading counts as "playing" for resume purposes: if the user started a
// station and a call interrupts the load, they expect it back afterward.
let wasActive: Bool = {
switch state {
case .playing, .loading: return true
case .idle, .paused, .error: return false
}
}()
// A repeated pause (e.g. interruption + route change for the same outage)
// must not overwrite the armed resume state. Pausing while inactive arms
// nothing β there is no playback to bring back.
if !isSuspended {
wasPlayingBeforeInterruption = wasActive
interruptedStationId = wasActive ? stationId : nil
}
isSuspended = true
schedulingTask?.cancel()
schedulingTask = nil
for player in _spinPlayers { player.stop() }
for (_, downloadId) in activeDownloadIds {
_ = fileDownloadManager.cancelDownload(id: downloadId)
}
activeDownloadIds.removeAll()
switch state {
case .playing(let spin): state = .paused(spin)
case .loading: state = .idle // no spin to show; resume re-fetches and republishes .loading
default: break
}
}
/// Host-driven resume. Restarts the engine and replays the interrupted station
/// re-synced to the current wall clock. The host owns the `AVAudioSession` and
/// MUST have re-activated it before calling this. No-op if nothing was
/// interrupted OR if playback wasn't active when the pause happened.
///
/// Consumes the interruption up-front: this is a one-shot attempt. On failure
/// it both throws AND publishes `.error` state (so a host driving UI from
/// `$state` sees the failure without catching the throw). **To retry, call
/// `play(stationId:)`** β the station is available as the player's `stationId`
/// (still set after a failed resume; only `stop()` clears it). The resume is
/// abandoned cleanly if the host starts or stops playback while it's in flight.
public func resumeAfterInterruption() async throws {
guard let stationToResume = interruptedStationId,
wasPlayingBeforeInterruption
else { return }
// Consume the armed interruption now β this attempt owns it. (play() would
// clear these at its first lines anyway; clearing here keeps the failure
// path honest: retry is via play(), not a second resume call.)
isSuspended = false
interruptedStationId = nil
wasPlayingBeforeInterruption = false
// restartEngine() before play(): after an interruption the engine may be in
// a stopped-but-stale CoreAudio state; play()'s lazy engine start does not
// re-prepare a torn-down graph. restartEngine() (stop+prepare+start) is
// idempotent when healthy.
let generation = playGeneration
do {
try await mainMixer.restartEngine()
} catch {
// Mirror play()'s error path so a host observing $state isn't left on a
// stale .paused with no working resume β but only if a host stop()/play()
// hasn't superseded us during the restart.
if generation == playGeneration {
state = .error(
.playbackError("Failed to restart audio engine on resume: \(error.localizedDescription)"))
}
throw error
}
// A host stop()/play() during restartEngine bumped the generation β abandon
// the resume so we don't revive a station the host stopped or override a
// station it just started. (Supersession during play()'s own awaits is
// handled by play()'s generation gate.)
guard generation == playGeneration else { return }
try await play(stationId: stationToResume)
}
/// Recovers the SDK's own engine graph when AVAudioEngine reconfigures itself
/// (hardware/format/route change) and stops. This is engine ownership, not
/// session ownership: it touches no AVAudioSession API. Skipped while the host
/// has paused for an interruption (the host drives resume then). Only acts
/// while actively playing; re-syncs to the live wall clock via play().
///
/// Internal (not public): it's a notification selector target, not host API β
/// the host never calls it. `@objc` is required for `#selector`.
@objc func handleAudioEngineConfigurationChange(_ notification: Notification) {
os_log("Audio engine configuration changed", log: PlayolaStationPlayer.logger, type: .info)
guard !isSuspended, isPlaying, let stationToRecover = stationId else { return }
Task { @MainActor in
do {
try await mainMixer.restartEngine()
try await play(stationId: stationToRecover)
} catch {
await errorReporter.reportError(
error, context: "Failed to recover engine after configuration change", level: .error)
}
}
}
deinit {
fileDownloadManager.cancelAllDownloads()
}
}
extension PlayolaStationPlayer: SpinPlayerDelegate {
public func player(_ player: SpinPlayer, startedPlaying spin: Spin) {
// Ignore callbacks from a superseded session β otherwise a spin scheduled by
// a prior play() could flip a newer .loading/.error/.idle state to .playing.
guard isCurrentGeneration(player.playGeneration) else {
os_log(
"Ignoring startedPlaying from superseded generation: %@",
log: PlayolaStationPlayer.logger, type: .info, spin.id)
return
}
os_log("Started playing: %@", log: PlayolaStationPlayer.logger, type: .info, spin.id)
self.state = .playing(spin)
schedulingTask?.cancel()
schedulingTask = Task { [generation = playGeneration] in
do {
await self.scheduleUpcomingSpins(generation: generation)
// Get a list of active file paths to exclude from pruning
let activePaths = self._spinPlayers
.compactMap { $0.localUrl?.path }
// Use the new pruning method with proper error handling
try self.fileDownloadManager.pruneCache(maxSize: nil, excludeFilepaths: activePaths)
} catch {
Task {
await errorReporter.reportError(
error, context: "Error during cache pruning after starting playback", level: .warning)