Skip to content

Instantly share code, notes, and snippets.

@T-X
Last active June 8, 2024 00:20
Show Gist options
  • Save T-X/fdd8b713626a5f37d9447e0a439be6dc to your computer and use it in GitHub Desktop.
Save T-X/fdd8b713626a5f37d9447e0a439be6dc to your computer and use it in GitHub Desktop.
#!/bin/sh
[ -z "$FRAMESIZES" ] && FRAMESIZES="10 20 60"
[ -z "$EXPECTED_LOSSES" ] && EXPECTED_LOSSES="0 15 30 50"
[ -z "$BITRATE" ] && BITRATE="24 32 64 96 128 196"
[ -z "$LOSSES" ] && LOSSES="0 10 20 30 40 50 70"
flac_to_opus() {
local fs="$1"
local loss="$2"
local bitrate="$3"
local expect_loss
[ -f "/tmp/test-inputs/exploss$loss-fs$fs-rate$bitrate.opus" ] && return
if [ "$loss" -eq 0 ]; then
expect_loss=""
else
expect_loss="--expect-loss $loss"
fi
opusenc --set-ctl-int 4012=1 --framesize "$fs" --bitrate "$bitrate" $expect_loss /tmp/test.flac /tmp/test-inputs/exploss$loss-fs$fs-rate$bitrate.opus.tmp || {
echo "Error: opusenc returned: $?" >&2
exit 1
}
mv /tmp/test-inputs/exploss$loss-fs$fs-rate$bitrate.opus.tmp /tmp/test-inputs/exploss$loss-fs$fs-rate$bitrate.opus
}
rtpopus_receive() {
local fs="$1"
local exploss="$2"
local bitrate="$3"
local loss="$4"
local netsim
if [ "$loss" -eq 0 ]; then
netsim=""
else
netsim="netsim drop-probability=0.$loss !"
fi
gst-launch-1.0 \
udpsrc address=::1 port=5000 caps="application/x-rtp, media=audio, clock-rate=48000, encoding-name=OPUS, payload=96" ! \
$netsim \
rtpjitterbuffer latency=1500 ! \
rtpopusdepay ! \
opusparse ! \
oggmux ! \
filesink location=/tmp/test-outputs/exploss$exploss-fs$fs-loss$loss-rate$bitrate.opus.tmp &
RTPOPUS_RECEIVE_PID="$!"
sleep 5
}
rtpopus_send() {
local fs="$1"
local loss="$2"
local bitrate="$3"
gst-launch-1.0 -v \
filesrc location=/tmp/test-inputs/exploss$loss-fs$fs-rate$bitrate.opus ! \
oggdemux ! \
audio/x-opus,channels=2 ! \
rtpopuspay ! \
udpsink host=::1 port=5000
sleep 10
}
mkdir /tmp/test-inputs 2> /dev/null
mkdir /tmp/test-outputs 2> /dev/null
for fs in $FRAMESIZES; do
for exploss in $EXPECTED_LOSSES; do
for br in $BITRATE; do
flac_to_opus "$fs" "$exploss" "$br"
for loss in $LOSSES; do
[ -f "/tmp/test-outputs/exploss$exploss-fs$fs-loss$loss-rate$br.opus" ] && continue
rtpopus_receive "$fs" "$exploss" "$br" "$loss"
rtpopus_send "$fs" "$exploss" "$br" "$loss"
mv /tmp/test-outputs/exploss$exploss-fs$fs-loss$loss-rate$br.opus.tmp \
/tmp/test-outputs/exploss$exploss-fs$fs-loss$loss-rate$br.opus
kill $RTPOPUS_RECEIVE_PID
done
done
done
done
#!/bin/sh
# SPDX-License-Identifier: CC0-1.0
[ -z "$FRAMESIZES" ] && FRAMESIZES="10 20 60"
[ -z "$EXPECTED_LOSSES" ] && EXPECTED_LOSSES="0 15 30 50"
[ -z "$BITRATE" ] && BITRATE="24 32 64 96 128 196"
[ -z "$LOSSES" ] && LOSSES="0 10 20 30 40 50 70"
[ -z "$INPUT" ] && INPUT="/tmp/test.ogg"
[ -z "$OUTPUT" ] && OUTPUT="/tmp/tests"
sigint_handler() {
exit 1
}
exit_handler() {
echo "Exit handler, CURLOCK: $CURLOCK" > /tmp/bla.txt
[ -n "$CURLOCK" ] && [ -f "$CURLOCK" ] && my_lockfile_unlock
[ -n "$PORTLOCK" ] && [ -f "$PORTLOCK" ] && myport_lockfile_unlock
}
decode_to_wav() {
if ogginfo "$INPUT" | grep -q "Vorbis stream"; then
oggdec -o - "$INPUT"
elif flac --test "$INPUT"; then
flac --decode -o - "$INPUT"
else
exit 2
fi
}
my_lockfile() {
if ! lockfile $@; then
return 1
fi
shift $(($# - 1))
# small race condition on unexpected exit here
CURLOCK="$1"
echo "CURLOCK: $CURLOCK" >&2
return 0
}
my_lockfile_unlock() {
[ -z "$CURLOCK" ] && {
echo "Error: not holding a lock" >&2
exit 4
}
[ ! -f "$CURLOCK" ] && {
echo "Error: lockfile \"$CURLOCK\" does not exist" >&2
exit 4
}
rm -f "$CURLOCK"
CURLOCK=""
}
myport_lockfile() {
if ! lockfile $@; then
return 1
fi
shift $(($# - 1))
# small race condition on unexpected exit here
PORTLOCK="$1"
echo "PORTLOCK: $PORTLOCK" >&2
return 0
}
myport_lockfile_unlock() {
[ -z "$PORTLOCK" ] && {
echo "Error: not holding a lock" >&2
exit 4
}
[ ! -f "$PORTLOCK" ] && {
echo "Error: lockfile \"$PORTLOCK\" does not exist" >&2
exit 4
}
rm -f "$PORTLOCK"
PORTLOCK=""
}
wav_to_opus() {
local fs="$1"
local loss="$2"
local bitrate="$3"
local expect_loss
local decoder
my_lockfile "$OUTPUT/inputs/exploss$loss-fs$fs-rate$bitrate.lock"
[ -f "$OUTPUT/inputs/exploss$loss-fs$fs-rate$bitrate.opus" ] && {
my_lockfile_unlock
return
}
if [ "$loss" -eq 0 ]; then
expect_loss=""
else
expect_loss="--expect-loss $loss"
fi
#decode_to_wav 2> /dev/null | \
decode_to_wav 2> /dev/null | \
opusenc \
--quiet \
--set-ctl-int 4012=1 \
--framesize "$fs" \
--bitrate "$bitrate" \
$expect_loss - $OUTPUT/inputs/exploss$loss-fs$fs-rate$bitrate.opus.tmp || {
echo "Error: opusenc returned: $?" >&2
my_lockfile_unlock
exit 1
}
mv --no-clobber $OUTPUT/inputs/exploss$loss-fs$fs-rate$bitrate.opus.tmp \
$OUTPUT/inputs/exploss$loss-fs$fs-rate$bitrate.opus || {
echo "Error: mv failed: $?" >&2
my_lockfile_unlock
exit 3
}
my_lockfile_unlock
}
rtpopus_receive() {
local port="$1"
local fs="$2"
local exploss="$3"
local bitrate="$4"
local loss="$5"
local netsim
if [ "$loss" -eq 0 ]; then
netsim=""
else
netsim="netsim drop-probability=0.$loss !"
fi
gst-launch-1.0 \
udpsrc address=::1 port=$port caps="application/x-rtp, media=audio, clock-rate=48000, encoding-name=OPUS, payload=96" ! \
$netsim \
rtpjitterbuffer latency=1500 ! \
rtpopusdepay ! \
opusparse ! \
oggmux ! \
filesink location=$OUTPUT/outputs/exploss$exploss-fs$fs-rate$bitrate-loss$loss.opus.tmp &
RTPOPUS_RECEIVE_PID="$!"
sleep 5
}
rtpopus_send() {
local port="$1"
local fs="$2"
local exploss="$3"
local bitrate="$4"
local loss="$5"
local retval
gst-launch-1.0 -v \
filesrc location=$OUTPUT/inputs/exploss$exploss-fs$fs-rate$bitrate.opus ! \
oggdemux ! \
audio/x-opus,channels=2 ! \
rtpopuspay ! \
udpsink host=::1 port=$port
retval="$?"
sleep 15
return $retval
}
create_lossy_opus() {
local fs="$1"
local exploss="$2"
local bitrate="$3"
local loss="$4"
local port
my_lockfile -r0 "$OUTPUT/outputs/exploss$exploss-fs$fs-rate$bitrate-loss$loss.lock" || return
[ -f "$OUTPUT/outputs/exploss$exploss-fs$fs-rate$bitrate-loss$loss.opus" ] && {
my_lockfile_unlock
return
}
# find an unused port (by our script - we don't check actual usage here)
for port in `seq 5000 65535`; do
myport_lockfile -r0 "$OUTPUT/outputs/port$port.lock" && break
done
rtpopus_receive "$port" "$fs" "$exploss" "$bitrate" "$loss"
rtpopus_send "$port" "$fs" "$exploss" "$bitrate" "$loss" && {
mv $OUTPUT/outputs/exploss$exploss-fs$fs-rate$bitrate-loss$loss.opus.tmp \
$OUTPUT/outputs/exploss$exploss-fs$fs-rate$bitrate-loss$loss.opus
}
kill $RTPOPUS_RECEIVE_PID
rm "$OUTPUT/inputs/exploss$loss-fs$fs-rate$bitrate-loss$loss.pipe"
myport_lockfile_unlock
my_lockfile_unlock
}
trap sigint_handler INT
trap exit_handler EXIT
mkdir -p $OUTPUT/inputs 2> /dev/null
mkdir -p $OUTPUT/outputs 2> /dev/null
for fs in $FRAMESIZES; do
for exploss in $EXPECTED_LOSSES; do
for br in $BITRATE; do
wav_to_opus "$fs" "$exploss" "$br"
for loss in $LOSSES; do
create_lossy_opus "$fs" "$exploss" "$br" "$loss"
done
done
done
done
#!/bin/sh
[ -n "$1" ] && FRAMESIZES="$1" || FRAMESIZES="10 20 60"
[ -n "$2" ] && EXPECTED_LOSSES="$2" || EXPECTED_LOSSES="0 15 30 50"
[ -n "$3" ] && LOSSES="$3" || LOSSES="0 10 20 30 40 50 70"
flac_to_opus() {
local fs="$1"
local loss="$2"
local expect_loss
[ -f "/tmp/test-inputs/exploss$loss-fs$fs.opus" ] && return
if [ "$loss" -eq 0 ]; then
expect_loss=""
else
expect_loss="--expect-loss $loss"
fi
opusenc --framesize "$fs" $expect_loss /tmp/test.flac /tmp/test-inputs/exploss$loss-fs$fs.opus.tmp
mv /tmp/test-inputs/exploss$loss-fs$fs.opus.tmp /tmp/test-inputs/exploss$loss-fs$fs.opus
}
rtpopus_receive() {
local fs="$1"
local exploss="$2"
local loss="$3"
local netsim
if [ "$loss" -eq 0 ]; then
netsim=""
else
netsim="netsim drop-probability=0.$loss !"
fi
gst-launch-1.0 \
udpsrc address=::1 port=5000 caps="application/x-rtp, media=audio, clock-rate=48000, encoding-name=OPUS, payload=96" ! \
$netsim \
rtpjitterbuffer latency=1500 ! \
rtpopusdepay ! \
opusparse ! \
oggmux ! \
filesink location=/tmp/test-outputs/exploss$exploss-fs$fs-loss$loss.opus.tmp &
RTPOPUS_RECEIVE_PID="$!"
sleep 5
}
rtpopus_send() {
local fs="$1"
local loss="$2"
gst-launch-1.0 -v \
filesrc location=/tmp/test-inputs/exploss$loss-fs$fs.opus ! \
oggdemux ! \
audio/x-opus,channels=2 ! \
rtpopuspay ! \
udpsink host=::1 port=5000
sleep 10
}
mkdir /tmp/test-inputs 2> /dev/null
mkdir /tmp/test-outputs 2> /dev/null
for fs in $FRAMESIZES; do
for exploss in $EXPECTED_LOSSES; do
flac_to_opus "$fs" "$exploss"
for loss in $LOSSES; do
[ -f "/tmp/test-outputs/exploss$exploss-fs$fs-loss$loss.opus" ] && continue
rtpopus_receive "$fs" "$exploss" "$loss"
rtpopus_send "$fs" "$exploss" "$loss"
mv /tmp/test-outputs/exploss$exploss-fs$fs-loss$loss.opus.tmp \
/tmp/test-outputs/exploss$exploss-fs$fs-loss$loss.opus
kill $RTPOPUS_RECEIVE_PID
done
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment