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.
|
|
|
|
*/
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
2015-06-14 06:06:39 +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>
|
2015-09-22 01:11:07 +00:00
|
|
|
#include <srs_protocol_utility.hpp>
|
2015-06-14 06:06:39 +00:00
|
|
|
#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>
|
2017-04-30 04:03:31 +00:00
|
|
|
#include <srs_app_recv_thread.hpp>
|
2018-08-11 11:09:56 +00:00
|
|
|
#include <srs_app_http_hooks.hpp>
|
2015-06-14 06:06:39 +00:00
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferCache::SrsBufferCache(SrsSource* s, SrsRequest* r)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2018-08-11 11:09:56 +00:00
|
|
|
req = r->copy()->as_http();
|
2015-06-14 06:06:39 +00:00
|
|
|
source = s;
|
|
|
|
queue = new SrsMessageQueue(true);
|
2017-06-04 11:13:56 +00:00
|
|
|
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-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferCache::~SrsBufferCache()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-05-29 09:56:26 +00:00
|
|
|
srs_freep(trd);
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
srs_freep(queue);
|
|
|
|
srs_freep(req);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsBufferCache::update(SrsSource* s, SrsRequest* r)
|
2016-10-14 08:05:19 +00:00
|
|
|
{
|
|
|
|
srs_freep(req);
|
|
|
|
req = r->copy();
|
|
|
|
source = s;
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2016-10-14 08:05:19 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t SrsBufferCache::start()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
if ((err = trd->start()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "corotine");
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsBufferCache::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
if (fast_cache <= 0) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_wrap(err, "dump packets");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_trace("http: dump cache %d msgs, duration=%dms, cache=%.2fs", queue->size(), queue->duration(), fast_cache);
|
2015-06-14 06:06:39 +00:00
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t SrsBufferCache::cycle()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
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);
|
2017-06-11 10:44:20 +00:00
|
|
|
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.
|
2015-06-14 06:06:39 +00:00
|
|
|
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");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
SrsAutoFree(SrsConsumer, consumer);
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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.
|
2015-06-14 06:06:39 +00:00
|
|
|
// TODO: FIXME: support reload.
|
2015-07-16 10:42:27 +00:00
|
|
|
queue->set_queue_size(fast_cache);
|
2015-06-14 06:06:39 +00:00
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
while (true) {
|
|
|
|
if ((err = trd->pull()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "buffer cache");
|
|
|
|
}
|
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
pprint->elapse();
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count <= 0) {
|
2017-01-17 04:25:30 +00:00
|
|
|
srs_info("http: sleep %dms for no msg", SRS_CONSTS_RTMP_PULSE_TMMS);
|
2015-06-14 06:06:39 +00:00
|
|
|
// 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);
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
// ignore when nothing got.
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
if (pprint->can_print()) {
|
2017-05-06 06:05:22 +00:00
|
|
|
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);
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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);
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
ISrsBufferEncoder::ISrsBufferEncoder()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
ISrsBufferEncoder::~ISrsBufferEncoder()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsTsStreamEncoder::SrsTsStreamEncoder()
|
|
|
|
{
|
2017-02-12 13:50:02 +00:00
|
|
|
enc = new SrsTsTransmuxer();
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsTsStreamEncoder::~SrsTsStreamEncoder()
|
|
|
|
{
|
|
|
|
srs_freep(enc);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsTsStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* /*c*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-09-22 13:50:54 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
2017-09-22 13:50:54 +00:00
|
|
|
if ((err = enc->initialize(w)) != srs_success) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_wrap(err, "init encoder");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsTsStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
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;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsTsStreamEncoder::write_video(int64_t timestamp, char* data, int size)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
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;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsTsStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsTsStreamEncoder::has_cache()
|
|
|
|
{
|
|
|
|
// for ts stream, use gop cache of SrsSource is ok.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsTsStreamEncoder::dump_cache(SrsConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
// for ts stream, ignore cache.
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsFlvStreamEncoder::SrsFlvStreamEncoder()
|
|
|
|
{
|
2017-02-12 13:50:02 +00:00
|
|
|
enc = new SrsFlvTransmuxer();
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsFlvStreamEncoder::~SrsFlvStreamEncoder()
|
|
|
|
{
|
|
|
|
srs_freep(enc);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsFlvStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* /*c*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = enc->initialize(w)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init encoder");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// write flv header.
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = enc->write_header()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "write header");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsFlvStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
return enc->write_audio(timestamp, data, size);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsFlvStreamEncoder::write_video(int64_t timestamp, char* data, int size)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
return enc->write_video(timestamp, data, size);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsFlvStreamEncoder::write_metadata(int64_t timestamp, char* data, int size)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-02-12 12:38:39 +00:00
|
|
|
return enc->write_metadata(SrsFrameTypeScript, data, size);
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsFlvStreamEncoder::has_cache()
|
|
|
|
{
|
|
|
|
// for flv stream, use gop cache of SrsSource is ok.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsFlvStreamEncoder::dump_cache(SrsConsumer* /*consumer*/, SrsRtmpJitterAlgorithm /*jitter*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
// for flv stream, ignore cache.
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SRS_PERF_FAST_FLV_ENCODER
|
|
|
|
SrsFastFlvStreamEncoder::SrsFastFlvStreamEncoder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsFastFlvStreamEncoder::~SrsFastFlvStreamEncoder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsFastFlvStreamEncoder::write_tags(SrsSharedPtrMessage** msgs, int count)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
return enc->write_tags(msgs, count);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SrsAacStreamEncoder::SrsAacStreamEncoder()
|
|
|
|
{
|
2017-02-12 13:50:02 +00:00
|
|
|
enc = new SrsAacTransmuxer();
|
2015-06-14 06:06:39 +00:00
|
|
|
cache = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsAacStreamEncoder::~SrsAacStreamEncoder()
|
|
|
|
{
|
|
|
|
srs_freep(enc);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsAacStreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* c)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
cache = c;
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = enc->initialize(w)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init encoder");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsAacStreamEncoder::write_audio(int64_t timestamp, char* data, int size)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
return enc->write_audio(timestamp, data, size);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsAacStreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
// aac ignore any flv video.
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsAacStreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
// aac ignore any flv metadata.
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsAacStreamEncoder::has_cache()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsAacStreamEncoder::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
srs_assert(cache);
|
|
|
|
return cache->dump_cache(consumer, jitter);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsMp3StreamEncoder::SrsMp3StreamEncoder()
|
|
|
|
{
|
2017-02-12 13:50:02 +00:00
|
|
|
enc = new SrsMp3Transmuxer();
|
2015-06-14 06:06:39 +00:00
|
|
|
cache = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsMp3StreamEncoder::~SrsMp3StreamEncoder()
|
|
|
|
{
|
|
|
|
srs_freep(enc);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsMp3StreamEncoder::initialize(SrsFileWriter* w, SrsBufferCache* c)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
cache = c;
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = enc->initialize(w)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init encoder");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = enc->write_header()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init encoder");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsMp3StreamEncoder::write_audio(int64_t timestamp, char* data, int size)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
return enc->write_audio(timestamp, data, size);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsMp3StreamEncoder::write_video(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
// mp3 ignore any flv video.
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsMp3StreamEncoder::write_metadata(int64_t /*timestamp*/, char* /*data*/, int /*size*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
// mp3 ignore any flv metadata.
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SrsMp3StreamEncoder::has_cache()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsMp3StreamEncoder::dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
srs_assert(cache);
|
|
|
|
return cache->dump_cache(consumer, jitter);
|
|
|
|
}
|
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferWriter::SrsBufferWriter(ISrsHttpResponseWriter* w)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
writer = w;
|
|
|
|
}
|
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferWriter::~SrsBufferWriter()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsBufferWriter::open(std::string /*file*/)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
void SrsBufferWriter::close()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
bool SrsBufferWriter::is_open()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
int64_t SrsBufferWriter::tellg()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsBufferWriter::write(void* buf, size_t count, ssize_t* pnwrite)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
if (pnwrite) {
|
|
|
|
*pnwrite = count;
|
|
|
|
}
|
|
|
|
return writer->write((char*)buf, (int)count);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsBufferWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
return writer->writev(iov, iovcnt, pnwrite);
|
|
|
|
}
|
|
|
|
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsLiveStream::SrsLiveStream(SrsSource* s, SrsRequest* r, SrsBufferCache* c)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
source = s;
|
|
|
|
cache = c;
|
2018-08-11 11:09:56 +00:00
|
|
|
req = r->copy()->as_http();
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsLiveStream::~SrsLiveStream()
|
|
|
|
{
|
|
|
|
srs_freep(req);
|
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsLiveStream::update(SrsSource* s, SrsRequest* r)
|
2016-10-14 08:05:19 +00:00
|
|
|
{
|
|
|
|
source = s;
|
2018-08-11 11:09:56 +00:00
|
|
|
|
2018-08-11 11:23:51 +00:00
|
|
|
srs_freep(req);
|
|
|
|
req = r->copy()->as_http();
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_success;
|
2016-10-14 08:05:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-29 13:39:57 +00:00
|
|
|
srs_error_t SrsLiveStream::serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-06-11 10:44:20 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
2019-04-05 10:44:50 +00:00
|
|
|
if ((err = http_hooks_on_play(r)) != srs_success) {
|
2018-08-11 11:23:51 +00:00
|
|
|
return srs_error_wrap(err, "http hook");
|
2018-08-11 11:09:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 11:23:51 +00:00
|
|
|
err = do_serve_http(w, r);
|
2018-08-11 11:09:56 +00:00
|
|
|
|
2019-04-05 10:16:05 +00:00
|
|
|
http_hooks_on_stop(r);
|
2018-08-11 11:09:56 +00:00
|
|
|
|
2018-08-11 11:23:51 +00:00
|
|
|
return err;
|
2018-08-11 11:09:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 11:23:51 +00:00
|
|
|
srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r)
|
2018-08-11 11:09:56 +00:00
|
|
|
{
|
2018-08-11 11:23:51 +00:00
|
|
|
srs_error_t err = srs_success;
|
2018-08-11 11:09:56 +00:00
|
|
|
|
2018-08-05 12:30:04 +00:00
|
|
|
string enc_desc;
|
2015-09-22 00:57:31 +00:00
|
|
|
ISrsBufferEncoder* enc = NULL;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
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
|
2018-08-05 12:23:17 +00:00
|
|
|
bool realtime = _srs_config->get_realtime_enabled(req->vhost);
|
|
|
|
if (realtime) {
|
2018-08-05 12:30:04 +00:00
|
|
|
enc_desc = "FLV";
|
2018-08-05 12:23:17 +00:00
|
|
|
enc = new SrsFlvStreamEncoder();
|
|
|
|
} else {
|
2018-08-05 12:30:04 +00:00
|
|
|
enc_desc = "FastFLV";
|
2018-08-05 12:23:17 +00:00
|
|
|
enc = new SrsFastFlvStreamEncoder();
|
|
|
|
}
|
2015-06-14 06:06:39 +00:00
|
|
|
#else
|
2018-08-05 12:30:04 +00:00
|
|
|
enc_desc = "FLV";
|
2015-06-14 06:06:39 +00:00
|
|
|
enc = new SrsFlvStreamEncoder();
|
|
|
|
#endif
|
|
|
|
} else if (srs_string_ends_with(entry->pattern, ".aac")) {
|
|
|
|
w->header()->set_content_type("audio/x-aac");
|
2018-08-05 12:30:04 +00:00
|
|
|
enc_desc = "AAC";
|
2015-06-14 06:06:39 +00:00
|
|
|
enc = new SrsAacStreamEncoder();
|
|
|
|
} else if (srs_string_ends_with(entry->pattern, ".mp3")) {
|
|
|
|
w->header()->set_content_type("audio/mpeg");
|
2018-08-05 12:30:04 +00:00
|
|
|
enc_desc = "MP3";
|
2015-06-14 06:06:39 +00:00
|
|
|
enc = new SrsMp3StreamEncoder();
|
|
|
|
} else if (srs_string_ends_with(entry->pattern, ".ts")) {
|
|
|
|
w->header()->set_content_type("video/MP2T");
|
2018-08-05 12:30:04 +00:00
|
|
|
enc_desc = "TS";
|
2015-06-14 06:06:39 +00:00
|
|
|
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-06-14 06:06:39 +00:00
|
|
|
}
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsAutoFree(ISrsBufferEncoder, enc);
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
// 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");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
SrsAutoFree(SrsConsumer, consumer);
|
|
|
|
srs_verbose("http: consumer created success.");
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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();
|
2017-12-31 04:11:48 +00:00
|
|
|
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
|
|
|
}
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
// the memory writer.
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferWriter writer(w);
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = enc->initialize(&writer, cache)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init encoder");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if gop cache enabled for encoder, dump to consumer.
|
|
|
|
if (enc->has_cache()) {
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = enc->dump_cache(consumer, source->jitter())) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "encoder dump cache");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef SRS_PERF_FAST_FLV_ENCODER
|
|
|
|
SrsFastFlvStreamEncoder* ffe = dynamic_cast<SrsFastFlvStreamEncoder*>(enc);
|
|
|
|
#endif
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2017-04-30 04:03:31 +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());
|
|
|
|
|
2018-08-05 12:30:04 +00:00
|
|
|
// 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);
|
2018-08-05 12:30:04 +00:00
|
|
|
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);
|
2018-08-05 12:30:04 +00:00
|
|
|
}
|
2018-08-05 12:23:17 +00:00
|
|
|
|
2017-04-30 04:03:31 +00:00
|
|
|
SrsHttpRecvThread* trd = new SrsHttpRecvThread(hc);
|
|
|
|
SrsAutoFree(SrsHttpRecvThread, trd);
|
|
|
|
|
2017-06-11 10:44:20 +00:00
|
|
|
if ((err = trd->start()) != srs_success) {
|
2017-07-29 13:39:57 +00:00
|
|
|
return srs_error_wrap(err, "start recv thread");
|
2017-04-30 04:03:31 +00:00
|
|
|
}
|
2018-08-05 12:30:04 +00:00
|
|
|
|
|
|
|
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) {
|
2015-06-14 06:06:39 +00:00
|
|
|
pprint->elapse();
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2017-04-30 04:03:31 +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");
|
2017-04-30 04:03:31 +00:00
|
|
|
}
|
2015-06-14 06:06: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");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count <= 0) {
|
2018-08-05 12:30:04 +00:00
|
|
|
// 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);
|
2015-06-14 06:06:39 +00:00
|
|
|
// ignore when nothing got.
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
if (pprint->can_print()) {
|
2018-08-05 12:30:04 +00:00
|
|
|
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));
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendout all messages.
|
|
|
|
#ifdef SRS_PERF_FAST_FLV_ENCODER
|
|
|
|
if (ffe) {
|
2017-12-31 04:11:48 +00:00
|
|
|
err = ffe->write_tags(msgs.msgs, count);
|
2015-06-14 06:06:39 +00:00
|
|
|
} else {
|
2017-12-31 04:11:48 +00:00
|
|
|
err = streaming_send_messages(enc, msgs.msgs, count);
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
#else
|
2017-12-31 04:11:48 +00:00
|
|
|
err = streaming_send_messages(enc, msgs.msgs, count);
|
2015-06-14 06:06:39 +00:00
|
|
|
#endif
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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.
|
2017-12-31 04:11:48 +00:00
|
|
|
if (err != srs_success) {
|
|
|
|
return srs_error_wrap(err, "send messages");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-29 13:39:57 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 10:44:50 +00:00
|
|
|
srs_error_t SrsLiveStream::http_hooks_on_play(ISrsHttpMessage* r)
|
2018-08-11 11:09:56 +00:00
|
|
|
{
|
2018-08-11 11:23:51 +00:00
|
|
|
srs_error_t err = srs_success;
|
2018-08-11 11:09:56 +00:00
|
|
|
|
|
|
|
if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) {
|
2018-08-11 11:23:51 +00:00
|
|
|
return err;
|
2018-08-11 11:09:56 +00:00
|
|
|
}
|
2019-04-05 10:16:05 +00:00
|
|
|
|
|
|
|
// Create request to report for the specified connection.
|
|
|
|
SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
|
|
|
|
SrsRequest* nreq = hr->to_request(req->vhost);
|
|
|
|
SrsAutoFree(SrsRequest, nreq);
|
2018-08-11 11:09:56 +00:00
|
|
|
|
|
|
|
// 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) {
|
2019-04-05 10:16:05 +00:00
|
|
|
SrsConfDirective* conf = _srs_config->get_vhost_on_play(nreq->vhost);
|
2018-08-11 11:09:56 +00:00
|
|
|
|
|
|
|
if (!conf) {
|
2018-08-11 11:23:51 +00:00
|
|
|
return err;
|
2018-08-11 11:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
2018-08-11 11:09:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-11 11:23:51 +00:00
|
|
|
return err;
|
2018-08-11 11:09:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-05 10:16:05 +00:00
|
|
|
void SrsLiveStream::http_hooks_on_stop(ISrsHttpMessage* r)
|
2018-08-11 11:09:56 +00:00
|
|
|
{
|
|
|
|
if (!_srs_config->get_vhost_http_hooks_enabled(req->vhost)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-04-05 10:16:05 +00:00
|
|
|
|
|
|
|
// Create request to report for the specified connection.
|
|
|
|
SrsHttpMessage* hr = dynamic_cast<SrsHttpMessage*>(r);
|
|
|
|
SrsRequest* nreq = hr->to_request(req->vhost);
|
|
|
|
SrsAutoFree(SrsRequest, nreq);
|
2018-08-11 11:09:56 +00:00
|
|
|
|
|
|
|
// 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) {
|
2019-04-05 10:16:05 +00:00
|
|
|
SrsConfDirective* conf = _srs_config->get_vhost_on_stop(nreq->vhost);
|
2018-08-11 11:09:56 +00:00
|
|
|
|
|
|
|
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);
|
2019-04-05 10:16:05 +00:00
|
|
|
SrsHttpHooks::on_stop(url, nreq);
|
2018-08-11 11:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-01 11:39:57 +00:00
|
|
|
srs_error_t SrsLiveStream::streaming_send_messages(ISrsBufferEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2018-01-01 11:39:57 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
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);
|
2015-06-14 06:06:39 +00:00
|
|
|
} else if (msg->is_video()) {
|
2018-01-01 11:39:57 +00:00
|
|
|
err = enc->write_video(msg->timestamp, msg->payload, msg->size);
|
2015-06-14 06:06:39 +00:00
|
|
|
} else {
|
2018-01-01 11:39:57 +00:00
|
|
|
err = enc->write_metadata(msg->timestamp, msg->payload, msg->size);
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 11:39:57 +00:00
|
|
|
if (err != srs_success) {
|
|
|
|
return srs_error_wrap(err, "send messages");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 11:39:57 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 01:08:56 +00:00
|
|
|
SrsLiveEntry::SrsLiveEntry(std::string m)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
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-06-14 06:06:39 +00:00
|
|
|
|
2015-10-13 08:06:37 +00:00
|
|
|
std::string ext = srs_path_filext(m);
|
2015-06-14 06:06:39 +00:00
|
|
|
_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);
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsHttpStreamServer::~SrsHttpStreamServer()
|
|
|
|
{
|
|
|
|
mux.unhijack(this);
|
2015-07-09 05:52:41 +00:00
|
|
|
_srs_config->unsubscribe(this);
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
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);
|
2015-06-14 06:06:39 +00:00
|
|
|
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()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-06-09 05:29:23 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
// remux rtmp to flv live streaming
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = initialize_flv_streaming()) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http flv stream");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-06-09 05:29:23 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: FIXME: rename for HTTP FLV mount.
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsHttpStreamServer::http_mount(SrsSource* s, SrsRequest* r)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-06-10 07:20:48 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
// 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;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
SrsLiveEntry* tmpl = tflvs[r->vhost];
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
std::string mount = tmpl->mount;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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
|
|
|
|
2015-06-14 06:06: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);
|
2015-06-14 06:06:39 +00:00
|
|
|
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;
|
2018-08-11 11:09:56 +00:00
|
|
|
tmpl->req = r->copy()->as_http();
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
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) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_wrap(err, "http: mount flv stream for vhost=%s failed", sid.c_str());
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start http stream cache thread
|
2017-06-11 10:44:20 +00:00
|
|
|
if ((err = entry->cache->start()) != srs_success) {
|
2017-12-31 04:11:48 +00:00
|
|
|
return srs_error_wrap(err, "http: start stream cache failed");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
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());
|
2015-06-14 06:06:39 +00:00
|
|
|
} else {
|
|
|
|
entry = sflvs[sid];
|
2016-10-14 08:05:19 +00:00
|
|
|
entry->stream->update(s, r);
|
|
|
|
entry->cache->update(s, r);
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
if (entry->stream) {
|
|
|
|
entry->stream->entry->enabled = true;
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SrsHttpStreamServer::http_unmount(SrsSource* s, SrsRequest* r)
|
|
|
|
{
|
|
|
|
std::string sid = r->get_stream_url();
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
if (sflvs.find(sid) == sflvs.end()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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)
|
2015-08-03 05:35:25 +00:00
|
|
|
{
|
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");
|
2015-08-03 05:35:25 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2017-09-22 08:14:30 +00:00
|
|
|
return err;
|
2015-08-03 05:35:25 +00:00
|
|
|
}
|
|
|
|
|
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()) {
|
2017-12-31 04:11:48 +00:00
|
|
|
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.
|
2017-12-31 04:11:48 +00:00
|
|
|
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)
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-07-29 13:39:57 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
|
|
|
// 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;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2015-06-14 06:06:39 +00:00
|
|
|
entry = it->second;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
} else if (entry->is_ts()) {
|
|
|
|
if (ext != ".ts") {
|
2017-07-29 13:39:57 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
} else if (entry->is_mp3()) {
|
|
|
|
if (ext != ".mp3") {
|
2017-07-29 13:39:57 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
} else if (entry->is_aac()) {
|
|
|
|
if (ext != ".aac") {
|
2017-07-29 13:39:57 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-07-29 13:39:57 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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
|
|
|
|
2016-12-13 09:57:49 +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");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
srs_assert(s != NULL);
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
// create http streaming handler.
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = http_mount(s, r)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "http mount");
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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());
|
2015-06-14 06:06:39 +00:00
|
|
|
|
2017-07-29 13:39:57 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsHttpStreamServer::initialize_flv_streaming()
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06: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
|
|
|
|
2017-12-31 04:11:48 +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
|
|
|
}
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
2017-12-31 04:11:48 +00:00
|
|
|
|
|
|
|
return err;
|
2015-07-09 05:52:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
srs_error_t SrsHttpStreamServer::initialize_flv_entry(std::string vhost)
|
2015-07-09 05:52:41 +00:00
|
|
|
{
|
2017-12-31 04:11:48 +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)) {
|
2017-12-31 04:11:48 +00:00
|
|
|
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());
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
return err;
|
2015-06-14 06:06:39 +00:00
|
|
|
}
|
|
|
|
|