On Oct 27 07:56:51, Pawel S. Veselov wrote:
> >*Usually* (I know) it finishes OK, and the *ogg is a valid ogg stream.
> >In this failing case, it *also* is a valid ogg stream, but much
> >shorter than usual.
> >
> >So I suppose the background nc dies before I try to kill it myself
> >(that is, after sleeping for $LENGTH seconds).

> Since only happens infrequently, I'd start 'nc' under trace, and
> preserve the trace file in the case when 'kill' has nothing to kill.
> Trace file should show what 'nc' encountered on the network...

Thanks for the advice, Pawel - I did run nc(1) under ktrace(1), and
found that after (many) sucessfull calls of read(2), the next read(2)
call returns 0, upon which nc(1) exits.

As this is reading a streamed ogg, there are always more bytes
to be read - what are the possible reasons for read(2)ing on a socket
to return 0 (except EOF being read)? Can a network timeout cause this?

        Thanks

                Jan



#!/bin/sh

# A simple recorder of streaming internet radio.
# $1 is the station, $2 is length in seconds, $3 is the output file.
# If $3 is not given, it is invented from the station name and date.

# We suppose it's an ogg/mp3 FILE, reachable at a given PORT of a given HOST,
# which we HTTP GET. The response's HTTP header needs to be trimmed off.
# (FIXME: test for HTTP errors)

# Supported stations: add yours here
praha="http://amp1.cesnet.cz:8000/cro2.ogg";
vltava="http://amp1.cesnet.cz:8000/cro3.ogg";
#testfm="host.org:8000/some/path/script.cgi?stream:yes;file=name.ogg"
#testfm="http://stream.rozhlas.cz:8000/cro2_low.mp3";


usage() {
        echo "usage   : $0 station length [outfile]" 2>&1
        echo "stations: praha vltava" 2>&1
}


eval URL=\$$1
test -n "$URL" || { usage ; exit 1 ; }
test $# -ge 2  || { usage ; exit 1 ; }

NC=`which nc 2>/dev/null`
test -x $NC || exit 1

STATION=${1}
SECONDS=${2}
OUTFILE=${3}
AUXFIFO="/tmp/radio.$$"
NCTRACE="/tmp/radio.$$.nc"
NCERROR=0

eval `echo $URL | sed \
-e "s,^http://,HOST='," \
-e "s,:,' ; PORT='," \
-e "s,/,' ; FILE='," \
-e "s,$,',"`

test -n "$OUTFILE" || OUTFILE="$STATION-`date +%Y%m%d%H%M%S`.${FILE##*.}"
test -e "$OUTFILE" && { echo "$OUTFILE already exists" >&2 ; exit 1 ; }

mkfifo  $AUXFIFO || { echo "Cannot create output stream $AUXFIFO" >&2; exit 1; }
sed -n -e '1,/^
/!p' < $AUXFIFO > $OUTFILE &

{ echo "GET /$FILE HTTP/1.0" ; echo ; } \
| $NC $HOST $PORT > $AUXFIFO &

PID=$!

ktrace -p $PID -f $NCTRACE || {
        echo "Cannot ktrace $PID ($NC)" >&2
        NCERROR=1;
}

sleep $SECONDS 

if ps -p $PID >/dev/null 2>&1 ; then
        kill -9 $PID >/dev/null 2>&1
else
        echo "$NC ($PID) is already dead (see $NCTRACE)" >&2
        NCERROR=1
fi

test $NCERROR -eq 0 && rm -f $NCTRACE
rm -f $AUXFIFO

exit $NCERROR

Reply via email to