#!/bin/sh

# checksum tests to verify that captured audio is correct.

BASEDIR="`dirname "${0}"`/.."
. "${BASEDIR}/functions"

TKINDS="mono stereo"
OFMTS_mono="wav raw w64"
OFMTS_stereo="wav w64"

ofiles=""
extractaudio_RFILES="call1_alaw call1_ulaw call1_g722 call1_g729 call1_gsm \
 call1_g722_srtp corrupted_g722"

for tkind in ${TKINDS}
do
  if [ ${tkind} = "mono" ]
  then
    EXTRACTAUDIO_ARGS=""
    OFMTS="${OFMTS_mono}"
    CHFMT="m"
  else
    EXTRACTAUDIO_ARGS="-s"
    OFMTS="${OFMTS_stereo}"
    CHFMT="s"
  fi
  for rfile in ${extractaudio_RFILES}
  do
    ofile="extractaudio/${rfile}.output"
    for ofmt in ${OFMTS}
    do
      DFMTS=`cat extractaudio/extractaudio.${CHFMT}.${ofmt}.dfmts`
      for dfmt in ${DFMTS}
      do
        wfile="${rfile}.${tkind}.${dfmt}.${ofmt}"
        sfile="${rfile}.${tkind}.${dfmt}.${ofmt}.tout"
        logfile="${rfile}.${tkind}.${dfmt}.${ofmt}.rlog"
        if [ -e "${wfile}" ]
        then
          rm "${wfile}"
        fi
        afile="extractaudio/${rfile}.args"
        if [ -e "${afile}" ]
        then
          EXTRACTAUDIO_EARGS=`cat "${afile}"`
        else
          EXTRACTAUDIO_EARGS=""
        fi
        ${EXTRACTAUDIO} -F ${ofmt} -D ${dfmt} ${EXTRACTAUDIO_ARGS} \
         ${EXTRACTAUDIO_EARGS} extractaudio/${rfile} ${wfile} 2>${logfile} >${sfile}
        report "${EXTRACTAUDIO} extractaudio/${rfile}"
        test -s ${wfile}
        report "${rfile} -> ${wfile}"
        sha256_verify ${wfile} extractaudio/extractaudio.checksums
        ${DIFF} "${ofile}" "${sfile}"
        report "checking RTP stats for the ${rfile}"
      done
    done
  done
done
