使用 Google Nest Mini 串流 AirPlay 藉由 macOS

之前有寫一篇「使用 Google Nest Mini 串流 AirPlay 藉由 Raspberry Pi」,後來發現效能不是很好有點 lag,所以改用 Mac 桌機來幫忙串流。目標還是把 Google Nest Mini 變成可以透過 AirPlay 串流音樂。

可以先瀏覽 AirConnect 使用已經發佈的版本:https://github.com/philippe44/AirConnect/releases

然後我選擇當前的版本 1.7.0 ,先下載好該版本 AirConnect-1.7.0.zip 。建議先建立好一個目錄再解開,要不然會有一堆檔案散落在您的 Downloads 目錄。

sam@liho ~ % mkdir ~/Downloads/AirConnect-1.7.0
sam@liho ~ % cd ~/Downloads/AirConnect-1.7.0
sam@liho AirConnect-1.7.0 % unzip ~/Downloads/AirConnect-1.7.0.zip
Archive:  /Users/sam/Downloads/AirConnect-1.7.0.zip
  inflating: aircast-freebsd-x86_64
  inflating: aircast-freebsd-x86_64-static
  inflating: aircast-linux-aarch64
  inflating: aircast-linux-aarch64-static
  inflating: aircast-linux-arm
  inflating: aircast-linux-arm-static
  inflating: aircast-linux-armv5
  inflating: aircast-linux-armv5-static
  inflating: aircast-linux-armv6
  inflating: aircast-linux-armv6-static
  inflating: aircast-linux-mips
  inflating: aircast-linux-mips-static
  inflating: aircast-linux-mipsel
  inflating: aircast-linux-mipsel-static
  inflating: aircast-linux-powerpc
  inflating: aircast-linux-powerpc-static
  inflating: aircast-linux-sparc64
  inflating: aircast-linux-sparc64-static
  inflating: aircast-linux-x86
  inflating: aircast-linux-x86-static
  inflating: aircast-linux-x86_64
  inflating: aircast-linux-x86_64-static
  inflating: aircast-macos
  inflating: aircast-macos-arm64
  inflating: aircast-macos-arm64-static
  inflating: aircast-macos-static
  inflating: aircast-macos-x86_64
  inflating: aircast-macos-x86_64-static
  inflating: aircast-solaris-x86_64
  inflating: aircast-solaris-x86_64-static
  inflating: aircast-static.exe
  inflating: aircast.exe
  inflating: airupnp-freebsd-x86_64
  inflating: airupnp-freebsd-x86_64-static
  inflating: airupnp-linux-aarch64
  inflating: airupnp-linux-aarch64-static
  inflating: airupnp-linux-arm
  inflating: airupnp-linux-arm-static
  inflating: airupnp-linux-armv5
  inflating: airupnp-linux-armv5-static
  inflating: airupnp-linux-armv6
  inflating: airupnp-linux-armv6-static
  inflating: airupnp-linux-mips
  inflating: airupnp-linux-mips-static
  inflating: airupnp-linux-mipsel
  inflating: airupnp-linux-mipsel-static
  inflating: airupnp-linux-powerpc
  inflating: airupnp-linux-powerpc-static
  inflating: airupnp-linux-sparc64
  inflating: airupnp-linux-sparc64-static
  inflating: airupnp-linux-x86
  inflating: airupnp-linux-x86-static
  inflating: airupnp-linux-x86_64
  inflating: airupnp-linux-x86_64-static
  inflating: airupnp-macos
  inflating: airupnp-macos-arm64
  inflating: airupnp-macos-arm64-static
  inflating: airupnp-macos-static
  inflating: airupnp-macos-x86_64
  inflating: airupnp-macos-x86_64-static
  inflating: airupnp-solaris-x86_64
  inflating: airupnp-solaris-x86_64-static
  inflating: airupnp-static.exe
  inflating: airupnp.exe
  inflating: libcrypto-1_1.dll
  inflating: libssl-1_1.dll
  inflating: ucrtbase.dll
  inflating: vcruntime140.dll

安裝前先安裝 openssl

sam@liho AirConnect-1.7.0 % brew install openssl

如果 homebrew 位於 /opt/homebrew 路徑,那要變成 root 並設定 openssl 環境

sam@liho AirConnect-1.7.0 % sudo -s
Password:
root@liho AirConnect-1.7.0 # ln -s /opt/homebrew/lib/libssl.dylib /usr/local/lib/libssl.dylib
root@liho AirConnect-1.7.0 # ln -s /opt/homebrew/lib/libcrypto.dylib /usr/local/lib/libcrypto.dylib

然後把下載預先編譯好的執行檔案複製到相對應的地方,並且設定可以執行。

root@liho AirConnect-1.7.0 # mkdir -p /usr/local/bin
root@liho AirConnect-1.7.0 # cp aircast-macos-x86_64-static /usr/local/bin/aircast
root@liho AirConnect-1.7.0 # chmod 755 /usr/local/bin/aircast

可以先用 aircast 執行確認一下是否有抓到 Google Nest Mini,因為我的環境有兩個網段,所以要指定綁定在哪個網段。如果沒有別的網段就可以忽略 -b 的選項。

root@liho AirConnect-1.7.0 # /usr/local/bin/aircast -b en0

執行後會出現安全警告,的確也要注意因為有可能裡面會有惡意程式,我選擇相信它 ,所以先按 Cancel:

然後進入「系統偏好」內「安全及隱私」允許 aircast 這支程式,解鎖然後按「Allow Anyway」

再次執行前述指令「/usr/local/bin/aircast -b en0」,然後按「Open」繼續。

用上述的指令,就可以啟動服務,但是還沒設定成背景執行。 先用 command ⌘ + C 跳出來。然後產生 config 檔案,用下列指令:

root@liho AirConnect-1.7.0 # mkdir -p /usr/local/etc
root@liho AirConnect-1.7.0 # /usr/local/bin/aircast -b en0 -i /usr/local/etc/aircast.xml

然後進入「/usr/local/etc/aircast.xml」檔案編輯,看哪些要 disable 哪些要重新命名。下面的範例省略其他裝置,如果要 disable 就把 「<enabled>1</enabled>」改成「<enabled>0</enabled>」,如果要改名就編輯在「<name>XXXXXXX</name>」裡面的值。

root@liho AirConnect-1.7.0 # vi /usr/local/etc/aircast.xml
(omit)
<device>
<udn>FE49198C-0AFE-4687-A516-XXXXXXXXXXXX</udn>
<name>Google Nest Mini</name>
<mac>cc:cc:b7:XX:XX:XX</mac>
<enabled>1</enabled>
</device>
(omit)

然後用下列指令啟動服務:

root@liho AirConnect-1.7.0 # /usr/local/bin/aircast -b en0 -l -z -x /usr/local/etc/aircast.xml

沒有問題的話應該可以看到剛剛設定的 Google Nest Mini

然後在 console 按 command ⌘ + C 跳出來,開始設定每次開機自動執行 aircast 程式,先產生 Launch Daemons 組態檔。

root@liho AirConnect-1.7.0 # vi /Library/LaunchDaemons/aircast.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Label</key>
        <string>aircast</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/local/bin/aircast</string>
            <string>-b</string>
            <string>en0</string>
            <string>-l</string>
            <string>-z</string>
            <string>-x</string>
            <string>/usr/local/etc/aircast.xml</string>
        </array>
        <key>NetworkState</key>
        <true/>
        <key>RunAtLoad</key>
        <true/>
        <key>LaunchOnlyOnce</key>
        <true/>
        <key>KeepAlive</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/var/log/aircast.log</string>
        <key>EnvironmentVariables</key>
        <dict>
            <key>PATH</key>
            <string>/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
        </dict>
    </dict>
</plist>

然後設定開機後就執行

root@liho AirConnect-1.7.0 # launchctl enable system/aircast
root@liho AirConnect-1.7.0 # launchctl bootstrap system /Library/LaunchDaemons/aircast.plist

如果開機不想load可以用下列指令:

root@liho ~ # launchctl unload -w /Library/LaunchDaemons/aircast.plist
root@liho ~ # rm -rf /Library/LaunchDaemons/aircast.plist