[Vietnam Mobile Day 2013] - How to build video streaming server in 15 minutes
Upcoming SlideShare
Loading in...5

[Vietnam Mobile Day 2013] - How to build video streaming server in 15 minutes



Diễn giả: Nguyễn Kim Tuyến

Diễn giả: Nguyễn Kim Tuyến
Đơn vị: Công ty cổ phần giải trí Minh Châu



Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

[Vietnam Mobile Day 2013] - How to build video streaming server in 15 minutes [Vietnam Mobile Day 2013] - How to build video streaming server in 15 minutes Presentation Transcript

  • Nguy n Kim Tuy nễ ếCTO – MC CORPM: +84912610253|| E: tuyennk@mc-corp.vnW: http://mc-corp.vnSkype: nktuyenHOW TO BUILD VIDEOSTREAMING SERVER IN 15MINS
  • 1STMINUTEAudience & Goal
  • Audience & Goal• Audience object:– Student– Mobile developer– Network administrator– Newbie– ….=> For everyone who care about Video StreamingServer
  • GOAL• Provide a free solution.• Quick & easy – even for newbie• Security & efficiency=> You can build your-own video streaming server foryour business without pay money.
  • 2NDMINUTEMobile Video Formats
  • Mobile Video formats• Mobile TV and mobile video come in only a fewformats, and all are compressed:– 3GPP (3rd Generation Partnership Project)– MPEG-4 (Motion Picture Experts Group)– Flash Lite– RTSP (Real Time Streaming Protocol)
  • Let’s pick up a video format• 3GPP or MP4?– Both MP4 and 3GP are lossy formats which sacrificesquality for file size.– MP4 was created by Apple as a container for QuickTimewhile 3GP is for mobiles with less resources(light weight andvideos lower in quality).– MP4 is the industry standard with better quality and hasmore widespread support than 3GP– MP4 stores everything at higher bit rate than that of 3gp
  • I choose MP4, why?
  • 3RDMINUTETechnique selection
  • LAMP, why not?• L (Linux)– Security, uptimes, stability, total free , Worldwidecommunity …• A (Apache)– The most popular HTTP server , 63.7% of all active websitesin the world(12/2012 ).• M (MySQL)– Scalability, Flexibility, High Performance, Strong DataProtection, free …• P (Php)– Easy to Learn, variety support …
  • H264 Streaming Module• is a plugin for your existing Apache/Lighttpd/Nginxwebserver• Main features:– Time shifting seek– Virtual video clips– Network efficiency– Encoding
  • H264 Streaming Module• Time shifting seek– Enable your viewers to immediately jump to any part ofthe video regardless of the length of the video or whetherit has all been downloaded yet.– http://www.example.com/video.mp4?start=8
  • H264 Streaming Module• Virtual video clips– So, lets replace the preview URL ​http://www.example.com/video.mp4?start=15&end=45by 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=45RewriteRule ^(.*)/preview?(start=.*)$ $1?$2&vbegin=15&vend=45 [L]
  • 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 onaverage 17% CPU on just one of the four cores.
  • H264 Streaming Module• Encoding: MPEG4/H264 industry standard– There is no need to re-encode your MP4 videos, you canuse your existing video files– Using some tool to convert such as:• Ffmpeg• Mencoder & mp4creator• …
  • System Prerequisite• Linux: Centos 5.x or above• Apache: Version 2x– Module: rewrite_url• MySQL: Version 5.x• Php: version > 5.0
  • Install module H264• Download the source of the H264 Streaming Module forApache:• Buildcd ~wget http://h264.code-shop.com/download/apache_mod_h264_streaming-2.2.7.tar.gztar -zxvf apache_mod_h264_streaming-2.2.7.tar.gzcd ~/mod_h264_streaming-2.2.7./configuremakesudo make install
  • Install module H264• Configuration:• Add the lines• Restart Apachesudo vi /etc/httpd/conf/httpd.confLoadModule h264_streaming_module /usr/lib/httpd/modules/mod_h264_streaming.soAddHandler h264-streaming.extensions .mp4sudo /etc/init.d/httpd restart
  • Install module H264• Some error may occurs:– Missing gcc-c++, gcc …– Missing httpd devel libyum groupinstall “Development tools”yum install httpd-devel
  • CONFIGURATION• Configuration:• Add the lines• Restart Apachesudo vi /etc/httpd/conf/httpd.confLoadModule h264_streaming_module /usr/lib/httpd/modules/mod_h264_streaming.soAddHandler h264-streaming.extensions .mp4sudo /etc/init.d/httpd restart
  • CONFIGURATION• Check if module is loaded or not:– The output will be something likeapachectl -t -D DUMP_MODULESLoaded Modules:dir_module (static)actions_module (static)userdir_module (static)rewrite_module (static)h264_streaming_module (shared)…..
  • Test new module• Using mobile browser to check seek function:– http://www.example.com/video.mp4?start=8
  • Test new module• Using flash player: jwplayer:jwplayer(myElement).seek(8);<div id="myElement">Loading the player ...</div><script type="text/javascript">jwplayer("myElement").setup({file: "http://www.example.com/video.mp4",height: 360,image: "/uploads/example.jpg",width: 640});</script>
  • VIDEOS MANAGEMENT• FTP:– Client tool:• FileZilla• SmartFTP• …– FTP Server:• Use default SFTP• Pure-FTP• VSFTP• ….
  • VIDEOS MANAGEMENT• PHP Uploader:– Ajaxuploader:• http://www.albanx.com/ajaxuploader/– Uploadify:• http://www.uploadify.com/download/– jQuery-File-Upload:• https://github.com/blueimp/jQuery-File-Upload/wiki– ….
  • Encode video in MP4/H264• Ffmpeg/x264• Mencoder & mp4creator• Telestream Episode Engine Pro 5 (Paid product)• Encoding H264 on Tiger (Mac OS)
  • Encode video in MP4/H264• Ffmpeg/x264– Create bash file: vim convert.shinfile = $1tmpfile= $2outfile= $3options="-vcodec libx264 -b 512k -flags +loop+mv4 -cmp 256 -partitions +parti4x4+parti8x8+partp4x4+partp8x8+partb8x8 -me_method hex -subq 7 -trellis 1 -refs 5 -bf 3 -flags2 +bpyramid+wpred+mixed_refs+dct8x8 -coder 1 -me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -qmin 10-qmax 51 -qdiff 4"ffmpeg -y -i "$infile" -an -pass 1 -threads 2 $options "$tmpfile"ffmpeg -y -i "$infile" -acodec libfaac -ar 44100 -ab 96k -pass 2 -threads 2 $options"$tmpfile"qt-faststart "$tmpfile" "$outfile"
  • Encode video in MP4/H264• Ffmpeg/x264– Chmod:– Execute:– For more detail, please refer below link for detail:• http://ffmpeg.org/trac/ffmpeg/wiki/x264EncodingGuidechmod +x convert.sh./convert.sh "video_in.avi“ "video_tmp.mp4“ “video_out.mp4"
  • TEST NEW ENCODED VIDEO• Copy new encoded video to your web documentdirectory• Test seek function:cp newvideo.mp4 path_to_web_document_dirhttp://www.example.com/newvideo.mp4?start=8
  • 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 ApacheAuthentication 
  • PRIVATE/PAID VIDEOS, HOW?• There are three types of modules involved in theauthentication and authorization process:– Authentication type• mod_auth_basic• mod_auth_digest – Authentication provider• mod_authn_anon• mod_authnz_ldap• …– Authorization• mod_authnz_ldap• …
  • 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 filehtpasswd -c /usr/local/htaccess_auth testvim private/ .htaccessAuthType BasicAuthName "Restricted Files“AuthBasicProvider fileAuthUserFile /usr/local/htaccess_authRequire user test
  • AUTHENTICATION TESTING• Test with safari in iPhone:• Enter video URL• Example :– http://www.example.com/private/video.mp4
  • AUTHENTICATION TESTING• Let’s check with Test with some others browser/OS
  • MANAGE USERS WITH PHP&MYSQL• Suppose we have table :• Users (userid, username, password)
  • 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 ‘);
  • MANAGE USERS WITH PHP&MYSQL• Function rand_salt_crypt()• For more detail about Htpasswd Formats, please refer tohttp://www.askapache.com/online-tools/htpasswd-generator/
  • MANAGE USERS WITH PHP&MYSQL• Function put_htpasswd()
  • MANAGE USERS WITH PHP&MYSQL• Update .htaccess file
  • System SummaryAdminUpload/convert videosAuthentication certificate MoviesPHPMySQL ServerApache steaming server
  • THANK YOUNguy n Kim Tuy nễ ếCTO – MC CORPM: +84912610253|| E: tuyennk@mc-corp.vnW: http://mc-corp.vnSkype: nktuyen