aboutsummaryrefslogtreecommitdiffstats
path: root/encode-jack.sh
blob: ab7e9aa8fab68445321ddfe79ca1df5fddd2025a (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
#!/bin/bash
#
# mplayer - jack - jack-stdout - dabplus-enc
# encoder script.
#
# Used to encode webstreams for DAB+, requires
# jackd -d dummy -r 32000
# to be running

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 [[ "$#" < 3 ]] ; then
    echo "Usage $0 url jack-id destination [volume]"
    echo "The volume setting is optional"
    exit 1
fi

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

if [[ "$#" == 4 ]] ; then
    VOL=$4
else
    VOL=""
fi

BITRATE=80
RATE=32000

encoderalive=0
mplayerpid=0
encoderpid=0

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

    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

    printmsg "Goodbye"
    exit
}

trap sigint_trap SIGINT

while true
do
    mplayer_ok=0

    if [[ "$mplayerpid" == "0" ]] ; then
        if [[ "$VOL" == "" ]] ; then
            mplayer -quiet -af resample=$RATE:0:2 -ao jack:name=$ID $URL &
            mplayerpid=$!
        else
            mplayer -quiet -af resample=$RATE:0:2 -af volume=$VOL -ao jack:name=$ID $URL &
            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" ]] ; then
        jack-stdout $ID:out_0 $ID:out_1 | \
            dabplus-enc -i /dev/stdin -l \
            -b $BITRATE -r $RATE -f raw -a -o $DST &
        encoderpid=$!
    fi

    printmsg "Started encoder with pid $encoderpid"

    sleep 5

    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
            checkloop=0

            # mark as dead
            mplayerpid=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

                checkloop=0

                printerr "Encoder died"
            fi
        fi
    done

    sleep 5

done