diff --git a/App/Audio/Player.swift b/App/Audio/Player.swift index 4540c4b..1c32b8b 100644 --- a/App/Audio/Player.swift +++ b/App/Audio/Player.swift @@ -44,14 +44,17 @@ final class Player: ObservableObject { var current: Song? { queue.indices.contains(index) ? queue[index] : nil } private init() { + Log.i("player: init") setupSession() setupRemote() restore() + Log.i("player: restored \(queue.count) queued songs") } // MARK: Engines private func engine(for s: Song) -> PlaybackEngine { + activateSession() // the engines must be built and started with an active session let kind = EngineKind(rawValue: prefs.engine) ?? .standard if s.isStream || (kind == .standard && StandardEngine.canPlay(s)) { return standard } universal.eqAllowed = kind == .fx @@ -64,10 +67,15 @@ final class Player: ObservableObject { e.onFailed = { [weak self] msg in self?.failed(msg) } } + private var sessionActive = false + private func activateSession() { + guard !sessionActive else { return } do { + sessionActive = true try AVAudioSession.sharedInstance().setActive(true) } catch { + sessionActive = false Log.e("audio session activate failed: \(error.localizedDescription)") } } diff --git a/App/Audio/UniversalEngine.swift b/App/Audio/UniversalEngine.swift index 8b3944f..8d3756a 100644 --- a/App/Audio/UniversalEngine.swift +++ b/App/Audio/UniversalEngine.swift @@ -150,7 +150,7 @@ final class UniversalEngine: PlaybackEngine { MainActor.assumeIsolated { guard let self, self.playing else { return } Log.i("universal: configuration change, restarting") - if self.start() { self.decks[self.active].node.play() } + if self.start() { _ = self.safePlay(self.decks[self.active].node) } } } applyEQ() @@ -166,6 +166,7 @@ final class UniversalEngine: PlaybackEngine { @discardableResult private func start() -> Bool { if engine.isRunning { return true } + Log.i("universal: starting engine, output \(engine.outputNode.outputFormat(forBus: 0))") do { try engine.start() return true @@ -205,12 +206,27 @@ final class UniversalEngine: PlaybackEngine { plan.set(after: lastIndex, song.map { ($0, index) }) } + /// node.play() throws an NSException when the engine isn't really running; catch, restart once, report. + private func safePlay(_ node: AVAudioPlayerNode) -> Bool { + if let err = mulberry_try({ node.play() }) { + Log.e("universal: play threw \(err) (running=\(engine.isRunning)) — restarting engine") + engine.stop() + guard start() else { return false } + if let err2 = mulberry_try({ node.play() }) { + Log.e("universal: play threw again: \(err2)") + onFailed?("Couldn't start playback (\(err2))") + return false + } + } + return true + } + func play(fade: Bool, volume: Float) { self.volume = volume guard start() else { return } let node = decks[active].node node.volume = fade ? 0 : volume - node.play() + guard safePlay(node) else { return } playing = true if fade { rampVolume(from: 0, to: volume, ms: 220) { node.volume = $0 } } } @@ -241,7 +257,7 @@ final class UniversalEngine: PlaybackEngine { decks[1 - active].stop() decks[active].load(src, index: lastIndex, from: AVAudioFramePosition(t * Decoder.rate)) decks[active].node.volume = volume - if playing { decks[active].node.play() } + if playing { _ = safePlay(decks[active].node) } } var position: Double { @@ -261,7 +277,7 @@ final class UniversalEngine: PlaybackEngine { let nd = decks[active] nd.load(src, index: index, from: 0) nd.node.volume = 0 - nd.node.play() + guard safePlay(nd.node) else { active = 1 - active; return false } self.volume = volume let ms = seconds * 1000 rampVolume(from: old.node.volume, to: 0, ms: ms, set: { old.node.volume = $0 }) { old.stop() } diff --git a/App/MulberryApp.swift b/App/MulberryApp.swift index 29d8942..348a420 100644 --- a/App/MulberryApp.swift +++ b/App/MulberryApp.swift @@ -6,6 +6,7 @@ struct MulberryApp: App { @Environment(\.scenePhase) var phase init() { + Log.i("app: launch") _ = Player.shared Library.shared.startWatching() Library.shared.scan() diff --git a/Native/Bridging.h b/Native/Bridging.h new file mode 100644 index 0000000..f0d4a64 --- /dev/null +++ b/Native/Bridging.h @@ -0,0 +1,5 @@ +// Swift bridging header for the app target. +#include "ObjCTry.h" +#ifdef MULBERRY_FFMPEG +#include "ffdecode.h" +#endif diff --git a/Native/ObjCTry.h b/Native/ObjCTry.h new file mode 100644 index 0000000..e09cdad --- /dev/null +++ b/Native/ObjCTry.h @@ -0,0 +1,4 @@ +// Runs a block and returns the Objective-C exception reason it threw, or nil (Swift can't catch NSException). +#pragma once +#import +NSString *_Nullable mulberry_try(void (^_Nonnull block)(void)); diff --git a/Native/ObjCTry.m b/Native/ObjCTry.m new file mode 100644 index 0000000..7f31a71 --- /dev/null +++ b/Native/ObjCTry.m @@ -0,0 +1,10 @@ +#import "ObjCTry.h" + +NSString *mulberry_try(void (^block)(void)) { + @try { + block(); + return nil; + } @catch (NSException *e) { + return [NSString stringWithFormat:@"%@: %@", e.name, e.reason ?: @"(no reason)"]; + } +} diff --git a/build.sh b/build.sh index acdaedb..a23ab03 100755 --- a/build.sh +++ b/build.sh @@ -39,14 +39,15 @@ link() { } LD=link "$SWIFT/clang" -target "$TARGET" -isysroot "$SDK" -O2 -c Native/compat.c -o "$OUT/obj/compat.o" +"$SWIFT/clang" -target "$TARGET" -isysroot "$SDK" -O2 -fobjc-arc -c Native/ObjCTry.m -o "$OUT/obj/objctry.o" # FFmpeg for the extra formats, when Native/build-ffmpeg.sh has been run. -ff=() -fflibs=() +ff=(-import-objc-header Native/Bridging.h) +fflibs=("$OUT/obj/objctry.o") if [ -f Native/ffmpeg/lib/libavformat.a ]; then "$SWIFT/clang" -target "$TARGET" -isysroot "$SDK" -O2 -INative/ffmpeg/include -c Native/ffdecode.c -o "$OUT/obj/ffdecode.o" - ff=(-D MULBERRY_FFMPEG -import-objc-header Native/ffdecode.h) - fflibs=("$OUT/obj/ffdecode.o" Native/ffmpeg/lib/libavformat.a Native/ffmpeg/lib/libavcodec.a + ff=(-D MULBERRY_FFMPEG -Xcc -DMULBERRY_FFMPEG -import-objc-header Native/Bridging.h) + fflibs+=("$OUT/obj/ffdecode.o" Native/ffmpeg/lib/libavformat.a Native/ffmpeg/lib/libavcodec.a Native/ffmpeg/lib/libswresample.a Native/ffmpeg/lib/libavutil.a -lz -lbz2 -liconv -framework AudioToolbox -framework CoreFoundation -framework CoreMedia -framework CoreVideo -framework Security -framework VideoToolbox) fi