Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ALSA support inside docker #403

Closed
dbuezas opened this issue May 1, 2023 · 14 comments
Closed

Add ALSA support inside docker #403

dbuezas opened this issue May 1, 2023 · 14 comments
Labels
enhancement New feature or request

Comments

@dbuezas
Copy link
Contributor

dbuezas commented May 1, 2023

Since I didn't manage to get the audio stream from the microphone, I made a local addon that runs this command:

ffmpeg -f alsa -ac 1 -i default -c:a libopus -ar:a 16000 -ac:a 1 -af adelay=0ms -rtsp_transport tcp -f rtsp rtsp://localhost:8554/webcam

updated: had pasted the wrong command

And I have a stream called webcam in the go2rtc config with only video. When I open it i the webrtcard, it magically picks up both video and audio. Is this the right way of doing it?
I also tried creating the final stream with both video and audio from my addon, but then I don't have where to send it to (since no stream is defined in go2rtc)

What would be the right way of doing this?


Btw, in my local addon I have these packages:

RUN  apk add --no-cache \
        alsa-plugins-pulse=1.2.7.1-r0 \
        alsa-utils=1.2.8-r0 \
        pulseaudio-utils=16.1-r6 \
        ffmpeg 

and in the config.yaml, I added:

audio: true
video: true

Maybe adding alsa and pulseaudio to the go2rtc docker container would allow for using the mic?

@dbuezas
Copy link
Contributor Author

dbuezas commented May 1, 2023

Here's the addon (it's extremely basic) /~https://github.com/dbuezas/microphone-stream-addon

@dbuezas
Copy link
Contributor Author

dbuezas commented May 1, 2023

(updated the ffmpeg command, I had pasted the wrong one)

@AlexxIT AlexxIT added the enhancement New feature or request label May 2, 2023
@AlexxIT
Copy link
Owner

AlexxIT commented May 2, 2023

streams:
  camera1:
  - <some video source>
  - exec:ffmpeg -f alsa <other args> -rtsp_transport tcp -f rtsp {output}

@dbuezas
Copy link
Contributor Author

dbuezas commented May 2, 2023

Right, that would be perfect, but alsa doesn't work for me in the frigate nor go2rtc AddOns (because of missing packages I believe)

@AlexxIT
Copy link
Owner

AlexxIT commented May 2, 2023

Have you tried go2rtc hardware version (Debian linux)? Maybe it support alsa out of box. Looks like your example for Alpine.

@dbuezas
Copy link
Contributor Author

dbuezas commented May 2, 2023

I have not, I'll give it a try. Unfortunately frigate seems to crash often (and even crash the computer due to full ram) when using go2rtc 1.4.x
I'll retest this, maybe it depends on which streams i have or just a coincidence

@dbuezas
Copy link
Contributor Author

dbuezas commented May 3, 2023

With go2rtc hardware:

19:56:58.087 DBG [exec] run url="exec:ffmpeg -f alsa -ac 1 -i default -c:a libopus -ar:a 16000 -ac:a 1 -af adelay=0ms -rtsp_transport tcp -f rtsp rtsp://127.0.0.1:8554/56c284069c96908a52d5ac801ed10e26"
ffmpeg version 5.1.2-3 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 12 (Debian 12.2.0-14)
  configuration: --prefix=/usr --extra-version=3 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libglslang --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librist --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --disable-sndio --enable-libjxl --enable-pocketsphinx --enable-librsvg --enable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-librav1e --enable-shared
  libavutil      57. 28.100 / 57. 28.100
  libavcodec     59. 37.100 / 59. 37.100
  libavformat    59. 27.100 / 59. 27.100
  libavdevice    59.  7.100 / 59.  7.100
  libavfilter     8. 44.100 /  8. 44.100
  libswscale      6.  7.100 /  6.  7.100
  libswresample   4.  7.100 /  4.  7.100
  libpostproc    56.  6.100 / 56.  6.100
ALSA lib dlmisc.c:337:(snd_dlobj_cache_get0) Cannot open shared library libasound_module_pcm_pulse.so (/usr/lib/x86_64-linux-gnu/alsa-lib/libasound_module_pcm_pulse.so: cannot open shared object file: No such file or directory)
[alsa @ 0x5628b2871b00] cannot open audio device default (No such device or address)
default: Input/output error

@dbuezas
Copy link
Contributor Author

dbuezas commented May 3, 2023

I tried making a local add on with go2rtc so I could add the missing packages but I couldn't manage to get it to build the image (not fluent in docker). I think that addinig these should suffice:

RUN  apk add --no-cache \
        alsa-plugins-pulse=1.2.7.1-r0 \
        alsa-utils=1.2.8-r0 \
        pulseaudio-utils=16.1-r6 

@AlexxIT AlexxIT changed the title Mix audio from external stream Add ALSA support inside docker May 4, 2023
@AlexxIT
Copy link
Owner

AlexxIT commented May 4, 2023

Was added to main and to hardware docker containers
/~https://github.com/AlexxIT/go2rtc/releases/tag/v1.5.0

@AlexxIT AlexxIT closed this as completed May 4, 2023
@dbuezas
Copy link
Contributor Author

dbuezas commented May 4, 2023

Great! Will test&report tonight then

@dbuezas
Copy link
Contributor Author

dbuezas commented May 6, 2023

It worked! 🎉

webcam:
    - ffmpeg:device?video=/dev/video2&input_format=yuyv422&video_size=640x480#video=h264#hardware
    - exec:ffmpeg -f alsa -ac 1 -i default -c:a libopus -ar:a 16000 -ac:a 1 -af adelay=0ms -rtsp_transport tcp -b:a 32k -listen 1 -reconnect 1 -f rtsp {output}

@dbuezas
Copy link
Contributor Author

dbuezas commented May 6, 2023

It's perfect!
I ended up dividing making a video only stream for frigate, and another one that copies is and adds audio for webrtc. This way there is no extra cpu usage from the microphone (which is surprisingly high) when it is not in use.
Thank you AlexxIT!

@dbuezas
Copy link
Contributor Author

dbuezas commented May 6, 2023

BTW, what is this?
image

Is it an automatic suggestion from the add on for the microphone or is it detecting this from somewhere else that I may have misconfigured?

@dbuezas
Copy link
Contributor Author

dbuezas commented May 6, 2023

If this is coming from the add on as a suggestion for the microphone, I suggest to also add:
exec:ffmpeg -f alsa -ac 1 -i default -c:a libopus -ar:a 16000 -ac:a 1 -af adelay=0ms -rtsp_transport tcp -f rtsp {output}

libopus has much better latency (for me at least). The adelay=0ms makes the audio not choppy for some reason.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants