1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00
srs/trunk/src/app/srs_app_http_stream.cpp

1136 lines
32 KiB
C++
Raw Normal View History

2017-03-25 09:21:39 +00:00
/**
* The MIT License (MIT)
*
2019-01-01 13:37:28 +00:00
* Copyright (c) 2013-2019 Winlin
2017-03-25 09:21:39 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_app_http_stream.hpp>
2019-04-07 08:25:52 +00:00
#define SRS_STREAM_CACHE_CYCLE (30 * SRS_UTIME_SECONDS)
2015-07-17 06:05:34 +00:00
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
2015-09-22 01:01:47 +00:00
#include <srs_protocol_stream.hpp>
#include <srs_protocol_utility.hpp>
#include <srs_kernel_log.hpp>
#include <srs_kernel_error.hpp>
#include <srs_app_st.hpp>
#include <srs_core_autofree.hpp>
#include <srs_app_config.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_kernel_file.hpp>
#include <srs_kernel_flv.hpp>
#include <srs_rtmp_stack.hpp>
#include <srs_app_source.hpp>
#include <srs_rtmp_msg_array.hpp>
#include <srs_kernel_aac.hpp>
#include <srs_kernel_mp3.hpp>
#include <srs_kernel_ts.hpp>
#include <srs_app_pithy_print.hpp>
#include <srs_app_source.hpp>
#include <srs_app_server.hpp>
2015-09-17 05:36:02 +00:00
#include <srs_app_statistic.hpp>
#include <srs_app_recv_thread.hpp>
#include <srs_app_http_hooks.hpp>
2015-09-22 00:57:31 +00:00
SrsBufferCache::SrsBufferCache(SrsSource* s, SrsRequest* r)
{
req = r->copy()->as_http();
source = s;
queue = new SrsMessageQueue(true);
trd = new SrsSTCoroutine("http-stream", this);
2015-07-16 10:42:27 +00:00
// TODO: FIXME: support reload.
fast_cache = _srs_config->get_vhost_http_remux_fast_cache(req->vhost);
}
2015-09-22 00:57:31 +00:00
SrsBufferCache::~SrsBufferCache()
{
srs_freep(trd);
srs_freep(queue);
srs_freep(req);
}
srs_error_t SrsBufferCache::update(SrsSource* s, SrsRequest* r)
{
srs_freep(req);
req = r->copy();
source = s;
return srs_success;
}
srs_error_t SrsBufferCache::start()
{
srs_error_t err = srs_success;
if ((err = trd->start()) != srs_success) {
return srs_error_wrap(err, "corotine");
}
return err;
}
srs_error_t SrsBufferCache::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter)
{
2017-09-23 14:12:33 +00:00
srs_error_t err = srs_success;
2017-03-25 09:21:39 +00:00
if (fast_cache <= 0) {
return err;
}
// the jitter is get from SrsSource, which means the time_jitter of vhost.
2017-09-23 14:12:33 +00:00
if ((err = queue->dump_packets(consumer, false, jitter)) != srs_success) {
return srs_error_wrap(err, "dump packets");
}
srs_trace("http: dump cache %d msgs, duration=%dms, cache=%.2fs", queue->size(), queue->duration(), fast_cache);
return err;
}
srs_error_t SrsBufferCache::cycle()
{
srs_error_t err = srs_success;
2015-07-16 10:42:27 +00:00
// TODO: FIXME: support reload.
if (fast_cache <= 0) {
2019-04-07 08:25:52 +00:00
srs_usleep(SRS_STREAM_CACHE_CYCLE);
return err;
2015-07-16 10:42:27 +00:00
}
// the stream cache will create consumer to cache stream,
// which will trigger to fetch stream from origin for edge.
SrsConsumer* consumer = NULL;
2017-09-23 14:12:33 +00:00
if ((err = source->create_consumer(NULL, consumer, false, false, true)) != srs_success) {
return srs_error_wrap(err, "create consumer");
}
SrsAutoFree(SrsConsumer, consumer);
2017-03-25 09:21:39 +00:00
SrsPithyPrint* pprint = SrsPithyPrint::create_http_stream_cache();
SrsAutoFree(SrsPithyPrint, pprint);
SrsMessageArray msgs(SRS_PERF_MW_MSGS);
2017-03-25 09:21:39 +00:00
2015-07-16 10:42:27 +00:00
// set the queue size, which used for max cache.
// TODO: FIXME: support reload.
2015-07-16 10:42:27 +00:00
queue->set_queue_size(fast_cache);
while (true) {
if ((err = trd->pull()) != srs_success) {
return srs_error_wrap(err, "buffer cache");
}
pprint->elapse();
2017-03-25 09:21:39 +00:00
// get messages from consumer.
// each msg in msgs.msgs must be free, for the SrsMessageArray never free them.
int count = 0;
2017-09-23 14:12:33 +00:00
if ((err = consumer->dump_packets(&msgs, count)) != srs_success) {
return srs_error_wrap(err, "consumer dump packets");
}
if (count <= 0) {
srs_info("http: sleep %dms for no msg", SRS_CONSTS_RTMP_PULSE_TMMS);
// directly use sleep, donot use consumer wait.
2019-04-07 08:25:52 +00:00
srs_usleep(SRS_CONSTS_RTMP_PULSE_TMMS * SRS_UTIME_MILLISECONDS);
// ignore when nothing got.
continue;
}
2017-03-25 09:21:39 +00:00
if (pprint->can_print()) {
srs_trace("-> " SRS_CONSTS_LOG_HTTP_STREAM_CACHE " http: got %d msgs, age=%d, min=%d, mw=%d",
2017-03-25 09:21:39 +00:00
count, pprint->age(), SRS_PERF_MW_MIN_MSGS, SRS_CONSTS_RTMP_PULSE_TMMS);
}
2017-03-25 09:21:39 +00:00
// free the messages.
for (int i = 0; i < count; i++) {
SrsSharedPtrMessage* msg = msgs.msgs[i];
2015-07-16 10:42:27 +00:00
queue->enqueue(msg);
}
}
return err;
}
2015-09-22 00:57:31 +00:00
ISrsBufferEncoder::ISrsBufferEncoder()
{
}
2015-09-22 00:57:31 +00:00
ISrsBufferEncoder::~ISrsBufferEncoder()
{
}
SrsTsStreamEncoder::SrsTsStreamEncoder()
{
enc = new SrsTsTransmuxer();
}
SrsTsStreamEncoder::~SrsTsStreamEncoder()
{
srs_freep(enc);
}
srs_error_t SrsTsStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* /*c*/)
{
srs_error_t err = srs_success;
if ((err = enc->initialize(w)) != srs_success) {
return srs_error_wrap(err, "init encoder");
}
return err;
}
srs_error_t SrsTsStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{
srs_error_t err = srs_success;
if ((err = enc->write_audio(timestamp, data, size)) != srs_success) {
return srs_error_wrap(err, "write audio");
}
return err;
}
srs_error_t SrsTsStreamEncoder::write_video(int64_t timestamp, char* data, int size)
{
srs_error_t err = srs_success;
if ((err = enc->write_video(timestamp, data, size)) != srs_success) {
return srs_error_wrap(err, "write video");
}
return err;
}
srs_error_t SrsTsStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{
return srs_success;
}
bool SrsTsStreamEncoder::has_cache()
{
// for ts stream, use gop cache of SrsSource is ok.
return false;
}
srs_error_t SrsTsStreamEncoder::dump_cache(SrsConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/)
{
// for ts stream, ignore cache.
return srs_success;
}
SrsFlvStreamEncoder::SrsFlvStreamEncoder()
{
enc = new SrsFlvTransmuxer();
}
SrsFlvStreamEncoder::~SrsFlvStreamEncoder()
{
srs_freep(enc);
}
srs_error_t SrsFlvStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* /*c*/)
{
srs_error_t err = srs_success;
if ((err = enc->initialize(w)) != srs_success) {
return srs_error_wrap(err, "init encoder");
}
// write flv header.
if ((err = enc->write_header()) != srs_success) {
return srs_error_wrap(err, "write header");
}
return err;
}
srs_error_t SrsFlvStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{
return enc->write_audio(timestamp, data, size);
}
srs_error_t SrsFlvStreamEncoder::write_video(int64_t timestamp, char* data, int size)
{
return enc->write_video(timestamp, data, size);
}
srs_error_t SrsFlvStreamEncoder::write_metadata(int64_t timestamp, char* data, int size)
{
2017-02-12 12:38:39 +00:00
return enc->write_metadata(SrsFrameTypeScript, data, size);
}
bool SrsFlvStreamEncoder::has_cache()
{
// for flv stream, use gop cache of SrsSource is ok.
return false;
}
srs_error_t SrsFlvStreamEncoder::dump_cache(SrsConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/)
{
// for flv stream, ignore cache.
return srs_success;
}
#ifdef SRS_PERF_FAST_FLV_ENCODER
SrsFastFlvStreamEncoder::SrsFastFlvStreamEncoder()
{
}
SrsFastFlvStreamEncoder::~SrsFastFlvStreamEncoder()
{
}
srs_error_t SrsFastFlvStreamEncoder::write_tags(SrsSharedPtrMessage** msgs, int count)
{
return enc->write_tags(msgs, count);
}
#endif
SrsAacStreamEncoder::SrsAacStreamEncoder()
{
enc = new SrsAacTransmuxer();
cache = NULL;
}
SrsAacStreamEncoder::~SrsAacStreamEncoder()
{
srs_freep(enc);
}
srs_error_t SrsAacStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* c)
{
srs_error_t err = srs_success;
cache = c;
if ((err = enc->initialize(w)) != srs_success) {
return srs_error_wrap(err, "init encoder");
}
return err;
}
srs_error_t SrsAacStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{
return enc->write_audio(timestamp, data, size);
}
srs_error_t SrsAacStreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{
// aac ignore any flv video.
return srs_success;
}
srs_error_t SrsAacStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{
// aac ignore any flv metadata.
return srs_success;
}
bool SrsAacStreamEncoder::has_cache()
{
return true;
}
srs_error_t SrsAacStreamEncoder::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter)
{
srs_assert(cache);
return cache->dump_cache(consumer, jitter);
}
SrsMp3StreamEncoder::SrsMp3StreamEncoder()
{
enc = new SrsMp3Transmuxer();
cache = NULL;
}
SrsMp3StreamEncoder::~SrsMp3StreamEncoder()
{
srs_freep(enc);
}
srs_error_t SrsMp3StreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* c)
{
srs_error_t err = srs_success;
cache = c;
if ((err = enc->initialize(w)) != srs_success) {
return srs_error_wrap(err, "init encoder");
}
if ((err = enc->write_header()) != srs_success) {
return srs_error_wrap(err, "init encoder");
}
return err;
}
srs_error_t SrsMp3StreamEncoder::write_audio(int64_t timestamp, char* data, int size)
{
return enc->write_audio(timestamp, data, size);
}
srs_error_t SrsMp3StreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{
// mp3 ignore any flv video.
return srs_success;
}
srs_error_t SrsMp3StreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
{
// mp3 ignore any flv metadata.
return srs_success;
}
bool SrsMp3StreamEncoder::has_cache()
{
return true;
}
srs_error_t SrsMp3StreamEncoder::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter)
{
srs_assert(cache);
return cache->dump_cache(consumer, jitter);
}
2015-09-22 00:57:31 +00:00
SrsBufferWriter::SrsBufferWriter(ISrsHttpResponseWriter* w)
{
writer = w;
}
2015-09-22 00:57:31 +00:00
SrsBufferWriter::~SrsBufferWriter()
{
}
srs_error_t SrsBufferWriter::open(std::string /*file*/)
{
return srs_success;
}
2015-09-22 00:57:31 +00:00
void SrsBufferWriter::close()
{
}
2015-09-22 00:57:31 +00:00
bool SrsBufferWriter::is_open()
{
return true;
}
2015-09-22 00:57:31 +00:00
int64_t SrsBufferWriter::tellg()
{
return 0;
}
srs_error_t SrsBufferWriter::write(void* buf, size_t count, ssize_t* pnwrite)
{
if (pnwrite) {
*pnwrite = count;
}
return writer->write((char*)buf, (int)count);
}
srs_error_t SrsBufferWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite)
{
return writer->writev(iov, iovcnt, pnwrite);
}
2015-09-22 00:57:31 +00:00
SrsLiveStream::SrsLiveStream(SrsSource* s, SrsRequest* r, SrsBufferCache* c)
{
source = s;
cache = c;
req = r->copy()->as_http();
}
SrsLiveStream::~SrsLiveStream()
{
srs_freep(req);
}
srs_error_t SrsLiveStream::update(SrsSource* s, SrsRequest* r)
{
source = s;
srs_freep(req);
req = r->copy()->as_http();
2017-03-25 09:21:39 +00:00
return srs_success;
}
2017-07-29 13:39:57 +00:00
srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
srs_error_t err = srs_success;
2019-04-05 10:44:50 +00:00
if ((err = http_hooks_on_play(r)) != srs_success) {
return srs_error_wrap(err, "http hook");
}
err = do_serve_http(w, r);
http_hooks_on_stop(r);
return err;
}
srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
{
srs_error_t err = srs_success;
string enc_desc;
2015-09-22 00:57:31 +00:00
ISrsBufferEncoder* enc = NULL;
srs_assert(entry);
if (srs_string_ends_with(entry->pattern, ".flv")) {
w->header()->set_content_type("video/x-flv");
#ifdef SRS_PERF_FAST_FLV_ENCODER
bool realtime = _srs_config->get_realtime_enabled(req->vhost);
if (realtime) {
enc_desc = "FLV";
enc = new SrsFlvStreamEncoder();
} else {
enc_desc = "FastFLV";
enc = new SrsFastFlvStreamEncoder();
}
#else
enc_desc = "FLV";
enc = new SrsFlvStreamEncoder();
#endif
} else if (srs_string_ends_with(entry->pattern, ".aac")) {
w->header()->set_content_type("audio/x-aac");
enc_desc = "AAC";
enc = new SrsAacStreamEncoder();
} else if (srs_string_ends_with(entry->pattern, ".mp3")) {
w->header()->set_content_type("audio/mpeg");
enc_desc = "MP3";
enc = new SrsMp3StreamEncoder();
} else if (srs_string_ends_with(entry->pattern, ".ts")) {
w->header()->set_content_type("video/MP2T");
enc_desc = "TS";
enc = new SrsTsStreamEncoder();
} else {
2017-07-29 13:39:57 +00:00
return srs_error_new(ERROR_HTTP_LIVE_STREAM_EXT, "invalid pattern=%s", entry->pattern.c_str());
}
2015-09-22 00:57:31 +00:00
SrsAutoFree(ISrsBufferEncoder, enc);
// create consumer of souce, ignore gop cache, use the audio gop cache.
SrsConsumer* consumer = NULL;
2017-09-23 14:12:33 +00:00
if ((err = source->create_consumer(NULL, consumer, true, true, !enc->has_cache())) != srs_success) {
return srs_error_wrap(err, "create consumer");
}
SrsAutoFree(SrsConsumer, consumer);
srs_verbose("http: consumer created success.");
2017-03-25 09:21:39 +00:00
SrsPithyPrint* pprint = SrsPithyPrint::create_http_stream();
SrsAutoFree(SrsPithyPrint, pprint);
SrsMessageArray msgs(SRS_PERF_MW_MSGS);
2017-03-25 09:21:39 +00:00
2015-09-17 05:36:02 +00:00
// update the statistic when source disconveried.
SrsStatistic* stat = SrsStatistic::instance();
if ((err = stat->on_client(_srs_context->get_id(), req, NULL, SrsRtmpConnPlay)) != srs_success) {
return srs_error_wrap(err, "stat on client");
2015-09-17 05:36:02 +00:00
}
// the memory writer.
2015-09-22 00:57:31 +00:00
SrsBufferWriter writer(w);
if ((err = enc->initialize(&writer, cache)) != srs_success) {
return srs_error_wrap(err, "init encoder");
}
// if gop cache enabled for encoder, dump to consumer.
if (enc->has_cache()) {
if ((err = enc->dump_cache(consumer, source->jitter())) != srs_success) {
return srs_error_wrap(err, "encoder dump cache");
}
}
#ifdef SRS_PERF_FAST_FLV_ENCODER
SrsFastFlvStreamEncoder* ffe = dynamic_cast<SrsFastFlvStreamEncoder*>(enc);
#endif
2017-03-25 09:21:39 +00:00
// Use receive thread to accept the close event to avoid FD leak.
// @see https://github.com/ossrs/srs/issues/636#issuecomment-298208427
SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
SrsResponseOnlyHttpConn* hc = dynamic_cast<SrsResponseOnlyHttpConn*>(hr->connection());
// Set the socket options for transport.
bool tcp_nodelay = _srs_config->get_tcp_nodelay(req->vhost);
if (tcp_nodelay) {
if ((err = hc->set_tcp_nodelay(tcp_nodelay)) != srs_success) {
return srs_error_wrap(err, "set tcp nodelay");
}
}
2019-04-09 01:39:16 +00:00
srs_utime_t mw_sleep = _srs_config->get_mw_sleep(req->vhost);
if ((err = hc->set_socket_buffer(mw_sleep)) != srs_success) {
2019-04-09 01:39:16 +00:00
return srs_error_wrap(err, "set mw_sleep %" PRId64, mw_sleep);
}
SrsHttpRecvThread* trd = new SrsHttpRecvThread(hc);
SrsAutoFree(SrsHttpRecvThread, trd);
if ((err = trd->start()) != srs_success) {
2017-07-29 13:39:57 +00:00
return srs_error_wrap(err, "start recv thread");
}
srs_trace("FLV %s, encoder=%s, nodelay=%d, mw_sleep=%dms, cache=%d, msgs=%d",
2019-04-09 01:39:16 +00:00
entry->pattern.c_str(), enc_desc.c_str(), tcp_nodelay, int(mw_sleep / SRS_UTIME_MILLISECONDS),
enc->has_cache(), msgs.max);
2015-07-09 05:52:41 +00:00
// TODO: free and erase the disabled entry after all related connections is closed.
while (entry->enabled) {
pprint->elapse();
2017-03-25 09:21:39 +00:00
// Whether client closed the FD.
2017-07-29 13:39:57 +00:00
if ((err = trd->pull()) != srs_success) {
return srs_error_wrap(err, "recv thread");
}
// get messages from consumer.
// each msg in msgs.msgs must be free, for the SrsMessageArray never free them.
int count = 0;
2017-09-23 14:12:33 +00:00
if ((err = consumer->dump_packets(&msgs, count)) != srs_success) {
return srs_error_wrap(err, "consumer dump packets");
}
if (count <= 0) {
// Directly use sleep, donot use consumer wait, because we couldn't awake consumer.
2019-04-09 01:39:16 +00:00
srs_usleep(mw_sleep);
// ignore when nothing got.
continue;
}
2017-03-25 09:21:39 +00:00
if (pprint->can_print()) {
srs_trace("-> " SRS_CONSTS_LOG_HTTP_STREAM " http: got %d msgs, age=%d, min=%d, mw=%d",
2019-04-09 01:39:16 +00:00
count, pprint->age(), SRS_PERF_MW_MIN_MSGS, int(mw_sleep / SRS_UTIME_MILLISECONDS));
}
// sendout all messages.
#ifdef SRS_PERF_FAST_FLV_ENCODER
if (ffe) {
err = ffe->write_tags(msgs.msgs, count);
} else {
err = streaming_send_messages(enc, msgs.msgs, count);
}
#else
err = streaming_send_messages(enc, msgs.msgs, count);
#endif
2017-03-25 09:21:39 +00:00
// free the messages.
for (int i = 0; i < count; i++) {
SrsSharedPtrMessage* msg = msgs.msgs[i];
srs_freep(msg);
}
// check send error code.
if (err != srs_success) {
return srs_error_wrap(err, "send messages");
}
}
2017-07-29 13:39:57 +00:00
return err;
}
2019-04-05 10:44:50 +00:00
srs_error_t SrsLiveStream::http_hooks_on_play(ISrsHttpMessage* r)
{
srs_error_t err = srs_success;
if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) {
return err;
}
// Create request to report for the specified connection.
SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
SrsRequest* nreq = hr->to_request(req->vhost);
SrsAutoFree(SrsRequest, nreq);
// the http hooks will cause context switch,
// so we must copy all hooks for the on_connect may freed.
// @see https://github.com/ossrs/srs/issues/475
vector<string> hooks;
if (true) {
SrsConfDirective* conf = _srs_config->get_vhost_on_play(nreq->vhost);
if (!conf) {
return err;
}
hooks = conf->args;
}
for (int i = 0; i < (int)hooks.size(); i++) {
std::string url = hooks.at(i);
2019-04-05 10:44:50 +00:00
if ((err = SrsHttpHooks::on_play(url, nreq)) != srs_success) {
return srs_error_wrap(err, "http on_play %s", url.c_str());
}
}
return err;
}
void SrsLiveStream::http_hooks_on_stop(ISrsHttpMessage* r)
{
if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) {
return;
}
// Create request to report for the specified connection.
SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
SrsRequest* nreq = hr->to_request(req->vhost);
SrsAutoFree(SrsRequest, nreq);
// the http hooks will cause context switch,
// so we must copy all hooks for the on_connect may freed.
// @see https://github.com/ossrs/srs/issues/475
vector<string> hooks;
if (true) {
SrsConfDirective* conf = _srs_config->get_vhost_on_stop(nreq->vhost);
if (!conf) {
srs_info("ignore the empty http callback: on_stop");
return;
}
hooks = conf->args;
}
for (int i = 0; i < (int)hooks.size(); i++) {
std::string url = hooks.at(i);
SrsHttpHooks::on_stop(url, nreq);
}
return;
}
2018-01-01 11:39:57 +00:00
srs_error_t SrsLiveStream::streaming_send_messages(ISrsBufferEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs)
{
2018-01-01 11:39:57 +00:00
srs_error_t err = srs_success;
for (int i = 0; i < nb_msgs; i++) {
SrsSharedPtrMessage* msg = msgs[i];
if (msg->is_audio()) {
2018-01-01 11:39:57 +00:00
err = enc->write_audio(msg->timestamp, msg->payload, msg->size);
} else if (msg->is_video()) {
2018-01-01 11:39:57 +00:00
err = enc->write_video(msg->timestamp, msg->payload, msg->size);
} else {
2018-01-01 11:39:57 +00:00
err = enc->write_metadata(msg->timestamp, msg->payload, msg->size);
}
2018-01-01 11:39:57 +00:00
if (err != srs_success) {
return srs_error_wrap(err, "send messages");
}
}
2018-01-01 11:39:57 +00:00
return err;
}
2017-01-05 01:08:56 +00:00
SrsLiveEntry::SrsLiveEntry(std::string m)
{
mount = m;
stream = NULL;
cache = NULL;
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
req = NULL;
source = NULL;
2015-10-13 08:06:37 +00:00
std::string ext = srs_path_filext(m);
_is_flv = (ext == ".flv");
_is_ts = (ext == ".ts");
_is_mp3 = (ext == ".mp3");
_is_aac = (ext == ".aac");
}
bool SrsLiveEntry::is_flv()
{
return _is_flv;
}
bool SrsLiveEntry::is_ts()
{
return _is_ts;
}
bool SrsLiveEntry::is_aac()
{
return _is_aac;
}
bool SrsLiveEntry::is_mp3()
{
return _is_mp3;
}
SrsHttpStreamServer::SrsHttpStreamServer(SrsServer* svr)
{
server = svr;
mux.hijack(this);
2015-07-09 05:52:41 +00:00
_srs_config->subscribe(this);
}
SrsHttpStreamServer::~SrsHttpStreamServer()
{
mux.unhijack(this);
2015-07-09 05:52:41 +00:00
_srs_config->unsubscribe(this);
if (true) {
std::map<std::string, SrsLiveEntry*>::iterator it;
for (it = tflvs.begin(); it != tflvs.end(); ++it) {
SrsLiveEntry* entry = it->second;
2015-07-09 05:52:41 +00:00
srs_freep(entry->req);
srs_freep(entry);
}
tflvs.clear();
}
if (true) {
std::map<std::string, SrsLiveEntry*>::iterator it;
for (it = sflvs.begin(); it != sflvs.end(); ++it) {
SrsLiveEntry* entry = it->second;
srs_freep(entry);
}
sflvs.clear();
}
}
2017-06-09 05:29:23 +00:00
srs_error_t SrsHttpStreamServer::initialize()
{
2017-06-09 05:29:23 +00:00
srs_error_t err = srs_success;
// remux rtmp to flv live streaming
if ((err = initialize_flv_streaming()) != srs_success) {
return srs_error_wrap(err, "http flv stream");
}
2017-06-09 05:29:23 +00:00
return err;
}
// TODO: FIXME: rename for HTTP FLV mount.
srs_error_t SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r)
{
2017-06-10 07:20:48 +00:00
srs_error_t err = srs_success;
// the id to identify stream.
std::string sid = r->get_stream_url();
SrsLiveEntry* entry = NULL;
// create stream from template when not found.
if (sflvs.find(sid) == sflvs.end()) {
if (tflvs.find(r->vhost) == tflvs.end()) {
2018-01-01 11:39:57 +00:00
return err;
}
2017-03-25 09:21:39 +00:00
SrsLiveEntry* tmpl = tflvs[r->vhost];
2017-03-25 09:21:39 +00:00
std::string mount = tmpl->mount;
2017-03-25 09:21:39 +00:00
// 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);
2017-03-25 09:21:39 +00:00
// remove the default vhost mount
mount = srs_string_replace(mount, SRS_CONSTS_RTMP_DEFAULT_VHOST"/", "/");
2017-01-05 01:08:56 +00:00
entry = new SrsLiveEntry(mount);
2017-03-25 09:21:39 +00:00
2015-09-22 00:57:31 +00:00
entry->cache = new SrsBufferCache(s, r);
entry->stream = new SrsLiveStream(s, r, entry->cache);
2017-03-25 09:21:39 +00:00
2015-07-14 03:31:17 +00:00
// TODO: FIXME: maybe refine the logic of http remux service.
// if user push streams followed:
// rtmp://test.com/live/stream1
// rtmp://test.com/live/stream2
// and they will using the same template, such as: [vhost]/[app]/[stream].flv
// so, need to free last request object, otherwise, it will cause memory leak.
srs_freep(tmpl->req);
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
tmpl->source = s;
tmpl->req = r->copy()->as_http();
sflvs[sid] = entry;
// mount the http flv stream.
// we must register the handler, then start the thread,
// for the thread will cause thread switch context.
2015-11-11 02:37:50 +00:00
// @see https://github.com/ossrs/srs/issues/404
2017-06-10 07:20:48 +00:00
if ((err = mux.handle(mount, entry->stream)) != srs_success) {
return srs_error_wrap(err, "http: mount flv stream for vhost=%s failed", sid.c_str());
}
// start http stream cache thread
if ((err = entry->cache->start()) != srs_success) {
return srs_error_wrap(err, "http: start stream cache failed");
}
2018-08-05 10:58:23 +00:00
srs_trace("http: mount flv stream for sid=%s, mount=%s", sid.c_str(), mount.c_str());
} else {
entry = sflvs[sid];
entry->stream->update(s, r);
entry->cache->update(s, r);
}
2017-03-25 09:21:39 +00:00
if (entry->stream) {
entry->stream->entry->enabled = true;
return err;
}
return err;
}
void SrsHttpStreamServer::http_unmount(SrsSource* s, SrsRequest* r)
{
std::string sid = r->get_stream_url();
2017-03-25 09:21:39 +00:00
if (sflvs.find(sid) == sflvs.end()) {
return;
}
2017-03-25 09:21:39 +00:00
SrsLiveEntry* entry = sflvs[sid];
entry->stream->entry->enabled = false;
}
2017-09-22 08:14:30 +00:00
srs_error_t SrsHttpStreamServer::on_reload_vhost_added(string vhost)
{
2017-09-22 08:14:30 +00:00
srs_error_t err = srs_success;
2017-03-25 09:21:39 +00:00
2017-09-22 08:14:30 +00:00
if ((err = on_reload_vhost_http_remux_updated(vhost)) != srs_success) {
return srs_error_wrap(err, "reload vhost added");
}
2017-03-25 09:21:39 +00:00
2017-09-22 08:14:30 +00:00
return err;
}
2017-09-22 08:14:30 +00:00
srs_error_t SrsHttpStreamServer::on_reload_vhost_http_remux_updated(string vhost)
2015-07-09 05:52:41 +00:00
{
2017-09-22 08:14:30 +00:00
srs_error_t err = srs_success;
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
if (tflvs.find(vhost) == tflvs.end()) {
if ((err = initialize_flv_entry(vhost)) != srs_success) {
return srs_error_wrap(err, "init flv entry");
2015-07-09 05:52:41 +00:00
}
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
// http mount need SrsRequest and SrsSource param, only create a mapping template entry
// and do mount automatically on playing http flv if this stream is a new http_remux stream.
2017-09-22 08:14:30 +00:00
return err;
2015-07-09 05:52:41 +00:00
}
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
SrsLiveEntry* tmpl = tflvs[vhost];
SrsRequest* req = tmpl->req;
SrsSource* source = tmpl->source;
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
if (source && req) {
// cleanup the exists http remux.
http_unmount(source, req);
}
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
if (!_srs_config->get_vhost_http_remux_enabled(vhost)) {
2017-09-22 08:14:30 +00:00
return err;
2015-07-09 05:52:41 +00:00
}
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
string old_tmpl_mount = tmpl->mount;
string new_tmpl_mount = _srs_config->get_vhost_http_remux_mount(vhost);
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
/**
* TODO: not support to reload different mount url for the time being.
* if the mount is change, need more logical thing to deal with.
* such as erase stream from sflvs and free all related resource.
*/
srs_assert(old_tmpl_mount == new_tmpl_mount);
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
// do http mount directly with SrsRequest and SrsSource if stream is played already.
if (req) {
std::string sid = req->get_stream_url();
2017-01-05 01:08:56 +00:00
2015-07-09 05:52:41 +00:00
// remount stream.
if ((err = http_mount(source, req)) != srs_success) {
return srs_error_wrap(err, "vhost %s http_remux reload failed", vhost.c_str());
2015-07-09 05:52:41 +00:00
}
} else {
// for without SrsRequest and SrsSource if stream is not played yet, do http mount automatically
// when start play this http flv stream.
}
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
srs_trace("vhost %s http_remux reload success", vhost.c_str());
2017-03-25 09:21:39 +00:00
2017-09-22 08:14:30 +00:00
return err;
2015-07-09 05:52:41 +00:00
}
2017-07-29 13:39:57 +00:00
srs_error_t SrsHttpStreamServer::hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph)
{
2017-07-29 13:39:57 +00:00
srs_error_t err = srs_success;
// when handler not the root, we think the handler is ok.
ISrsHttpHandler* h = *ph? *ph : NULL;
if (h && h->entry && h->entry->pattern != "/") {
2017-07-29 13:39:57 +00:00
return err;
}
// only hijack for http streaming, http-flv/ts/mp3/aac.
std::string ext = request->ext();
if (ext.empty()) {
2017-07-29 13:39:57 +00:00
return err;
}
2017-03-25 09:21:39 +00:00
// find the actually request vhost.
SrsConfDirective* vhost = _srs_config->get_vhost(request->host());
if (!vhost || !_srs_config->get_vhost_enabled(vhost)) {
2017-07-29 13:39:57 +00:00
return err;
}
// find the entry template for the stream.
SrsLiveEntry* entry = NULL;
if (true) {
// no http streaming on vhost, ignore.
std::map<std::string, SrsLiveEntry*>::iterator it = tflvs.find(vhost->arg0());
if (it == tflvs.end()) {
2017-07-29 13:39:57 +00:00
return err;
}
2017-01-05 01:08:56 +00:00
// hstrs always enabled.
2015-07-17 06:05:34 +00:00
// for origin, the http stream will be mount already when publish,
// so it must never enter this line for stream already mounted.
// for edge, the http stream is trigger by hstrs and mount by it,
// so we only hijack when only edge and hstrs is on.
entry = it->second;
2017-03-25 09:21:39 +00:00
// check entry and request extension.
if (entry->is_flv()) {
if (ext != ".flv") {
2017-07-29 13:39:57 +00:00
return err;
}
} else if (entry->is_ts()) {
if (ext != ".ts") {
2017-07-29 13:39:57 +00:00
return err;
}
} else if (entry->is_mp3()) {
if (ext != ".mp3") {
2017-07-29 13:39:57 +00:00
return err;
}
} else if (entry->is_aac()) {
if (ext != ".aac") {
2017-07-29 13:39:57 +00:00
return err;
}
} else {
2017-07-29 13:39:57 +00:00
return err;
}
}
2017-03-25 09:21:39 +00:00
// convert to concreate class.
SrsHttpMessage* hreq = dynamic_cast<SrsHttpMessage*>(request);
srs_assert(hreq);
// hijack for entry.
SrsRequest* r = hreq->to_request(vhost->arg0());
SrsAutoFree(SrsRequest, r);
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
std::string sid = r->get_stream_url();
2015-07-17 06:05:34 +00:00
// check whether the http remux is enabled,
// for example, user disable the http flv then reload.
2015-07-09 05:52:41 +00:00
if (sflvs.find(sid) != sflvs.end()) {
2015-07-14 03:31:17 +00:00
SrsLiveEntry* s_entry = sflvs[sid];
if (!s_entry->stream->entry->enabled) {
2015-07-17 06:05:34 +00:00
// only when the http entry is disabled, check the config whether http flv disable,
// for the http flv edge use hijack to trigger the edge ingester, we always mount it
// eventhough the origin does not exists the specified stream.
if (!_srs_config->get_vhost_http_remux_enabled(r->vhost)) {
2017-07-29 13:39:57 +00:00
return srs_error_new(ERROR_HTTP_HIJACK, "stream disabled");
2015-07-17 06:05:34 +00:00
}
2015-07-09 05:52:41 +00:00
}
}
2017-03-25 09:21:39 +00:00
SrsSource* s = NULL;
2017-09-23 14:12:33 +00:00
if ((err = SrsSource::fetch_or_create(r, server, &s)) != srs_success) {
return srs_error_wrap(err, "source create");
}
srs_assert(s != NULL);
2017-03-25 09:21:39 +00:00
// create http streaming handler.
if ((err = http_mount(s, r)) != srs_success) {
return srs_error_wrap(err, "http mount");
}
2017-03-25 09:21:39 +00:00
// use the handler if exists.
if (ph) {
if (sflvs.find(sid) != sflvs.end()) {
entry = sflvs[sid];
*ph = entry->stream;
}
}
// trigger edge to fetch from origin.
bool vhost_is_edge = _srs_config->get_vhost_is_edge(r->vhost);
2017-01-05 01:08:56 +00:00
srs_trace("flv: source url=%s, is_edge=%d, source_id=%d[%d]",
2017-07-29 13:39:57 +00:00
r->get_stream_url().c_str(), vhost_is_edge, s->source_id(), s->source_id());
2017-07-29 13:39:57 +00:00
return err;
}
srs_error_t SrsHttpStreamServer::initialize_flv_streaming()
{
srs_error_t err = srs_success;
2017-03-25 09:21:39 +00:00
// http flv 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;
}
2017-03-25 09:21:39 +00:00
if ((err = initialize_flv_entry(conf->arg0())) != srs_success) {
return srs_error_wrap(err, "init flv entries");
2015-07-10 05:45:21 +00:00
}
}
return err;
2015-07-09 05:52:41 +00:00
}
srs_error_t SrsHttpStreamServer::initialize_flv_entry(std::string vhost)
2015-07-09 05:52:41 +00:00
{
srs_error_t err = srs_success;
2017-03-25 09:21:39 +00:00
2015-07-09 05:52:41 +00:00
if (!_srs_config->get_vhost_http_remux_enabled(vhost)) {
return err;
2015-07-09 05:52:41 +00:00
}
2017-03-25 09:21:39 +00:00
SrsLiveEntry* entry = new SrsLiveEntry(_srs_config->get_vhost_http_remux_mount(vhost));
2015-07-09 05:52:41 +00:00
tflvs[vhost] = entry;
2017-03-25 09:21:39 +00:00
srs_trace("http flv live stream, vhost=%s, mount=%s", vhost.c_str(), entry->mount.c_str());
return err;
}