1. Why to use HTTP Live streaming
Refer to Wikipedia
HTTP Live Streaming (abbreviated as HLS) is an HTTP-based streaming network Transfer protocol proposed by Apple. is part of Apple's QuickTime X and iphone software system. It works by dividing the entire stream into small HTTP-based files to download, one at a time. When media streaming is playing, clients can choose to download the same resources at different rates from many different alternative sources, allowing streaming media sessions to accommodate different data rates. When you start a streaming session, the client downloads a extended m3u (m3u8) playlist file that contains metadata to find the available media streams.
HLS only requests basic HTTP messages, and unlike real-time Transport Protocol (RTP), HLS can pass through any firewall or proxy server that allows HTTP data to pass through. It is also easy to use a content distribution network to transfer media streams.
2. HTTP Live Streaming Technology Solution
HTTP Service: Use Nginx to provide HTTP service and increase support for HLS by using Nginx-rtmp-module https://github.com/arut/nginx-rtmp-module
Use FFmpeg to complete conversions of FLV, MP4, MP3, and other formats
Use FFmpeg or Segmenter to complete the cutting of video/audio format files, cut to m3u8 format and TS files
3. Preparatory work
Operating system CentOS
3.1. Ready to install remove installed packages
Yum Erase ffmpeg x264 x264-devel
3.2. Install various dependency packages
Yum install gcc make nasm pkgconfig wget Curl curl-devel zlib-devel openssl-devel perl cpio Expat-devel gettext-devel Lib Tool mhash.x86_64 perl-digest-sha1.x86_64 pcre.i386 pcre.x86_64 pcre-devel.i386 pcre-devel.x86_64
3.3. Install git
Wget-o Git-devel.zip Https://github.com/msysgit/git/archive/devel.zip
Unzip Git-devel.zip
CD git-devel/
Autoconf
./configure
Make
Make install
3.4. Create the installation package directory
mkdir ~/ffmpeg-source
4. Installing ffmpeg and its dependent packages
4.1, Yasm
CD ~/ffmpeg-source
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
Tar xzvf yasm-1.2.0.tar.gz
CD yasm-1.2.0
./configure
Make
Make install
4.2, x264
CD ~/ffmpeg-source
git clone git://git.videolan.org/x264
CD x264
./configure–enable-shared
Make
Make install
4.3, LAME
CD ~/ffmpeg-source
wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
Tar xzvf lame-3.99.5.tar.gz
CD lame-3.99.5
./configure–enable-nasm
Make
Make install
4.4, Libogg
CD ~/ffmpeg-source
wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
Tar xzvf libogg-1.3.0.tar.gz
CD libogg-1.3.0
./configure
Make
Make install
4.5, Libvorbis
CD ~/ffmpeg-source
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
Tar xzvf libvorbis-1.3.3.tar.gz
CD libvorbis-1.3.3
./configure
Make
Make install
4.6, LIBVPX
CD ~/ffmpeg-source
git clone http://git.chromium.org/webm/libvpx.git
CD LIBVPX
./configure–enable-shared
Make
Make install
4.7, FAAD2
CD ~/ffmpeg-source
wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
Tar zxvf faad2-2.7.tar.gz
CD faad2-2.7
./configure
Make
Make install
4.8, FAAC
CD ~/ffmpeg-source
wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
Tar zxvf faac-1.28.tar.gz
CD faac-1.28
./configure
Make
Make install
4.9, Xvid
CD ~/ffmpeg-source
wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
Tar zxvf xvidcore-1.3.2.tar.gz
CD Xvidcore/build/generic
./configure
Make
Make install
4.10, FFmpeg
CD ~/ffmpeg-source
git clone git://source.ffmpeg.org/ffmpeg
CD FFmpeg
./configure–enable-version3–enable-libvpx–enable-libfaac–enable-libmp3lame–enable-libvorbis–enable-libx264– Enable-libxvid–enable-shared–enable-gpl–enable-postproc–enable-nonfree–enable-avfilter–enable-pthreads
Make
Make install
Ldconfig–v
5, installation of Nginx and Nginx-rtmp-module
mkdir ~/nginx-source
CD ~/nginx-source
wget http://nginx.org/download/nginx-1.2.4.tar.gz
Tar zxvf nginx-1.2.4.tar.gz
Wget-o Nginx-rtmp-module.zip Https://github.com/arut/nginx-rtmp-module/archive/master.zip
Unzip Nginx-rtmp-module.zip
Wget-o Ngx_cache_purge.zip Https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip
Unzip Ngx_cache_purge.zip
CD nginx-1.2.4
./configure–user=daemon–group=daemon–prefix=/usr/local/nginx-1.2.4/–add-module=. /nginx-rtmp-module-master–add-module=. /nginx-rtmp-module-master/hls–add-module=. /ngx_cache_purge-master–with-http_stub_status_module–with-http_ssl_module–with-http_sub_module–with-md5=/usr/ Lib–with-sha1=/usr/lib–with-http_gzip_static_module
Increase the configuration of the rtmp module in nginx.conf, configuration example
rtmp {
server {
Listen 1935;
Chunk_size 4000;
# TV Mode:one Publisher, many subscribers
Application MyTV {
# Enable Live streaming
Live on;
# Record first 1K of stream
Record all;
Record_path/tmp/av;
Record_max_size 1K;
# Append current timestamp to each FLV
Record_unique on;
# Publish only from localhost
Allow publish 127.0.0.1;
Deny publish all;
#allow play all;
}
# transcoding (FFmpeg needed)
Application Big {
Live on;
# on every pusblished stream run the This command (FFMPEG)
# with substitutions: $app/${app}, $name/${name} for application & stream name.
#
# This ffmpeg call receives stream from this application &
# reduces the resolution down to 32x32. The stream is the published to
# ' small ' application (see below) under the same name.
#
# ffmpeg can do anything with the stream like Video/audio
# transcoding, resizing, altering container/codec params etc
#
# Multiple exec lines can be specified.
Exec/usr/local/bin/ffmpeg-re-i rtmp://localhost:1935/$app/$name-vcodec flv-acodec copy-s 32x32-f flv Rtmp://localho St:1935/small/${name};
}
Application Small {
Live on;
# Video with reduced resolution comes-from ffmpeg
}
Application Mypush {
Live on;
# every stream published here
# is automatically pushed to
# These, machines
#push rtmp1.example.com;
#push rtmp2.example.com:1934;
}
Application Mypull {
Live on;
# Pull all streams from remote machine
# and Play locally
#pull rtmp://rtmp3.example.com pageurl=www.example.com/index.html;
}
# Video on Demand
Application VOD {
Play/var/flvs;
}
Application Vod2 {
play/var/mp4s;
}
# Many publishers, many subscribers
# no checks, no recording
Application VideoChat {
Live on;
# The following notifications receive all
# The session variables as well as
# particular call arguments in HTTP POST
# request
# Make HTTP request & use HTTP Retcode
# to decide whether to allow publishing
# Connection or not
On_publish Http://localhost:8080/publish;
# Same with playing
On_play Http://localhost:8080/play;
# Publish/play End (repeats on disconnect)
On_done Http://localhost:8080/done;
# All above mentioned notifications receive
# Standard Connect () arguments as well as
# play/publish ones. If any arguments is sent
# with Get-style syntax to play & Publish
# These is also included.
# Example URL:
# Rtmp://localhost/myapp/mystream?a=b&c=d
# Record Video keyframes (no audio) every 2 minutes
Record keyframes;
RECORD_PATH/VAR/VC;
Record_max_frames 10;
Record_interval 2m;
# Async notify about an FLV recorded
On_record_done Http://localhost:8080/record_done;
}
# HLS
# HLS requires Libavformat & should be configured as a separate
# NGINX module In addition to Nginx-rtmp-module:
#./configure ... –add-module=/path/to/nginx-rtmp-module/hls ...
# for HLS to work, create a directory in Tmpfs (/tmp/app here)
# for the fragments. The directory contents is served via HTTP (see
# http{} section in config)
#
# Incoming Stream must is in H264/aac/mp3. For iPhones use baseline H264
# profiles (see ffmpeg example).
# This example creates RTMP stream from the movie Ready for HLS:
#
# ffmpeg-loglevel Verbose-re-i Movie.avi-vcodec libx264
#-vprofile Baseline-acodec Libmp3lame-ar 44100-ac 1
#-F FLV Rtmp://localhost:1935/hls/movie
#
# If you need to transcode live stream use ' exec ' feature.
#
Application HLS {
HLS on;
Hls_path/var/app;
Hls_fragment 5s;
}
}
}
# HTTP can be used for accessing RTMP stats
HTTP {
server {
Listen 8080;
# This URL provides RTMP statistics in XML
Location/stat {
Rtmp_stat all;
# stylesheet to view XML as Web page
# in Browser
Rtmp_stat_stylesheet stat.xsl;
}
location/stat.xsl {
# XML stylesheet to view RTMP stats.
# Copy stat.xsl wherever you want
# and put the full directory path here
root/path/to/stat.xsl/;
}
Location/hls {
# Serve HLS Fragments
Alias/var/app;
}
}
}
6, installation Segmenter
SVN Co http://httpsegmenter.googlecode.com/svn/
CD Svn/trunk
Gcc-wall-g Segmenter.c-o Segmenter-lavformat-lavcodec-lavutil-std=c99
CP segmenter/usr/bin/
favorably cool download a video file, assumed to be baluobu.flv
Find a MP3 file, assumed to be 10year.mp3
Mkdir/var/flvs/var/mp4s/var/vc/var/app/var/app/10year/var/app/baluobu
Use FFmpeg to convert test video and audio to MPEG TS format files
Ffmpeg-i/var/flvs/baluobu.flv-f mpegts-acodec libmp3lame-ar 48000-ab 128k-vcodec libx264-b 96k-flags +loop-cmp +chroma-partitions +parti4x4+partp8x8+partb8x8-subq 5-trellis 1-refs 1-coder 0-me_range 16-keyint_min 25-sc_thresh Old 40-i_qfactor 0.71-bt 200k-maxrate 96k-bufsize 96k-rc_eq ' blurcplx^ (1-qcomp) '-qcomp 0.6-qmin 10-qmax 51-qdiff 4-level 30-aspect 320:240-g 30-async 2/var/app/baluobu/baluobu.ts
Ffmpeg-i/var/flvs/10year.mp3-f mpegts-acodec libmp3lame-ar 48000-ab 128k-vcodec libx264-b 96k-flags +loop-cmp + Chroma-partitions +parti4x4+partp8x8+partb8x8-subq 5-trellis 1-refs 1-coder 0-me_range 16-keyint_min 25-sc_thresho LD 40-i_qfactor 0.71-BT 200k-maxrate 96k-bufsize 96k-rc_eq ' blurcplx^ (1-qcomp) '-qcomp 0.6-qmin 10-qmax 51-qdiff 4 -level 30-aspect 320:240-g 30-async 2/var/app/10year/10year.ts
Cd/var/app/10year
Segmenter-i 10year.ts-d 5-o 10year-x 10year.m3u8
Cd/var/app/baluobu
Segmenter-i baluobu.ts-d 5-o buluobu-x baluobu.m3u8
7. Testing
Use VLC testing for simplicity.
Http://192.168.1.11:8080/hls/10year/10year.m3u8
Http://192.168.1.11:8080/hls/baluobu/baluobu.m3u8
If there is no problem, the HTTP Live streaming (HLS) service has been successfully configured correctly
You can use Safari on your iOS device to access the above address
or after installing Vplayer on the Android machine, access the above address
If you need to provide online video in the form of Web pages, you need to add the following MIME type to the Nginx mime.types file:
. m3u8 application/x-mpegurl. TS video/mp2t
Use the video tag in the HTML5 page to include the m3u8 file
<video controls>
<source src=http://192.168.1.11:8080/hls/baluobu/baluobu.m3u8/>
</video>
8. Development instructions
iOS has native support for HTTP Live streaming (HLS), just use MPMoviePlayerController to play the above address
Apple Documentation: HTTP://BIT.LY/RNPSEF
Android 3.x later through the new Nuplayer class to support the HTTP Live streaming, and the function is also weak, in order to simplify processing, you can use
Vitamio http://vov.io/vitamio/or http://code.taobao.org/p/oplayer/src/download
Servestream http://sourceforge.net/projects/servestream/
The nginx-rtmp-module itself also supports the RTMP protocol, which can also be used to build an online live system based on Nginx.
Build an online on-demand system based on HTTP Live streaming (HLS)