Catch AVAudioPlayerNode exceptions, activate the session before building engines, launch logging
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
parent
a65db499f9
commit
aa9b34b764
7 changed files with 53 additions and 8 deletions
|
|
@ -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)")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() }
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ struct MulberryApp: App {
|
|||
@Environment(\.scenePhase) var phase
|
||||
|
||||
init() {
|
||||
Log.i("app: launch")
|
||||
_ = Player.shared
|
||||
Library.shared.startWatching()
|
||||
Library.shared.scan()
|
||||
|
|
|
|||
5
Native/Bridging.h
Normal file
5
Native/Bridging.h
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
// Swift bridging header for the app target.
|
||||
#include "ObjCTry.h"
|
||||
#ifdef MULBERRY_FFMPEG
|
||||
#include "ffdecode.h"
|
||||
#endif
|
||||
4
Native/ObjCTry.h
Normal file
4
Native/ObjCTry.h
Normal file
|
|
@ -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 <Foundation/Foundation.h>
|
||||
NSString *_Nullable mulberry_try(void (^_Nonnull block)(void));
|
||||
10
Native/ObjCTry.m
Normal file
10
Native/ObjCTry.m
Normal file
|
|
@ -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)"];
|
||||
}
|
||||
}
|
||||
9
build.sh
9
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
|
||||
|
|
|
|||
Loading…
Reference in a new issue