#!/bin/sh

# Tests forwarding of payloads from one end-point to another, and the
# reverse of that scenario. This also tests "record session to a file"
# functionality and subsequent decoding of the audio using extractaudio.
#
# The setup is as follows. We setup 3 rtpproxy instances:
#
# +-----+      +-----+      +-----+
# |Gen.O|<---->| Fwd |<---->|Gen.A|
# +-----+      +-----+      +-----+
#
# Uni-directional RTP streams are generated by the Gen.O and Gen.A
# instances simultaneously and they are delivered to the Fwd instance.
# Each of the Gen.O and Gen.A instances also records incoming streams into
# disk files, Gen.O in the AdHoc format and Gen.A in the PCAP format. All
# streams are then decoded and checksum is compared to the reference value.
# 
# We also setup several streams (for all supported codecs) and enable
# re-packetization from 10ms for most codecs (20ms for GSM) to 40ms in one
# direction and 60ms in the other.

. $(dirname $0)/functions

TEST_CODECS="0 3 8 18 9"
#TEST_CODECS="9 9 9 9 9"
FWD_SPORT=13342
GENO_SPORT=12000
GENA_SPORT=15000

rotate_CODECSA() {
  first_codec="${1}"
  shift
  CODECSA="${@} ${first_codec}"
}

setup_gen() {
  direction="${1}"
  START_PORT="${2}"
  shift 2
  TRASH_PORT1=38322
  PORT=${START_PORT}
  i=1
  for codec in ${1} ${2}
  do
    CID="forwarding1_${direction}_${i}"
    SED_TRANS="s|%%PORT1%%|${PORT}| ; s|%%PORT2%%|${TRASH_PORT1}| ; s|%%CALLID%%|${CID}|"
    ${SED} "${SED_TRANS}" forwarding1.rec.input
    PORT=$((${PORT} + 4))
    i=$((${i}+1))
  done
  sleep 1
  i=1
  for codec in ${1} ${2}
  do
    CID="forwarding1_${direction}_${i}"
    SED_TRANS="s|%%CODEC%%|${codec}| ; s|%%CALLID%%|${CID}|"
    ${SED} "${SED_TRANS}" forwarding1.gen.input
    i=$((${i}+1))
  done
  sleep 32
  cat session_timeouts.stats.input forwarding1.stats.input
}

setup_fwd() {
  PORTO=$((${1} + 2))
  PORTA=$((${2} + 2))
  shift 2
  i=1
  for codec in ${1}
  do
    CID="forwarding1_${i}"
    SED_TRANS="s|%%PORTO%%|${PORTO}| ; s|%%PORTA%%|${PORTA}| ; s|%%CALLID%%|${CID}|"
    ${SED} "${SED_TRANS}" forwarding1.input
    PORTO=$((${PORTO} + 4))
    PORTA=$((${PORTA} + 4))
    i=$((${i}+1))
  done
  for codec in ${2}
  do
    CID="forwarding1_${i}"
    SED_TRANS="s|%%PORTO%%|${PORTO}| ; s|%%PORTA%%|${PORTA}| ; s|%%CALLID%%|${CID}|"
    ${SED} "${SED_TRANS}" forwarding1.repack.input
    PORTO=$((${PORTO} + 4))
    PORTA=$((${PORTA} + 4))
    i=$((${i}+1))
  done
  sleep 36
  j=$((${i}-1))
  while [ ${j} -gt $((${i}/2)) ]
  do
    CID="forwarding1_${j}"
    SED_TRANS="s|%%CALLID%%|${CID}|"
    ${SED} "${SED_TRANS}" forwarding1.delete.input
    j=$((${j} - 1))
  done  
  cat session_timeouts.stats.input forwarding1.stats.input
}

verify_results()
{
  direction="${1}"
  shift
  i=1
  for codec in ${1} ${2}
  do
    rm -f forwarding1.${codec}.wav
    ${EXTRACTAUDIO} -n -s forwarding1_${direction}_${i}=from_tag_1 forwarding1.${codec}.wav | grep -v 'delta '
    report "extractaudio forwarding1_${direction}_${i} forwarding1.${codec}.wav"
    sha256_verify forwarding1.${codec}.wav forwarding1.checksums
    i=$((${i}+1))
  done
}

CODECSO="${TEST_CODECS}"
CODECSA="${TEST_CODECS}"
PORTO=$((${FWD_SPORT} + 2))

FORWARDING_RFILES=""
for codec in ${TEST_CODECS}
do
  FORWARDING_RFILES="forwarding1.${codec} ${FORWARDING_RFILES}"
done
rm -f ${FORWARDING_RFILES}
${MAKEANN} ${BASEDIR}/ringback.sln ${BASEDIR}/forwarding1
report "makeann forwarding1"

_CODECSO=""
_CODECSA=""
i=0
while [ ${i} -eq 0 -o "${CODECSO}" != "${CODECSA}" ]
do
  i=1
  _CODECSO="${_CODECSO} ${CODECSO}"
  _CODECSA="${_CODECSA} ${CODECSA}"
  rotate_CODECSA ${CODECSA}
done
REPACK_CODECSO="${CODECSO}"
rotate_CODECSA ${CODECSA}
REPACK_CODECSA="${CODECSA}"
CODECSO="${_CODECSO}"
CODECSA="${_CODECSA}"

RECORD_DIR="${BUILDDIR}"

setup_buffers

#sts=`date "+%Y%m%d%H%M%S"`
(setup_fwd ${GENO_SPORT} ${GENA_SPORT} "${CODECSO}" "${REPACK_CODECSO}" | ${RTPPROXY} -i -T5 -b -s stdio: -s cunix:/tmp/forwarding1.sock -f -m ${FWD_SPORT} > forwarding1.rout 2>/dev/null) &
FWD_PID=${!}
(setup_gen "o" ${FWD_SPORT} "${CODECSO}" "${REPACK_CODECSO}" | ${RTPPROXY} -P -r "${RECORD_DIR}" -T5 -b -m ${GENO_SPORT} -s stdio: -f -d info > forwarding1.geno.rout 2>/dev/null) &
GENO_PID=${!}
(setup_gen "a" ${PORTO} "${CODECSA}" "${REPACK_CODECSA}" | ${RTPPROXY} -r "${RECORD_DIR}" -T5 -b -m ${GENA_SPORT} -s stdio: -f -d info > forwarding1.gena.rout 2>/dev/null) &
GENA_PID=${!}
wait ${FWD_PID}
#ets=`date "+%Y%m%d%H%M%S"`
#echo $((${ets} - ${sts}))
report "wait for rtpproxy Fwd shutdown, PID: ${FWD_PID}"
wait ${GENO_PID}
report "wait for rtpproxy Gen.O shutdown, PID: ${GENO_PID}"
wait ${GENA_PID}
report "wait for rtpproxy Gen.A shutdown, PID: ${GENA_PID}"
${DIFF} forwarding1.output forwarding1.rout
DIFF_FWD_RC=${?}
${DIFF} forwarding1.geno.output forwarding1.geno.rout
DIFF_GENO_RC=${?}
${DIFF} forwarding1.gena.output forwarding1.gena.rout
DIFF_GENA_RC=${?}

verify_results "o" "${CODECSA}" "${REPACK_CODECSA}"
verify_results "a" "${CODECSO}" "${REPACK_CODECSO}"

#report_rc ${DIFF_FWD_RC} "checking rtpproxy.fwd stdout"
#report_rc ${DIFF_GENO_RC} "checking rtpproxy.geno stdout"
#report_rc ${DIFF_GENA_RC} "checking rtpproxy.gena stdout"
