]> git.vomp.tv Git - vompserver.git/blob - test1.sh
15 years that line of code has been waiting to crash
[vompserver.git] / test1.sh
1 #! /bin/sh
2 #testcommand for media player
3 #parameters: command [filename] [...]
4 #  test
5 #  play filename xsize ysize
6 #return -1 if any error (no output!)
7 #this script is prepared for tools writing to stdout or requiring a file name
8 #  for tools writing to stdout simple create a new convertXXX function
9 #    that sends the tool to background and sets MPID to the pid
10 #  for tools requiring a filename call createFifo first, then
11 #    also create a function that sends the tool in background
12 #  after starting the conversion tool call waitFkt, this will kill the conversion
13 #  tool and the fifo reader if we are killed or if one of the tools finishes
14
15 #set -x
16
17 FIFO=/tmp/vdrfifo$$
18
19 log() {
20   echo >/dev/null
21   #echo "$$:`date `:$*" >> /tmp/test1.log
22 }
23
24 trapfkt() {
25   log "trap received"
26   if [ "$MPID" != "" ] ; then
27     kill $MPID > /dev/null 2>&1
28     kill -9 $MPID > /dev/null 2>&1
29   fi
30   if [ "$FPID" != "" ] ; then
31     kill $FPID > /dev/null 2>&1
32     kill -9 $FPID > /dev/null 2>&1
33   fi
34   if [ -p $FIFO ] ; then
35     rm -f $FIFO
36   fi
37 }
38
39 #create a fifo and start a cat
40 createFifo() {
41   mkfifo $FIFO >/dev/null 2>&1 || exit 1
42   cat $FIFO 2>&1 &
43   FPID=$!
44 }
45   
46 #convert AVI to mpeg2, suing mencoder see
47 #http://www.wiki.csoft.at/index.php/MEncoder_Scripts
48 convertAVI() {
49    #mencoder  -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd -vf harddup -srate 48000 -af lavcresample=48000 -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:keyint=15:acodec=mp2:abitrate=192 $RTOP -o $FIFO $1 > /dev/null 2>&1 &
50    mencoder  -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd -vf harddup -srate 48000 -af lavcresample=48000 -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=5000:vbitrate=3000:keyint=15:acodec=mp2:abitrate=192 $RTOP -o $FIFO $1 > /dev/null 2>&1 &
51   MPID=$!
52 }
53
54 #convert WAV to mpeg using lame
55 convertWAV() {
56 lame -v -b 192 -S $1 - - 2>&1 &
57 MPID=$!
58 }
59
60 #convert pictures using GraphicsMagic
61 convertPicture(){
62   if [ "$2" != "" -a "$3" != "" ] ; then
63     fopt="-geometry ${2}x$3"
64   fi
65   gm convert $fopt "$1" jpeg:- 2>&1 &
66   MPID=$!
67 }
68
69 #cat data
70 catData() {
71   cat "$1" 2>&1 &
72   MPID=$!
73 }
74
75 #convert a playlist
76 #handle http:// as audio-url
77 convertList() {
78   cat "$1" 2>&1 | sed 's?^ *http:\(.*\)?/http:\1.http-audio?'
79 }
80
81 #get a HTTP url via wget
82 getHTTP() {
83   wget -q -O - "$1" 2>&1 &
84   MPID=$!
85 }
86
87
88
89
90
91 #wait until the cat or the converter have been stopped
92 waitFkt() {
93   while [ 1 = 1 ]
94   do
95   dokill=0
96   if [ "$MPID" != "" ] ; then
97     kill -0 $MPID >/dev/null 2>&1 || dokill=1
98   fi
99   if [ "$FPID" != "" ] ; then
100     kill -0 $FPID >/dev/null 2>&1 || dokill=1
101   fi
102   if [ $dokill = 1 ] ; then
103     log "killing MPID=$MPID FPID=$FPID"
104     kill -9 $MPID > /dev/null 2>&1
105     MPID=""
106     kill -9 $FPID > /dev/null 2>&1
107     FPID=""
108   fi
109   if [ "$FPID" = "" -a "$MPID" = "" ] ; then
110     log leaving waitFkt
111     break
112   fi
113   usleep 100000
114   done
115   exit 0
116 }
117     
118
119 log started with param "$*"
120 if [ "$1" = "" ] ; then
121   echo inavlid call
122   exit 1
123 fi
124 trap trapfkt 0 1 2 3 4 5 6 7 8 10 11 12 13 14 15
125 case $1 in
126   check)
127   exit 0
128   ;;
129   play)
130   exts=`echo "$2" | sed 's/.*\.//'` 
131   if [ "$exts" = "" ] ; then
132     exit 1
133   fi
134   fname=`echo "$2" | sed 's/\.[^.]*$//'`
135   case $exts in
136     xmpg)
137     #mpeg2 test
138     nname="$fname.mpg"
139     cat "$nname" &
140     MPID=$!
141     waitFkt
142     ;;
143     xjpg)
144     #jpeg test
145     nname="$fname.jpg"
146     [ "$nname" = "" ] && exit 1
147     [ ! -f "$nname" ] && exit 1
148     convertPicture "$nname" $3 $4
149     waitFkt
150     ;;
151     xmp3)
152     #mp3 test
153     nname="$fname.mp3"
154     cat "$nname" &
155     MPID=$!
156     waitFkt
157     ;;
158     #real converting functions
159     bmp|BMP|tiff|TIFF|png|PNG)
160     convertPicture "$2" $3 $4
161     waitFkt
162     ;;
163     AVI|avi)
164     createFifo
165     convertAVI "$2" $3 $4
166     waitFkt
167     ;;
168     WAV|wav)
169     convertWAV "$2"
170     waitFkt
171     ;;
172     xdir)
173     catData "$2"
174     waitFkt
175     ;;
176     m3u|dir)
177     convertList "$2"
178     waitFkt
179     ;;
180     http-audio)
181     fn=`echo "$2" | sed 's/\.[^.]*$//' | sed 's?^ */??'`
182     getHTTP "$fn"
183     waitFkt
184     ;;
185     *)
186     exit 1
187     ;;
188   esac
189   ;;
190   *)
191   exit 1
192   ;;
193 esac