1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

for #513, remove hls ram from srs2 to srs3+. 2.0.224

This commit is contained in:
winlin 2016-12-15 14:48:38 +08:00
parent 34e5cdcd12
commit 759c84a395
17 changed files with 26 additions and 556 deletions

View file

@ -760,22 +760,6 @@ SrsHttpStreamServer::~SrsHttpStreamServer()
}
sflvs.clear();
}
if (true) {
std::map<std::string, SrsHlsEntry*>::iterator it;
for (it = thls.begin(); it != thls.end(); ++it) {
SrsHlsEntry* entry = it->second;
srs_freep(entry);
}
thls.clear();
}
if (true) {
std::map<std::string, SrsHlsEntry*>::iterator it;
for (it = shls.begin(); it != shls.end(); ++it) {
SrsHlsEntry* entry = it->second;
srs_freep(entry);
}
shls.clear();
}
}
int SrsHttpStreamServer::initialize()
@ -787,11 +771,6 @@ int SrsHttpStreamServer::initialize()
return ret;
}
// remux rtmp to hls live streaming
if ((ret = initialize_hls_streaming()) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
@ -957,190 +936,6 @@ int SrsHttpStreamServer::on_reload_vhost_http_remux_updated(string vhost)
return ret;
}
int SrsHttpStreamServer::mount_hls(SrsRequest* r)
{
int ret = ERROR_SUCCESS;
std::string sid = r->get_stream_url();
if (shls.find(sid) == shls.end()) {
srs_info("ignore mount hls stream for disabled");
return ret;
}
SrsHlsEntry* entry = shls[sid];
// TODO: FIXME: supports reload.
std::map<std::string, ISrsHttpHandler*>::iterator it;
for (it = entry->streams.begin(); it != entry->streams.end(); ++it) {
ISrsHttpHandler* stream = it->second;
stream->entry->enabled = true;
}
return ret;
}
int SrsHttpStreamServer::hls_update_m3u8(SrsRequest* r, string m3u8)
{
int ret = ERROR_SUCCESS;
std::string mount;
std::string sid = r->get_stream_url();
SrsHlsEntry* entry = NULL;
// create stream from template when not found.
if (shls.find(sid) == shls.end()) {
if (thls.find(r->vhost) == thls.end()) {
srs_info("ignore mount hls stream for disabled");
return ret;
}
SrsHlsEntry* tmpl = thls[r->vhost];
srs_assert(tmpl);
entry = new SrsHlsEntry();
mount = tmpl->mount;
// replace the vhost variable
mount = srs_string_replace(mount, "[vhost]", r->vhost);
mount = srs_string_replace(mount, "[app]", r->app);
mount = srs_string_replace(mount, "[stream]", r->stream);
// remove the default vhost mount
mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
entry->tmpl = tmpl;
entry->mount = mount;
shls[sid] = entry;
if (entry->streams.find(mount) == entry->streams.end()) {
ISrsHttpHandler* he = new SrsHlsM3u8Stream();
entry->streams[mount] = he;
if ((ret = mux.handle(mount, he)) != ERROR_SUCCESS) {
srs_error("handle mount=%s failed. ret=%d", mount.c_str(), ret);
return ret;
}
}
} else {
entry = shls[sid];
}
mount = entry->mount;
// update the m3u8 stream.
SrsHlsM3u8Stream* hms = dynamic_cast<SrsHlsM3u8Stream*>(entry->streams[mount]);
if (hms) {
hms->set_m3u8(m3u8);
}
srs_trace("hls update m3u8 ok, mount=%s", mount.c_str());
return ret;
}
int SrsHttpStreamServer::hls_update_ts(SrsRequest* r, string uri, string ts)
{
int ret = ERROR_SUCCESS;
std::string sid = r->get_stream_url();
// when no hls mounted, init with empty m3u8.
if (shls.find(sid) == shls.end()) {
if ((ret = hls_update_m3u8(r, "")) != ERROR_SUCCESS) {
return ret;
}
}
// find again, ignore if not exits.
if (shls.find(sid) == shls.end()) {
return ret;
}
SrsHlsEntry* entry = shls[sid];
srs_assert(entry);
srs_assert(entry->tmpl);
std::string mount = hls_mount_generate(r, uri, entry->tmpl->mount);
if (entry->streams.find(mount) == entry->streams.end()) {
ISrsHttpHandler* he = new SrsHlsTsStream();
entry->streams[mount] = he;
if ((ret = mux.handle(mount, he)) != ERROR_SUCCESS) {
srs_error("handle mount=%s failed. ret=%d", mount.c_str(), ret);
return ret;
}
}
// update the ts stream.
SrsHlsTsStream* hts = dynamic_cast<SrsHlsTsStream*>(entry->streams[mount]);
if (hts) {
hts->set_ts(ts);
}
srs_trace("hls update ts ok, mount=%s", mount.c_str());
return ret;
}
int SrsHttpStreamServer::hls_remove_ts(SrsRequest* r, string uri)
{
int ret = ERROR_SUCCESS;
std::string sid = r->get_stream_url();
// when no hls mounted, ignore.
if (shls.find(sid) == shls.end()) {
return ret;
}
SrsHlsEntry* entry = shls[sid];
srs_assert(entry);
srs_assert(entry->tmpl);
std::string mount = hls_mount_generate(r, uri, entry->tmpl->mount);
// ignore when no ts mounted.
if (entry->streams.find(mount) == entry->streams.end()) {
return ret;
}
// update the ts stream.
SrsHlsTsStream* hts = dynamic_cast<SrsHlsTsStream*>(entry->streams[mount]);
if (hts) {
hts->set_ts("");
// TODO: FIXME: unmount and remove the http handler.
}
srs_trace("hls remove ts ok, mount=%s", mount.c_str());
return ret;
}
void SrsHttpStreamServer::unmount_hls(SrsRequest* r)
{
std::string sid = r->get_stream_url();
if (shls.find(sid) == shls.end()) {
srs_info("ignore unmount hls stream for disabled");
return;
}
SrsHlsEntry* entry = shls[sid];
std::map<std::string, ISrsHttpHandler*>::iterator it;
for (it = entry->streams.begin(); it != entry->streams.end(); ++it) {
ISrsHttpHandler* stream = it->second;
stream->entry->enabled = false;
}
}
int SrsHttpStreamServer::on_reload_vhost_hls(string vhost)
{
int ret = ERROR_SUCCESS;
// TODO: FIXME: implements it.
return ret;
}
int SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph)
{
int ret = ERROR_SUCCESS;
@ -1229,7 +1024,7 @@ int SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph)
}
SrsSource* s = NULL;
if ((ret = SrsSource::fetch_or_create(r, server, server, &s)) != ERROR_SUCCESS) {
if ((ret = SrsSource::fetch_or_create(r, server, &s)) != ERROR_SUCCESS) {
return ret;
}
srs_assert(s != NULL);
@ -1295,62 +1090,5 @@ int SrsHttpStreamServer::initialize_flv_entry(std::string vhost)
return ret;
}
int SrsHttpStreamServer::initialize_hls_streaming()
{
int ret = ERROR_SUCCESS;
// http hls live stream mount for each vhost.
SrsConfDirective* root = _srs_config->get_root();
for (int i = 0; i < (int)root->directives.size(); i++) {
SrsConfDirective* conf = root->at(i);
if (!conf->is_vhost()) {
continue;
}
std::string vhost = conf->arg0();
if (!_srs_config->get_hls_enabled(vhost)) {
continue;
}
std::string storage = _srs_config->get_hls_storage(vhost);
if (storage != "ram" && storage != "both") {
continue;
}
SrsHlsEntry* entry = new SrsHlsEntry();
entry->mount = _srs_config->get_hls_mount(vhost);
thls[vhost] = entry;
srs_trace("http hls live stream, vhost=%s, mount=%s",
vhost.c_str(), entry->mount.c_str());
}
return ret;
}
string SrsHttpStreamServer::hls_mount_generate(SrsRequest* r, string uri, string tmpl)
{
std::string mount = tmpl;
// the ts is relative from the m3u8, the same start dir.
size_t pos = string::npos;
if ((pos = mount.rfind("/")) != string::npos) {
mount = mount.substr(0, pos);
}
// replace the vhost variable
mount = srs_string_replace(mount, "[vhost]", r->vhost);
mount = srs_string_replace(mount, "[app]", r->app);
// remove the default vhost mount
mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
// mount with ts.
mount += "/";
mount += uri;
return mount;
}
#endif