Streaming multimedia files with VLC
I used VLC (from VideoLAN) at my very first meeting with Linux. Quickly it became my favorite player even in Windows because I haven`t seen any multimedia file that VLC wasn`t able to play. But VLC has many features along with simple playback. It can also read input from cameras to show it on screen or save to file, convert between any codecs it can play (almost all). And the most important - it can stream files (music or video) to network. Instalation is very easy, just type:
apt-get install vlc
Now when VLC is succesfuly installed, you can try to start it. To run VLC in command line without GUI, use "cvlc".
cvlc --version
Because you are probably logged in as root, you will get error message about running VLC as root is not a good idea.
VLC is not supposed to be run as root. Sorry.
If you need to use real-time priorities and/or privileged TCP ports
you can use vlc-wrapper (make sure it is Set-UID root and
cannot be run by non-trusted users first).
Through installation, VLC created user "vlc" which should be used instead. To run any command under another user, type "sudo -u user" before desired command.
sudo -u vlc cvlc --version
And you will get information about version installed. Now you know VLC is installed and working. Next step is to copy some multimedia file (audio or video) you wish to stream. As Iron maiden fan, I will use "IronMaiden-TheNumberOfTheBeast.ogg" in example commands. Song is from legally obtained CD, of course... ;)
Stream multimedia files using unicast
VLC support many protocols to stream. With unicast streaming I mainly use HTTP. It`s not designed to this kind of usage, but on LAN it simply works. To stream this file to network using HTTP, use this command:
sudo -u vlc cvlc "IronMaiden-TheNumberOfTheBeast.ogg" --sout '#http{mux=ffmpeg{mux=flv},dst=:65000/}' --loop
To join connect to this stream, use VLC, click Media > Open Network Stream and to empty line write "http://192.168.1.1:65000" and replace IP address to RPi`s one. And click "Play". With the same command you can stream also video files.
Stream multimedia files using multicast
With previous command, VLC must generate stream for everybody who connects. It is no problem when you have connected up to 3-4 users. But if there are more of them, link will be excessively loaded. In this case you should use multicast, when one stream is generated for an arbitrary number of listeners. In multicast streaming, you can not use HTTP (because of TCP which is used). Instead you can stream data in pure UDP. To stream previous song via UDP use command like this:
sudo -u vlc cvlc "IronMaiden-TheNumberOfTheBeast.ogg" --sout udp://239.255.1.1:1234 --loop
You can use same multicast address as me or pick any from "Administratively Scoped IPv4 Multicast addresses" (239.0.0.0/8). Now connect to this stream with VLC by typing "udp://239.255.1.1:1234". Be aware, if you have RPi and clients in another networks, you will have to setup multicast routing on your router. Give up if you have device like D-Link, TP-Link, Zyxel... (No offense, I know they can do multicast routing but problem is in poor documentation - compared to Cisco or Juniper devices). If you have Cisco router between RPi and clients like me, I recommend "CCIE Routing TCPIP Volume II" which helps me to get multicast routing working.
Quality of Service
If your network equipment is aware of QoS, you can set DSCP value of packets transmitted by VLC by adding this argument:
--dscp 0xC0
Stream in background
By default stream is running in foreground. To move it to background add ampersand after command, for example:
sudo -u vlc cvlc "IronMaiden-TheNumberOfTheBeast.ogg" --sout udp://239.255.1.1:1234 --loop &
Start stream at boot
Programs to run at strat are located at crontab file. Edit it with:
crontab -e
Add line with your command to run VLC and prepend "@reboot" which tell crontab to run following command at start. Your command should look like:
@reboot sudo -u vlc cvlc "IronMaiden-TheNumberOfTheBeast.ogg" --sout udp://239.255.1.1:1234 --loop
Close file (CTRL+X...) and save changes (...press "y" and Enter). After next reboot, VLC stream will be executed in background.