Monday, February 22, 2016

LInux Video Streaming

In this post, we are going to see how we can stream from our computer's webcam or any usb camera connected to the computer. We will further create a hotspot for other computers to connect to and access the video stream. Finally, we would do some port forwarding and access the video stream via the internet with some basic http authentication. This turns our linux computer into an ip camera (which is basically a linux computer).


For this demonstration we need the following:

  1. An android smartphone with RCcontroller installed. (Might not be required)
  2. A linux computer (Eg: desktop/laptop, beaglebone black, raspberry pi, intel galileo/edison). 
  3. A port forwarding capable router. (Might not be required)


Let's get started. First, we update the package repositories:


sudo apt-get update


We then install motion (A video streaming application for linux)

sudo apt-get install motion


If you want motion to automatically start running after boot, change the file /etc/rc.local by add sudo motion before exit 0 like so:

sudo motion
exit 



Now let's configure motion:
Make the following configuration changes in the file /etc/motion/motion.conf

###########################################################
# Capture device options
############################################################
videodevice /dev/video0     <-------- Your video device. Find this
                                                                        through: ls /dev/video*

norm 0                      <-------- Video format.


width 320                   <-------- Device resolution width.

height 240                  <-------- Device resolution height.

framerate 30                <-------- Device framerate.


############################################################
# Image File Output
############################################################
output_pictures off         <-------- Turn off saving of pictures to
                                                                        hard drive. (Very annoying if
                                                                        you're only streaming !!!)


############################################################
# FFMPEG related options
# Film (movies) file output, and deinterlacing of the video input
# The options movie_filename and timelapse_filename are also used
# by the ffmpeg feature
############################################################

# Use ffmpeg to encode movies in realtime (default: off)
ffmpeg_output_movies off        <-------- Turn off saving stream to                                                                                                                         video


############################################################
# Text Display
# %Y = year, %m = month, %d = date,
# %H = hour, %M = minute, %S = second, %T = HH:MM:SS,
# %v = event, %q = frame number, %t = thread (camera) number,
# %D = changed pixels, %N = noise level, \n = new line,
# %i and %J = width and height of motion area,
# %K and %L = X and Y coordinates of motion center
# %C = value defined by text_event - do not use with text_event!
# You can put quotation marks around the text to allow
# leading spaces
############################################################

text_right %Y-%m-%d\n%T-%q  <-------- Delete everything after
                                                                        text_right if you don't want to
                                                                        see timestamp.


############################################################
# Live Stream Server
############################################################

stream_port  2020            <-------- Stream port.

stream_maxrate 30           <-------- Maximum stream rate. (Should
                                                    be the same as framerate for
                                                                        consistency).

stream_localhost off        <-------- Has to be off in order not to
                                                                        restrict streaming to just
                                                                        localhost.

# Set the authentication method (default: 0)
# 0 = disabled
# 1 = Basic authentication
# 2 = MD5 digest (the safer authentication)
stream_auth_method 1

# Authentication for the stream. Syntax username:password
# Default: not defined (Disabled)
stream_authentication Joachim:helloWorld 


That's all for the configuration. I know you wouldn't add this line.

Start motion by typing into the terminal:

sudo motion

 
To access the stream on the same computer to test it out, just type into the browser the url:

localhost:2020

You would be prompted to enter a username and password.






You can now do port forwarding on your router to your linux computer at port 2020 and stream the video from anywhere with internet connection.

For those of you who don't have a router, we can make a router with our linux computer. Let's do it!


Setup the wifi hotspot:

Let's download hostapd to help us configure the wifi adapter as a hotspot and isc-dhcp-server to take care of assigning ip addresses to connected clients

sudo apt-get install hostapd isc-dhcp-server -y

 

To make a wireless access point, edit the file /etc/hostapd/hostapd.conf to this

# Define network interface. Note: choose the appropriate wireless
# interface. Mine is wlan0. Find out with by typing ifconfig into the terminal
interface=wlan0
# Set the network driver
driver=nl80211
# Choose an access point name
ssid=MyVidStream
# Set access point harware mode to 802.11g
hw_mode=g
# Choose WIFI channel (can be easily changed)
channel=6
# Type of encryption: WPA2 only (1 for WPA, 2 for WPA2, 3 for
# WPA + WPA2)
wpa=2
# password
wpa_passphrase=simplepass

 

Give the wireless interface a static ip address to make sure it doesn't change everytime the hotspot is set up. Do this by adding to /etc/network/interfaces:

auto wlan0
iface wlan0 inet static
address 192.168.6.1

 

To make the hotspot set configure itself on start up, we simply make the hostapd daemon run after boot. Do this by editing the file /etc/default/hostapd as follows:
# Defaults for hostapd initscript
#
# See /usr/share/doc/hostapd/README.Debian for information about alternative
# methods of managing hostapd.
#
# Uncomment and set DAEMON_CONF to the absolute path of a hostapd configuration
# file and hostapd will be started during system boot. An example configuration
# file can be found at /usr/share/doc/hostapd/examples/hostapd.conf.gz
#
RUN_DAEMON="yes"
DAEMON_CONF="/etc/hostapd/hostapd.conf"

# Additional daemon options to be appended to hostapd command:-
#   -d   show more debug messages (-dd for even more)
#   -K   include key data in debug messages
#   -t   include timestamps in some debug messages
#
# Note that -B (daemon mode) and -P (pidfile) options are automatically
# configured by the init.d script and must not be added to DAEMON_OPTS.
#
DAEMON_OPTS="-dd"

 

Setup the dhcp server to assign ip addresses to connected clients by commenting out the following lines in /etc/dhcp/dhcpd.conf

# option definitions common to all supported networks...
#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

#default-lease-time 600;
#max-lease-time 7200;


#Finally to restrict the range of ip addresses assigned to clients

#we choose the range from 192.168.6.2 to 192.168.6.20 (up to 18 clients)

subnet 192.168.6.0 netmask 255.255.255.0 {
  range 192.168.6.2 192.168.6.20;
  option routers 192.168.6.1;
}




Restart the computer after all this configuration

Watching the video:

 
Connect a client to the network (the ssid name you chose) and enter
password.
 

Open a browser or streaming app and enter the url;
http://192.168.6.1:2020 or just 192.168.6.1:2020 in a browser
<protocol>://<ipaddress>:<port number>


See the video below for a demonstration:
 










No comments:

Post a Comment