radio-audio-streaming.md 15 KB
Newer Older
pedrolab's avatar
pedrolab committed
1
2
<!-- START doctoc.sh generated TOC please keep comment here to allow auto update -->
<!-- DO NOT EDIT THIS SECTION, INSTEAD RE-RUN doctoc.sh TO UPDATE -->
pedro-nonfree's avatar
add toc    
pedro-nonfree committed
3
4
5
6
7
8
**Table of Contents**

- [icecast](#icecast)
- [libretime](#libretime)
  - [docs](#docs)
- [liquidsoap](#liquidsoap)
pedro-nonfree's avatar
pedro-nonfree committed
9
  - [install](#install)
pedrolab's avatar
pedrolab committed
10
11
    - [method 1 - latest version](#method-1-latest-version)
    - [method 2 - DYI](#method-2-dyi)
pedro-nonfree's avatar
pedro-nonfree committed
12
  - [daemonize](#daemonize)
pedro-nonfree's avatar
add toc    
pedro-nonfree committed
13
14
  - [sending signals](#sending-signals)
  - [testing switch with (catalan) voice and sounds](#testing-switch-with-catalan-voice-and-sounds)
pedrolab's avatar
pedrolab committed
15
16
  - [alternative to libretime/airtime](#alternative-to-libretimeairtime)
  - [other references](#other-references)
pedrolab's avatar
pedrolab committed
17
18
- [other radios / examples](#other-radios-examples)
- [script to launch a liquidsoap icecast server to send your sound to another place](#script-to-launch-a-liquidsoap-icecast-server-to-send-your-sound-to-another-place)
pedrolab's avatar
pedrolab committed
19
20
21
- [send sound using udp experimental feature of liquidsoap](#send-sound-using-udp-experimental-feature-of-liquidsoap)
- [sender ffmpeg to receiver vlc](#sender-ffmpeg-to-receiver-vlc)
- [send the sound to another place (best realtime experience)](#send-the-sound-to-another-place-best-realtime-experience)
pedro-nonfree's avatar
add toc    
pedro-nonfree committed
22

pedrolab's avatar
pedrolab committed
23
<!-- END doctoc.sh generated TOC please keep comment here to allow auto update -->
pedro-nonfree's avatar
add toc    
pedro-nonfree committed
24

pedro-nonfree's avatar
pedro-nonfree committed
25
26
## icecast

27
we assume that SSL/TLS/HTTPS is important (we are going to achieve: https://example.com:8443), that's why we are compiling it. if you don't care about doing `apt install icecast2` is fine
pedro-nonfree's avatar
pedro-nonfree committed
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

why we need to compile to get "security" capabilities:

- https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=744815
- https://trac.xiph.org/ticket/2310

```
apt-get build-dep icecast2
# trust me: https://downloads.xiph.org/releases -> https://ftp.osuosl.org/pub/xiph/releases/
wget https://ftp.osuosl.org/pub/xiph/releases/icecast/icecast-2.4.3.tar.gz
tar xvf icecast-2.4.3.tar.gz
cd icecast-2.4.3
./configure
make
make install
```

install icecast2 to get the service

    apt install icecast2

change /etc/init.d/icecast2

    #DAEMON=/usr/bin/icecast2
    DAEMON=/usr/local/bin/icecast

replace:

```
    <paths>
        <!-- basedir is only used if chroot is enabled -->
        <basedir>/usr/share/icecast2</basedir>

        <!-- Note that if <chroot> is turned on below, these paths must both
             be relative to the new root, not the original root -->
        <logdir>/var/log/icecast2</logdir>
        <webroot>/usr/share/icecast2/web</webroot>
        <adminroot>/usr/share/icecast2/admin</adminroot>
```

with:

```
    <paths>
        <!-- basedir is only used if chroot is enabled -->
        <basedir>/usr/local/share/icecast</basedir>

        <!-- Note that if <chroot> is turned on below, these paths must both
             be relative to the new root, not the original root -->
        <logdir>/var/log/icecast2</logdir>
        <webroot>/usr/local/share/icecast/web</webroot>
        <adminroot>/usr/local/share/icecast/admin</adminroot>
```

then we can remove icecast2 debian package

    apt remove icecast2

assuming you configured successfully a certbot / letsencrypt HTTPS certificate:

    cat /etc/letsencrypt/live/example.com/cert.pem /etc/letsencrypt/live/example.com/privkey.pem > /usr/local/share/icecast/icecast.pem

90
91
92
93
94
95
96
97
98
99
100
uncomment this:

    <listen-socket>
        <port>8443</port>
        <ssl>1</ssl>
    </listen-socket>

uncomment / adapt:

    <ssl-certificate>/usr/local/share/icecast/icecast.pem</ssl-certificate>

pedro-nonfree's avatar
pedro-nonfree committed
101
We cannot change or renew certificate reloading the service -> src https://wiki.xiph.org/Icecast_Server/known_https_restrictions
pedro-nonfree's avatar
pedro-nonfree committed
102

pedro-nonfree's avatar
pedro-nonfree committed
103
TODO smart cron to minimize number of restarts of the service. (!) A restart is going to break the streaming
pedro-nonfree's avatar
pedro-nonfree committed
104
105
106
107
108
109

references:

- check ssl in compilation src https://jksinton.com/articles/rpi-compiling-icecast-support-openssl
- service stuff inspired by src https://stackoverflow.com/questions/42188137/how-to-make-icecast-as-service-and-restart-it

pedro-nonfree's avatar
pedro-nonfree committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
## libretime

don't do this on a public server !!

```
git clone https://github.com/LibreTime/libretime
cd libretime
./install -p -i -a
```

### docs

switching between live and scheduled playout https://www.youtube.com/watch?v=qRqYtNsg-UM

## liquidsoap

pedro-nonfree's avatar
fix toc    
pedro-nonfree committed
126
### install
pedro-nonfree's avatar
pedro-nonfree committed
127

pedro-nonfree's avatar
update    
pedro-nonfree committed
128
convince yourself early that you need to compile yourself liquidsoap (if you are on debian 9 stretch on 2018-03-26) - https://github.com/LibreTime/libretime/issues/192
pedro-nonfree's avatar
pedro-nonfree committed
129
130
131
132
133

is required to send signals and to have libretime working

to install icecast2 is enough to `apt install icecast2` and put appropriate passwords to avoid intrusion

pedro-nonfree's avatar
fix toc    
pedro-nonfree committed
134
#### method 1 - latest version
pedro-nonfree's avatar
pedro-nonfree committed
135

pedro-nonfree's avatar
pedro-nonfree committed
136
137
only first and last command as root, later we use a specific user

pedrolab's avatar
pedrolab committed
138
139
(tested with debian10)

pedro-nonfree's avatar
pedro-nonfree committed
140
```
pedrolab's avatar
pedrolab committed
141
142
143
# good general reference: https://www.liquidsoap.info/doc-1.4.1/install.html
# bug: because mccs is not dependency of opam
apt install opam mccs
pedro-nonfree's avatar
pedro-nonfree committed
144

pedro-nonfree's avatar
pedro-nonfree committed
145
146
147
adduser liquidsoap
gpasswd -a liquidsoap sudo

pedrolab's avatar
pedrolab committed
148
su -l liquidsoap
pedro-nonfree's avatar
pedro-nonfree committed
149

pedrolab's avatar
pedrolab committed
150
151
152
153
# bug: if we don't specify solver in a 4GB RAM PC does not work
# related info (partial help on the topic)
#   https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=907636
#   https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=908203
pedrolab's avatar
pedrolab committed
154
opam init --solver mccs -ya
pedrolab's avatar
pedrolab committed
155

pedrolab's avatar
pedrolab committed
156
# run next line if `ocaml --version` is lower than 4.08.0 (again, put solver mccs)
pedrolab's avatar
pedrolab committed
157
opam switch create 4.08.0 --solver mccs
pedro-nonfree's avatar
pedro-nonfree committed
158

pedrolab's avatar
pedrolab committed
159
# execute next line and put it in `~/.bashrc`
pedrolab's avatar
pedrolab committed
160
eval $(opam env)
pedro-nonfree's avatar
pedro-nonfree committed
161

pedro-nonfree's avatar
pedro-nonfree committed
162
163
# general
# next command will need sudo
pedrolab's avatar
pedrolab committed
164
165
opam depext taglib mad lame vorbis cry samplerate liquidsoap opus pulseaudio gstreamer
opam install taglib mad lame vorbis cry samplerate liquidsoap opus pulseaudio gstreamer
pedro-nonfree's avatar
pedro-nonfree committed
166
167
168
```

install "using opam" -> src http://liquidsoap.info/download.html
pedro-nonfree's avatar
pedro-nonfree committed
169

pedrolab's avatar
pedrolab committed
170
171
172
173
174
upgrade:

    opam update
    opam upgrade

pedro-nonfree's avatar
fix toc    
pedro-nonfree committed
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#### method 2 - DYI

```
apt-get build-dep liquidsoap
apt-get source liquidsoap
cd liquidsoap-<version>
./configure
make
make install
```

src http://wiki.occupyboston.org/wiki/Multi-usb_mic/jackd/liquidsoap_setup

### daemonize
pedro-nonfree's avatar
pedro-nonfree committed
189

pedro-nonfree's avatar
pedro-nonfree committed
190
191
transform your liquidsoap script as a service

pedro-nonfree's avatar
pedro-nonfree committed
192
```
pedro-nonfree's avatar
pedro-nonfree committed
193
opam install liquidsoap-daemon
pedro-nonfree's avatar
pedro-nonfree committed
194
cd /to/the/directory/you/want/files/created # in my case: liked `cd ~`
pedro-nonfree's avatar
pedro-nonfree committed
195
196
197
198
199
200
201
daemonize-liquidsoap.sh
```

modify accordingly `~/script/main.liq`

after that:

pedrolab's avatar
pedrolab committed
202
    systemctl status main-liquidsoap.service
pedro-nonfree's avatar
pedro-nonfree committed
203

pedro-nonfree's avatar
pedro-nonfree committed
204
205
206
207
208
209
210
211
212
213
214
215
src https://github.com/savonet/liquidsoap-daemon

next service would be

    daemonize-liquidsoap.sh newservice

and modify accordingly `~/script/newservice.liq`

to remove:

    mode=remove init_type=systemd daemonize-liquidsoap newservice

pedro-nonfree's avatar
pedro-nonfree committed
216
### sending signals
pedro-nonfree's avatar
pedro-nonfree committed
217
218
219
220
221

send mp3 file (that loops indefinetely) or your input microphone (using pulseaudio)

explanation for jack -> src http://wiki.occupyboston.org/wiki/Multi-usb_mic/jackd/liquidsoap_setup

pedrolab's avatar
pedrolab committed
222
send directly to icecast
pedro-nonfree's avatar
pedro-nonfree committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

```liquidsoap
#!/usr/bin/liquidsoap
set("log.file.path", "/path/to/liq.log")
signal = single("/path/to/song.mp3")
#signal = input.pulseaudio(clock_safe=false)
output.icecast(%mp3,
     host = "<ip or URL>", port = 8000,
     password = "hackme", mount = "jazz.mp3",
     mksafe(signal))
```

send to libretime to master source

```liquidsoap
#!/usr/bin/liquidsoap
set("log.file.path", "/path/to/liq.log")
signal = single("/path/to/song.mp3")
#signal = input.pulseaudio(clock_safe=false)
output.icecast(%mp3,
     host = "<ip or URL>", port = 8001,
     password = "hackme", mount = "master",
     mksafe(signal))
```
pedro-nonfree's avatar
pedro-nonfree committed
247
248
249

### testing switch with (catalan) voice and sounds

pedro-nonfree's avatar
pedro-nonfree committed
250
251
252
extra requirements (voice synthesis)

    apt install festival festvox-ca-ona-hts
pedro-nonfree's avatar
pedro-nonfree committed
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

add to `/etc/festival.scm`:

    (set! voice_default voice_upc_ca_ona_hts)

strings with accents will be destroyed, for example "interrupció" -> http://festcat.talp.cat/en/usage.php

```
# concat strings -> https://bytesandbones.wordpress.com/2014/07/18/liquidsoap-concat-strings/
# random weigths -> src "A simple radio" http://savonet.sourceforge.net/doc-svn/quick_start.html
def template(text, freq)
  msg = random(
    weights = [1, 2, 10],
    [
      single("say: #{text}"),
      sine(480.0, amplitude = 0.1, duration = 0.5),
      blank(duration = 1.0)
    ])
  add([sine(freq, amplitude = 0.05), msg], normalize = true)
end

# A simple cross-fade -> src "Switch-based transitions" http://savonet.sourceforge.net/doc-svn/cookbook.html
def crossfade(a,b)
  add(normalize=false,
      [ sequence([ blank(duration=1.),
                   fade.initial(duration=2.,b) ]),
        fade.final(duration=1.,a) ])
end

init = template("tros de 0 a deu segons cada minut", 800.0)
init2 = template("tros de trenta a quaranta cinc segons cada minut", 1500.0)
#p1 = template("program number 1")
p2 = template("programa de deu a catorze hores", 440.0)
p3 = template("programa de quinze a vint hores", 440.0)
pedro-nonfree's avatar
pedro-nonfree committed
287
default_source = template("programa per defecte", 440.0)
pedro-nonfree's avatar
pedro-nonfree committed
288
289
290
291
292
293
294
295
296
297
298

default_transition = [
  crossfade, # init
  crossfade, # init2
  crossfade, # p2
  crossfade, # p3
  crossfade # default source
]

signal = switch(
  track_sensitive = false,
pedro-nonfree's avatar
pedro-nonfree committed
299
300
  transitions = default_transition,
  [
pedro-nonfree's avatar
pedro-nonfree committed
301
302
303
304
305
306
307
308
309
    ({ 0s-10s }, init),
    ({ 30s-45s }, init2),
    ({ 10h-14h }, p2),
    ({ 15h-20h }, p3),
    ({ true }, default_source)
  ])

#out(signal)

310
output.icecast(%mp3,
pedro-nonfree's avatar
pedro-nonfree committed
311
312
313
314
     host = "localhost", port = 8000,
     password = "hackme", mount = "test2.mp3",
     mksafe(signal))
```
pedro-nonfree's avatar
pedro-nonfree committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

### alternative to libretime/airtime

- main: take care of having always a fallback source (never stops, at the end we have silence running, and should never be modified or restarted). In this priority:
    - live1: intented to be for live with static icecast mountpoint
    - live2: intended to be for live with dynamic/spontaneous/ephemeral icecast mountpoint
    - schedule: executes the scheduled radio

```
su - liquidsoap
cd ~
daemonize-liquidsoap.sh main
daemonize-liquidsoap.sh live1
daemonize-liquidsoap.sh live2
daemonize-liquidsoap.sh schedule

# live1 and live2 should only be started manually
pedrolab's avatar
pedrolab committed
332
systemctl disable live1-liquidsoap.service
pedro-nonfree's avatar
pedro-nonfree committed
333
334
335
systemctl disable live2-liquidsoap.service
```

pedro-nonfree's avatar
pedro-nonfree committed
336
files of the service -> https://github.com/guifi-exo/xrcb-scheduler
pedro-nonfree's avatar
pedro-nonfree committed
337

pedro-nonfree's avatar
pedro-nonfree committed
338
339
340
341
### other references

https://www.linuxjournal.com/content/creating-internet-radio-station-icecast-and-liquidsoap

pedrolab's avatar
pedrolab committed
342
## other radios / examples
pedro-nonfree's avatar
pedro-nonfree committed
343
344

http://dir.xiph.org/
345

pedrolab's avatar
pedrolab committed
346
## script to launch a liquidsoap icecast server to send your sound to another place
pedrolab's avatar
pedrolab committed
347
348
349
350
351
352

tested in debian10 (debian9 is not going to work, then upgrade or use opam). Install liquidsoap:

    sudo apt install liquidsoap

on the server, create a file `script.liq` with:
353
354
355

```
set("log.file.path", "/tmp/script.liq.log")
pedrolab's avatar
pedrolab committed
356
# src "Usage" https://www.liquidsoap.info/doc-1.3.3/harbor.html
357
358
359
360
361
362
363
364
365
set("harbor.bind_addr","0.0.0.0")

# send sine as test
#source=sine(440.0, amplitude=0.01)
# send mixer output -> thanks https://sourceforge.net/p/savonet/mailman/message/28168771/
#   pacmd list-sources | grep "name:"
# (get the one with .monitor at the end)
mysoundcard="alsa_output.pci-0000_00_1b.0.analog-stereo.monitor"
source=input.pulseaudio(device=mysoundcard)
pedrolab's avatar
pedrolab committed
366
myport=8000
367
368

# thanks https://sourceforge.net/p/savonet/mailman/message/27916432/
pedrolab's avatar
pedrolab committed
369
output.harbor(%mp3,mount="test",port=myport,source)
370
371
372
373
# touching buffer, chunk, burst was a mess and people say "don't do it" https://sourceforge.net/p/savonet/mailman/message/23325265/
# +info: https://www.liquidsoap.info/doc-1.3.3/reference.html#output.harbor
```

pedrolab's avatar
pedrolab committed
374
375
on any client with web browser: http://<server_ip>:8000/test

pedrolab's avatar
pedrolab committed
376
## send sound using udp experimental feature of liquidsoap
377

pedrolab's avatar
pedrolab committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
on sender:

```
set("log.file.path", "/tmp/script.liq.log")

# src https://www.liquidsoap.info/doc-1.3.3/reference.html#output.udp

# send mixer output -> thanks https://sourceforge.net/p/savonet/mailman/message/28168771/
#   pacmd list-sources | grep "name:"
# (get the one with .monitor at the end)
mysoundcard="alsa_output.pci-0000_00_1b.0.analog-stereo.monitor"
source=input.pulseaudio(device=mysoundcard)

output.udp(%mp3(bitrate=128), host="192.168.16.80", port=5004, source)

# #not working, see below #1089
# output.opus(%opus(bitrate=128), host="192.168.16.80", port=5004, source)
```

on receiver:

```
set("log.file.path", "/tmp/script.liq.log")

# adjust buffer the lower you can. looks like mp3 is great with 0.1, lower is becoming more and more cracky
out(input.udp(host="192.168.16.80", port=5004, buffer=0.05, "audio/mpeg")

# #not working, see below #1089
# out(input.udp(host="192.168.16.80", port=5004, buffer=0.05, "audio/opus")
```

filled bug to include opus mime type and have greater features in latency and bandwidth https://github.com/savonet/liquidsoap/issues/1089

pedrolab's avatar
pedrolab committed
411
412
413
414
415
other interesting references:

- https://github.com/savonet/liquidsoap/issues/109
- https://anarc.at/services/radio/#playing-on-the-sound-card-and-live-rtp-output

pedrolab's avatar
pedrolab committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
## sender ffmpeg to receiver vlc

a good tradeoff between having good latency and being more crossplatform software

in sender

```
# thanks https://unix.stackexchange.com/questions/488063/record-screen-and-internal-audio-with-ffmpeg
A="$(pacmd list-sources | grep -PB 1 "analog.*monitor" | head -n 1 | perl -pe 's/.* //g')"
# thanks https://stackoverflow.com/questions/43656892/stream-opus-audio-rtp-to-android-device/48673205#48673205
ffmpeg -re -stream_loop -1 -f pulse -i default -acodec libopus -ac 1 -ab 96k -vn -f rtp rtp://10.42.0.137:5000
```

in `pavucontrol` go to *playback* tab and select input or monitor (depending on what you want to emit)

in receiver

from the sender, copy `ffmpeg` output block after `SDP:` to a file like `myfile.sdp`, in my case (as an example):

```
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 10.42.0.137
t=0 0
a=tool:libavformat 58.20.100
m=audio 5000 RTP/AVP 97
b=AS:96
a=rtpmap:97 opus/48000/2
```

then, open `myfile.sdp` with [VLC](https://www.videolan.org/index.es.html), it should start working

adjust preferences to quit caching (thanks https://www.softzone.es/programas/multimedia/aumentar-bufer-vlc/). Because by default there is a buffer of 1.5 seconds

- preferences / all / input/codecs / advanced / live capture caching (ms): 0 ms
- preferences / all / input/codecs / advanced / network caching (ms): 0
- preferences / all / stream output / stream output muxer caching (ms): 0 ms

## send the sound to another place (best realtime experience)

I used http://www.pogo.org.uk/~mark/trx/ with pulseaudio sound systems and 2 debian 10 laptops
458
459
460
461
462
463
464
465
466

clone repository:

    git clone http://www.pogo.org.uk/~mark/trx.git

dependencies required (tested in debian 10):

    sudo apt install libortp-dev libasound2-dev libopus-dev

467
468
469
470
471
enter directory and compile

    cd trx
    make

472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
you need to enable realtime or you will see that *[something] is not permitted*

easiest way is to install jack

    sudo apt install qjackctl

manually, looks like is creating this file `/etc/security/limits.d/audio.conf` with this content:

```
@audio - rtprio 95
@audio - memlock unlimited
```

and maybe *if you want to enable/disable realtime permissions*, run:

    dpkg-reconfigure -p high jackd

two laptops with debian 10, on one I did:

    ./tx -h 192.168.0.32         # send audio from default soundcard to the given host

in the other

    ./rx

by default it caught my microphone, to get the *speakers* or the *emitting sound* from the other host you have to go *pavucontrol* (`sudo apt install pavucontrol`), *recording* tab and you will see *ALSA plug-in [tx]: ALSA Capture from*, change the box from *Built-in Audio Analog Stereo* to *Monitor of Built-in Audio Analog Stereo* (change appropiately with whatever soundcard you are using)