天天看點

搭建簡單hls直播測試服務

經過實踐,通過h5 video 直接m3u8直播, ios 是都沒有問題的。android 4.2以上才基本上沒問題。4.2以下的各品牌有些問題需要做不同的相容。

HTTP Live Streaming(縮寫是 HLS)是一個由蘋果公司提出的基于HTTP的流媒體網絡傳輸協定。是蘋果公司QuickTime X和iPhone軟體系統的一部分。它的工作原理是把整個流分成一個個小的基于HTTP的檔案來下載下傳,每次隻下載下傳一些。當媒體流正在播放時,用戶端可以選擇從許多不同的備用源中以不同的速率下載下傳同樣的資源,允許流媒體會話适應不同的資料速率。在開始一個流媒體會話時,用戶端會下載下傳一個包含中繼資料的extended M3U (m3u8) playlist檔案,用于尋找可用的媒體流。

在測試直播各種相容性時, 為了測試友善,可以自己搭建一個直播伺服器來輸入視訊流供測試。

rtmp直播流會被動态切分為ts片段和一個不斷重新整理的u3m8檔案, 這個正是h5直播時的方式,是以我們通過配置nginx 的rtmp 子產品來支援 rtmp 流媒體直播服務。

主要以下幾步, 一些具體的安裝步驟就不細說了:

1. 安裝強大的音視訊轉換工具ffmpeg , 相信你對這個不陌生.

2.安裝nginx 以及 nginx-rtmp-module 子產品(自行搜尋下載下傳)

進入你的nginx源碼目錄, 執行以下指令, 注意, 之前安裝的子產品如果有其他配置需要帶上

./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/nginx-rtmp-module --with-debug
make
make install
           

 3.配置 nginx 以支援rtmp

在nginx.conf配置檔案末尾加上以下配置:

rtmp{
        server{ 
                listen 1935;
                chunk_size      4000;   
                # For HLS to work please 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 be in H264/AAC. For iPhones use baseline H264  
        # profile (see ffmpeg example).  
        # This example creates RTMP stream from 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{
                        live on;
                        hls on; 
                        hls_path        /data/maxwellxwma/html5hls/html/hls;
                        hls_fragment 5s;
                }       
        
        }       
}
           

 這樣配置好後, 我們就可以對目前伺服器生成rtmp 直播流了,執行以下指令會不斷向/data/maxwellxwma/html5hls/html/hls下面寫入ts片段和m3u8檔案:

 如下圖所示:

搭建簡單hls直播測試服務
搭建簡單hls直播測試服務

邊生成邊播放邊删除:

搭建簡單hls直播測試服務

4. 配置vhosts支援外部調用m3u8檔案播放:

server{

        listen 8080;
        server_name html5hls.qq.com;
        error_log /data/maxwellxwma/logs/nginx/hls.qq.com_error.log;

        index index.html index.php;
        root /data/maxwellxwma/html5hls/html;
        location        /hls{   
                #server HLS fragments
                types{  
                        text/html html htm;
                        #application/vnd.apple.mpegurl m3u8;
                        application/x-mpegurl m3u8;
                        video/mp2t ts;
                }       
                root /data/maxwellxwma/html5hls/html;
                index index.html;
                expires -1;
        }       

}
           

 這些配置可以直接加入到nginx.conf裡, 但建立虛拟主機配置,增加可維護性

然後重新開機nginx  ,  直播服務就搭建好了。

用VLC播放器測試一下正常:

搭建簡單hls直播測試服務

5. html5  video 标簽嵌入播放測試:

<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>HLS Player</title>
</head>
<body>
<video id="video" src="http://10.6.224.185:8080/hls/mystream.m3u8" width="100%" heigh="100%" autoplay="autoplay" controls="controls">不支援videos</video>
</body>
</html> 
           

m3u8檔案記錄了待播放的ts清單:

搭建簡單hls直播測試服務

好了, 到此終于可以随心随地随時,随心所欲的測試直播了, 還有那些非常蛋疼的安卓相容 性問題