winlin

add ts muxer to write hls/ts file

@@ -223,7 +223,7 @@ int SrsClient::check_vhost() @@ -223,7 +223,7 @@ int SrsClient::check_vhost()
223 } 223 }
224 224
225 SrsConfDirective* conf = NULL; 225 SrsConfDirective* conf = NULL;
226 - if ((conf = vhost->get(RTMP_VHOST_ENABLED)) != NULL && conf->arg0() != "on") { 226 + if ((conf = config->get_vhost_enabled(req->vhost)) != NULL && conf->arg0() != "on") {
227 ret = ERROR_RTMP_VHOST_NOT_FOUND; 227 ret = ERROR_RTMP_VHOST_NOT_FOUND;
228 srs_error("vhost %s disabled. ret=%d", req->vhost.c_str(), ret); 228 srs_error("vhost %s disabled. ret=%d", req->vhost.c_str(), ret);
229 return ret; 229 return ret;
@@ -336,7 +336,7 @@ int SrsClient::publish(SrsSource* source, bool is_fmle) @@ -336,7 +336,7 @@ int SrsClient::publish(SrsSource* source, bool is_fmle)
336 SrsHLS* hls = source->get_hls(); 336 SrsHLS* hls = source->get_hls();
337 337
338 // notify the hls to prepare when publish start. 338 // notify the hls to prepare when publish start.
339 - if ((ret = hls->on_publish()) != ERROR_SUCCESS) { 339 + if ((ret = hls->on_publish(req->vhost)) != ERROR_SUCCESS) {
340 srs_error("hls on_publish failed. ret=%d", ret); 340 srs_error("hls on_publish failed. ret=%d", ret);
341 return ret; 341 return ret;
342 } 342 }
@@ -551,6 +551,17 @@ SrsConfDirective* SrsConfig::get_vhost(std::string vhost) @@ -551,6 +551,17 @@ SrsConfDirective* SrsConfig::get_vhost(std::string vhost)
551 return NULL; 551 return NULL;
552 } 552 }
553 553
  554 +SrsConfDirective* SrsConfig::get_vhost_enabled(std::string vhost)
  555 +{
  556 + SrsConfDirective* conf = get_vhost(vhost);
  557 +
  558 + if (!conf) {
  559 + return NULL;
  560 + }
  561 +
  562 + return conf->get("enabled");
  563 +}
  564 +
554 SrsConfDirective* SrsConfig::get_gop_cache(std::string vhost) 565 SrsConfDirective* SrsConfig::get_gop_cache(std::string vhost)
555 { 566 {
556 SrsConfDirective* conf = get_vhost(vhost); 567 SrsConfDirective* conf = get_vhost(vhost);
@@ -668,6 +679,8 @@ int SrsConfig::parse_file(const char* filename) @@ -668,6 +679,8 @@ int SrsConfig::parse_file(const char* filename)
668 "directive \"listen\" is empty, ret=%d", (conf? conf->conf_line:0), ret); 679 "directive \"listen\" is empty, ret=%d", (conf? conf->conf_line:0), ret);
669 return ret; 680 return ret;
670 } 681 }
  682 + // TODO: check the hls.
  683 + // TODO: check other config.
671 684
672 return ret; 685 return ret;
673 } 686 }
@@ -37,8 +37,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -37,8 +37,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 // default vhost for rtmp 37 // default vhost for rtmp
38 #define RTMP_VHOST_DEFAULT "__defaultVhost__" 38 #define RTMP_VHOST_DEFAULT "__defaultVhost__"
39 39
40 -// conf node: enabled.  
41 -#define RTMP_VHOST_ENABLED "enabled" 40 +#define SRS_CONF_DEFAULT_HLS_PATH "./hls"
42 41
43 class SrsFileBuffer 42 class SrsFileBuffer
44 { 43 {
@@ -108,6 +107,7 @@ public: @@ -108,6 +107,7 @@ public:
108 public: 107 public:
109 virtual int parse_options(int argc, char** argv); 108 virtual int parse_options(int argc, char** argv);
110 virtual SrsConfDirective* get_vhost(std::string vhost); 109 virtual SrsConfDirective* get_vhost(std::string vhost);
  110 + virtual SrsConfDirective* get_vhost_enabled(std::string vhost);
111 virtual SrsConfDirective* get_gop_cache(std::string vhost); 111 virtual SrsConfDirective* get_gop_cache(std::string vhost);
112 virtual SrsConfDirective* get_hls(std::string vhost); 112 virtual SrsConfDirective* get_hls(std::string vhost);
113 virtual SrsConfDirective* get_hls_path(std::string vhost); 113 virtual SrsConfDirective* get_hls_path(std::string vhost);
@@ -109,5 +109,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -109,5 +109,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
109 109
110 #define ERROR_HLS_METADATA 600 110 #define ERROR_HLS_METADATA 600
111 #define ERROR_HLS_DECODE_ERROR 601 111 #define ERROR_HLS_DECODE_ERROR 601
  112 +#define ERROR_HLS_BUSY 602
112 113
113 #endif 114 #endif
@@ -27,27 +27,62 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -27,27 +27,62 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <srs_core_codec.hpp> 27 #include <srs_core_codec.hpp>
28 #include <srs_core_amf0.hpp> 28 #include <srs_core_amf0.hpp>
29 #include <srs_core_protocol.hpp> 29 #include <srs_core_protocol.hpp>
  30 +#include <srs_core_config.hpp>
30 31
31 SrsHLS::SrsHLS() 32 SrsHLS::SrsHLS()
32 { 33 {
  34 + hls_enabled = false;
33 codec = new SrsCodec(); 35 codec = new SrsCodec();
34 sample = new SrsCodecSample(); 36 sample = new SrsCodecSample();
  37 + muxer = NULL;
35 } 38 }
36 39
37 SrsHLS::~SrsHLS() 40 SrsHLS::~SrsHLS()
38 { 41 {
39 srs_freep(codec); 42 srs_freep(codec);
40 srs_freep(sample); 43 srs_freep(sample);
  44 + srs_freep(muxer);
41 } 45 }
42 46
43 -int SrsHLS::on_publish() 47 +int SrsHLS::on_publish(std::string _vhost)
44 { 48 {
45 int ret = ERROR_SUCCESS; 49 int ret = ERROR_SUCCESS;
  50 +
  51 + if (muxer) {
  52 + ret = ERROR_HLS_BUSY;
  53 + srs_error("hls is busy, something error, "
  54 + "vhost=%s, ret=%d", _vhost.c_str(), ret);
  55 + return ret;
  56 + }
  57 +
  58 + vhost = _vhost;
  59 + muxer = new SrsTSMuxer();
  60 +
  61 + // try to open the HLS muxer
  62 + SrsConfDirective* conf = config->get_hls(vhost);
  63 + if (!conf && conf->arg0() == "off") {
  64 + return ret;
  65 + }
  66 +
  67 + hls_enabled = true;
  68 +
  69 + std::string path = SRS_CONF_DEFAULT_HLS_PATH;
  70 + if ((conf = config->get_hls_path(vhost)) != NULL) {
  71 + path = conf->arg0();
  72 + }
  73 +
  74 + if ((ret = muxer->open(path)) != ERROR_SUCCESS) {
  75 + srs_error("open hls muxer failed. ret=%d", ret);
  76 + return ret;
  77 + }
  78 +
46 return ret; 79 return ret;
47 } 80 }
48 81
49 void SrsHLS::on_unpublish() 82 void SrsHLS::on_unpublish()
50 { 83 {
  84 + hls_enabled = false;
  85 + srs_freep(muxer);
51 } 86 }
52 87
53 int SrsHLS::on_meta_data(SrsOnMetaDataPacket* metadata) 88 int SrsHLS::on_meta_data(SrsOnMetaDataPacket* metadata)
@@ -145,6 +180,11 @@ int SrsHLS::on_audio(SrsCommonMessage* audio) @@ -145,6 +180,11 @@ int SrsHLS::on_audio(SrsCommonMessage* audio)
145 return ret; 180 return ret;
146 } 181 }
147 182
  183 + // TODO: maybe donot need to demux the aac?
  184 + if (!hls_enabled) {
  185 + return ret;
  186 + }
  187 +
148 return ret; 188 return ret;
149 } 189 }
150 190
@@ -161,6 +201,25 @@ int SrsHLS::on_video(SrsCommonMessage* video) @@ -161,6 +201,25 @@ int SrsHLS::on_video(SrsCommonMessage* video)
161 return ret; 201 return ret;
162 } 202 }
163 203
  204 + // TODO: maybe donot need to demux the avc?
  205 + if (!hls_enabled) {
  206 + return ret;
  207 + }
  208 +
  209 + return ret;
  210 +}
  211 +
  212 +SrsTSMuxer::SrsTSMuxer()
  213 +{
  214 +}
  215 +
  216 +SrsTSMuxer::~SrsTSMuxer()
  217 +{
  218 +}
  219 +
  220 +int SrsTSMuxer::open(std::string path)
  221 +{
  222 + int ret = ERROR_SUCCESS;
164 return ret; 223 return ret;
165 } 224 }
166 225
@@ -29,25 +29,40 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -29,25 +29,40 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */ 29 */
30 #include <srs_core.hpp> 30 #include <srs_core.hpp>
31 31
  32 +#include <string>
  33 +
32 class SrsOnMetaDataPacket; 34 class SrsOnMetaDataPacket;
33 class SrsCommonMessage; 35 class SrsCommonMessage;
34 class SrsCodecSample; 36 class SrsCodecSample;
  37 +class SrsTSMuxer;
35 class SrsCodec; 38 class SrsCodec;
36 39
37 class SrsHLS 40 class SrsHLS
38 { 41 {
39 private: 42 private:
  43 + std::string vhost;
  44 + bool hls_enabled;
40 SrsCodec* codec; 45 SrsCodec* codec;
41 SrsCodecSample* sample; 46 SrsCodecSample* sample;
  47 + SrsTSMuxer* muxer;
42 public: 48 public:
43 SrsHLS(); 49 SrsHLS();
44 virtual ~SrsHLS(); 50 virtual ~SrsHLS();
45 public: 51 public:
46 - virtual int on_publish(); 52 + virtual int on_publish(std::string _vhost);
47 virtual void on_unpublish(); 53 virtual void on_unpublish();
48 virtual int on_meta_data(SrsOnMetaDataPacket* metadata); 54 virtual int on_meta_data(SrsOnMetaDataPacket* metadata);
49 virtual int on_audio(SrsCommonMessage* audio); 55 virtual int on_audio(SrsCommonMessage* audio);
50 virtual int on_video(SrsCommonMessage* video); 56 virtual int on_video(SrsCommonMessage* video);
51 }; 57 };
52 58
  59 +class SrsTSMuxer
  60 +{
  61 +public:
  62 + SrsTSMuxer();
  63 + virtual ~SrsTSMuxer();
  64 +public:
  65 + virtual int open(std::string path);
  66 +};
  67 +
53 #endif 68 #endif