2017-03-25 09:21:39 +00:00
|
|
|
/**
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-03-25 13:29:29 +00:00
|
|
|
* Copyright (c) 2013-2017 OSSRS(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
|
|
|
|
|
|
|
#ifndef SRS_APP_HTTP_STREAM_HPP
|
|
|
|
#define SRS_APP_HTTP_STREAM_HPP
|
|
|
|
|
|
|
|
#include <srs_core.hpp>
|
|
|
|
|
|
|
|
#include <srs_app_http_conn.hpp>
|
|
|
|
|
2017-02-12 13:50:02 +00:00
|
|
|
class SrsAacTransmuxer;
|
|
|
|
class SrsMp3Transmuxer;
|
|
|
|
class SrsFlvTransmuxer;
|
|
|
|
class SrsTsTransmuxer;
|
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* for the srs http stream cache,
|
|
|
|
* for example, the audio stream cache to make android(weixin) happy.
|
|
|
|
* we start a thread to shrink the queue.
|
|
|
|
*/
|
2015-09-22 00:57:31 +00:00
|
|
|
class SrsBufferCache : public ISrsEndlessThreadHandler
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
2015-07-16 10:42:27 +00:00
|
|
|
private:
|
|
|
|
double fast_cache;
|
2015-06-14 06:06:39 +00:00
|
|
|
private:
|
|
|
|
SrsMessageQueue* queue;
|
|
|
|
SrsSource* source;
|
|
|
|
SrsRequest* req;
|
|
|
|
SrsEndlessThread* pthread;
|
|
|
|
public:
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferCache(SrsSource* s, SrsRequest* r);
|
|
|
|
virtual ~SrsBufferCache();
|
2016-10-14 08:05:19 +00:00
|
|
|
virtual int update(SrsSource* s, SrsRequest* r);
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
virtual int start();
|
|
|
|
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
|
|
|
|
// interface ISrsEndlessThreadHandler.
|
|
|
|
public:
|
|
|
|
virtual int cycle();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* the stream encoder in some codec, for example, flv or aac.
|
|
|
|
*/
|
2015-09-22 00:57:31 +00:00
|
|
|
class ISrsBufferEncoder
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
public:
|
2015-09-22 00:57:31 +00:00
|
|
|
ISrsBufferEncoder();
|
|
|
|
virtual ~ISrsBufferEncoder();
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* initialize the encoder with file writer(to http response) and stream cache.
|
|
|
|
* @param w the writer to write to http response.
|
|
|
|
* @param c the stream cache for audio stream fast startup.
|
|
|
|
*/
|
2015-09-22 00:57:31 +00:00
|
|
|
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c) = 0;
|
2015-06-14 06:06:39 +00:00
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* write rtmp video/audio/metadata.
|
|
|
|
*/
|
2015-06-14 06:06:39 +00:00
|
|
|
virtual int write_audio(int64_t timestamp, char* data, int size) = 0;
|
|
|
|
virtual int write_video(int64_t timestamp, char* data, int size) = 0;
|
|
|
|
virtual int write_metadata(int64_t timestamp, char* data, int size) = 0;
|
|
|
|
public:
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* for some stream, for example, mp3 and aac, the audio stream,
|
|
|
|
* we use large gop cache in encoder, for the gop cache of SrsSource is ignore audio.
|
|
|
|
* @return true to use gop cache of encoder; otherwise, use SrsSource.
|
|
|
|
*/
|
2015-06-14 06:06:39 +00:00
|
|
|
virtual bool has_cache() = 0;
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* dumps the cache of encoder to consumer.
|
|
|
|
*/
|
2015-06-14 06:06:39 +00:00
|
|
|
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* the flv stream encoder, remux rtmp stream to flv stream.
|
|
|
|
*/
|
2015-09-22 00:57:31 +00:00
|
|
|
class SrsFlvStreamEncoder : public ISrsBufferEncoder
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
protected:
|
2017-02-12 13:50:02 +00:00
|
|
|
SrsFlvTransmuxer* enc;
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
SrsFlvStreamEncoder();
|
|
|
|
virtual ~SrsFlvStreamEncoder();
|
|
|
|
public:
|
2015-09-22 00:57:31 +00:00
|
|
|
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c);
|
2015-06-14 06:06:39 +00:00
|
|
|
virtual int write_audio(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_video(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_metadata(int64_t timestamp, char* data, int size);
|
|
|
|
public:
|
|
|
|
virtual bool has_cache();
|
|
|
|
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef SRS_PERF_FAST_FLV_ENCODER
|
|
|
|
/**
|
|
|
|
* the fast flv stream encoder.
|
2015-11-11 02:37:50 +00:00
|
|
|
* @see https://github.com/ossrs/srs/issues/405
|
2015-06-14 06:06:39 +00:00
|
|
|
*/
|
|
|
|
class SrsFastFlvStreamEncoder : public SrsFlvStreamEncoder
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SrsFastFlvStreamEncoder();
|
|
|
|
virtual ~SrsFastFlvStreamEncoder();
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* write the tags in a time.
|
|
|
|
*/
|
|
|
|
virtual int write_tags(SrsSharedPtrMessage** msgs, int count);
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* the ts stream encoder, remux rtmp stream to ts stream.
|
|
|
|
*/
|
2015-09-22 00:57:31 +00:00
|
|
|
class SrsTsStreamEncoder : public ISrsBufferEncoder
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-02-12 13:50:02 +00:00
|
|
|
SrsTsTransmuxer* enc;
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
SrsTsStreamEncoder();
|
|
|
|
virtual ~SrsTsStreamEncoder();
|
|
|
|
public:
|
2015-09-22 00:57:31 +00:00
|
|
|
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c);
|
2015-06-14 06:06:39 +00:00
|
|
|
virtual int write_audio(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_video(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_metadata(int64_t timestamp, char* data, int size);
|
|
|
|
public:
|
|
|
|
virtual bool has_cache();
|
|
|
|
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* the aac stream encoder, remux rtmp stream to aac stream.
|
|
|
|
*/
|
2015-09-22 00:57:31 +00:00
|
|
|
class SrsAacStreamEncoder : public ISrsBufferEncoder
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-02-12 13:50:02 +00:00
|
|
|
SrsAacTransmuxer* enc;
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferCache* cache;
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
SrsAacStreamEncoder();
|
|
|
|
virtual ~SrsAacStreamEncoder();
|
|
|
|
public:
|
2015-09-22 00:57:31 +00:00
|
|
|
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c);
|
2015-06-14 06:06:39 +00:00
|
|
|
virtual int write_audio(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_video(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_metadata(int64_t timestamp, char* data, int size);
|
|
|
|
public:
|
|
|
|
virtual bool has_cache();
|
|
|
|
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* the mp3 stream encoder, remux rtmp stream to mp3 stream.
|
|
|
|
*/
|
2015-09-22 00:57:31 +00:00
|
|
|
class SrsMp3StreamEncoder : public ISrsBufferEncoder
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-02-12 13:50:02 +00:00
|
|
|
SrsMp3Transmuxer* enc;
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferCache* cache;
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
SrsMp3StreamEncoder();
|
|
|
|
virtual ~SrsMp3StreamEncoder();
|
|
|
|
public:
|
2015-09-22 00:57:31 +00:00
|
|
|
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c);
|
2015-06-14 06:06:39 +00:00
|
|
|
virtual int write_audio(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_video(int64_t timestamp, char* data, int size);
|
|
|
|
virtual int write_metadata(int64_t timestamp, char* data, int size);
|
|
|
|
public:
|
|
|
|
virtual bool has_cache();
|
|
|
|
virtual int dump_cache(SrsConsumer* consumer, SrsRtmpJitterAlgorithm jitter);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* write stream to http response direclty.
|
|
|
|
*/
|
2015-09-22 00:57:31 +00:00
|
|
|
class SrsBufferWriter : public SrsFileWriter
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
ISrsHttpResponseWriter* writer;
|
|
|
|
public:
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferWriter(ISrsHttpResponseWriter* w);
|
|
|
|
virtual ~SrsBufferWriter();
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
virtual int open(std::string file);
|
|
|
|
virtual void close();
|
|
|
|
public:
|
|
|
|
virtual bool is_open();
|
|
|
|
virtual int64_t tellg();
|
|
|
|
public:
|
|
|
|
virtual int write(void* buf, size_t count, ssize_t* pnwrite);
|
2017-01-30 11:35:04 +00:00
|
|
|
virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite);
|
2015-06-14 06:06:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* the flv live stream supports access rtmp in flv over http.
|
|
|
|
* srs will remux rtmp to flv streaming.
|
|
|
|
*/
|
2015-06-14 06:06:39 +00:00
|
|
|
class SrsLiveStream : public ISrsHttpHandler
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsRequest* req;
|
|
|
|
SrsSource* source;
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferCache* cache;
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsLiveStream(SrsSource* s, SrsRequest* r, SrsBufferCache* c);
|
2015-06-14 06:06:39 +00:00
|
|
|
virtual ~SrsLiveStream();
|
2016-10-14 08:05:19 +00:00
|
|
|
virtual int update(SrsSource* s, SrsRequest* r);
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
virtual int serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r);
|
|
|
|
private:
|
2015-09-22 00:57:31 +00:00
|
|
|
virtual int streaming_send_messages(ISrsBufferEncoder* enc, SrsSharedPtrMessage** msgs, int nb_msgs);
|
2015-06-14 06:06:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* the srs live entry
|
|
|
|
*/
|
2015-06-14 06:06:39 +00:00
|
|
|
struct SrsLiveEntry
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
bool _is_flv;
|
|
|
|
bool _is_ts;
|
|
|
|
bool _is_aac;
|
|
|
|
bool _is_mp3;
|
2015-07-09 05:52:41 +00:00
|
|
|
public:
|
|
|
|
SrsRequest* req;
|
|
|
|
SrsSource* source;
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
// for template, the mount contains variables.
|
|
|
|
// for concrete stream, the mount is url to access.
|
|
|
|
std::string mount;
|
|
|
|
|
|
|
|
SrsLiveStream* stream;
|
2015-09-22 00:57:31 +00:00
|
|
|
SrsBufferCache* cache;
|
2015-06-14 06:06:39 +00:00
|
|
|
|
2017-01-05 01:08:56 +00:00
|
|
|
SrsLiveEntry(std::string m);
|
2017-03-25 09:21:39 +00:00
|
|
|
|
2015-06-14 06:06:39 +00:00
|
|
|
bool is_flv();
|
|
|
|
bool is_ts();
|
|
|
|
bool is_mp3();
|
|
|
|
bool is_aac();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-03-25 09:21:39 +00:00
|
|
|
* the http stream server instance,
|
|
|
|
* serve http stream, for example, flv/ts/mp3/aac live stream.
|
|
|
|
*/
|
2017-02-12 13:50:02 +00:00
|
|
|
// TODO: Support multiple stream.
|
2015-06-14 06:06:39 +00:00
|
|
|
class SrsHttpStreamServer : virtual public ISrsReloadHandler
|
2017-03-25 09:21:39 +00:00
|
|
|
, virtual public ISrsHttpMatchHijacker
|
2015-06-14 06:06:39 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
SrsServer* server;
|
|
|
|
public:
|
|
|
|
SrsHttpServeMux mux;
|
|
|
|
// the http live streaming template, to create streams.
|
|
|
|
std::map<std::string, SrsLiveEntry*> tflvs;
|
|
|
|
// the http live streaming streams, crote by template.
|
|
|
|
std::map<std::string, SrsLiveEntry*> sflvs;
|
|
|
|
public:
|
|
|
|
SrsHttpStreamServer(SrsServer* svr);
|
|
|
|
virtual ~SrsHttpStreamServer();
|
|
|
|
public:
|
|
|
|
virtual int initialize();
|
2017-03-25 09:21:39 +00:00
|
|
|
// http flv/ts/mp3/aac stream
|
2015-06-14 06:06:39 +00:00
|
|
|
public:
|
|
|
|
virtual int http_mount(SrsSource* s, SrsRequest* r);
|
|
|
|
virtual void http_unmount(SrsSource* s, SrsRequest* r);
|
|
|
|
// interface ISrsReloadHandler.
|
|
|
|
public:
|
2015-08-03 05:44:24 +00:00
|
|
|
virtual int on_reload_vhost_added(std::string vhost);
|
2015-07-09 05:52:41 +00:00
|
|
|
virtual int on_reload_vhost_http_remux_updated(std::string vhost);
|
2015-06-14 06:06:39 +00:00
|
|
|
// interface ISrsHttpMatchHijacker
|
|
|
|
public:
|
|
|
|
virtual int hijack(ISrsHttpMessage* request, ISrsHttpHandler** ph);
|
|
|
|
private:
|
|
|
|
virtual int initialize_flv_streaming();
|
2015-07-09 05:52:41 +00:00
|
|
|
virtual int initialize_flv_entry(std::string vhost);
|
2015-06-14 06:06:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|