aboutsummaryrefslogtreecommitdiffstats
path: root/encode-jack-dls-jamin.sh
blob: 459d2cba99272853f6349370b42e5f04bdb070ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/bash
#
# Encode programme using mplayer, connect through JACK
# to jamin, which connects to dabplus-enc or toolame
#
# Read webstream from URL using mplayer
# Launch dabplus-enc or toolame encoder
# Launch jamin
# connect all through JACK
# monitor processes, and restart if necessary
# Optionally send an email when restart happens
#
# Extract ICY Text from stream and use it for DLS

printerr() {
    echo -e "\033[01;31m$1\033[0m"
}

printmsg() {
    echo -e "\033[01;32m$1\033[0m"
}

set -u

# check number of arguments
if [[ "$#" -lt 3 ]] ; then
    echo "Usage $0 url jack-id destination [volume] [encoder]"
    echo "The volume setting is optional"
    exit 1
fi

if [[ "$#" -gt 2 ]] ; then
    URL=$1
    ID=$2
    DST=$3
fi

if [[ "$#" == 4 ]] ; then
    VOL=$4
    ENC="dabplus-enc"
elif [[ "$#" == 5 ]] ; then
    VOL=$4
    ENC=$5
else
    VOL="0"
    ENC="dabplus-enc"
fi


BITRATE=96
RATE=48

if [[ "$ENC" == "toolame" && "$RATE" == "32" ]] ; then
    echo "32kHz not supported for toolame"
    exit 1
fi

DLSDIR=site/dls
SLIDEDIR=site/slide
JAMIN=site/jamin


mplayerpid=0
encoderpid=0
motencoderpid=0
jaminpid=0
running=1

mplayer_ok=0
encoder_ok=0
jamin_ok=0

# The trap for Ctrl-C
sigint_trap() {
    printerr "Got Ctrl-C, killing mplayer and encoder"
    running=0

    if [[ "$mplayerpid" != "0" ]] ; then
        kill -TERM $mplayerpid
        sleep 2
        kill -KILL $mplayerpid
    fi

    if [[ "$encoderpid" != "0" ]] ; then
        kill -TERM $encoderpid
        sleep 2
        kill -KILL $encoderpid
    fi

    if [[ "$motencoderpid" != "0" ]] ; then
        kill -TERM $motencoderpid
        sleep 2
        kill -KILL $motencoderpid
    fi
    if [[ "$jaminpid" != "0" ]] ; then
        kill -TERM $jaminpid
        sleep 2
        kill -KILL $jaminpid
    fi

    printmsg "Goodbye"
    exit
}

trap sigint_trap SIGTERM
trap sigint_trap SIGINT

while [[ "$running" == "1" ]]
do
    if [[ "$mplayerpid" == "0" ]] ; then
        if [[ "$VOL" == "0" ]] ; then
            mplayer -quiet -af resample=${RATE}000:0:2 -ao jack:name=$ID "$URL" | \
                ./icy-info.py $DLSDIR/${ID}.dls $DLSDIR/${ID}-default.dls &
            mplayerpid=$!
        else
            mplayer -quiet -af resample=${RATE}000:0:2 -af volume=$VOL -ao jack:name=$ID "$URL" | \
                ./icy-info.py $DLSDIR/${ID}.dls $DLSDIR/${ID}-default.dls &
            mplayerpid=$!
        fi

        printmsg "Started mplayer with pid $mplayerpid"

        # give some time to mplayer to set up and
        # wait until port becomes visible
        timeout=10

        while [[ "$mplayer_ok" == "0" ]]
        do
            printmsg "Waiting for mplayer to connect to jack ($timeout)"
            sleep 1
            mplayer_ok=$(jack_lsp $ID:out_0 | wc -l)

            timeout=$(( $timeout - 1 ))

            if [[ "$timeout" == "0" ]] ; then
                printerr "mplayer doesn't connect to jack !"
                kill $mplayerpid
                break
            fi
        done
    else
        printmsg "No need to start mplayer: $mplayerpid"
    fi

    if [[ "$mplayer_ok" == "1" && "$encoder_ok" == "0" ]] ; then
        if [[ "$ENC" == "dabplus-enc" ]] ; then
            dabplus-enc -j ${ID}enc -l \
                -p 34 -P $DLSDIR/${ID}.pad \
                -b $BITRATE -r ${RATE}000 -f raw -o $DST &
            encoderpid=$!
        elif [[ "$ENC" == "toolame" ]] ; then
            toolame -b $BITRATE -s $RATE \
                -p 34 -P $DLSDIR/${ID}.pad \
                -j ${ID}enc $DST &
            encoderpid=$!
        fi

        #jasmin

        if [[ "$jaminpid" == "0" ]] ; then
            jamin -g -f $JAMIN/$ID.jam $ID:out_0 $ID:out_1 ${ID}enc:input0 ${ID}enc:input1 &
            jaminpid=$!
        fi

        printmsg "Started jamin with pid $jaminpid"

        # give some time to the encoder to set up and
        # wait until port becomes visible
        timeout=10

        encoder_connected=0

        while [[ "$encoder_connected" == "0" ]]
        do
            printmsg "Waiting for encoder to connect to jack ($timeout)"
            sleep 1
            encoder_connected=$(jack_lsp ${ID}enc:input0 | wc -l)

            timeout=$(( $timeout - 1))

            if [[ "$timeout" == "0" ]] ; then
                printerr "encoder doesn't connect to jack !"
                kill $encoderpid
                break
            fi
        done

#        if [[ "$encoder_connected" == "1" ]] ; then
#            jack_connect ${ID}:out_0 ${ID}enc:input0 && \
#            jack_connect ${ID}:out_1 ${ID}enc:input1 #&& \
#            jack_connect ${ID}:out_0 ${ID}enc:input0 && \
#           jack_connect ${ID}:out_0 ${ID}enc:input1
#           connect_ret=$?
#
#            if [[ "$connect_ret" == "0" ]] ; then
#                encoder_ok=1
#            else
#                encoder_ok=0
#            fi

encoder_ok=1
#
#            if [[ "$encoder_ok" == "1" ]] ; then
#                printmsg "Started encoder with pid $encoderpid"
#            else
#                if [[ "$encoderpid" != "0" ]] ; then
#                    kill -TERM $encoderpid
#                fi
#            fi
#        fi
    fi

    if [[ "$encoder_ok" == "1" && "$motencoderpid" == "0" ]] ; then
        # Check if the slides folder exists, and start mot-encoder accordingly
        if [[ -d "$SLIDEDIR/$ID" ]] ; then
            mot-encoder -o $DLSDIR/${ID}.pad -t $DLSDIR/${ID}.dls -p 34 -v \
                -e -d $SLIDEDIR/${ID} &
            motencoderpid=$!
        else
            mot-encoder -o $DLSDIR/${ID}.pad -t $DLSDIR/${ID}.dls -p 34 -v &
            motencoderpid=$!
        fi

        printmsg "Started mot-encoder with pid $encoderpid"
    fi


    sleep 8

    checkloop=1
    while [[ "$checkloop" == "1" ]]
    do
        sleep 2

        kill -s 0 $mplayerpid
        if [[ "$?" != "0" ]] ; then
            # mplayer died
            # we must kill jack-stdout, because we cannot reconnect it
            # to a new mplayer, since we do not know the jack-stdout name.
            # And it has no cmdline option to set one, Rrrrongntudtjuuu!

            if [[ "$encoderpid" != "0" ]] ; then
                kill -TERM $encoderpid
            fi

            if [[ "$motencoderpid" != "0" ]] ; then
                kill -TERM $motencoderpid
            fi

            if [[ "$jaminpid" != "0" ]] ; then
                kill -TERM $jaminpid
            fi

            # mark as dead
            mplayerpid=0
            mplayer_ok=0
            encoderpid=0
            encoder_ok=0
            jaminpid=0
            jamin_ok=0
            motencoderpid=0

            checkloop=0

            printerr "Mplayer died"
        fi

        if [[ "$encoderpid" != "0" ]] ; then
            kill -s 0 $encoderpid
            if [[ "$?" != "0" ]] ; then
                # the encoder died,
                # no need to kill the mplayer, we can reconnect to it

                if [[ "$motencoderpid" != "0" ]] ; then
                    kill -TERM $motencoderpid
                fi

                if [[ "$jaminpid" != "0" ]] ; then
                    kill -TERM $jaminpid
                fi

                encoderpid=0
                encoder_ok=0
                jaminpid=0
                jamin_ok=0
                motencoderpid=0

                checkloop=0

                printerr "Encoder died"
            fi
        fi

        if [[ "$jaminpid" != "0" ]] ; then
            kill -s 0 $jaminpid
            if [[ "$?" != "0" ]] ; then
                # the jamin died,

                if [[ "$motencoderpid" != "0" ]] ; then
                    kill -TERM $motencoderpid
                fi

                if [[ "$encoderpid" != "0" ]] ; then
                    kill -TERM $encoderpid
                fi

                encoderpid=0
                encoder_ok=0
                jaminpid=0
                motencoderpid=0

                checkloop=0

                printerr "Jamin died"
            fi

        fi

        if [[ "$motencoderpid" != "0" ]] ; then
            kill -s 0 $motencoderpid
            if [[ "$?" != "0" ]] ; then
                # mot-encoder died
                # let's try restarting it

                motencoderpid=0

                checkloop=0

                printerr "mot-encoder died"
            fi
        fi
    done

    MAILTO=$(cat site/mail-warning.txt)

    if [[ "$MAILTO" != "" ]] ; then
        NOW=$(date)

        mail -s "Encoder $ID restart $URL" "$MAILTO" << EOF
The encoder id:$ID
encoding $URL -> $DST using encode-jack-dls-jamin was restarted at
$NOW

mplayer ok? $mplayer_ok

EOF

    fi
    sleep 5

done