├── README.md ├── deploy.sh ├── livestream-working.jpg ├── nginx.conf ├── stream.sh └── stream.supervisor.conf /README.md: -------------------------------------------------------------------------------- 1 | # Streaming live video from a webcam over RTMP using a Raspberry Pi and Nginx 2 | 3 | 4 | ![Live stream for foosball in action, much inception!](/livestream-working.jpg?raw=true "Live stream for foosball in action, much inception!") 5 | 6 | **Live stream for foosball in action, much inception!** 7 | 8 | - - - 9 | 10 | Imagine you're in a shared office with a foosball table and many people in the office like to play foosball. Then sometimes when you want to play you'll make a trip down the stairs just to find out it's in use and you'll have to go up the stairs again. That's pretty sad, useless and a waste of time. That time should be spent in awesome projects like these. 11 | 12 | The fix is obvious: Hackaton! Get a Raspberry Pi, a webcam and stream the foosball table so you'll always know if it's free! There are also some additional bennefits, such as secretly analysing competitor tactics 13 | 14 | Because we ran into quite a few difficulties we decided to write a little how-to. Sharing is caring ;) 15 | 16 | If you find anything is missing, please create an issue [here](https://github.com/Tomtomgo/raspberry_livestream/issues)! 17 | 18 | ## Hardware 19 | 20 | This is the hardware we used. You will probably have some other stuff... which should work as well. 21 | 22 | - RaspberryPi B+ with Raspbian installed 23 | - 8 GB SD card 24 | - Logitech C310 webcam 25 | - 2A power adapter 26 | - All these things connected appropriately 27 | 28 | ## Installing the necessary software 29 | 30 | Everything here should be done on the Raspberry Pi (as opposed to on your computes) unless stated otherwise. 31 | 32 | ### Basics 33 | 34 | Some basic dependencies: 35 | 36 | ```bash 37 | sudo apt-get install ffmpeg supervisor 38 | ``` 39 | 40 | ### Build Nginx with RTMP module 41 | 42 | To stream our video to the web we use [Nginx](http://nginx.org/) with an [RTMP module](https://github.com/arut/nginx-rtmp-module). This module has to be compiled into Nginx, so let's do it: 43 | 44 | ```bash 45 | cd /tmp 46 | wget https://github.com/arut/nginx-rtmp-module/archive/master.zip 47 | wget http://nginx.org/download/nginx-1.7.9.tar.gz 48 | tar -zxvf nginx-1.7.9.tar.gz 49 | unzip master.zip 50 | 51 | cd nginx-1.7.9 52 | ./configure --add-module=/tmp/nginx-rtmp-module-master 53 | make # <- This takes a few minutes on a Raspberry Pi 54 | sudo make install 55 | ``` 56 | 57 | ### Get the files! 58 | 59 | You can fork the entire repo, or clone to your computer: 60 | 61 | ```bash 62 | git clone https://github.com/Tomtomgo/raspberry_livestream.git 63 | ``` 64 | 65 | ### Edit `stream.sh` for your environment 66 | 67 | Replace \ and \ in `stream.sh` with suitable values for you. 68 | 69 | ### Copy files to Raspberry Pi 70 | 71 | From this folder on your machine: 72 | 73 | ```bash 74 | scp -r ./ pi@:/home/pi 75 | ``` 76 | 77 | ### Copy config files 78 | 79 | ```bash 80 | cd ~ 81 | sudo cp nginx.conf /usr/local/nginx/conf/nginx.conf 82 | cp stream.supervisor.conf /etc/supervisor/conf.d/stream.supervisor.conf 83 | ``` 84 | 85 | ### Run the systems! 86 | 87 | This will (re)start Nginx and the stream itself. 88 | 89 | ```bash 90 | sudo service supervisor stop 91 | sudo service supervisor start 92 | ``` 93 | 94 | ## Verify the stream 95 | 96 | You can check that it's actually streaming by opening this URI in [VLC](http://www.videolan.org/vlc/index.html) on your computes: 97 | 98 | ```uri 99 | rtmp:// 100 | ``` 101 | 102 | ## Showing the stream on a web page 103 | 104 | We used [HDW Player](http://www.hdwplayer.com) for showing the RTMP-stream, but there are probably many more. 105 | 106 | Download it and put the `player` folder in a project folder somewhere. 107 | 108 | Then to show the video you can do something like this (replace ): 109 | 110 | ```html 111 | 112 | 113 | 114 | 115 | 116 |
117 | 130 | 131 | 132 | ``` 133 | 134 | That should do it! Go fot it! 135 | 136 | ## Adventures 137 | As mentioned, we ran into a little bit of trouble before it worked. Hereby a short summary what we tried and why it did not work; 138 | 139 | 1. Stream to [bambuser.com](http://bambuser.com) 140 | 141 | We never got the stream connected at Bamuser. The RMTP-addresses were unclear and did not accepted the connections. 142 | 143 | 2. Stream to [twitch.tv](http://twitch.tv) 144 | 145 | This did not worked as we got banned several times, because we were not streaming gaming content. Wierd! Foosball is the best sport ever. 146 | 147 | 3. Stream to [youtube.com](http://youtube.com) 148 | 149 | This attempt almost worked, we had a few cases were the stram was received. But in the end YouTube did not accepted the low bitrate coming from the RasberryPi. 150 | 151 | ## Credits 152 | 153 | Built at [Rockstart](http://rockstart.com) by [Sjoerd Huisman](https://github.com/shuisman) from [Congressus](https://www.congressus.nl/) and [Tom Aizenberg](https://github.com/Tomtomgo) from [Achieved](http://achieved.co). 154 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | if [[ $1 != "" ]] 4 | then 5 | echo "Deploying to $1" 6 | else 7 | echo "You should set PI's IP address!" 8 | exit 1 9 | fi 10 | 11 | scp -r ./ pi@$1:/home/pi -------------------------------------------------------------------------------- /livestream-working.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Tomtomgo/raspberry_livestream/8d7f03a5fbb19cb0a3d80f326546fe64dd2c28c2/livestream-working.jpg -------------------------------------------------------------------------------- /nginx.conf: -------------------------------------------------------------------------------- 1 | # This is the nginx configuration file for setting up a simple live streaming server. 2 | worker_processes 1; 3 | 4 | events { 5 | worker_connections 1024; 6 | } 7 | 8 | rtmp { 9 | server { 10 | listen 1935; 11 | ping 30s; 12 | notify_method get; 13 | 14 | application live { 15 | live on; 16 | } 17 | } 18 | } 19 | 20 | http { 21 | include mime.types; 22 | default_type application/octet-stream; 23 | 24 | sendfile on; 25 | keepalive_timeout 65; 26 | 27 | server { 28 | listen 80; 29 | server_name localhost; 30 | 31 | 32 | location / { 33 | rtmp_stat all; 34 | rtmp_stat_stylesheet stat.xsl; 35 | } 36 | 37 | location /control { 38 | rtmp_control all; 39 | } 40 | 41 | location /stat.xsl { 42 | root /home/pi/; 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /stream.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | # Run avconv to stream the webcam's video to the RTMP server. 4 | 5 | avconv -f video4linux2 \ # Webcam format goes in 6 | -s 320x176 \ # Small is big enough 7 | -r "10" \ # Fixed framerate at 10fps, somehow this needs to be a string 8 | -b 256k \ # Fixed bitrate 9 | -i /dev/video0 \ # Webcam device 10 | -vcodec libx264 \ # h264.1 video encoder 11 | -preset ultrafast \ # Use the fastest encoding preset 12 | -f flv \ # Flash video goes out 13 | -an \ # No audio! 14 | 'rtmp:///live/' 15 | -------------------------------------------------------------------------------- /stream.supervisor.conf: -------------------------------------------------------------------------------- 1 | # The minimal Supervisord configuration for running the live stream. 2 | [program:serve] 3 | command=/home/pi/stream.sh 4 | process_name=%(program_name)s 5 | numprocs=1 6 | directory=/home/pi 7 | autostart=true 8 | autorestart=true 9 | user=pi 10 | 11 | [program:nginx] 12 | command=/usr/local/nginx/sbin/nginx 13 | process_name=%(program_name)s 14 | numprocs=1 15 | autostart=true 16 | autorestart=true 17 | user=root --------------------------------------------------------------------------------