[Vietnam Mobile Day 2013] - How to build video streaming server in 15 minutes
1. Nguy n Kim Tuy nễ ế
CTO – MC CORP
M: +84912610253|| E: tuyennk@mc-corp.vn
W: http://mc-corp.vn
Skype: nktuyen
HOW TO BUILD VIDEO
STREAMING SERVER IN 15
MINS
3. Audience & Goal
• Audience object:
– Student
– Mobile developer
– Network administrator
– Newbie
– ….
=> For everyone who care about Video Streaming
Server
4. GOAL
• Provide a free solution.
• Quick & easy – even for newbie
• Security & efficiency
=> You can build your-own video streaming server for
your business without pay money.
6. Mobile Video formats
• Mobile TV and mobile video come in only a few
formats, and all are compressed:
– 3GPP (3rd Generation Partnership Project)
– MPEG-4 (Motion Picture Experts Group)
– Flash Lite
– RTSP (Real Time Streaming Protocol)
7. Let’s pick up a video format
• 3GPP or MP4?
– Both MP4 and 3GP are lossy formats which sacrifices
quality for file size.
– MP4 was created by Apple as a container for QuickTime
while 3GP is for mobiles with less resources(light weight and
videos lower in quality).
– MP4 is the industry standard with better quality and has
more widespread support than 3GP
– MP4 stores everything at higher bit rate than that of 3gp
10. LAMP, why not?
• L (Linux)
– Security, uptimes, stability, total free , Worldwide
community …
• A (Apache)
– The most popular HTTP server , 63.7% of all active websites
in the world(12/2012 ).
• M (MySQL)
– Scalability, Flexibility, High Performance, Strong Data
Protection, free …
• P (Php)
– Easy to Learn, variety support …
12. H264 Streaming Module
• is a plugin for your existing Apache/Lighttpd/Nginx
webserver
• Main features:
– Time shifting seek
– Virtual video clips
– Network efficiency
– Encoding
13. H264 Streaming Module
• Time shifting seek
– Enable your viewers to immediately jump to any part of
the video regardless of the length of the video or whether
it has all been downloaded yet.
– http://www.example.com/video.mp4?start=8
14. H264 Streaming Module
• Virtual video clips
– So, let's replace the preview URL '
http://www.example.com/video.mp4?start=15&end=45'
by 'http://www.example.com/video.mp4/preview' and
move the URL rewriting to the server side configuration..
– Open .htaccess file:
RewriteEngine On
# From: http://www.example.com/video.mp4/preview?start=xyz&foo=bar
# To:http://www.example.com/video.mp4?start=xyz&foo=bar&vbegin=15&vend=45
RewriteRule ^(.*)/preview?(start=.*)$ $1?$2&vbegin=15&vend=45 [L]
15. H264 Streaming Module
• Network efficiency
– Tested on system:
• Nginx 0.7.64
• mod_h264_streaming 2.2.7
• Centos 5.3
• Quadcore Xeon X3220 @ 2.40GHz
• 8G RAM
• 2x 300G 15k SAS hardware Raid0
– => At 280 connections and pushing 180Mbit it takes on
average 17% CPU on just one of the four cores.
16. H264 Streaming Module
• Encoding: MPEG4/H264 industry standard
– There is no need to re-encode your MP4 videos, you can
use your existing video files
– Using some tool to convert such as:
• Ffmpeg
• Mencoder & mp4creator
• …
18. System Prerequisite
• Linux: Centos 5.x or above
• Apache: Version 2x
– Module: rewrite_url
• MySQL: Version 5.x
• Php: version > 5.0
19. Install module H264
• Download the source of the H264 Streaming Module for
Apache:
• Build
cd ~
wget http://h264.code-shop.com/download/apache_mod_h264_streaming-2.2.7.tar.gz
tar -zxvf apache_mod_h264_streaming-2.2.7.tar.gz
cd ~/mod_h264_streaming-2.2.7
./configure
make
sudo make install
24. CONFIGURATION
• Check if module is loaded or not:
– The output will be something like
apachectl -t -D DUMP_MODULES
Loaded Modules:
dir_module (static)
actions_module (static)
userdir_module (static)
rewrite_module (static)
h264_streaming_module (shared)
…..
34. Encode video in MP4/H264
• Ffmpeg/x264
– Chmod:
– Execute:
– For more detail, please refer below link for detail:
• http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuide
chmod +x convert.sh
./convert.sh "video_in.avi“ "video_tmp.mp4“ “video_out.mp4"
36. TEST NEW ENCODED VIDEO
• Copy new encoded video to your web document
directory
• Test seek function:
cp newvideo.mp4 path_to_web_document_dir
http://www.example.com/newvideo.mp4?start=8
38. PRIVATE/PAID VIDEOS, HOW?
• Take a look at Video URL:
– http://www.example.com/private/video.mp4
• How can we hide it? => impossible
• How can we handle by using PHP? => impossible
• Bla bla ??? …
⇒We have to authenticate user first, but how?
⇒The answer is: Authenticate by using Apache
Authentication
39. PRIVATE/PAID VIDEOS, HOW?
• There are three types of modules involved in the
authentication and authorization process:
– Authentication type
• mod_auth_basic
• mod_auth_digest
– Authentication provider
• mod_authn_anon
• mod_authnz_ldap
• …
– Authorization
• mod_authnz_ldap
• …
41. USERS AUTHENTICATION
• Let’s choose the most basic Authentication type:
• Create new user “test” and store in file: /usr/local/htaccess_auth
– Enter password require for this user.
• Create .htaccess file
htpasswd -c /usr/local/htaccess_auth test
vim private/ .htaccess
AuthType Basic
AuthName "Restricted Files“
AuthBasicProvider file
AuthUserFile /usr/local/htaccess_auth
Require user test
46. MANAGE USERS WITH PHP&MYSQL
• Suppose we have table :
• Users (userid, username, password)
47. MANAGE USERS WITH PHP&MYSQL
• On user insert event (e.g: on register )
• Get user info
• Generates a htpasswd compatible crypted password string
• then put to htaccess_auth
$user = new User($username, $pwd);
public function User($username, $pwd){
$this->username = $username;
$this->htpasswd = $this->rand_salt_crypt($pwd);
}
$user->put_htpasswd(‘/usr/local/htaccess_auth ‘);
48. MANAGE USERS WITH PHP&MYSQL
• Function rand_salt_crypt()
• For more detail about Htpasswd Formats, please refer to
http://www.askapache.com/online-tools/htpasswd-
generator/