vietnam mobile day 2013: how to build video streaming server in 15 mins

Post on 20-Jun-2015

938 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

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

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

1ST MINUTEAudience & Goal

Audience & Goal• Audience object:

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

=> For everyone who care about Video Streaming Server

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.

2ND MINUTEMobile Video Formats

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)

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

I choose MP4, why?

3RD MINUTETechnique selection

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 …

4TH MINUTEH264 MODULE INTRODUCTION

H264 Streaming Module• is a plugin for your existing

Apache/Lighttpd/Nginx webserver• 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 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

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]

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.

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

5TH MINUTEINSTALLATION

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

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

Install module H264• Some error may occurs:

– Missing gcc-c++, gcc …

– Missing httpd devel lib

yum groupinstall “Development tools”

yum install httpd-devel

6TH MINUTECONFIGURATION

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

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

7TH MINUTETESTING

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>

8TH MINUTEVIDEOS MANAGEMENT

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

– ….

9TH MINUTEVIDEO ENCODING

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.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"

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"

10TH MINUTETEST NEW ENCODED VIDEO

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

11 ST MINUTEPRIVATE/PAID VIDEOS

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 

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

12 ND MINUTEUSERS AUTHENTICATION

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

13 RD MINUTETESTING

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

14 TH MINUTEMANAGE USERS

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 to http://www.askapache.com/online-tools/htpasswd-generator/

MANAGE USERS WITH PHP&MYSQL• Function put_htpasswd()

MANAGE USERS WITH PHP&MYSQL• Update .htaccess file

15 TH MINUTETIME FOR TEA & REVIEWING

System Summary

Admin

Upload/convert videos

Authentication certificate Movies

PHP

MySQL Server

Apache steaming server

THANK YOU

Nguyễn Kim TuyếnCTO – MC CORP

M: +84912610253|| E: tuyennk@mc-corp.vn

W: http://mc-corp.vn

Skype: nktuyen

top related