找回密码
 -注册-
查看: 11894|回复: 58
打印 上一主题 下一主题

五分钟让HQPlayer OS内置airplay功能(无需LMS无需其他硬件),兼谈AirPlay码率

[复制链接]
跳转到指定楼层
1
发表于 2022-7-22 15:15 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 17:13 编辑

1. 背景

2. 所需材料

3. 让HQPlayer OS支持airplay

4. 让实现更优雅(可选)

5. 关于码率的讨论



来自 2楼
 楼主| 发表于 2022-7-22 15:15 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 15:56 编辑

1. 背景

本文简要记录了在HQPlayer OS中安装airconnect,实现airplay功能的过程,需要读者对Linux有初步的了解,本方案的思路源自网友门的耳朵(详见【1】55楼),只是简化了实现。我自己的主力系统是Roon方案(详见【2】),apple music只搭配tws用,也并不认同软件升频的方案,本帖只讨论如何实现和技术原理,请不要和楼主讨论听感。

【1】http://erji.net/forum.php?mod=viewthread&tid=2269222&extra=
【2】http://erji.net/forum.php?mod=viewthread&tid=2253401&extra=

回复

使用道具 举报

来自 3楼
 楼主| 发表于 2022-7-22 15:16 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 15:57 编辑

2. 所需材料

为了显示本方案的简洁,本帖假定在u盘上安装HQPlayer OS,如果读者想把HQPlayer OS安装在机器内置硬盘上,可参考【3】。

【3】http://erji.net/forum.php?mod=viewthread&tid=2272124&extra=
回复

使用道具 举报

来自 4楼
 楼主| 发表于 2022-7-22 15:23 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 16:59 编辑

3. 让HQPlayer OS支持airplay

3.1 把HQPlayer OS烧录至u盘(可选)
这步其实非常简单,我们假定读者可能已经做好了HQPlayer OS。为了完整起见还是简单记录一下,首先在电脑上下载HQPlayer OS的img,如果你买了正版链接在【4】。

用dd/rufus/balenaEtcher/unetbootin等方法将其刻录至u盘即可。如果你和我一样使用macbook pro,且只有一个内置硬盘,那么只需在terminal输入:
  1. sudo /bin/dd if=~/Downloads/gparted-live-1.4.0-1-amd64.iso of=/dev/rdisk2s1
复制代码
其他软件按提示即可,也比较容易。

3.2 获得airupnp


下载链接【5】的airupnp-x86-64-static文件,并将其复制到刚刚烧录的u盘的第一个分区,
  1. # ls /boot/
  2. EFI              bzImage          loader
复制代码

3.3 准备启动脚本


编辑文件airupnp.service,
  1. [Unit]
  2. Description=AirUPnP bridge
  3. After=network-online.target
  4. Wants=network-online.target

  5. [Service]
  6. ExecStart=/var/lib/airconnect/airupnp-x86-64-static -c flc -Z
  7. Restart=on-failure
  8. RestartSec=30

  9. [Install]
  10. WantedBy=multi-user.target
复制代码
也放在u盘的第一个分区,
  1. # ls /boot/
  2. EFI              airupnp.service  bzImage          loader
复制代码

3.3 启动HQPlayer OS并部署airconnect


启动HQPlayer OS,用户名密码之类可以不管也可以参考【3】。
  1. mkdir -p /var/lib/airconnect/
  2. mv /boot/airupnp-x86-64-static /var/lib/airconnect/
  3. mv /boot/airupnp.service /etc/systemd/system/
  4. systemctl enable --now airupnp.service
复制代码
大功告成,下次重启也会自动打开,不知道读者用了五分钟没有。

【4】https://www.signalyst.eu/bins/hqplayerd/images/
【5】https://github.com/philippe44/AirConnect/tree/master/bin

回复

使用道具 举报

来自 5楼
 楼主| 发表于 2022-7-23 13:03 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-23 13:13 编辑
中关村东路 发表于 2022-7-22 15:23
3. 让HQPlayer OS支持airplay

3.1 把HQPlayer OS烧录至u盘(可选)

补充一下,感谢网友nblinan提醒,稳妥起见,上面帖子最后一段代码中给airupnp-x86-64-static增加执行权限(如果你用的是linux/unix/macox电脑应该不需要这行,但加上也没什么问题),修改成:
  1. mkdir -p /var/lib/airconnect/
  2. mv /boot/airupnp-x86-64-static /var/lib/airconnect/
  3. mv /boot/airupnp.service /etc/systemd/system/
  4. chmod +x /var/lib/airconnect/airupnp-x86-64-static
  5. systemctl enable --now airupnp.service
复制代码


回复

使用道具 举报

来自 6楼
发表于 2022-10-30 13:01 来自手机 | 只看该作者 来自 福建省福州市
hujdong 发表于 2022-10-28 09:01
谢谢回复,airupnp-x86-64-stati和编辑好的airupnp.service的文件都已经提前复制到hqplayer所在的EFI分区 ...

跟你一样,win10下把文件拷U盘,然后找不着,hp居然不支持putty远程,也没办法直接上传了,找了半天才找到在/run/media/sdb1目录下,供参考

回复

使用道具 举报

7
 楼主| 发表于 2022-7-22 15:20 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 20:15 编辑

重复
回复

使用道具 举报

8
 楼主| 发表于 2022-7-22 15:20 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 20:15 编辑

重复
回复

使用道具 举报

9
 楼主| 发表于 2022-7-22 15:30 | 只看该作者 来自 北京市
4. 让实现更优雅(可选)

上述操作略显草率,不但产生大量无用日志,如果读者家里有电视盒子等设备还会让airplay多了好多无谓的终端。下面我们定制一下airconnect,让实现更优雅。首先生成配置文件:
  1. /var/lib/airconnect//airupnp-x86-64-static -i /var/lib/airconnect/config.xml
复制代码
需要扫描几秒,扫完命令会自动退出。打开配置文件:
  1. nano /var/lib/airconnect/config.xml
复制代码

4.1 精简日志:

配置文件中一共有如下四项日志,中间的info/warn都改成error
  1. <main_log>info</main_log>
  2. <upnp_log>info</upnp_log>
  3. <util_log>warn</util_log>
  4. <raop_log>info</raop_log>
复制代码
修改日志大小的限制,下列“-1”改为1
  1. <log_limit>-1</log_limit>
复制代码

4.2 删除其他upnp设备

airconnect默认自动发现局域网内所有upnp设备供airplay选择,可以定制仅保留所需终端。例如我的配置文件中,末尾有如下代码:
  1. <device>
  2. <udn>uuid:111a1fda-fccb-49b8-b96f-4954a8ec8069</udn>
  3. <name>HQ:SignalystHQPlayer4+</name>
  4. <mac>bb:bb:14:cc:f2:72</mac>
  5. <enabled>1</enabled>
  6. </device>
  7. <device>
  8. <udn>uuid:tv_08d88c9c6b938c50c8100cfced4c5800_1612364126232_4220193050_89358878</udn>
  9. <name>    ^n^e ^z^d  ^g  ^b ^~^|TV(110)+</name>
  10. <mac>bb:bb:a9:cd:40:96</mac>
  11. <enabled>1</enabled>
  12. </device>
  13. <device>
  14. <udn>uuid:23456789-1234-1010-8000-045D4B6CFCB9</udn>
  15. <name>          ^j     ^t   ^f+</name>
  16. <mac>bb:bb:9a:13:55:fe</mac>
  17. <enabled>1</enabled>
  18. </device>
复制代码
这段代码实际上是三个设备,每个<device> xxxx </device>是一个,删掉你不需要的即可。例如我只保留了:
  1. <device>
  2. <udn>uuid:111a1fda-fccb-49b8-b96f-4954a8ec8069</udn>
  3. <name>HQ:SignalystHQPlayer4+</name>
  4. <mac>bb:bb:14:cc:f2:72</mac>
  5. <enabled>1</enabled>
  6. </device>
复制代码

4.2 修改启动文件

打开启动文件:
  1. nano /etc/systemd/system/airupnp.service
复制代码

找到ExecStart一行,改为:
  1. ExecStart=/var/lib/airconnect/airupnp-x86-64-static -Z -x /var/lib/airconnect/config.xml
复制代码
重启服务即可,
  1. systemctl daemon-reload
  2. systemctl restart airupnp.service
复制代码



回复

使用道具 举报

10
 楼主| 发表于 2022-7-22 15:30 | 只看该作者 来自 北京市
5. 关于码率的讨论
5.1 airconnect方案的视线
大学毕业之后就把c语言还给老师了,简单看了一下airconnect的实现,他做了个转接,接收airplay协议之后,使用alac解码,之后用flac@44/16编码,转发给upnp终端。

解码的代码在hairtunes_init中,
        ctx->alac_codec = alac_init(fmtp);

编码的代码在flac_init中,
        ok &= FLAC__stream_encoder_set_verify(codec, false);
        ok &= FLAC__stream_encoder_set_compression_level(codec, ctx->encode.config.flac.level);
        ok &= FLAC__stream_encoder_set_channels(codec, 2);
        ok &= FLAC__stream_encoder_set_bits_per_sample(codec, 16);
        ok &= FLAC__stream_encoder_set_sample_rate(codec, 44100);
        ok &= FLAC__stream_encoder_set_blocksize(codec, FLAC_BLOCK_SIZE);
        ok &= FLAC__stream_encoder_set_streamable_subset(codec, true);
        ok &= !FLAC__stream_encoder_init_stream(codec, flac_write_callback, NULL, NULL, NULL, ctx);
可以看到,接收到的文件用alac解码后之后编码的时候是强制44100/16的。似乎是有点让人失望,但是可能看了下面三小节内容,可能就没那么失望了。

5.2 philippe44对airplay协议的解释
参考【6】。简单的说,airplay是支持有损aac接收的,但airconnect方案只实现了无损的alac/pcm解码。之后默认封装成无损flac发给upnp。

AirConnect receives ALAC or raw PCM from its controller / source, there is no AAC involved. Then I re-encode and send to the player either FLAC, PCM/WAV or MP3. So to be crystal clear AirConnect does NOT receive AAC from controllers.
Now, what was that file before it has been sent over AirPlay, I have no idea. The controller can do any sort of transcoding it wants. All I know is that it sends lossless ALAC or raw PCM

As said in my previous response, I only support PCM and ALAC in my AirPlay manifest, hence AirConnect will always receive one of these. AFAIK, there is support for AAC and AAC-ELD in AirPlay 1 but I don't support these.

But as a summary, AirConnect receives 16@44.1 alac or plain PCM and it translates it to raw pcm/or wav (just adding header) or flac lossless (0..9 compression level) or mp3 lossy (64..320 Kbps) and then send that to the Chromecast device.. Nothing more nothing less. Does that answer your question? I'm not sure anymore what you're looking for

5.3 mikebrady对airplay协议的解释
参考【7】。简单的说,airplay1只支持固定的44100/16。airplay2支持两种模式,实时的alac CD质量,和带缓冲的aac。

Shairport Sync uses the AirPlay 1 protocol. In my experience, the AirPlay 1 protocol uses a fixed format: 44,100 frames per second, 16 bit linear PCM, interleaved stereo, losslessly compressed in ALAC -- let's call it "CD quality ALAC" . It doesn't matter what it's being sent from or what it's being sent to, the format remains the same. If the source material is encoded in a different form, it is transcoded to this format before transmission. This accords with your experience. If the source material is actually CD quality, stored losslessly, then AFAIR it used to be the case that it was transmitted bit-perfectly over AirPlay 1 by iTunes. I don't know if that is still the case, but I presume it is.

It follows that there is no scope for, and no need for, adjustments to Shairport Sync to control how audio is encoded -- it's always sent in CD-quality ALAC.

At the receiving end, Shairport Sync is meant to modify the audio data as little as possible. For example -- assuming you are outputting to the alsa back end -- if you have a CD-quality DAC and use its hardware mixer for volume control, or if you choose to ignore the volume control information coming from the source, or if you set the volume control to max, the audio material is sent to the DAC unmodified except for an occasional sync event.

You can force Shairport Sync to choose a particular output depth, but if it's set to auto, it will select the greatest depth. For instance, if you have a CD-quality DAC and a hardware mixer but with 24 or 32 bit resolution, the highest resolution is chosen and the material is recoded from 16 to 24 or 32 bits but without doing anything to it -- 8 or 16 binary zeroes are simply added to each sample as appropriate. In other words, as before, the audio material is effectively unaltered except for occasional sync events.

If you use Shairport Sync's software mixer or if you select monophonic output (in other words, if Shairport Sync has to process the audio material), then the necessary calculations are done at 32-bit precision (giving 64-bit results for multiplications) and dithered to the appropriate bit depth -- 32 / 24 / 16 or 8 (!) bits for the output device.

As far as AirPlay 2 is concerned, what we know is that it uses two transmission modes -- Realtime and Buffered -- and two encoding schemes: CD-Quality ALAC for Realtime streams and AAC for Buffered streams. So it fits that the new lossless formats might be transcoded to AAC for onward transmission to AirPlay 2 players.

5.4 苹果官方对airplay协议的解释
我简单搜索了一下,得到的结论是:

A. airplay/airplay2码率是44/48,但是48只有视频文件才支持,详见【8】。

AirPlay (1 and 2) is capable of ALAC (lossless) streaming up to 44.1 kHz (48 kHz for video contents).

B. airplay2只支持16bit。详见【9】。



【6】https://github.com/philippe44/AirConnect/issues/356
【7】https://github.com/mikebrady/shairport-sync/issues/1205
【8】https://discussions.apple.com/thread/252828942
【9】https://www.reddit.com/r/AppleMusic/comments/nkj26w/does_airplay_support_24_bit_how_do_we_take/

5.5 小结
综合5.1/5.2/5.3小节内容,我们知道airplay1/2对音频的支持上限就是alac@44/16,因此本帖子讨论的airconnect已经完美实现了无损的解码编码,所有的转码都发生在苹果自己的apple music投放airplay过程中。
也就是说,如果你希望使用airplay投放,可以放心的使用本帖方案;如果你追求高码率,那么直接使用ipad otg直连解码是更好的选择。
回复

使用道具 举报

11
 楼主| 发表于 2022-7-22 15:38 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 15:55 编辑

5. 关于码率的讨论

5.1 airconnect方案的视线

大学毕业之后就把c语言还给老师了,简单看了一下airconnect的实现,他做了个转接,接收airplay协议之后,使用alac解码,之后用flac@44/16编码,转发给upnp终端。

解码的代码在hairtunes_init中,
  1. ctx->alac_codec = alac_init(fmtp);
复制代码

编码的代码在flac_init中,
  1. ok &= FLAC__stream_encoder_set_verify(codec, false);
  2.         ok &= FLAC__stream_encoder_set_compression_level(codec, ctx->encode.config.flac.level);
  3.         ok &= FLAC__stream_encoder_set_channels(codec, 2);
  4.         ok &= FLAC__stream_encoder_set_bits_per_sample(codec, 16);
  5.         ok &= FLAC__stream_encoder_set_sample_rate(codec, 44100);
  6.         ok &= FLAC__stream_encoder_set_blocksize(codec, FLAC_BLOCK_SIZE);
  7.         ok &= FLAC__stream_encoder_set_streamable_subset(codec, true);
  8.         ok &= !FLAC__stream_encoder_init_stream(codec, flac_write_callback, NULL, NULL, NULL, ctx);
复制代码

可以看到,接收到的文件用alac解码后之后编码的时候是强制44100/16的。似乎是有点让人失望,但是看了下面三小节内容,可能就没那么失望了。


回复

使用道具 举报

12
 楼主| 发表于 2022-7-22 15:39 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 15:46 编辑

5.2 philippe44对airplay协议的解释

参考【6】。简单的说,airplay是支持有损aac接收的,但airconnect方案只实现了无损的alac/pcm解码。之后默认封装成无损flac发给upnp。
回复

使用道具 举报

13
 楼主| 发表于 2022-7-22 15:39 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 15:46 编辑

5.3 mikebrady对airplay协议的解释

参考【7】。简单的说,airplay1只支持固定的44100/16。airplay2支持两种模式,实时的alac CD质量,和带缓冲的aac。
回复

使用道具 举报

14
 楼主| 发表于 2022-7-22 15:40 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 15:46 编辑

5.4 苹果官方对airplay协议的解释

我简单搜索了一下,得到的结论是:

A. airplay/airplay2码率是44/48,但是48只有视频文件才支持,详见【8】。

B. airplay2只支持16bit。详见【9】,见图。


回复

使用道具 举报

15
 楼主| 发表于 2022-7-22 15:41 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 20:15 编辑

重复
回复

使用道具 举报

16
 楼主| 发表于 2022-7-22 15:43 | 只看该作者 来自 北京市
本帖最后由 中关村东路 于 2022-7-22 16:06 编辑

5.5 小结

综合5.1/5.2/5.3/5.4小节内容,我们知道airplay1/2对音频的支持上限是alac@44/16,因此airconnect工作流程内并未转码,所有的转码都发生在苹果自己的apple music中。
也就是说,如果你希望使用airplay,可以放心按本帖实现;高码率就只能使用otg直连解码。
回复

使用道具 举报

17
 楼主| 发表于 2022-7-22 15:45 | 只看该作者 来自 北京市
【6】https://g,i,t,h,u,b.com/philippe44/AirConnect/issues/356
【7】https://g,i,t,h,u,b.com/mikebrady/shairport-sync/issues/1205
【8】https://discussions.apple.com/thread/252828942
【9】https://www.r,e,d,d,i,t.com/r/AppleMusic/comments/nkj26w/does_airplay_support_24_bit_how_do_we_take/
回复

使用道具 举报

18
发表于 2022-7-22 15:50 | 只看该作者 来自 北京市
强贴!
回复

使用道具 举报

19
 楼主| 发表于 2022-7-22 16:07 | 只看该作者 来自 北京市

小号都这么高积分
回复

使用道具 举报

20
发表于 2022-7-22 16:17 | 只看该作者 来自 北京市

托老毛子的福,在资源区搬点砖就涨起来了。

论质量还得是你这种干货技术贴。
回复

使用道具 举报

21
 楼主| 发表于 2022-7-22 16:24 | 只看该作者 来自 北京市
留得青山在 发表于 2022-7-22 16:17
托老毛子的福,在资源区搬点砖就涨起来了。

论质量还得是你这种干货技术贴。

放暑假住乡下重新折腾了一套系统。就整理了两份笔记。现在已经都删了装回gentoo+roon了,硬件升频dsd512
http://erji.net/forum.php?mod=viewthread&tid=2253401&extra=
回复

使用道具 举报

22
发表于 2022-7-22 16:35 | 只看该作者 来自 广西
本帖最后由 万远 于 2022-7-22 16:38 编辑

只能请一声牛di!在编码的代码在flac_init中里改成高码能实现高码传输吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | -注册-

本版积分规则

Archiver|手机版|粤icp备09046054号|耳机网-耳机大家坛

粤公网安备 44030602000598号 耳机大家坛、www.erji.net、网站LOGO图形均为注册商标

GMT+8, 2024-5-14 21:55

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表