Skip to content

Instantly share code, notes, and snippets.

@Iljo
Last active May 24, 2020 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Iljo/457be1918fc4ce4c7533935aabad7c25 to your computer and use it in GitHub Desktop.
Save Iljo/457be1918fc4ce4c7533935aabad7c25 to your computer and use it in GitHub Desktop.
Use phone as microphone
#!/bin/bash
# Use phone as microphone
#
# This is a striped down version of the ipwebcam-gst tool, initially
# made for learning purposes, with two little addons:
# - Remove forwarded ports after streaming is finished
# - Increase microphone volume
# - No waiting for enter to end streaming, just press ctrl-C (habit)
#
# Original tool can be found on https://github.com/bluezio/ipwebcam-gst
GST_AUDIO_MIMETYPE="audio/x-raw"
GST_AUDIO_FORMAT="format=S16LE"
GST_AUDIO_LAYOUT=",layout=interleaved"
GST_AUDIO_RATE="rate=44100"
GST_AUDIO_CHANNELS="channels=1"
##########################################################
# ADB forward ports #
##########################################################
adb forward tcp:4747 tcp:4747
##########################################################
# Pulse Audio setup #
##########################################################
DEFAULT_SINK=$(pacmd dump | grep set-default-sink | cut -f2 -d " ")
DEFAULT_SOURCE=$(pacmd dump | grep set-default-source | cut -f2 -d " ")
module_id_by_sinkname() {
pacmd list-sinks | grep -e 'name:' -e 'module:' | grep -A1 "name: <$1>" | grep module: | cut -f2 -d: | tr -d ' '
}
SINK_NAME="ipwebcam"
SINK_ID=$(module_id_by_sinkname $SINK_NAME)
ECANCEL_ID=$(module_id_by_sinkname "${SINK_NAME}_echo_cancel")
PA_AUDIO_CAPS="$GST_AUDIO_FORMAT $GST_AUDIO_RATE $GST_AUDIO_CHANNELS"
# Registering audio device if not yet registered
if [ -z $SINK_ID ] ; then
SINK_ID=$(pactl load-module module-null-sink \
sink_name="$SINK_NAME" \
$PA_AUDIO_CAPS \
sink_properties="device.description='IP\ Webcam'")
fi
if [ -z $ECANCEL_ID ] ; then
ECANCEL_ID=$(pactl load-module module-echo-cancel \
sink_name="${SINK_NAME}_echo_cancel" \
source_master="$SINK_NAME.monitor" \
sink_master="$DEFAULT_SINK" \
$PA_AUDIO_CAPS \
aec_method="webrtc" save_aec=true use_volume_sharing=true)
fi
pactl set-default-source $SINK_NAME.monitor
# Either sleep or use queue in gstreamer audio caps
sleep 2
##########################################################
# Start streaming #
##########################################################
GST_AUDIO_CAPS="$GST_AUDIO_MIMETYPE,$GST_AUDIO_FORMAT$GST_AUDIO_LAYOUT,$GST_AUDIO_RATE,$GST_AUDIO_CHANNELS"
pipeline_audio() {
echo souphttpsrc location=http://localhost:4747/audio.wav do-timestamp=true is-live=true \
! $GST_AUDIO_CAPS \
! volume volume=5 \
! pulsesink device="$SINK_NAME"
}
gst-launch-1.0 -e -vt --gst-plugin-spew --gst-debug="souphttpsrc:0,pulse:0" $( pipeline_audio )
##########################################################
# Clean after streaming #
##########################################################
pactl set-default-source ${DEFAULT_SOURCE}
pactl unload-module ${ECANCEL_ID}
pactl unload-module ${SINK_ID}
adb forward --remove tcp:4747
echo "Bye"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment