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.hpp

305 lines
8.9 KiB
C++
Raw Normal View History

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.
*/
#ifndef SRS_APP_HTTP_STREAM_HPP
#define SRS_APP_HTTP_STREAM_HPP
#include <srs_core.hpp>
#include <srs_app_http_conn.hpp>
class SrsAacTransmuxer;
class SrsMp3Transmuxer;
class SrsFlvTransmuxer;
class SrsTsTransmuxer;
/**
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-07-16 10:42:27 +00:00
private:
double fast_cache;
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();
virtual int update(SrsSource* s, SrsRequest* r);
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
{
public:
2015-09-22 00:57:31 +00:00
ISrsBufferEncoder();
virtual ~ISrsBufferEncoder();
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;
/**
2017-03-25 09:21:39 +00:00
* write rtmp video/audio/metadata.
*/
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.
*/
virtual bool has_cache() = 0;
/**
2017-03-25 09:21:39 +00:00
* dumps the cache of encoder to consumer.
*/
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
{
protected:
SrsFlvTransmuxer* enc;
public:
SrsFlvStreamEncoder();
virtual ~SrsFlvStreamEncoder();
public:
2015-09-22 00:57:31 +00:00
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c);
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
*/
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
{
private:
SrsTsTransmuxer* enc;
public:
SrsTsStreamEncoder();
virtual ~SrsTsStreamEncoder();
public:
2015-09-22 00:57:31 +00:00
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c);
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
{
private:
SrsAacTransmuxer* enc;
2015-09-22 00:57:31 +00:00
SrsBufferCache* cache;
public:
SrsAacStreamEncoder();
virtual ~SrsAacStreamEncoder();
public:
2015-09-22 00:57:31 +00:00
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c);
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
{
private:
SrsMp3Transmuxer* enc;
2015-09-22 00:57:31 +00:00
SrsBufferCache* cache;
public:
SrsMp3StreamEncoder();
virtual ~SrsMp3StreamEncoder();
public:
2015-09-22 00:57:31 +00:00
virtual int initialize(SrsFileWriter* w, SrsBufferCache* c);
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
{
private:
ISrsHttpResponseWriter* writer;
public:
2015-09-22 00:57:31 +00:00
SrsBufferWriter(ISrsHttpResponseWriter* w);
virtual ~SrsBufferWriter();
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);
virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite);
};
/**
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.
*/
class SrsLiveStream : public ISrsHttpHandler
{
private:
SrsRequest* req;
SrsSource* source;
2015-09-22 00:57:31 +00:00
SrsBufferCache* cache;
public:
2015-09-22 00:57:31 +00:00
SrsLiveStream(SrsSource* s, SrsRequest* r, SrsBufferCache* c);
virtual ~SrsLiveStream();
virtual int update(SrsSource* s, SrsRequest* r);
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);
};
/**
2017-03-25 09:21:39 +00:00
* the srs live entry
*/
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;
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;
2017-01-05 01:08:56 +00:00
SrsLiveEntry(std::string m);
2017-03-25 09:21: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.
*/
// TODO: Support multiple stream.
class SrsHttpStreamServer : virtual public ISrsReloadHandler
2017-03-25 09:21:39 +00:00
, virtual public ISrsHttpMatchHijacker
{
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
public:
virtual int http_mount(SrsSource* s, SrsRequest* r);
virtual void http_unmount(SrsSource* s, SrsRequest* r);
// interface ISrsReloadHandler.
public:
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);
// 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);
};
#endif