lar.ven – this.ven's wiki

Authority through knowledge (of FLOSS and GNU/Linux)

User Tools

Site Tools


floss_media_studio:streaming_setup

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
floss_media_studio:streaming_setup [2021/06/14 17:01] – Corrected Wikipedia links to use interwiki links thisvenfloss_media_studio:streaming_setup [2022/09/13 18:35] (current) – [Controlling listeners] Styling of code this.ven
Line 1: Line 1:
 +====== Streaming setup ======
  
 +[[https://en.wikipedia.org/wiki/Live_streaming|Live streaming]] can be done independently from 3rd parties by using a streaming [[wp>Media_server|media server]] like [[services/icecast|Icecast]] and a [[https://icecast.org/apps/#source-clients|source client]] for encoding and streaming to that server.
 +
 +I'm using Icecast and [[https://r-w-x.org/r/oggfwd|oggfwd]] in combination with [[https://ffmpeg.org|ffmpeg]] as source client to realize live streaming concerts at https://this.ven.uber.space/music/live/. Although there are many official source clients available, oggfwd seems to be the only solution when using [[https://jackaudio.org|JACK]] and encrypted connections with [[wp>Transport_Layer_Security|Transport Layer Security (TLS)]]. Mixing and monitoring is done by [[https://ardour.org|ardour]], hence the signal flow can be represented like this:
 +
 +''ardour => ffmpeg => oggfwd => icecast => listener''
 +
 +===== Prerequisites =====
 +
 +For this guide a running Icecast [[services/icecast#advanced_configuration|advanced configuration]] and a working [[..floss_media_studio|FLOSS media studio]] setup for [[floss_media_studio/audio_setup|audio]] is assumed. General knowledge on how to install software and use a terminal in your GNU/Linux distribution is necessary, too.
 +
 +==== Installation ====
 +
 +Get ffmpeg and oggfwd package by using your distribution's [[wp>Package_manager|package manager]] or compile from sources. You also need to have [[https://mpv.io|mpv]] installed to play waiting music as well as [[https://www.gnu.org/software/wget/|wget]] for triggering tasks via Icecast's [[https://www.icecast.org/docs/icecast-trunk/admin_interface/|Admin Interface]].
 +
 +==== Set environment ====
 +
 +To simply copy and paste the commands used in this guide, set the following [[wp>Environment_variable#Unix|environment variables]] to your needs. Most of the values depend on your [[services/icecast#basic_setup|Icecast basic setup]]. This is an example:
 +
 +<code bash streaming_setup-1_set_environment.sh>export CONCERT_TITLE="My streaming concert"
 +export ARTIST_NAME="Just me"
 +export HOSTNAME="icecast.example.org"
 +export PORT="1234"
 +export SOURCE_PASSWORD="secret-S0URC3-passwd"
 +export STREAM_MOUNTPOINT="/stream.ogg"
 +export FALLBACK_MOUNTPOINT="/greeting.ogg"
 +export AUDIO_FILE="/path/to/waiting_music.ogg"
 +export ADMIN_USERNAME="admin"
 +export ADMIN_PASSWORD="@dmin-P4SSW0RD"</code>
 +
 +==== Session preparation ====
 +
 +Create or open an [[https://manual.ardour.org/working-with-sessions/the-new-session-dialog/|ardour session]] to route, control and mix physical inputs as well as [[https://manual.ardour.org/adding-pre-existing-material/|pre-recorded tracks]], if any. Setup your session: Level gains, pan tracks and apply effects as you wish, but keep in mind to save some resources for encoding. If a recording is desired, it's enabled in [[https://manual.ardour.org/ardours-interface/the-transport-bar/|transport bar]] and [[https://manual.ardour.org/working-with-tracks/audio-track-controls/|tracks are armed]], too.
 +
 +===== Start source streaming =====
 +
 +The actual source streaming happens when ardour's output is encoded by ffmpeg and [[wp>Pipeline_(Unix)|piped]] to oggfwd, which sends the stream to Icecast for distribution to listeners.
 +
 +==== Encode and forward ====
 +
 +The output of ardour must be encoded to [[wp>Vorbis|Ogg Vorbis]] container format and provided at [[wp>Standard_streams#Standard_output_(stdout)|stdout]] to be forwarded by oggfwd. This can be achieved by using ffmpeg's JACK input device as well as audio encoding parameters and piping to oggfwd using the ''|'' operator:
 +
 +<code bash streaming_setup-2_encode_forward_stream.sh>ffmpeg -f jack -i ffmpeg -vn -acodec libvorbis -b:a 192k -minrate 192k -maxrate 192k \
 +-metadata title="$CONCERT_TITLE" -metadata artist="$ARTIST_NAME" -f ogg -y /dev/stdout |
 +oggfwd $HOSTNAME $PORT $SOURCE_PASSWORD $STREAM_MOUNTPOINT</code>
 +
 +ffmpeg parameters:
 +  * ''[[https://ffmpeg.org/ffmpeg-devices.html#jack|-f jack -i ffmpeg]]'': Create a JACK input device called //ffmpeg//.
 +  * ''[[https://ffmpeg.org/ffmpeg.html#Stream-selection|-vn]]'': Specify that no video data is included, streaming audio only.
 +  * ''[[https://ffmpeg.org/ffmpeg.html#Audio-Options|-acodec libvorbis]]'': Set audio codec to Vorbis by using [[https://xiph.org/vorbis/|libvorbis]] library.
 +  * ''[[https://ffmpeg.org/ffmpeg.html#Stream-specifiers-1|-b:a 192k]] -minrate 192k -maxrate 192k'': Ensure constant bitrate at 192 kbps for encoding.
 +  * ''[[https://ffmpeg.org/ffmpeg.html#Main-options|-metadata title="$CONCERT_TITLE" -metadata artist="$ARTIST_NAME"]]'': Add stream metadata.
 +  * ''[[https://ffmpeg.org/ffmpeg.html#Main-options|-f ogg -y /dev/stdout]]'': Force Ogg Vorbis output format and write to stdout location in [[wp>Filesystem_Hierarchy_Standard|Filesystem Hierarchy]].
 +
 +==== Connect JACK clients ====
 +
 +Use another terminal to connect ardour's master outputs to ''ffmpeg:input_1'' and ''ffmpeg:input_2'' within JACK:
 +
 +<code bash streaming_setup-3_connect_jack_ports.sh>jack_connect "ardour:Master/audio_out 1" ffmpeg:input_1
 +jack_connect "ardour:Master/audio_out 2" ffmpeg:input_2</code>
 +
 +==== Saving time ====
 +
 +When setting up those things all by yourself, it might be necessary play waiting music immediately after the source streaming started to save time. I'm using mpv with [[https://mpv.io/manual/stable/#audio-output-drivers|JACK audio output driver]], auto-connecting to ffmpeg and [[https://mpv.io/manual/stable/#playback-control|looping]] the file $AUDIO_FILE in another terminal:
 +
 +<code bash streaming_setup-4_save_time.sh>mpv --ao=jack --jack-port=ffmpeg --loop-file=inf $AUDIO_FILE</code>
 +
 +Press the ''m'' key when starting the concert to mute mpv's output and press //Play// button in ardour's transport bar to start recording, if any.
 +
 +===== Controlling listeners =====
 +
 +Icecast's Admin Interface offers some functions to control listeners. I'm using wget to trigger those tasks at the terminal. General syntax is:
 +
 +<code bash>wget --quiet --output-document=/dev/null --http-user=$ADMIN_USERNAME \
 +--http-password=$ADMIN_PASSWORD https://$HOSTNAME:$PORT/admin/$WGET_URI</code>
 +
 +wget parameters:
 +  * ''[[https://www.gnu.org/software/wget/manual/html_node/HTTP-Options.html#HTTP-Options|--http-user=$ADMIN_USERNAME]]'': Use $ADMIN_USERNAME for Icecast web administration login
 +  * ''[[https://www.gnu.org/software/wget/manual/html_node/HTTP-Options.html#HTTP-Options|--http-password=$ADMIN_PASSWORD]]'': Use $ADMIN_PASSWORD for Icecast web administration login
 +  * ''[[https://www.gnu.org/software/wget/manual/html_node/Logging-and-Input-File-Options.html#Logging-and-Input-File-Options|--quiet]]'': Turn off terminal output.
 +  * ''[[https://www.gnu.org/software/wget/manual/html_node/Download-Options.html#Download-Options|--output-document=/dev/null]]'': Write output document /dev/null, discard the download.
 +
 +The value of ''$WGET_URI'' depends on specific task.
 +
 +==== Move listeners from fallback ====
 +
 +The fallback mountpoint is activated since Icecast is running and listeners might have connected before the actual source stream started. To [[https://www.icecast.org/docs/icecast-trunk/admin_interface/#move-clients-listeners|move listeners]] from one mountpoint to another, execute:
 +
 +<code bash streaming_setup-5_move_listeners.sh>export WGET_URI="moveclients.xsl?mount=$FALLBACK_MOUNTPOINT&destination=$STREAM_MOUNTPOINT"
 +wget --quiet --output-document=/dev/null --http-user=$ADMIN_USERNAME \
 +--http-password=$ADMIN_PASSWORD https://$HOSTNAME:$PORT/admin/$WGET_URI</code>
 +
 +==== Kill fallback streaming ====
 +
 +It's useful to [[https://www.icecast.org/docs/icecast-trunk/admin_interface/#kill-source|kill]] fallback streaming for misbehaving listener clients afterwards:
 +
 +<code bash streaming_setup-6_kill_fallback.sh>export WGET_URI="killsource.xsl?mount=$FALLBACK_MOUNTPOINT"
 +wget --quiet --output-document=/dev/null --http-user=$ADMIN_USERNAME \
 +--http-password=$ADMIN_PASSWORD https://$HOSTNAME:$PORT/admin/$WGET_URI
 +</code>
 +
 +===== Stop source streaming =====
 +
 +At first mpv's output is unmuted by pressing ''m'' to indicate the end of the concert. Then listener's will be moved back to fallback mount and source streaming is killed:
 +
 +<code bash streaming_setup-7_stop_source.sh>export WGET_URI="moveclients.xsl?mount=$STREAM_MOINTPOINT&destination=$FALLBACK_MOUNTPOINT"
 +wget --quiet --output-document=/dev/null --http-user=$ADMIN_USERNAME \
 +--http-password=$ADMIN_PASSWORD https://$HOSTNAME:$PORT/admin/$WGET_URI
 +export WGET_URI="killsource.xsl?mount=$STREAM_MOUNTPOINT"
 +wget --quiet --output-document=/dev/null --http-user=$ADMIN_USERNAME \
 +--http-password=$ADMIN_PASSWORD https://$HOSTNAME:$PORT/admin/$WGET_URI</code>
 +
 +Lastly ffmpeg, oggfwd and mpv processes are killed:
 +
 +<code bash streaming_setup-8_killall.sh>killall -9 ffmpeg oggfwd mpv</code>
 +
 +In ardour, recording is stopped and the session is saved for later production.
 +
 +{{tag>live_streaming media_server media_studio recording}}
 +
 +Category: [[./|FLOSS media studio]]