From 4af3982721049c4478abf4dd04785a56825f09af Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 24 Nov 2013 17:15:37 +0800 Subject: [PATCH] add ts muxer to write hls/ts file --- trunk/src/core/srs_core_client.cpp | 4 +- trunk/src/core/srs_core_config.cpp | 13 +++++++ trunk/src/core/srs_core_config.hpp | 4 +- trunk/src/core/srs_core_error.hpp | 1 + trunk/src/core/srs_core_hls.cpp | 61 +++++++++++++++++++++++++++++- trunk/src/core/srs_core_hls.hpp | 17 ++++++++- 6 files changed, 94 insertions(+), 6 deletions(-) diff --git a/trunk/src/core/srs_core_client.cpp b/trunk/src/core/srs_core_client.cpp index 20e9b74f3..2f4bc71e5 100644 --- a/trunk/src/core/srs_core_client.cpp +++ b/trunk/src/core/srs_core_client.cpp @@ -223,7 +223,7 @@ int SrsClient::check_vhost() } SrsConfDirective* conf = NULL; - if ((conf = vhost->get(RTMP_VHOST_ENABLED)) != NULL && conf->arg0() != "on") { + if ((conf = config->get_vhost_enabled(req->vhost)) != NULL && conf->arg0() != "on") { ret = ERROR_RTMP_VHOST_NOT_FOUND; srs_error("vhost %s disabled. ret=%d", req->vhost.c_str(), ret); return ret; @@ -336,7 +336,7 @@ int SrsClient::publish(SrsSource* source, bool is_fmle) SrsHLS* hls = source->get_hls(); // notify the hls to prepare when publish start. - if ((ret = hls->on_publish()) != ERROR_SUCCESS) { + if ((ret = hls->on_publish(req->vhost)) != ERROR_SUCCESS) { srs_error("hls on_publish failed. ret=%d", ret); return ret; } diff --git a/trunk/src/core/srs_core_config.cpp b/trunk/src/core/srs_core_config.cpp index 5575a551d..50d809654 100644 --- a/trunk/src/core/srs_core_config.cpp +++ b/trunk/src/core/srs_core_config.cpp @@ -551,6 +551,17 @@ SrsConfDirective* SrsConfig::get_vhost(std::string vhost) return NULL; } +SrsConfDirective* SrsConfig::get_vhost_enabled(std::string vhost) +{ + SrsConfDirective* conf = get_vhost(vhost); + + if (!conf) { + return NULL; + } + + return conf->get("enabled"); +} + SrsConfDirective* SrsConfig::get_gop_cache(std::string vhost) { SrsConfDirective* conf = get_vhost(vhost); @@ -668,6 +679,8 @@ int SrsConfig::parse_file(const char* filename) "directive \"listen\" is empty, ret=%d", (conf? conf->conf_line:0), ret); return ret; } + // TODO: check the hls. + // TODO: check other config. return ret; } diff --git a/trunk/src/core/srs_core_config.hpp b/trunk/src/core/srs_core_config.hpp index 8f25dbb6a..5e3fae16d 100644 --- a/trunk/src/core/srs_core_config.hpp +++ b/trunk/src/core/srs_core_config.hpp @@ -37,8 +37,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // default vhost for rtmp #define RTMP_VHOST_DEFAULT "__defaultVhost__" -// conf node: enabled. -#define RTMP_VHOST_ENABLED "enabled" +#define SRS_CONF_DEFAULT_HLS_PATH "./hls" class SrsFileBuffer { @@ -108,6 +107,7 @@ public: public: virtual int parse_options(int argc, char** argv); virtual SrsConfDirective* get_vhost(std::string vhost); + virtual SrsConfDirective* get_vhost_enabled(std::string vhost); virtual SrsConfDirective* get_gop_cache(std::string vhost); virtual SrsConfDirective* get_hls(std::string vhost); virtual SrsConfDirective* get_hls_path(std::string vhost); diff --git a/trunk/src/core/srs_core_error.hpp b/trunk/src/core/srs_core_error.hpp index dd409807a..3d1813366 100644 --- a/trunk/src/core/srs_core_error.hpp +++ b/trunk/src/core/srs_core_error.hpp @@ -109,5 +109,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define ERROR_HLS_METADATA 600 #define ERROR_HLS_DECODE_ERROR 601 +#define ERROR_HLS_BUSY 602 #endif \ No newline at end of file diff --git a/trunk/src/core/srs_core_hls.cpp b/trunk/src/core/srs_core_hls.cpp index 0f88bac85..596ba718f 100644 --- a/trunk/src/core/srs_core_hls.cpp +++ b/trunk/src/core/srs_core_hls.cpp @@ -27,27 +27,62 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include +#include SrsHLS::SrsHLS() { + hls_enabled = false; codec = new SrsCodec(); sample = new SrsCodecSample(); + muxer = NULL; } SrsHLS::~SrsHLS() { srs_freep(codec); srs_freep(sample); + srs_freep(muxer); } -int SrsHLS::on_publish() +int SrsHLS::on_publish(std::string _vhost) { int ret = ERROR_SUCCESS; + + if (muxer) { + ret = ERROR_HLS_BUSY; + srs_error("hls is busy, something error, " + "vhost=%s, ret=%d", _vhost.c_str(), ret); + return ret; + } + + vhost = _vhost; + muxer = new SrsTSMuxer(); + + // try to open the HLS muxer + SrsConfDirective* conf = config->get_hls(vhost); + if (!conf && conf->arg0() == "off") { + return ret; + } + + hls_enabled = true; + + std::string path = SRS_CONF_DEFAULT_HLS_PATH; + if ((conf = config->get_hls_path(vhost)) != NULL) { + path = conf->arg0(); + } + + if ((ret = muxer->open(path)) != ERROR_SUCCESS) { + srs_error("open hls muxer failed. ret=%d", ret); + return ret; + } + return ret; } void SrsHLS::on_unpublish() { + hls_enabled = false; + srs_freep(muxer); } int SrsHLS::on_meta_data(SrsOnMetaDataPacket* metadata) @@ -145,6 +180,11 @@ int SrsHLS::on_audio(SrsCommonMessage* audio) return ret; } + // TODO: maybe donot need to demux the aac? + if (!hls_enabled) { + return ret; + } + return ret; } @@ -161,6 +201,25 @@ int SrsHLS::on_video(SrsCommonMessage* video) return ret; } + // TODO: maybe donot need to demux the avc? + if (!hls_enabled) { + return ret; + } + + return ret; +} + +SrsTSMuxer::SrsTSMuxer() +{ +} + +SrsTSMuxer::~SrsTSMuxer() +{ +} + +int SrsTSMuxer::open(std::string path) +{ + int ret = ERROR_SUCCESS; return ret; } diff --git a/trunk/src/core/srs_core_hls.hpp b/trunk/src/core/srs_core_hls.hpp index 11facf06f..14ef95da7 100644 --- a/trunk/src/core/srs_core_hls.hpp +++ b/trunk/src/core/srs_core_hls.hpp @@ -29,25 +29,40 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include +#include + class SrsOnMetaDataPacket; class SrsCommonMessage; class SrsCodecSample; +class SrsTSMuxer; class SrsCodec; class SrsHLS { private: + std::string vhost; + bool hls_enabled; SrsCodec* codec; SrsCodecSample* sample; + SrsTSMuxer* muxer; public: SrsHLS(); virtual ~SrsHLS(); public: - virtual int on_publish(); + virtual int on_publish(std::string _vhost); virtual void on_unpublish(); virtual int on_meta_data(SrsOnMetaDataPacket* metadata); virtual int on_audio(SrsCommonMessage* audio); virtual int on_video(SrsCommonMessage* video); }; +class SrsTSMuxer +{ +public: + SrsTSMuxer(); + virtual ~SrsTSMuxer(); +public: + virtual int open(std::string path); +}; + #endif \ No newline at end of file