mirror of https://github.com/ossrs/srs.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
287 lines
7.3 KiB
C++
287 lines
7.3 KiB
C++
11 years ago
|
/*
|
||
|
The MIT License (MIT)
|
||
|
|
||
11 years ago
|
Copyright (c) 2013-2014 winlin
|
||
11 years ago
|
|
||
|
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.
|
||
|
*/
|
||
|
|
||
11 years ago
|
#ifndef SRS_APP_SOURCE_HPP
|
||
|
#define SRS_APP_SOURCE_HPP
|
||
11 years ago
|
|
||
|
/*
|
||
11 years ago
|
#include <srs_app_source.hpp>
|
||
11 years ago
|
*/
|
||
|
|
||
|
#include <srs_core.hpp>
|
||
|
|
||
|
#include <map>
|
||
|
#include <vector>
|
||
|
#include <string>
|
||
|
|
||
11 years ago
|
#include <srs_app_st.hpp>
|
||
|
#include <srs_app_reload.hpp>
|
||
11 years ago
|
|
||
11 years ago
|
class SrsSource;
|
||
|
class SrsCommonMessage;
|
||
|
class SrsOnMetaDataPacket;
|
||
|
class SrsSharedPtrMessage;
|
||
11 years ago
|
class SrsForwarder;
|
||
11 years ago
|
class SrsRequest;
|
||
11 years ago
|
#ifdef SRS_HLS
|
||
11 years ago
|
class SrsHls;
|
||
11 years ago
|
#endif
|
||
11 years ago
|
#ifdef SRS_FFMPEG
|
||
|
class SrsEncoder;
|
||
|
#endif
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* time jitter detect and correct,
|
||
|
* to ensure the rtmp stream is monotonically.
|
||
11 years ago
|
*/
|
||
11 years ago
|
class SrsRtmpJitter
|
||
11 years ago
|
{
|
||
|
private:
|
||
11 years ago
|
int64_t last_pkt_time;
|
||
|
int64_t last_pkt_correct_time;
|
||
11 years ago
|
public:
|
||
|
SrsRtmpJitter();
|
||
|
virtual ~SrsRtmpJitter();
|
||
|
public:
|
||
|
/**
|
||
|
* detect the time jitter and correct it.
|
||
|
*/
|
||
11 years ago
|
virtual int correct(SrsSharedPtrMessage* msg, int tba, int tbv);
|
||
11 years ago
|
/**
|
||
|
* get current client time, the last packet time.
|
||
|
*/
|
||
|
virtual int get_time();
|
||
|
};
|
||
|
|
||
11 years ago
|
/**
|
||
|
* the message queue for the consumer(client), forwarder.
|
||
|
* we limit the size in seconds, drop old messages(the whole gop) if full.
|
||
|
*/
|
||
|
class SrsMessageQueue
|
||
|
{
|
||
|
private:
|
||
|
int64_t av_start_time;
|
||
|
int64_t av_end_time;
|
||
|
int queue_size_ms;
|
||
|
std::vector<SrsSharedPtrMessage*> msgs;
|
||
|
public:
|
||
|
SrsMessageQueue();
|
||
|
virtual ~SrsMessageQueue();
|
||
|
public:
|
||
|
/**
|
||
|
* set the queue size
|
||
|
* @param queue_size the queue size in seconds.
|
||
|
*/
|
||
|
virtual void set_queue_size(double queue_size);
|
||
|
public:
|
||
|
/**
|
||
|
* enqueue the message, the timestamp always monotonically.
|
||
|
* @param msg, the msg to enqueue, user never free it whatever the return code.
|
||
|
*/
|
||
|
virtual int enqueue(SrsSharedPtrMessage* msg);
|
||
|
/**
|
||
11 years ago
|
* get packets in consumer queue.
|
||
|
* @pmsgs SrsMessages*[], output the prt array.
|
||
|
* @count the count in array.
|
||
|
* @max_count the max count to dequeue, 0 to dequeue all.
|
||
11 years ago
|
*/
|
||
|
virtual int get_packets(int max_count, SrsSharedPtrMessage**& pmsgs, int& count);
|
||
|
private:
|
||
|
/**
|
||
|
* remove a gop from the front.
|
||
|
* if no iframe found, clear it.
|
||
|
*/
|
||
|
virtual void shrink();
|
||
|
virtual void clear();
|
||
|
};
|
||
|
|
||
11 years ago
|
/**
|
||
|
* the consumer for SrsSource, that is a play client.
|
||
|
*/
|
||
|
class SrsConsumer
|
||
|
{
|
||
|
private:
|
||
|
SrsRtmpJitter* jitter;
|
||
11 years ago
|
SrsSource* source;
|
||
11 years ago
|
SrsMessageQueue* queue;
|
||
11 years ago
|
bool paused;
|
||
|
public:
|
||
|
SrsConsumer(SrsSource* _source);
|
||
|
virtual ~SrsConsumer();
|
||
11 years ago
|
public:
|
||
|
virtual void set_queue_size(double queue_size);
|
||
11 years ago
|
public:
|
||
|
/**
|
||
|
* get current client time, the last packet time.
|
||
|
*/
|
||
|
virtual int get_time();
|
||
|
/**
|
||
|
* enqueue an shared ptr message.
|
||
11 years ago
|
* @param tba timebase of audio.
|
||
|
* used to calc the audio time delta if time-jitter detected.
|
||
|
* @param tbv timebase of video.
|
||
|
* used to calc the video time delta if time-jitter detected.
|
||
11 years ago
|
*/
|
||
11 years ago
|
virtual int enqueue(SrsSharedPtrMessage* msg, int tba, int tbv);
|
||
11 years ago
|
/**
|
||
|
* get packets in consumer queue.
|
||
|
* @pmsgs SrsMessages*[], output the prt array.
|
||
|
* @count the count in array.
|
||
|
* @max_count the max count to dequeue, 0 to dequeue all.
|
||
|
*/
|
||
|
virtual int get_packets(int max_count, SrsSharedPtrMessage**& pmsgs, int& count);
|
||
|
/**
|
||
|
* when client send the pause message.
|
||
|
*/
|
||
|
virtual int on_play_client_pause(bool is_pause);
|
||
|
};
|
||
|
|
||
11 years ago
|
/**
|
||
|
* cache a gop of video/audio data,
|
||
|
* delivery at the connect of flash player,
|
||
|
* to enable it to fast startup.
|
||
|
*/
|
||
|
class SrsGopCache
|
||
|
{
|
||
|
private:
|
||
|
/**
|
||
|
* if disabled the gop cache,
|
||
|
* the client will wait for the next keyframe for h264,
|
||
|
* and will be black-screen.
|
||
|
*/
|
||
|
bool enable_gop_cache;
|
||
|
/**
|
||
|
* the video frame count, avoid cache for pure audio stream.
|
||
|
*/
|
||
|
int cached_video_count;
|
||
|
/**
|
||
|
* cached gop.
|
||
|
*/
|
||
|
std::vector<SrsSharedPtrMessage*> gop_cache;
|
||
|
public:
|
||
|
SrsGopCache();
|
||
|
virtual ~SrsGopCache();
|
||
|
public:
|
||
|
virtual void set(bool enabled);
|
||
|
/**
|
||
|
* only for h264 codec
|
||
|
* 1. cache the gop when got h264 video packet.
|
||
|
* 2. clear gop when got keyframe.
|
||
|
*/
|
||
|
virtual int cache(SrsSharedPtrMessage* msg);
|
||
|
virtual void clear();
|
||
|
virtual int dump(SrsConsumer* consumer, int tba, int tbv);
|
||
|
};
|
||
|
|
||
11 years ago
|
/**
|
||
|
* live streaming source.
|
||
|
*/
|
||
11 years ago
|
class SrsSource : public ISrsReloadHandler
|
||
11 years ago
|
{
|
||
|
private:
|
||
|
static std::map<std::string, SrsSource*> pool;
|
||
|
public:
|
||
|
/**
|
||
|
* find stream by vhost/app/stream.
|
||
11 years ago
|
* @param req the client request.
|
||
11 years ago
|
* @return the matched source, never be NULL.
|
||
|
* @remark stream_url should without port and schema.
|
||
|
*/
|
||
11 years ago
|
static SrsSource* find(SrsRequest* req);
|
||
11 years ago
|
private:
|
||
11 years ago
|
// deep copy of client request.
|
||
|
SrsRequest* req;
|
||
11 years ago
|
// to delivery stream to clients.
|
||
11 years ago
|
std::vector<SrsConsumer*> consumers;
|
||
11 years ago
|
// hls handler.
|
||
|
#ifdef SRS_HLS
|
||
|
SrsHls* hls;
|
||
11 years ago
|
#endif
|
||
|
// transcoding handler.
|
||
|
#ifdef SRS_FFMPEG
|
||
|
SrsEncoder* encoder;
|
||
11 years ago
|
#endif
|
||
|
// gop cache for client fast startup.
|
||
|
SrsGopCache* gop_cache;
|
||
11 years ago
|
// to forward stream to other servers
|
||
|
std::vector<SrsForwarder*> forwarders;
|
||
11 years ago
|
private:
|
||
|
/**
|
||
|
* the sample rate of audio in metadata.
|
||
|
*/
|
||
11 years ago
|
int sample_rate;
|
||
11 years ago
|
/**
|
||
|
* the video frame rate in metadata.
|
||
|
*/
|
||
11 years ago
|
int frame_rate;
|
||
11 years ago
|
/**
|
||
|
* can publish, true when is not streaming
|
||
|
*/
|
||
|
bool _can_publish;
|
||
11 years ago
|
private:
|
||
|
SrsSharedPtrMessage* cache_metadata;
|
||
|
// the cached video sequence header.
|
||
|
SrsSharedPtrMessage* cache_sh_video;
|
||
|
// the cached audio sequence header.
|
||
|
SrsSharedPtrMessage* cache_sh_audio;
|
||
|
public:
|
||
11 years ago
|
/**
|
||
|
* @param _req the client request object,
|
||
|
* this object will deep copy it for reload.
|
||
|
*/
|
||
|
SrsSource(SrsRequest* _req);
|
||
11 years ago
|
virtual ~SrsSource();
|
||
11 years ago
|
// interface ISrsReloadHandler
|
||
|
public:
|
||
11 years ago
|
virtual int on_reload_gop_cache(std::string vhost);
|
||
11 years ago
|
virtual int on_reload_queue_length(std::string vhost);
|
||
11 years ago
|
virtual int on_reload_forward(std::string vhost);
|
||
11 years ago
|
virtual int on_reload_hls(std::string vhost);
|
||
|
virtual int on_reload_transcode(std::string vhost);
|
||
11 years ago
|
public:
|
||
11 years ago
|
// for the SrsForwarder to callback to request the sequence headers.
|
||
11 years ago
|
virtual int on_forwarder_start(SrsForwarder* forwarder);
|
||
11 years ago
|
// for the SrsHls to callback to request the sequence headers.
|
||
|
virtual int on_hls_start();
|
||
11 years ago
|
public:
|
||
11 years ago
|
virtual bool can_publish();
|
||
11 years ago
|
virtual int on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata);
|
||
|
virtual int on_audio(SrsCommonMessage* audio);
|
||
|
virtual int on_video(SrsCommonMessage* video);
|
||
11 years ago
|
/**
|
||
|
* publish stream event notify.
|
||
|
* @param _req the request from client, the source will deep copy it,
|
||
|
* for when reload the request of client maybe invalid.
|
||
|
*/
|
||
|
virtual int on_publish(SrsRequest* _req);
|
||
11 years ago
|
virtual void on_unpublish();
|
||
11 years ago
|
public:
|
||
|
virtual int create_consumer(SrsConsumer*& consumer);
|
||
|
virtual void on_consumer_destroy(SrsConsumer* consumer);
|
||
|
virtual void set_cache(bool enabled);
|
||
11 years ago
|
private:
|
||
|
virtual int create_forwarders();
|
||
|
virtual void destroy_forwarders();
|
||
11 years ago
|
};
|
||
|
|
||
12 years ago
|
#endif
|