#!/bin/bash # connect_and_stream.sh SERVER_IP="localhost" SSH_PORT="2201" # or whatever SSH port you're using USERNAME="john_doe" # or the appropriate username REMOTE_AUDIO_PORT="5000" LOCAL_AUDIO_PORT="5000" PULSE_PORT="4713" # Function to find an available port find_available_port() { local port=$1 while ! nc -z localhost $port &>/dev/null; do ((port++)) done echo $port } # Function to clean up background processes on script exit cleanup() { echo "Cleaning up..." if [ ! -z "$SSH_PID" ]; then kill $SSH_PID fi exit } # Set up the cleanup function to run on script exit trap cleanup EXIT # Find available ports LOCAL_AUDIO_PORT=$(find_available_port $LOCAL_AUDIO_PORT) PULSE_PORT=$(find_available_port $PULSE_PORT) echo "Using local audio port: $LOCAL_AUDIO_PORT" echo "Using PulseAudio port: $PULSE_PORT" # Start SSH tunnel in the background ssh -f -N -L ${LOCAL_AUDIO_PORT}:localhost:${REMOTE_AUDIO_PORT} -L ${PULSE_PORT}:localhost:4713 -p ${SSH_PORT} ${USERNAME}@${SERVER_IP} SSH_PID=$! # Wait a moment for the tunnel to establish sleep 2 # Set PULSE_SERVER to use the forwarded port export PULSE_SERVER=tcp:localhost:${PULSE_PORT} # Start GStreamer client #gst-launch-1.0 udpsrc uri=udp://localhost:${LOCAL_AUDIO_PORT} caps="application/x-rtp,media=audio,clock-rate=48000,encoding-name=OPUS" ! rtpopusdepay ! opusdec ! audioconvert ! autoaudiosink gst-launch-1.0 udpsrc port=${LOCAL_AUDIO_PORT} caps="application/x-rtp,media=audio,clock-rate=48000,encoding-name=OPUS" ! rtpopusdepay ! opusdec ! audioconvert ! autoaudiosink # The script will keep running until you stop it (Ctrl+C) # When stopped, the cleanup function will kill the SSH tunnel