Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

53
Nguyễn Kim Tuyến CTO – MC CORP M: +84912610253|| E: [email protected] W: http://mc-corp.vn Skype: nktuyen HOW TO BUILD VIDEO STREAMING SERVER IN 15 MINS

description

Bài chia sẻ của anh Nguyễn Kim Tuyến, CTO Minh Châu Corp tại hội thảo Vietnam Mobile Day 2013 tổ chức tại TP.Hồ Chí Minh vào ngày 18/05/2013.

Transcript of Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

Page 1: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

Nguyễn Kim Tuyến

CTO – MC CORP

M: +84912610253|| E: [email protected]

W: http://mc-corp.vn

Skype: nktuyen

HOW TO BUILD VIDEO STREAMING SERVER

IN 15 MINS

Page 2: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

1ST MINUTEAudience & Goal

Page 3: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

Audience & Goal• Audience object:

– Student– Mobile developer– Network administrator– Newbie– ….

=> For everyone who care about Video Streaming Server

Page 4: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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.

Page 5: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

2ND MINUTEMobile Video Formats

Page 6: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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)

Page 7: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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

Page 8: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

I choose MP4, why?

Page 9: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

3RD MINUTETechnique selection

Page 10: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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 …

Page 11: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

4TH MINUTEH264 MODULE INTRODUCTION

Page 12: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

H264 Streaming Module• is a plugin for your existing

Apache/Lighttpd/Nginx webserver• Main features:

– Time shifting seek– Virtual video clips– Network efficiency– Encoding

Page 13: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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

Page 14: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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]

Page 15: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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.

Page 16: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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• …

Page 17: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

5TH MINUTEINSTALLATION

Page 18: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

System Prerequisite• Linux: Centos 5.x or above• Apache: Version 2x

– Module: rewrite_url

• MySQL: Version 5.x• Php: version > 5.0

Page 19: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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

Page 20: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

Install module H264• Configuration:

• Add the lines

• Restart Apache

sudo vi /etc/httpd/conf/httpd.conf

LoadModule h264_streaming_module /usr/lib/httpd/modules/mod_h264_streaming.soAddHandler h264-streaming.extensions .mp4

sudo /etc/init.d/httpd restart

Page 21: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

Install module H264• Some error may occurs:

– Missing gcc-c++, gcc …

– Missing httpd devel lib

yum groupinstall “Development tools”

yum install httpd-devel

Page 22: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

6TH MINUTECONFIGURATION

Page 23: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

CONFIGURATION• Configuration:

• Add the lines

• Restart Apache

sudo vi /etc/httpd/conf/httpd.conf

LoadModule h264_streaming_module /usr/lib/httpd/modules/mod_h264_streaming.soAddHandler h264-streaming.extensions .mp4

sudo /etc/init.d/httpd restart

Page 24: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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)…..

Page 25: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

7TH MINUTETESTING

Page 26: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

Test new module• Using mobile browser to check seek function:

– http://www.example.com/video.mp4?start=8

Page 27: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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>

Page 28: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

8TH MINUTEVIDEOS MANAGEMENT

Page 29: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

VIDEOS MANAGEMENT• FTP:

– Client tool: • FileZilla• SmartFTP• …

– FTP Server:• Use default SFTP• Pure-FTP• VSFTP• ….

Page 30: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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

– ….

Page 31: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

9TH MINUTEVIDEO ENCODING

Page 32: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

Encode video in MP4/H264• Ffmpeg/x264• Mencoder & mp4creator• Telestream Episode Engine Pro 5 (Paid product)• Encoding H264 on Tiger (Mac OS)

Page 33: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

Encode video in MP4/H264• Ffmpeg/x264

– Create bash file: vim convert.sh

infile = $1 tmpfile= $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"

Page 34: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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"

Page 35: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

10TH MINUTETEST NEW ENCODED VIDEO

Page 36: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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

Page 37: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

11 ST MINUTEPRIVATE/PAID VIDEOS

Page 38: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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 

Page 39: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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• …

Page 40: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

12 ND MINUTEUSERS AUTHENTICATION

Page 41: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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 BasicAuthName "Restricted Files“AuthBasicProvider fileAuthUserFile /usr/local/htaccess_authRequire user test

Page 42: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

13 RD MINUTETESTING

Page 43: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

AUTHENTICATION TESTING• Test with safari in iPhone:• Enter video URL

• Example :– http://www.example.com/private/video.mp4

Page 44: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

AUTHENTICATION TESTING• Let’s check with Test with some others browser/OS

Page 45: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

14 TH MINUTEMANAGE USERS

Page 46: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

MANAGE USERS WITH PHP&MYSQL• Suppose we have table :

• Users (userid, username, password)

Page 47: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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 ‘);

Page 48: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

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/

Page 49: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

MANAGE USERS WITH PHP&MYSQL• Function put_htpasswd()

Page 50: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

MANAGE USERS WITH PHP&MYSQL• Update .htaccess file

Page 51: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

15 TH MINUTETIME FOR TEA & REVIEWING

Page 52: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

System Summary

Admin

Upload/convert videos

Authentication certificate Movies

PHP

MySQL Server

Apache steaming server

Page 53: Vietnam Mobile Day 2013: How to build video streaming server in 15 mins

THANK YOU

Nguyễn Kim TuyếnCTO – MC CORP

M: +84912610253|| E: [email protected]

W: http://mc-corp.vn

Skype: nktuyen