2017年1月

基于nginx-rtmp实现电视回看业务

软硬件环境

  • ubuntu 16.04 64bit
  • Android Studio 2.2.3
  • OTT BOx with android 5.1.1
  • nginx 1.11.3
  • nginx-rtmp-module

简介

电视回看是数字电视领域的另一项基础功能,它能提供一定时长的电视回放,可以实现像点播业务中的暂停,快进,快退等功能,一般的运营商都会提供回看的服务.本文讲述如何基于nginx-rtmp来实现电视回看.

原理

电视回看的核心是节目的录制.你可以选择与直播同步录制或者指定时间段录制,录制结束后生成节目资源的播放url,客户端拿到url进行播放,播放的过程和点播一样.

实践

nginx-rtmp配置文件

先来看看配置文件nginx.conf

#user  nobody;
worker_processes  auto;

rtmp_auto_push on;

error_log  logs/error.log debug;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

events {
    worker_connections  1024;
}

rtmp {

    server {

        listen 1935;

        chunk_size 4000;
        
        # HLS
        application hls {
            live on;
            hls on;
            hls_path /opt/www/live;
            hls_continuous on;
            hls_sync 100ms;
            hls_nested on;
            wait_key on;
            hls_playlist_length 5m;
            hls_cleanup on;
            hls_fragment 10s;

            # hls_keys on;
            # hls_key_path /opt/www/keys;
            # hls_key_url http://10.10.10.79:8081/keys;
            # hls_fragments_per_key 10;

            recorder all {
                record all manual;
                record_suffix -%Y-%m-%d-%H_%M_%S.flv;
                record_unique on;
                record_path /opt/www/record;
            }
        }
    }
}

# http
http {

    server {

        listen      8081;

        location / {
            root /opt/www/;
        }

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            root /home/djstava/Workshop/Web/nginx-rtmp-module/;
        }

        location /control {  
            rtmp_control all;  
        }

        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /opt/www/;
            add_header Cache-Control no-cache;
        }

        location /dash {
            root /opt/www/;
            add_header Cache-Control no-cache;
        }
    }
}

主要关注下rtmp标签下的recorder和http标签下的control,录制的节目保存在/opt/www/record,/opt/www是nginx的根目录,便于http播放.

直播模拟

利用ffmpeg推送本地文件到服务器模拟直播

ffmpeg -re -i test.mp4 -vcodec copy -acodec copy -y -f flv rtmp://10.10.10.52/hls/live

节目录制

开始录制

curl "http://10.10.10.52:8081/control/record/start?app=hls&name=live&rec=all"

结束录制

curl "http://10.10.10.52:8081/control/record/stop?app=hls&name=live&rec=all"

录制完成后会在/opt/www/record目录下生成相应的flv文件

nginx_rtmp_record

客户端播放

播放需要的是url,把录制的文件名和主机nginx访问目录拼接一下,很简单,这里的话就是http://10.10.10.52:8081/record/-1483066808-2016-12-30-11_00_08.flv

参考文献

  • https://github.com/arut/nginx-rtmp-module/wiki/Control-module
  • http://www.xugaoxiang.com/2016/11/08/android-rtmp%E7%9B%B4%E6%92%AD%E7%BB%AD%E4%BA%8C/
  • http://www.xugaoxiang.com/2016/11/03/android-rtmp%E7%9B%B4%E6%92%AD%E7%BB%AD/
  • http://www.xugaoxiang.com/2016/08/20/android-rtmp%E7%9B%B4%E6%92%AD/

HLS直播点播加密

软硬件环境

  • ubuntu 16.04
  • Android Studio 2.2.3
  • OTT BOx with android 5.1.1
  • nginx 1.11.3
  • nginx-rtmp-module
  • VLC

前言

在基于HLS的视频直播/点播应用中,为了保护自己的数字内容,防止被下载/拷贝/传播,需要给视频进行加密.本文完成服务器端加密及Android端的解密播放.

nginx.conf

这里我把我使用的nginx.conf文件完整的列了出来,方便大家使用

#user  nobody;
worker_processes  auto;

rtmp_auto_push on;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

rtmp {

    server {

        listen 1935;

        chunk_size 4000;
        
        # exec ffmpeg -re -i udp://@224.0.0.1:4321 -vcodec libx264 -acodec aac -strict -2 -s 1280x720 -f flv rtmp://localhost/hls/livestream;

        # 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 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.
        }

        application small {
            live on;
            # Video with reduced resolution comes here from ffmpeg
        }

        #application webcam {
        #    live on;

            # Stream from local webcam
        #    exec_static ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an
                               #-f flv rtmp://localhost:1935/webcam/mystream;
        #}

#        application mypush {
#            live on;

            # Every stream published here
            # is automatically pushed to
            # these two 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;
#        }

#        application mystaticpull {
#            live on;

            # Static pull is started at nginx start
            #pull rtmp://rtmp4.example.com pageUrl=www.example.com/index.html name=mystream static;
#        }

        # video on demand
#        application vod {
#            play /opt/www/vod;
#        }

#        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
            # from this 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 are sent
            # with GET-style syntax to play & publish
            # these are also included.
            # Example URL:
            #   rtmp://localhost/myapp/mystream?a=b&c=d

            # record 10 video keyframes (no audio) every 2 minutes
          #  record keyframes;
          #  record_path /tmp/vc;
          #  record_max_frames 10;
          #  record_interval 2m;

            # Async notify about an flv recorded
          #  on_record_done http://localhost:8080/record_done;

        #}


        # HLS

        # For HLS to work please create a directory in tmpfs (/tmp/hls 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 /opt/www/live;
            hls_continuous on;
            hls_sync 100ms;
            hls_nested on;
            hls_playlist_length 5m;
            hls_fragment 10s;

            hls_variant _low BANDWIDTH=800000;
            hls_variant _mid BANDWIDTH=1200000;
            hls_variant _hi  BANDWIDTH=2000000;

            #hls key
            hls_keys on;
            hls_key_path /opt/www/keys;
            hls_key_url http://10.10.10.79:8081/keys/;
            hls_fragments_per_key 10;

            #exec /home/djstava/Workshop/Web/nginx-1.11.3/build/test.sh;
            #exec_kill_signal term;

            #recorder all {
            #    record all;
            #    record_suffix -%Y-%m-%d-%H_%M_%S.flv;
            #    record_max_size 6200000K;
            #    record_path /opt/www/record;
            #}
        }

    #application Upload {
    #    play /opt/www/record;
    #}

        # MPEG-DASH is similar to HLS

        #application dash {
        #    live on;
        #    dash on;
        #    dash_path /tmp/dash;
        #}
    }
}

# HTTP can be used for accessing RTMP stats
http {

    server {

        listen      8081;

        location / {
            root /opt/www/;
        }

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;

            # Use this 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 /home/djstava/Workshop/Web/nginx-rtmp-module/;
        }

        location /control {  
            rtmp_control all;  
        }

        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /opt/www/;
            add_header Cache-Control no-cache;
        }

        location /dash {
            # Serve DASH fragments
            root /opt/www/;
            add_header Cache-Control no-cache;
        }
    }
}

主要是关注rtmp中的hls application

hls_keys on;
hls_key_path /opt/www/keys;
hls_key_url http://10.10.10.79:8081/keys/;
hls_fragments_per_key 10;

各个标签的含义如下

hls_keys : 是否启用加密

hls_key_path : 产生的key文件存放路径

hls_key_path : key文件的url,方便网络访问

hls_fragments_per_key : 共用同一个key的视频切片数

推流

以本地视频文件为例,nginx服务器ip为10.10.10.79

ffmpeg -re -i /opt/www/xjcy.mp4 -vcodec copy -acodec copy -f flv -y rtmp://10.10.10.79/hls/test

查看生成的key文件

nginx_ffmpeg_key

再注意看下生成的m3u8文件,默认加密方式是128位的AES

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:357
#EXT-X-TARGETDURATION:19
#EXT-X-KEY:METHOD=AES-128,URI="http://10.10.10.79:8081/keys/test/354.key",IV=0x00000000000000000000000000000162
#EXTINF:11.679,
357.ts
#EXTINF:15.348,
358.ts
#EXTINF:19.395,
359.ts
#EXTINF:10.010,
360.ts
#EXTINF:10.010,
361.ts
#EXTINF:13.138,
362.ts
#EXTINF:13.305,
363.ts
#EXT-X-KEY:METHOD=AES-128,URI="http://10.10.10.79:8081/keys/test/364.key",IV=0x0000000000000000000000000000016C
#EXTINF:18.727,
364.ts
#EXTINF:16.350,
365.ts
#EXTINF:10.010,
366.ts
#EXTINF:10.010,
367.ts
#EXTINF:10.010,
368.ts
#EXTINF:17.225,
369.ts
#EXTINF:17.476,
370.ts
#EXTINF:15.223,
371.ts
#EXTINF:10.469,
372.ts
#EXTINF:10.010,
373.ts
#EXT-X-KEY:METHOD=AES-128,URI="http://10.10.10.79:8081/keys/test/374.key",IV=0x00000000000000000000000000000176

Android客户端

基于vitamio 5.0.2版本的android播放器,可以直接播放http://10.10.10.79:8081/live/test/index.m3u8这个链接.我把整个工程打了个包,提供给需要的朋友.下载地址: https://pan.baidu.com/s/1hsuSovy

参考文献

  • https://github.com/arut/nginx-rtmp-module/wiki/Directives
  • http://www.xugaoxiang.com/2016/08/20/android-rtmp%E7%9B%B4%E6%92%AD/
  • http://www.xugaoxiang.com/2016/11/03/android-rtmp%E7%9B%B4%E6%92%AD%E7%BB%AD/
  • http://www.xugaoxiang.com/2016/11/08/android-rtmp%E7%9B%B4%E6%92%AD%E7%BB%AD%E4%BA%8C/

Android RTMP直播(续二)

软硬件环境

  • ubuntu 16.04
  • Android Studio 2.1.3
  • OTT BOx with android 5.1.1
  • nginx 1.11.3
  • nginx-rtmp-module
  • VLC

前言

Android RTMP直播(续)介绍了HLS协议相关的基础内容,本文将继续深入学习HLS的其它高级特性.

服务端多码率支持

nginx.conf
#user  nobody;
worker_processes  auto;

rtmp_auto_push on;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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 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 ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec flv -acodec copy -s 32x32
                        #-f flv rtmp://localhost:1935/small/${name};
        #}

        #application small {
        #    live on;
        #    # Video with reduced resolution comes here from ffmpeg
        #}

        #application webcam {
        #    live on;

            # Stream from local webcam
        #    exec_static ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an
                               #-f flv rtmp://localhost:1935/webcam/mystream;
        #}

#        application mypush {
#            live on;

            # Every stream published here
            # is automatically pushed to
            # these two 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;
#        }

#        application mystaticpull {
#            live on;

            # Static pull is started at nginx start
            #pull rtmp://rtmp4.example.com pageUrl=www.example.com/index.html name=mystream static;
#        }

        # video on demand
#        application vod {
#            play /opt/www/vod;
#        }

#        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
            # from this 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 are sent
            # with GET-style syntax to play & publish
            # these are also included.
            # Example URL:
            #   rtmp://localhost/myapp/mystream?a=b&c=d

            # record 10 video keyframes (no audio) every 2 minutes
          #  record keyframes;
          #  record_path /tmp/vc;
          #  record_max_frames 10;
          #  record_interval 2m;

            # Async notify about an flv recorded
          #  on_record_done http://localhost:8080/record_done;

        #}


        # HLS

        # For HLS to work please create a directory in tmpfs (/tmp/hls 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 /opt/www/live;
            hls_nested on;

            hls_variant _low BANDWIDTH=800000;
            hls_variant _mid BANDWIDTH=1200000;
            hls_variant _hi  BANDWIDTH=2000000;
        }

        # MPEG-DASH is similar to HLS

        #application dash {
        #    live on;
        #    dash on;
        #    dash_path /tmp/dash;
        #}
    }
}

# HTTP can be used for accessing RTMP stats
http {

    server {

        listen      8081;

        location / {
            root /opt/www/;
        }

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;

            # Use this 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 /home/djstava/Workshop/Web/nginx-rtmp-module/;
        }

        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /opt/www/;
            add_header Cache-Control no-cache;
        }

        #location /dash {
            # Serve DASH fragments
        #    root /tmp;
        #    add_header Cache-Control no-cache;
        #}
    }
}

主要看看application hls的内容

application hls {
            live on;
            hls on;
            hls_path /opt/www/live;
            hls_nested on;

            hls_variant _low BANDWIDTH=800000;
            hls_variant _mid BANDWIDTH=1200000;
            hls_variant _hi  BANDWIDTH=2000000;
        }

这里设定当带宽分别为800k 1200k 2000k的时候,终端都播放相对应的m3u8索引文件

ffmpeg推流

这里需要利用ffmpeg推送3路不同的流,对应上面提到的低 中 高

ffmpeg -re -i ~/Videos/xjcy.mp4 -vcodec copy -acodec copy -b:v 800k -b:a 32k -f flv rtmp://10.10.10.59/hls/livestream_low
ffmpeg -re -i ~/Videos/xjcy.mp4 -vcodec copy -acodec copy -b:v 1200k -b:a 64k -f flv rtmp://10.10.10.59/hls/livestream_mid
ffmpeg -re -i ~/Videos/xjcy.mp4 -vcodec copy -acodec copy -b:v 2000k -b:a 128k -f flv rtmp://10.10.10.59/hls/livestream_hi

推送开始后,hls的root目录下就会生成相应的文件内容,如下图所示

nginx_ffmpeg_variant

此时livestream.m3u8文件内容为

#EXTM3U
#EXT-X-VERSION:3
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=800000
livestream_low/index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1200000
livestream_mid/index.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2000000
livestream_hi/index.m3u8

相应的,播放链接为 http://10.10.10.59:8081/live/livestream.m3u8,播放器需要做的就是根据自身的网络状况,切换到其它的索引文件.

直播节目的录制

直播进行的同时一般都会有本地录制的需求,nginx-rtmp-module提供了这个功能,接下来实践一下.还是看nginx.conf配置文件

application hls {
            live on;
            hls on;
            hls_path /opt/www/live;
            hls_nested on;

            hls_variant _low BANDWIDTH=800000;
            hls_variant _mid BANDWIDTH=1200000;
            hls_variant _hi  BANDWIDTH=2000000;

            recorder all {
                record all;
                record_suffix -%Y-%m-%d-%H_%M_%S.flv;
                record_max_size 200000K;
                record_path /opt/www/record;
            }
        }

record all录制所有内容,也可以只录音频或者视频.

推流后/opt/www/record路径下就会自动生成带对应时间戳的flv文件,用vlc测试播放OK.

nginx_ffmpeg_record

时移电视

要想实现时移电视(这里指的是服务器端)的话,首先需要在服务器上保留足够的切片文件,比如说你提供1小时的时移,就意味着要有1小时的切片文件,而且索引文件中包含前1小时的切片序列.

application hls {
            live on;
            hls on;
            hls_path /opt/www/live;
            hls_continuous on;
            hls_sync 100ms;
            hls_nested on;
            hls_playlist_length 5m;
            hls_fragment 10s;

            hls_variant _low BANDWIDTH=800000;
            hls_variant _mid BANDWIDTH=1200000;
            hls_variant _hi  BANDWIDTH=2000000;

            #exec /home/djstava/Workshop/Web/nginx-1.11.3/build/test.sh;
            #exec_kill_signal term;

            #recorder all {
            #    record all;
            #    record_suffix -%Y-%m-%d-%H_%M_%S.flv;
            #    record_max_size 6200000K;
            #    record_path /opt/www/record;
            #}
        }

hls_fragment指的是切片文件的长度,这里是10秒,hls_playlist_length指的是索引文件的长度,我这里设的是5分钟.推流开始后,你到切片生成的目录,会发现*.m3u8文件包含了30个ts序列.所以,在上面这种情况下,就只能进行5分钟的时移,当播放进度到达当前直播点时则继续回到直播状态.

执行外部shell脚本

比如有个脚本test.sh,内容如下

#!/bin/bash

on_die ()
{
    # kill all children
    pkill -KILL -P $$
}

trap 'on_die' TERM
ffmpeg -re -i /home/djstava/Videos/ygdx.mp4  -vcodec copy -acodec copy -f flv rtmp://10.10.10.48/hls/ygdx &
wait

我这里把它放在hls application中执行,则nginx.conf应如下

application hls {
            live on;
            hls on;
            hls_path /opt/www/live;
            hls_continuous on;
            hls_sync 100ms;
            hls_nested on;
            hls_playlist_length 5m;
            hls_fragment 10s;

            hls_variant _low BANDWIDTH=800000;
            hls_variant _mid BANDWIDTH=1200000;
            hls_variant _hi  BANDWIDTH=2000000;

            exec /home/djstava/Workshop/Web/nginx-1.11.3/build/test.sh;
            exec_kill_signal term;

            #recorder all {
            #    record all;
            #    record_suffix -%Y-%m-%d-%H_%M_%S.flv;
            #    record_max_size 6200000K;
            #    record_path /opt/www/record;
            #}
        }

当hls服务正常启动时(如上面写过的ffmpeg推流动作),外部脚本test.sh也被执行了.脚本中捕捉了退出的中断信号,也就说,如果ffmpeg推流动作中断了,那么test.sh脚本也就不再执行了.

制作RAMDISK

为了提高HLS的读写效率,可以把切片和索引文件操作放在内存中进行.

mount -t tmpfs -o size=512m tmpfs /opt/www/live

参考文献

1 https://github.com/arut/nginx-rtmp-module/wiki/Directives

Android RTMP直播(续)

软硬件环境

  • ubuntu 16.04
  • Android Studio 2.1.3
  • OTT BOx with android 5.1.1
  • nginx 1.11.3
  • nginx-rtmp-module
  • VLC

前言

之前的一篇博文http://www.xugaoxiang.com/2016/08/20/android-rtmp%E7%9B%B4%E6%92%AD/已经简单的介绍了如何利用nginx、nginx-rtmp-module和ffmpeg实现基于rtmp协议的直播.今天这篇继续直播这个话题,聊聊hls的应用.

HLS

HLS(Http Live Streaming)是由Apple公司定义的用于实时流传输的协议,HLS基于HTTP协议实现,传输内容包括两部分,一是M3U8描述文件,二是TS媒体文件。

m3u8文件
#EXTM3U
#EXT-X-VERSION:3
#EXT-X-MEDIA-SEQUENCE:6119
#EXT-X-TARGETDURATION:14
#EXTINF:10.625,
6119.ts
#EXTINF:13.667,
6120.ts
#EXTINF:10.000,
6121.ts

如上,m3u8文件是一个描述文件,必须以#EXTM3U开头,之后是切片TS文件的序列.对于直播来讲,m3u8文件需要进行实时的更新,只保留若干个TS切片序列,防止本地存储撑爆硬盘.

多码率支持

针对应用网络多变及不稳定的情况,多数直播都会提供多码率支持,播放器会根据用户当前的网络状况,自动切换到对应的码率上,大大提升用户体验.在服务器端.为了提供多码率的支持,就需要多级m3u8文件.在主m3u8文件不再有TS序列,而是二级m3u8文件,如下所示

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1280000
low.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=2560000
mid.m3u8
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=7680000
hi.m3u8

nginx-rtmp对HLS的支持

nginx-rtmp-module本身对rtmp和hls都有很好的支持,只需要在nginx.conf配置下就ok了

#user  nobody;
worker_processes  auto;

rtmp_auto_push on;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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 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 ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec flv -acodec copy -s 32x32
                        #-f flv rtmp://localhost:1935/small/${name};
        #}

        #application small {
        #    live on;
        #    # Video with reduced resolution comes here from ffmpeg
        #}

        #application webcam {
        #    live on;

            # Stream from local webcam
        #    exec_static ffmpeg -f video4linux2 -i /dev/video0 -c:v libx264 -an
                               #-f flv rtmp://localhost:1935/webcam/mystream;
        #}

#        application mypush {
#            live on;

            # Every stream published here
            # is automatically pushed to
            # these two 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;
#        }

#        application mystaticpull {
#            live on;

            # Static pull is started at nginx start
            #pull rtmp://rtmp4.example.com pageUrl=www.example.com/index.html name=mystream static;
#        }

        # video on demand
#        application vod {
#            play /opt/www/vod;
#        }

#        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
            # from this 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 are sent
            # with GET-style syntax to play & publish
            # these are also included.
            # Example URL:
            #   rtmp://localhost/myapp/mystream?a=b&c=d

            # record 10 video keyframes (no audio) every 2 minutes
          #  record keyframes;
          #  record_path /tmp/vc;
          #  record_max_frames 10;
          #  record_interval 2m;

            # Async notify about an flv recorded
          #  on_record_done http://localhost:8080/record_done;

        #}


        # HLS

        # For HLS to work please create a directory in tmpfs (/tmp/hls 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 /opt/www/live;
        }

        # MPEG-DASH is similar to HLS

        #application dash {
        #    live on;
        #    dash on;
        #    dash_path /tmp/dash;
        #}
    }
}

# HTTP can be used for accessing RTMP stats
http {

    server {

        listen      8081;

        location / {
            root /opt/www/;
        }

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;

            # Use this 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 /home/djstava/Workshop/Web/nginx-rtmp-module/;
        }

        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }

            root /opt/www/;
            add_header Cache-Control no-cache;
        }

        #location /dash {
            # Serve DASH fragments
        #    root /tmp;
        #    add_header Cache-Control no-cache;
        #}
    }
}

在rtmp标签下,指定hls application的根路径/opt/www/live,所有的TS切片文件都存放在这里

ffmpeg推流

推送本地文件
ffmpeg -re -i /opt/www/vod/dhxy1.mp4 -vcodec copy -acodec copy -f flv -y rtmp://192.168.1.88/hls/livestream1

推送成功后,你可以通过如下2个url播放对应的模拟实时流,请确保nginx服务已启动.

rtmp://192.168.1.88/hls/livestream1
http://192.168.1.88:8081/live/livestream1.m3u8

另外http://192.168.1.88:8081/stat页面可以显示当前服务的一些信息,如接入的客户端数量,音频 视频的信息等等,见下图

nginx_stat

推送UDP组播数据
ffmpeg -i udp://@224.0.0.2:9000 -vcodec libx264 -acodec aac -strict -2 -f flv -s 1280x720 -q 10 -ac 1 -ar 44100 rtmp://192.168.1.88/hls/livestream

nginx_udp

在以UDP数据为输入源时,ffmpeg会报如下图中的错误信息

nginx_udp_error

这时只需要重新修改下ffmpeg的推流命令就可以,如下

ffmpeg -i 'udp://@224.0.0.2:9000?fifo_size=2000000&overrun_nonfatal=1' -vcodec libx264 -acodec aac -strict -2 -f flv -s 1280x720 -q 10 -ac 1 -ar 44100 rtmp://192.168.1.88/hls/livestream

fifo_size的单位是字节,自己酌情增减.

参考文献

1 https://developer.apple.com/streaming/

2 https://github.com/arut/nginx-rtmp-module

Android RTMP直播

软硬件环境

  • ubuntu 16.04
  • Android Studio 2.1.3
  • OTT BOx with android 5.1.1
  • nginx 1.11.3
  • nginx-rtmp-module
  • vitamio

前言

当下,直播已经成为网络热词,它不单单是指传统广播电视的实时播放,更是更为广泛的音视频实时分享的延伸。早先,直播数据源只可能来自于电视台及节目制作中心,但是放眼现在,基于计算机技术的高速发展,任何人都可以独自完成内容的制作,再利用身边的终端设备完成分享,你不仅仅是受众,同样可以成为主角。可以说,时下流行的"网红"文化,直播技术立下了汗马功劳。本文旨在搭建一个最简单的视频直播系统,包括服务器端及android客户端,采用了nginx、nginx-rtmp、vitamio及ffmpeg。

rtmp协议

RTMP是Real Time Messaging Protocol的缩写,是被设计用来进行实时数据通信的网络协议。它是一个协议族,包括rtmpe、rtmpt、rtmps等,是直播技术中常用的协议

服务端配置

nginx添加rtmp支持

从http://nginx.org/en/download.html下载最新版1.11.3,然后从https://github.com/arut/nginx-rtmp-module下载nginx的rtmp补丁,下载的文件都放在目录/home/djstava(请根据实际情况自行修改)下,重新编译nginx

tar xvf nginx-1.11.3.tar.gz
cd nginx-1.11.3
mkdir build
./configure --prefix=/home/djstava/nginx-1.11.3/build --add-module=/home/djstava/nginx-rtmp-module
make
make install
修改配置文件nginx.conf

编辑/home/djstava/nginx-1.11.3/build/conf/nginx.conf,在文件末尾添加如下内容

rtmp {
    server {
        listen 1935;
        ping 30s;
        notify_method get;

        application myapp {
            live on;

            # sample play/publish handlers
            #on_play http://localhost:8080/on_play;
            #on_publish http://localhost:8080/on_publish;

            # sample recorder
            #recorder rec1 {
            #    record all;
            #    record_interval 30s;
            #    record_path /tmp;
            #    record_unique on;
            #}

            # sample HLS
            #hls on;
            #hls_path /tmp/hls;
            #hls_sync 100ms;
        }

        # Video on demand
        #application vod {
        #    play /var/Videos;
        #}

        # Video on demand over HTTP
        #application vod_http {
        #    play http://localhost:8080/vod/;
        #}
    }
}
启动nginx服务
/home/djstava/nginx-1.11.3/build/sbin/nginx
ffmpeg推送rtmp

找一个本地的视频文件进行推送,命令为

ffmpeg -re -i 大话西游之月光宝盒.BD1280超清国粤双语中英双字.mp4 -c copy -f flv rtmp://localhost/myapp/mystream
ffplay播放测试

如果没有安装ffplay的话,也可以用vlc

ffplay rtsmp://localhost/myapp/mystream
后记

前面的步骤都是在本机中进行的。可是在实际应用中,情况会复杂的多,nginx可能是一台服务器,ffmpeg推流的可能就是另一台服务器,这样的话,可将localhost换成对应的IP地址。如果数据源来自摄像头,同样可以通过ffmpeg进行推送,命令如下

ffmpeg -f dshow -i video="Integrated Camera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flv rtmp://10.10.10.84/myapp/mystream1

rtmp_02

Android客户端播放

之前已经写过一个基于vitamio的视频播放器,地址是https://github.com/djstava/DJMediaPlayer,我们就在它的基础上进行修改,找到MainActivity.java

private String[] files = {"rtmp demo","apple demo"};

在listview的item被点击后发送包含播放地址的intent

Intent intent = new Intent(MainActivity.this, VitamioVideoViewActivity.class);
intent.putExtra("movieUrl", "rtmp://10.10.10.84/myapp/mystream");
startActivity(intent);

rtmp_01

参考文献

  • https://ffmpeg.org/download.html
  • https://github.com/yixia/VitamioBundle
  • https://github.com/djstava/DJMediaPlayer
  • http://nginx-rtmp.blogspot.com