From b7b268dfbf9feb58af956a8b8c89f155d6d3a535 Mon Sep 17 00:00:00 2001 From: winlin Date: Mon, 19 Jan 2015 13:17:46 +0800 Subject: [PATCH] fix #293, support http live flv/aac/mp3 stream with fast cache. 2.0.100. --- README.md | 1 + trunk/conf/full.conf | 9 +++++++++ trunk/conf/http.aac.live.conf | 18 ++++++++++++++++++ trunk/conf/http.mp3.live.conf | 18 ++++++++++++++++++ trunk/src/app/srs_app_config.cpp | 26 +++++++++++++++++++++++++- trunk/src/app/srs_app_config.hpp | 5 +++++ trunk/src/app/srs_app_http_conn.cpp | 13 ++++++++----- trunk/src/app/srs_app_http_conn.hpp | 3 ++- trunk/src/core/srs_core.hpp | 2 +- 9 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 trunk/conf/http.aac.live.conf create mode 100644 trunk/conf/http.mp3.live.conf diff --git a/README.md b/README.md index 0bef22f658..a27a3540cb 100755 --- a/README.md +++ b/README.md @@ -515,6 +515,7 @@ Supported operating systems and hardware: ## History +* v2.0, 2015-01-19, fix [#293](https://github.com/winlinvip/simple-rtmp-server/issues/293), support http live flv/aac/mp3 stream with fast cache. 2.0.100. * v2.0, 2015-01-18, fix [#293](https://github.com/winlinvip/simple-rtmp-server/issues/293), support rtmp remux to http flv live stream. 2.0.99. * v2.0, 2015-01-17, fix [#277](https://github.com/winlinvip/simple-rtmp-server/issues/277), refine http server refer to go http-framework. 2.0.98 * v2.0, 2015-01-17, for [#277](https://github.com/winlinvip/simple-rtmp-server/issues/277), refine http api refer to go http-framework. 2.0.97 diff --git a/trunk/conf/full.conf b/trunk/conf/full.conf index 644fbba1cc..136d40cf74 100644 --- a/trunk/conf/full.conf +++ b/trunk/conf/full.conf @@ -371,12 +371,21 @@ vhost http.flv.srs.com { # whether enable the http flv live streaming service for vhost. # default: off enabled on; + # the fast cache for audio stream(mp3/aac), + # to cache more audio and send to client in a time to make android(weixin) happy. + # @remark the flv stream ignore it + # default: 30 + fast_cache 30; # the stream mout for rtmp to remux to flv live streaming. # for example, if mount to [vhost]/[app]/[stream].flv, user access by http://[vhost]/[app]/[stream].flv # the variables: # [vhost] current vhost for http flv live stream. # [app] current app for http flv live stream. # [stream] current stream for http flv live stream. + # the extension: + # .flv mount http live flv stream, use default gop cache. + # .mp3 mount http live mp3 stream, ignore video and audio mp3 codec required. + # .aac mount http live aac stream, ignore video and audio aac codec required. # default: [vhost]/[app]/[stream].flv mount [vhost]/[app]/[stream].flv; } diff --git a/trunk/conf/http.aac.live.conf b/trunk/conf/http.aac.live.conf new file mode 100644 index 0000000000..959619531a --- /dev/null +++ b/trunk/conf/http.aac.live.conf @@ -0,0 +1,18 @@ +# the config for srs to remux rtmp to aac live stream. +# @see https://github.com/winlinvip/simple-rtmp-server/wiki/v2_CN_DeliveryHttpFlvStream +# @see full.conf for detail config. + +listen 1935; +max_connections 1000; +http_stream { + enabled on; + listen 8080; + dir ./objs/nginx/html; +} +vhost __defaultVhost__ { + http_flv { + enabled on; + fast_cache 30; + mount [vhost]/[app]/[stream].aac; + } +} diff --git a/trunk/conf/http.mp3.live.conf b/trunk/conf/http.mp3.live.conf new file mode 100644 index 0000000000..4de96d17bc --- /dev/null +++ b/trunk/conf/http.mp3.live.conf @@ -0,0 +1,18 @@ +# the config for srs to remux rtmp to mp3 live stream. +# @see https://github.com/winlinvip/simple-rtmp-server/wiki/v2_CN_DeliveryHttpFlvStream +# @see full.conf for detail config. + +listen 1935; +max_connections 1000; +http_stream { + enabled on; + listen 8080; + dir ./objs/nginx/html; +} +vhost __defaultVhost__ { + http_flv { + enabled on; + fast_cache 30; + mount [vhost]/[app]/[stream].mp3; + } +} diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 2c17e7bea9..9b4dcb4b9e 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -1426,7 +1426,7 @@ int SrsConfig::check_config() } else if (n == "http_flv") { for (int j = 0; j < (int)conf->directives.size(); j++) { string m = conf->at(j)->name.c_str(); - if (m != "enabled" && m != "mount") { + if (m != "enabled" && m != "mount" && m != "fast_cache") { ret = ERROR_SYSTEM_CONFIG_INVALID; srs_error("unsupported vhost http_flv directive %s, ret=%d", m.c_str(), ret); return ret; @@ -3469,6 +3469,30 @@ bool SrsConfig::get_vhost_http_flv_enabled(string vhost) return false; } +double SrsConfig::get_vhost_http_flv_fast_cache(string vhost) +{ + SrsConfDirective* conf = get_vhost(vhost); + if (!conf) { + return SRS_CONF_DEFAULT_HTTP_AUDIO_FAST_CACHE; + } + + conf = conf->get("http_flv"); + if (!conf) { + return SRS_CONF_DEFAULT_HTTP_AUDIO_FAST_CACHE; + } + + conf = conf->get("fast_cache"); + if (!conf) { + return SRS_CONF_DEFAULT_HTTP_AUDIO_FAST_CACHE; + } + + if (conf->arg0().empty()) { + return SRS_CONF_DEFAULT_HTTP_AUDIO_FAST_CACHE; + } + + return ::atof(conf->arg0().c_str()); +} + string SrsConfig::get_vhost_http_flv_mount(string vhost) { SrsConfDirective* conf = get_vhost(vhost); diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index f5a9c6a18b..9e0956f1c8 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -68,6 +68,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define SRS_CONF_DEFAULT_HTTP_MOUNT "[vhost]/" #define SRS_CONF_DEFAULT_HTTP_FLV_MOUNT "[vhost]/[app]/[stream].flv" #define SRS_CONF_DEFAULT_HTTP_DIR SRS_CONF_DEFAULT_HLS_PATH +#define SRS_CONF_DEFAULT_HTTP_AUDIO_FAST_CACHE 30 #define SRS_CONF_DEFAULT_HTTP_STREAM_PORT 8080 #define SRS_CONF_DEFAULT_HTTP_API_PORT 1985 @@ -971,6 +972,10 @@ class SrsConfig */ virtual bool get_vhost_http_flv_enabled(std::string vhost); /** + * get the fast cache duration for http audio live stream. + */ + virtual double get_vhost_http_flv_fast_cache(std::string vhost); + /** * get the http flv live stream mount point for vhost. * used to generate the flv stream mount path. */ diff --git a/trunk/src/app/srs_app_http_conn.cpp b/trunk/src/app/srs_app_http_conn.cpp index 2e006efc01..ae5c19a77d 100644 --- a/trunk/src/app/srs_app_http_conn.cpp +++ b/trunk/src/app/srs_app_http_conn.cpp @@ -140,8 +140,9 @@ int SrsVodStream::serve_flv_stream(ISrsGoHttpResponseWriter* w, SrsHttpMessage* return ret; } -SrsStreamCache::SrsStreamCache(SrsSource* s) +SrsStreamCache::SrsStreamCache(SrsSource* s, SrsRequest* r) { + req = r->copy(); source = s; queue = new SrsMessageQueue(true); pthread = new SrsThread("http-stream", this, 0, false); @@ -153,6 +154,7 @@ SrsStreamCache::~SrsStreamCache() srs_freep(pthread); srs_freep(queue); + srs_freep(req); } int SrsStreamCache::start() @@ -168,7 +170,8 @@ int SrsStreamCache::dump_cache(SrsConsumer* consumer) return ret; } - srs_trace("http: dump cache %d msgs, duration=%dms", queue->size(), queue->duration()); + srs_trace("http: dump cache %d msgs, duration=%dms, cache=%.2fs", + queue->size(), queue->duration(), _srs_config->get_vhost_http_flv_fast_cache(req->vhost)); return ret; } @@ -187,8 +190,8 @@ int SrsStreamCache::cycle() SrsMessageArray msgs(SRS_PERF_MW_MSGS); // TODO: FIMXE: add pithy print. - // TODO: FIXME: config it. - queue->set_queue_size(60); + // TODO: FIXME: support reload. + queue->set_queue_size(_srs_config->get_vhost_http_flv_fast_cache(req->vhost)); while (true) { // get messages from consumer. @@ -616,7 +619,7 @@ int SrsHttpServer::mount(SrsSource* s, SrsRequest* r) // remove the default vhost mount mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/"); - entry->cache = new SrsStreamCache(s); + entry->cache = new SrsStreamCache(s, r); entry->stream = new SrsLiveStream(s, r, entry->cache); // start http stream cache thread diff --git a/trunk/src/app/srs_app_http_conn.hpp b/trunk/src/app/srs_app_http_conn.hpp index b5c9e4777a..ac0a4e3b31 100644 --- a/trunk/src/app/srs_app_http_conn.hpp +++ b/trunk/src/app/srs_app_http_conn.hpp @@ -77,9 +77,10 @@ class SrsStreamCache : public ISrsThreadHandler private: SrsMessageQueue* queue; SrsSource* source; + SrsRequest* req; SrsThread* pthread; public: - SrsStreamCache(SrsSource* s); + SrsStreamCache(SrsSource* s, SrsRequest* r); virtual ~SrsStreamCache(); public: virtual int start(); diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index ae535d6c19..52e7d7dd17 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // current release version #define VERSION_MAJOR 2 #define VERSION_MINOR 0 -#define VERSION_REVISION 99 +#define VERSION_REVISION 100 // server info. #define RTMP_SIG_SRS_KEY "SRS"