Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(media): make macos-media-remote work on macOS 15.4+ (#749)
Gabriel Stein07525aa771b1
diff --git a/resources/macos-media-remote.swift b/resources/macos-media-remote.swiftindex d165d9bef..6a698a84f 100644--- a/resources/macos-media-remote.swift+++ b/resources/macos-media-remote.swift@@ -1,69 +1,98 @@+import CoreFoundationimport Foundation-// MediaRemote.framework command constantslet kMRPlay: UInt32 = 0let kMRPause: UInt32 = 1-let kMRTogglePlayPause: UInt32 = 2+let usage = "Usage: macos-media-remote --is-playing|--pause|--play"+let playbackRateKey = "kMRMediaRemoteNowPlayingInfoPlaybackRate"-// C function type aliases from MediaRemote.framework-typealias MRMediaRemoteSendCommandType = @convention(c) (UInt32, Optional<AnyObject>) -> Bool-typealias MRMediaRemoteGetNowPlayingApplicationIsPlayingType = @convention(c) (DispatchQueue, @escaping (Bool) -> Void) -> Void+typealias MRSendCommand = @convention(c) (UInt32, Optional<AnyObject>) -> Bool+typealias MRRegister = @convention(c) (DispatchQueue) -> Void+// The get-callback is an Objective-C block, not a C function pointer;+// @convention(c) here would silently misroute the callback.+typealias MRGetInfo = @convention(c) (+ DispatchQueue, @escaping @convention(block) ([AnyHashable: Any]?) -> Void+) -> Void-func loadMediaRemote() -> (send: MRMediaRemoteSendCommandType, isPlaying: MRMediaRemoteGetNowPlayingApplicationIsPlayingType)? {- let frameworkPath = "/System/Library/PrivateFrameworks/MediaRemote.framework/MediaRemote"- guard let handle = dlopen(frameworkPath, RTLD_NOW) else { return nil }-- guard let sendPtr = dlsym(handle, "MRMediaRemoteSendCommand"),- let isPlayingPtr = dlsym(handle, "MRMediaRemoteGetNowPlayingApplicationIsPlaying") else {- return nil- }-- let send = unsafeBitCast(sendPtr, to: MRMediaRemoteSendCommandType.self)- let isPlaying = unsafeBitCast(isPlayingPtr, to: MRMediaRemoteGetNowPlayingApplicationIsPlayingType.self)- return (send: send, isPlaying: isPlaying)+struct MediaRemote {+ let send: MRSendCommand+ let register: MRRegister?+ let getInfo: MRGetInfo}-func checkIsPlaying(_ isPlayingFn: MRMediaRemoteGetNowPlayingApplicationIsPlayingType) -> Bool {- let semaphore = DispatchSemaphore(value: 0)- var playing = false- isPlayingFn(DispatchQueue.main) { result in- playing = result- semaphore.signal()+func loadMediaRemote() -> MediaRemote? {+ let url = URL(fileURLWithPath: "/System/Library/PrivateFrameworks/MediaRemote.framework")+ guard let bundle = CFBundleCreate(kCFAllocatorDefault, url as CFURL),+ let sendPtr = CFBundleGetFunctionPointerForName(bundle, "MRMediaRemoteSendCommand" as CFString),+ let infoPtr = CFBundleGetFunctionPointerForName(bundle, "MRMediaRemoteGetNowPlayingInfo" as CFString) else {+ return nil}- _ = semaphore.wait(timeout: .now() + 2)- return playing+ let regPtr = CFBundleGetFunctionPointerForName(+ bundle, "MRMediaRemoteRegisterForNowPlayingNotifications" as CFString)+ return MediaRemote(+ send: unsafeBitCast(sendPtr, to: MRSendCommand.self),+ register: regPtr.map { unsafeBitCast($0, to: MRRegister.self) },+ getInfo: unsafeBitCast(infoPtr, to: MRGetInfo.self)+ )}-guard let mr = loadMediaRemote() else {- print("ERROR")- exit(1)+func emit(_ message: String, exitCode: Int32) -> Never {+ print(message)+ exit(exitCode)}let args = CommandLine.argumentslet command = args.count > 1 ? args[1] : ""switch command {-case "--is-playing":- let playing = checkIsPlaying(mr.isPlaying)- print(playing ? "PLAYING" : "NOT_PLAYING")- exit(playing ? 0 : 1)+case "--play", "--is-playing", "--pause":+ break+default:+ emit(usage, exitCode: 1)+}-case "--pause":- let playing = checkIsPlaying(mr.isPlaying)- if !playing {- print("NOT_PLAYING")- exit(1)- }- let ok = mr.send(kMRPause, nil)- print(ok ? "OK" : "FAIL")- exit(ok ? 0 : 1)+// macOS 15.4 closed the now-playing daemon to unprivileged Mach-O processes;+// both reads and sends silently no-op. Emit UNKNOWN so the consumer falls+// back to its media-key path.+let v = ProcessInfo.processInfo.operatingSystemVersion+if v.majorVersion > 15 || (v.majorVersion == 15 && v.minorVersion >= 4) {+ emit("UNKNOWN", exitCode: 1)+}-case "--play":+guard let mr = loadMediaRemote() else {+ emit("ERROR", exitCode: 1)+}++// macOS 13+ requires this before any subsequent get-callback fires.+mr.register?(DispatchQueue.main)++if command == "--play" {let ok = mr.send(kMRPlay, nil)- print(ok ? "OK" : "FAIL")- exit(ok ? 0 : 1)+ emit(ok ? "OK" : "FAIL", exitCode: ok ? 0 : 1)+}-default:- print("Usage: macos-media-remote --is-playing|--pause|--play")- exit(1)+DispatchQueue.main.asyncAfter(deadline: .now() + 2) {+ emit("NOT_PLAYING", exitCode: 1)}++DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {+ mr.getInfo(DispatchQueue.main) { info in+ guard let info = info, !info.isEmpty else {+ emit("NOT_PLAYING", exitCode: 1)+ }+ let rate = (info[playbackRateKey] as? NSNumber)?.doubleValue ?? 0+ let playing = rate > 0+ switch command {+ case "--is-playing":+ emit(playing ? "PLAYING" : "NOT_PLAYING", exitCode: playing ? 0 : 1)+ case "--pause":+ if !playing { emit("NOT_PLAYING", exitCode: 1) }+ let ok = mr.send(kMRPause, nil)+ emit(ok ? "OK" : "FAIL", exitCode: ok ? 0 : 1)+ default:+ emit(usage, exitCode: 1)+ }+ }+}++CFRunLoopRun()