Clone
1
migrate_v5_CN_sample hls cluster
winlin edited this page 2022-07-31 13:16:33 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

HOME > CN > Edge Cluster

HLS Edge Cluster Example

Note: 如果觉得Github的Wiki访问太慢可以访问 Gitee 镜像。

如何创建分发HLS的边缘集群就像CDN一样分发HLS流。

假设服务器的IP是192.168.1.170

第一步获取SRS。详细参考GIT获取代码

git clone https://github.com/ossrs/srs
cd srs/trunk

或者使用git更新已有代码

git pull

第二步编译SRS。详细参考Build

./configure && make

第三步编写SRS源站配置文件生成HLS切片文件。详细参考HLS分发

将以下内容保存为文件,譬如conf/hls.origin.conf,服务器启动时指定该配置文件(srs的conf文件夹有该文件)。

# conf/hls.origin.conf
listen              1935;
max_connections     1000;
daemon              off;
srs_log_tank        console;
http_server {
    enabled         on;
    listen          8080;
}
vhost __defaultVhost__ {
    hls {
        enabled         on;
    }
}

第四步编写NGINX边缘配置文件分发HLS文件。详细参考Nginx for HLS

将以下内容保存为文件,譬如conf/hls.edge.conf,服务器启动时指定该配置文件(srs的conf文件夹有该文件)。

# conf/hls.edge.conf
worker_processes  3;
events {
    worker_connections  10240;
}

http {
    # For Proxy Cache.
    proxy_cache_path  /tmp/nginx-cache levels=1:2 keys_zone=srs_cache:8m max_size=1000m inactive=600m;
    proxy_temp_path /tmp/nginx-cache/tmp; 

    server {
        listen       8081;
        # For Proxy Cache.
        proxy_cache_valid  404      10s;
        proxy_cache_lock on;
        proxy_cache_lock_age 300s;
        proxy_cache_lock_timeout 300s;
        proxy_cache_min_uses 1;

        location ~ /.+/.*\.(m3u8)$ {
            proxy_pass http://127.0.0.1:8080$request_uri;
            # For Proxy Cache.
            proxy_cache srs_cache;
            proxy_cache_key $scheme$proxy_host$uri$args;
            proxy_cache_valid  200 302  10s;
        }
        location ~ /.+/.*\.(ts)$ {
            proxy_pass http://127.0.0.1:8080$request_uri;
            # For Proxy Cache.
            proxy_cache srs_cache;
            proxy_cache_key $scheme$proxy_host$uri;
            proxy_cache_valid  200 302  60m;
        }
    }
}

第五步启动SRS源站和NGINX边缘。

nginx -c $(pwd)/conf/hls.edge.conf
./objs/srs -c conf/edge.conf

Note: 请参考NGINX的说明下载和安装只要是NGINX就可以没有特别的要求。

第六步启动推流编码器推流到SRS生成HLS文件。

使用FFMPEG命令推流

for((;;)); do \
    ./objs/ffmpeg/bin/ffmpeg -re -i ./doc/source.flv \
    -c copy -f flv rtmp://192.168.1.170/live/livestream; \
    sleep 1; \
done

或使用OBS推流

Server: rtmp://192.168.1.170/live
StreamKey: livestream

第七步观看HLS流。

SRS源站的HLS流: http://192.168.1.170:8080/live/livestream.m3u8

NGINX边缘的HLS流: http://192.168.1.170:8081/live/livestream.m3u8

备注请将所有实例的IP地址192.168.1.170都换成部署的服务器IP地址。

第八步压测和添加更多的边缘NGINX。

可以使用srs-bench模拟很多客户端播放HLS流

docker run --rm -it --network=host --name sb ossrs/srs:sb \
  ./objs/sb_hls_load -c 100 -r http://192.168.1.170:8081/live/livestream.m3u8

可以多找几台服务器用同样的配置文件启动NGINX就成了一个边缘集群了。

Winlin 2014.3