Skip to content

Commit

Permalink
support hls+ over https, disconnect when nginx -s reload
Browse files Browse the repository at this point in the history
  • Loading branch information
im-pingo committed Apr 17, 2020
1 parent 3a8e3a5 commit b14be2c
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 47 deletions.
2 changes: 1 addition & 1 deletion docker/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ rtmp {
hls_playlist_length 12000ms;
hls_type event;

hls2memory on;
hls2 on;
mpegts_cache_time 20s;

hls2_fragment 1300ms;
Expand Down
6 changes: 6 additions & 0 deletions modules/nginx-rtmp-module/http/ngx_http_flv_live_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ ngx_http_flv_live_write_handler(ngx_http_request_t *r)
ngx_del_timer(wev);
}

if (ngx_rtmp_core_main_conf->fast_reload && (ngx_exiting || ngx_terminate)) {
r->error_page = 1;
ngx_http_finalize_request(r, NGX_HTTP_SERVICE_UNAVAILABLE);
return;
}

if (ngx_rtmp_prepare_merge_frame(s) == NGX_ERROR) {
ngx_http_finalize_request(r, NGX_ERROR);
return;
Expand Down
82 changes: 42 additions & 40 deletions modules/nginx-rtmp-module/mpegts/ngx_hls_http_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ ngx_hls_http_master_m3u8_handler(ngx_http_request_t *r,
ngx_str_t uri_tail;
ngx_buf_t *m3u8;
ngx_chain_t out;
ngx_str_t scheme = ngx_string("");
ngx_str_t http = ngx_string("http");
ngx_str_t https = ngx_string("https");

host = r->headers_in.host->value;

Expand Down Expand Up @@ -319,33 +322,29 @@ ngx_hls_http_master_m3u8_handler(ngx_http_request_t *r,
}
ngx_snprintf(uri.data, uri.len, "%V%V", &location, &uri_tail);
}
#if (NGX_HTTP_SSL)
if (r->connection->ssl) {
m3u8_url.len = ngx_strlen("https://") +
host.len +
uri.len +
NGX_HLS_LIVE_ARG_SESSION_LENGTH + 2 +
ngx_strlen(sstr);

m3u8_url.data = ngx_pcalloc(r->connection->pool, m3u8_url.len);

ngx_snprintf(m3u8_url.data, m3u8_url.len, "https://%V%V?%s=%s",
&host, &uri, NGX_HLS_LIVE_ARG_SESSION, sstr);

} else
ngx_http_arg(r, (u_char*)"scheme", 6, &scheme);
if (scheme.len == 0) {
#if (NGX_HTTP_SSL)
if (r->connection->ssl) {
scheme = https;
} else
#endif
{
m3u8_url.len = ngx_strlen("http://") +
host.len +
uri.len +
NGX_HLS_LIVE_ARG_SESSION_LENGTH + 2 +
ngx_strlen(sstr);
{
scheme = http;
}
}

m3u8_url.data = ngx_pcalloc(r->connection->pool, m3u8_url.len);
m3u8_url.len = scheme.len + ngx_strlen("://") +
host.len +
uri.len +
NGX_HLS_LIVE_ARG_SESSION_LENGTH + 2 +
ngx_strlen(sstr);

ngx_snprintf(m3u8_url.data, m3u8_url.len, "http://%V%V?%s=%s",
&host, &uri, NGX_HLS_LIVE_ARG_SESSION, sstr);
}
m3u8_url.data = ngx_pcalloc(r->connection->pool, m3u8_url.len);

ngx_snprintf(m3u8_url.data, m3u8_url.len, "%V://%V%V?%s=%s",
&scheme, &host, &uri, NGX_HLS_LIVE_ARG_SESSION, sstr);

m3u8 = ngx_create_temp_buf(r->connection->pool, 64 * 1024);
m3u8->memory = 1;
Expand Down Expand Up @@ -386,6 +385,9 @@ ngx_hls_http_redirect_handler(ngx_http_request_t *r,
ngx_str_t location = ngx_string("");
ngx_str_t uri;
ngx_str_t uri_tail;
ngx_str_t scheme = ngx_string("");
ngx_str_t http = ngx_string("http");
ngx_str_t https = ngx_string("https");

host = r->headers_in.host->value;

Expand Down Expand Up @@ -419,32 +421,28 @@ ngx_hls_http_redirect_handler(ngx_http_request_t *r,
ngx_snprintf(uri.data, uri.len, "%V%V", &location, &uri_tail);
}

#if (NGX_HTTP_SSL)
ngx_http_arg(r, (u_char*)"scheme", 6, &scheme);
if (scheme.len == 0) {
#if (NGX_HTTP_SSL)
if (r->connection->ssl) {
m3u8_url.len = ngx_strlen("https://") +
host.len +
uri.len +
NGX_HLS_LIVE_ARG_SESSION_LENGTH + 2 +
ngx_strlen(sstr);

m3u8_url.data = ngx_pcalloc(r->connection->pool, m3u8_url.len);

ngx_snprintf(m3u8_url.data, m3u8_url.len, "https://%V%V?%s=%s",
&host, &uri, NGX_HLS_LIVE_ARG_SESSION, sstr);
scheme = https;
} else
#endif
#endif
{
m3u8_url.len = ngx_strlen("http://") +
scheme = http;
}
}

m3u8_url.len = scheme.len + ngx_strlen("://") +
host.len +
uri.len +
NGX_HLS_LIVE_ARG_SESSION_LENGTH + 2 +
ngx_strlen(sstr);

m3u8_url.data = ngx_pcalloc(r->connection->pool, m3u8_url.len);
m3u8_url.data = ngx_pcalloc(r->connection->pool, m3u8_url.len);

ngx_snprintf(m3u8_url.data, m3u8_url.len, "http://%V%V?%s=%s",
&host, &uri, NGX_HLS_LIVE_ARG_SESSION, sstr);
}
ngx_snprintf(m3u8_url.data, m3u8_url.len, "%V://%V%V?%s=%s",
&scheme, &host, &uri, NGX_HLS_LIVE_ARG_SESSION, sstr);

ngx_http_set_header_out(r, &ngx_302_headers[0].key, &m3u8_url);

Expand Down Expand Up @@ -1091,6 +1089,10 @@ ngx_hls_http_handler(ngx_http_request_t *r)
return NGX_HTTP_NOT_ALLOWED;
}

if (ngx_rtmp_core_main_conf->fast_reload && (ngx_exiting || ngx_terminate)) {
return NGX_DECLINED;
}

if (r->uri.len < 4) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"hls-http: live_handler| donnot support the file type");
Expand Down
6 changes: 6 additions & 0 deletions modules/nginx-rtmp-module/mpegts/ngx_mpegts_http_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ ngx_mpegts_http_write_handler(ngx_http_request_t *r)
ngx_del_timer(wev);
}

if (ngx_rtmp_core_main_conf->fast_reload && (ngx_exiting || ngx_terminate)) {
r->error_page = 1;
ngx_http_finalize_request(r, NGX_HTTP_SERVICE_UNAVAILABLE);
return;
}

if (ngx_rtmp_prepare_merge_frame(s) == NGX_ERROR) {
ngx_http_finalize_request(r, NGX_ERROR);
return;
Expand Down
7 changes: 7 additions & 0 deletions modules/nginx-rtmp-module/ngx_live_relay_httpflv.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,13 @@ ngx_live_relay_httpflv_recv(void *request, ngx_http_request_t *hcr)
hcr->connection->log->handler);
s->log->connection = s->connection->number;

if (ngx_rtmp_core_main_conf->fast_reload && (ngx_exiting || ngx_terminate)) {
ngx_live_relay_httpflv_error(s, NGX_LIVE_PROCESS_EXIT);
s->finalize_reason = NGX_LIVE_PROCESS_EXIT;
ngx_http_client_finalize_request(hcr, 1);
return;
}

if (status_code != NGX_HTTP_OK) {
ngx_live_relay_httpflv_error(s, status_code);
s->finalize_reason = NGX_LIVE_FLV_RECV_ERR;
Expand Down
3 changes: 3 additions & 0 deletions modules/nginx-rtmp-module/ngx_rtmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ typedef struct ngx_rtmp_addr_conf_s ngx_rtmp_addr_conf_t;
#define NGX_LIVE_OCLP_RELAY_ERR 13
#define NGX_LIVE_OCLP_PARA_ERR 14
#define NGX_LIVE_RELAY_CLOSE 15
#define NGX_LIVE_PROCESS_EXIT 16

struct ngx_rtmp_session_s {
ngx_atomic_uint_t number;
Expand Down Expand Up @@ -518,6 +519,8 @@ typedef struct {


typedef struct {
ngx_flag_t fast_reload;

ngx_array_t servers; /* ngx_rtmp_core_srv_conf_t */

ngx_array_t events[NGX_RTMP_MAX_EVENT];
Expand Down
9 changes: 9 additions & 0 deletions modules/nginx-rtmp-module/ngx_rtmp_core_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ static ngx_command_t ngx_rtmp_core_commands[] = {
offsetof(ngx_rtmp_core_main_conf_t, server_names_hash_bucket_size),
NULL },

{ ngx_string("fast_reload"),
NGX_RTMP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_flag_slot,
NGX_RTMP_MAIN_CONF_OFFSET,
offsetof(ngx_rtmp_core_main_conf_t, fast_reload),
NULL },

{ ngx_string("server"),
NGX_RTMP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
ngx_rtmp_core_server,
Expand Down Expand Up @@ -300,6 +307,7 @@ ngx_rtmp_core_create_main_conf(ngx_conf_t *cf)

cmcf->server_names_hash_max_size = NGX_CONF_UNSET_UINT;
cmcf->server_names_hash_bucket_size = NGX_CONF_UNSET_UINT;
cmcf->fast_reload = NGX_CONF_UNSET;
cmcf->variables_hash_max_size = 1024;
cmcf->variables_hash_bucket_size = 64;

Expand All @@ -317,6 +325,7 @@ ngx_rtmp_core_init_main_conf(ngx_conf_t *cf, void *conf)
ngx_conf_init_uint_value(cmcf->server_names_hash_max_size, 512);
ngx_conf_init_uint_value(cmcf->server_names_hash_bucket_size,
ngx_cacheline_size);
ngx_conf_init_value(cmcf->fast_reload, 0);

return NGX_CONF_OK;
}
Expand Down
12 changes: 12 additions & 0 deletions modules/nginx-rtmp-module/ngx_rtmp_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ ngx_rtmp_recv(ngx_event_t *rev)
return;
}

if (ngx_rtmp_core_main_conf->fast_reload && (ngx_exiting || ngx_terminate)) {
s->finalize_reason = NGX_LIVE_PROCESS_EXIT;
ngx_rtmp_finalize_session(s);
return;
}

for( ;; ) {

st = &s->in_streams[s->in_csid];
Expand Down Expand Up @@ -697,6 +703,12 @@ ngx_rtmp_send(ngx_event_t *wev)
s->prepare_handler = ngx_rtmp_prepare_out_chain;
}

if (ngx_rtmp_core_main_conf->fast_reload && (ngx_exiting || ngx_terminate)) {
s->finalize_reason = NGX_LIVE_PROCESS_EXIT;
ngx_rtmp_finalize_session(s);
return;
}

if (ngx_rtmp_prepare_merge_frame(s) == NGX_ERROR) {
ngx_rtmp_finalize_session(s);
return;
Expand Down
1 change: 1 addition & 0 deletions modules/nginx-rtmp-module/ngx_rtmp_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ char *ngx_live_err[] = {
"notify_relay_err",
"notify_para_err",
"relay_close",
"process_exit",
};


Expand Down
8 changes: 2 additions & 6 deletions resource/conf-template/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ rtmp {
live_record on;
live_record_path /data/record;

# on_play http://127.0.0.1:9980/callBack stage=start,update,done;
recorder r1{
record all;
record_path /data/record;
}

# exec_publish bash -c "ffmepg -i rtmp://127.0.0.1/live/$name -c copy /tmp/mp4/$name-$starttime.mp4";
# oclp_play http://127.0.0.1:999 stage=start args=ip=$remote_host;
# exec_pull bash -c "ffmpeg -i rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov -c copy -f flv rtmp://127.0.0.1/live/1";
live on;
hls on;
hls_path /tmp/hls;
Expand Down Expand Up @@ -192,9 +188,9 @@ http {
proxy_buffers 8 1M;
proxy_busy_buffers_size 2M;
proxy_max_temp_file_size 0;
set $hls_args location=/hls2;
set $hls_args location=/hls2&scheme=$scheme;
if ($args) {
set $hls_args $args&location=/hls2;
set $hls_args $args&location=/hls2&scheme=$scheme;
}
proxy_set_header Host $host:$server_port;
rewrite ^/(.*)/(.*)\.(.*)$ /hlsm/$2.$3?$hls_args break;
Expand Down

0 comments on commit b14be2c

Please sign in to comment.