无头浏览器:音频输出至 Pulseaudio

无头浏览器:音频输出至 Pulseaudio

我正在尝试在 EC2/Ubuntu 20.04 实例上运行无头浏览器,然后将生成的音频流输出到默认的 Pulseaudio 接收器(然后由 DarkIce/Icecast 拾取)。我只想运行一个网页(我的网页,托管在另一台服务器上,需要 jQuery 和 Howler.js),并且生成的浏览器/流必须全天候保持打开状态。

我已成功获取实例上的音频文件以播放到 Icecast(使用 ogg123),因此 ogg123>Pulseaudio>Darkice>Icecast2 可以正常工作。我创建了一个默认接收器,如下所示

pactl load-module module-null-sink sink_name=radio
pacmd update-sink-proplist radio device.description=radio
pacmd set-default-sink radio

并通过创建 ~/.asoundrc 来包含 Pulseaudio 成为默认驱动程序

pcm.default pulse
ctl.default pulse

我不确定什么方法才能让浏览器播放顺畅。我尝试过(google-chome 和 chromium)直接指向音频文件和带有播放音频的 js 的页面(目的是最终在 Screen 中运行它),这两种方法似乎都找到了内容,但都没有播放任何音频。例如,

 google-chrome-stable --headless --disable-gpu --autoplay-policy=no-user-gesture-required --user-data-dir=/home/ubuntu/chromeUser --disable-accelerated-video-decode --disable-software-rasterizer --enable-logging=stderr --v=1  https://domain.name/stream.html 

生成以下内容

[0608/102421.257217:INFO:cpu_info.cc(53)] Available number of cores: 1
[0608/102421.258656:INFO:cpu_info.cc(53)] Available number of cores: 1
[0608/102421.258861:VERBOSE1:zygote_main_linux.cc(217)] ZygoteMain: initializing 0 fork delegates
[0608/102421.259318:VERBOSE1:zygote_main_linux.cc(217)] ZygoteMain: initializing 0 fork delegates
[0608/102421.271947:VERBOSE1:webrtc_internals.cc(118)] Could not get the download directory.
[0608/102421.280120:VERBOSE1:breakpad_linux.cc(2071)] Non Browser crash dumping enabled for: gpu-process
[0608/102421.283276:ERROR:gpu_init.cc(440)] Passthrough is not supported, GL is disabled
[0608/102421.286107:VERBOSE1:breakpad_linux.cc(2071)] Non Browser crash dumping enabled for: renderer
[0608/102421.288099:VERBOSE1:sandbox_linux.cc(69)] Activated seccomp-bpf sandbox for process type: gpu-process.
[0608/102421.293815:VERBOSE1:sandbox_linux.cc(69)] Activated seccomp-bpf sandbox for process type: renderer.
[0608/102421.303930:VERBOSE1:device_data_manager_x11.cc(216)] X Input extension not available
[0608/102421.356750:VERBOSE1:configured_proxy_resolution_service.cc(852)] PAC support disabled because there is no system implementation
[0608/102421.357514:VERBOSE1:configured_proxy_resolution_service.cc(852)] PAC support disabled because there is no system implementation
[0608/102421.359494:VERBOSE1:network_delegate.cc(32)] NetworkDelegate::NotifyBeforeURLRequest: https://domain.name/stream.html
[0608/102421.411474:VERBOSE1:document.cc(3974)] Document::DispatchUnloadEvents() URL = <null>
[0608/102421.411727:VERBOSE1:document.cc(4054)] Actually dispatching an UnloadEvent: URL = <null>
[0608/102421.421675:VERBOSE1:network_delegate.cc(32)] NetworkDelegate::NotifyBeforeURLRequest: /path/to/jquery.min.js
[0608/102421.424968:VERBOSE1:network_delegate.cc(32)] NetworkDelegate::NotifyBeforeURLRequest: /path/to/jquery-ui.min.js
[0608/102421.429480:VERBOSE1:network_delegate.cc(32)] NetworkDelegate::NotifyBeforeURLRequest: /path/to/howler.min.js

这是让它工作的正确方法吗(如果是,为什么不行?),或者我应该使用 Selenium/Puppeteer/其他东西?

谢谢,克里斯

[旁白:我试过事物的数量尝试删除“不支持 Passthrough,GL 是 swiftshader”错误,但没有成功,尽管它似乎并没有阻止浏览器访问该页面]

答案1

我设法通过运行 Puppeteer 来实现这一点,而不是尝试从命令行操控 Chromium。我使用过这个答案希望使用以下参数保持 Puppeteer 打开,以使音频自动启动。

 this.browser = await puppeteer.launch({
     headless: true,
     ignoreDefaultArgs: [
         "--mute-audio",
     ],
     args: [
         "--autoplay-policy=no-user-gesture-required",
     ],
  });

在 PulseAudio 中设置默认接收器并添加 ~/.ascoundrc(如上所述)后,一切正常(接收器音频由 DarkIce > IceCast > 广播拾取)。我可能会使用pm2来监督 Puppeteer,但目前这是一个可行的解决方案。

相关内容