droidcam icon indicating copy to clipboard operation
droidcam copied to clipboard

Automate loading of pulseaudio module

Open ngoonee opened this issue 4 years ago • 10 comments

Describe the feature you'd like to see added to the app Pulseaudio is pretty much the default on linux now, on almost all distros. Droidcam already supports audio (as long as snd-aloop is loaded), but this will not work OOTB in most distros because pulseaudio doesn't automatically pick up the loopback device.

Droidcam is already aware of the correct IDs (when I'm running droidcam-cli I get 6 lines of output, the last one looking like Audio:hw:2,1,0 - would it be possible to automatically load module-alsa-source using those ids?

It would be a bonus if exiting droidcam/droidcam-cli then unloaded the module, but that's less crucial I think.

ngoonee avatar Oct 15 '20 09:10 ngoonee

This can be scripted, eg.

#!/bin/bash -e
LOG="/dev/shm/droidcam.out"

stdbuf -i0 -o0 -e0 droidcam $@ >$LOG 2>&1 &
pid=$!
sleep 5
ps -p $pid > /dev/null
if [ $? -eq 0 ]; then
        audio_device=$(grep -oE "hw:[0-9],[0-9],[0-9]" $LOG)
        pacmd load-module module-alsa-source device=$audio_device
        wait
        pacmd unload-module module-alsa-source
        rm $LOG
fi

Modify to your needs. Create a file, eg launch.sh, and make it executable. You can put it on your Desktop or elsewhere. Test/debug via bash -x launch.sh to see all the commands.

aramg avatar Oct 16 '20 22:10 aramg

I can't get this script to work with the graphical version of droidcam, since it doesn't output its audio device on stdout. The droidcam-cli version works, but I don't want to have to keep launching the script with the exact parameters. (that's because I added a .desktop file for Droidcam that points to the script, and I am using rofi to show installed programs) Is there a way to make the graphical version output its audio device?

goll72 avatar Oct 21 '20 00:10 goll72

Workaround: you can have your desktop file just launch the script in a terminal? I definitely think this should be an option within the app as stated above, but that's obviously up to the maintainer. This script works =)

ngoonee avatar Oct 21 '20 01:10 ngoonee

Thanks for the ideia! I adapted the script to read user input, and then I used a regex to make sure the correct parameters are passed to droidcam-cli: ^(-a[ ]+|-v[ ]+){1,2}(adb|ios|(([0-9]{1,3}\.){3}[0-9]{1,3}))[ ]+([0-9]{4})|(-l[ ]+([0-9]{4}))$ (It doesn't work using bash's regex for some reason, I had to use grep -w -E). It's a hacky workaround, but works for the moment.

goll72 avatar Oct 21 '20 10:10 goll72

The script is working (I can launch Droidcam with the parameters I want) but I can't get the audio to work. I tried manually opening Droidcam and loading the alsa-source module, various times (before and after the connection was made) but it doesn't work. When I try to unload it, it says that the module is not loaded. That's why I think automatically loading the module is a much better solution, since it could account for every specific case where the loading would normally fail, because Droidcam has a much better control over its way of handling audio than a bash script using pacmd.

goll72 avatar Oct 21 '20 12:10 goll72

I can't get this script to work with the graphical version of droidcam, since it doesn't output its audio device on stdout.

Right, I had a debug build which is why it worked. You can just add a print line to droidcam.c to print the audio device (eg. at line 494):

@@ -491,6 +491,7 @@ int main(int argc, char *argv[])
                snprintf(info, sizeof(info), "Client v" APP_VER_STR ", Video: %s, Audio: %s",
                        v4l2_device, snd_device);
                gtk_label_set_text(GTK_LABEL(infoText), info);
+               printf("Audio: %s\n", snd_device);

Attached is a build with that line added droidcam.zip

aramg avatar Oct 21 '20 19:10 aramg

That's why I think automatically loading the module is a much better solution, since it could account for every specific case where the loading would normally fail, because Droidcam has a much better control over its way of handling audio than a bash script using pacmd.

Its just that the behaviour of PulseAudio seemed to vary between distros. Some people reported it "just worked", while others had mixed results. If there is a way to load the module in a way that works everywhere, then sure. Maybe there are other projects that already do it ?

aramg avatar Oct 21 '20 19:10 aramg

Sorry for my previous comment, I am so dumb! It was just a syntax error, audio works fine now. :man_facepalming:

Modified version of the script:

#!/usr/bin/bash

set -e

wht=$(tput sgr0)
red=$(tput setaf 1)

log="/dev/shm/droidcam.out"

read -erp "Parameters for Droidcam: " param

until grep -w -E "^(-a[ ]+|-v[ ]+){1,2}(adb|ios|(([0-9]{1,3}\.){3}[0-9]{1,3}))[ ]+([0-9]{4})|(-l[ ]+([0-9]{4}))$" <<< $param; do
    printf '\n%sWrong parameters. Try again.%s\n' "$red" "$wht"
    read -erp "> " param
done

stdbuf -i0 -o0 -e0 droidcam-cli $param >"$log" 2>&1 &

pid=$!

handleExit() {
    pkill droidcam-cli
    pacmd unload-module module-alsa-source
    rm "$log"
    exit
}

trap handleExit SIGINT

sleep 4

if ps -p $pid >/dev/null; then
    audioDev=$(grep -oE "hw:[0-9],[0-9],[0-9]" "$log")
    pacmd load-module module-alsa-source device="$audioDev"
    wait
fi

goll72 avatar Oct 21 '20 22:10 goll72

Everyone is saved as best they can :)

/usr/bin/droidcam.sh

#!/bin/bash

### DroidCam start with droidcam_audio script

[[ $(pidof droidcam) ]] && killall droidcam; droidcam &

while [[ -z $(pidof droidcam) ]]; do sleep 1; done;

# Mic Up (droidcam_audio)
[[ $(pactl list | grep droidcam_audio) ]] || pacmd load-module module-alsa-source device=hw:Loopback,1,0 source_properties=device.description=droidcam_audio

#Set droidcam_audio as Default Mic
pacmd set-default-source 'alsa_input.hw_Loopback_1_0'

exit 0;

AKotov-dev avatar Jul 17 '21 15:07 AKotov-dev

isn't there anyway to do it by editing /etc/pulse/default.pa ?? I tried so hard but I failed , but that's what the devs said on their website ! I followed this on the arch wiki by adding load-module module-alsa-source source_properties=device.description=DroidCam device=hw:Loopback,1,0 to the file but it didn't work

AmmarHaddadi avatar Oct 27 '22 13:10 AmmarHaddadi