Blame view

trunk/src/core/srs_core_source.hpp 7.3 KB
winlin authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
The MIT License (MIT)

Copyright (c) 2013 winlin

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_CORE_SOURCE_HPP
#define SRS_CORE_SOURCE_HPP

/*
#include <srs_core_source.hpp>
*/

#include <srs_core.hpp>

#include <map>
#include <vector>
#include <string>
winlin authored
37 38
#include <srs_core_reload.hpp>
winlin authored
39 40 41 42
class SrsSource;
class SrsCommonMessage;
class SrsOnMetaDataPacket;
class SrsSharedPtrMessage;
43
class SrsForwarder;
44
class SrsRequest;
45
#ifdef SRS_HLS
46
class SrsHls;
47
#endif
48 49 50
#ifdef SRS_FFMPEG
class SrsEncoder;
#endif
winlin authored
51 52

/**
53 54
* time jitter detect and correct,
* to ensure the rtmp stream is monotonically.
winlin authored
55
*/
56
class SrsRtmpJitter
winlin authored
57 58
{
private:
59 60
	int64_t last_pkt_time;
	int64_t last_pkt_correct_time;
61 62 63 64 65 66 67
public:
	SrsRtmpJitter();
	virtual ~SrsRtmpJitter();
public:
	/**
	* detect the time jitter and correct it.
	*/
68
	virtual int correct(SrsSharedPtrMessage* msg, int tba, int tbv);
69 70 71 72 73 74 75
	/**
	* get current client time, the last packet time.
	*/
	virtual int get_time();
};

/**
winlin authored
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
* 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);
	/**
102 103 104 105
	* 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.
winlin authored
106 107 108 109 110 111 112 113 114 115 116 117
	*/
	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();
};

/**
118 119 120 121 122 123
* the consumer for SrsSource, that is a play client.
*/
class SrsConsumer
{
private:
	SrsRtmpJitter* jitter;
winlin authored
124
	SrsSource* source;
winlin authored
125
	SrsMessageQueue* queue;
winlin authored
126 127 128 129 130
	bool paused;
public:
	SrsConsumer(SrsSource* _source);
	virtual ~SrsConsumer();
public:
winlin authored
131 132
	virtual void set_queue_size(double queue_size);
public:
winlin authored
133 134 135 136 137 138
	/**
	* get current client time, the last packet time.
	*/
	virtual int get_time();
	/**
	* enqueue an shared ptr message.
139 140 141 142
	* @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.
winlin authored
143
	*/
144
	virtual int enqueue(SrsSharedPtrMessage* msg, int tba, int tbv);
winlin authored
145 146 147 148 149 150 151 152 153 154 155 156 157 158
	/**
	* 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);
};

/**
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
* 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);
};

/**
winlin authored
196 197
* live streaming source.
*/
winlin authored
198
class SrsSource : public ISrsReloadHandler
winlin authored
199 200 201 202 203 204
{
private:
	static std::map<std::string, SrsSource*> pool;
public:
	/**
	* find stream by vhost/app/stream.
winlin authored
205
	* @param req the client request.
winlin authored
206 207 208
	* @return the matched source, never be NULL.
	* @remark stream_url should without port and schema.
	*/
winlin authored
209
	static SrsSource* find(SrsRequest* req);
winlin authored
210
private:
winlin authored
211 212
	// deep copy of client request.
	SrsRequest* req;
winlin authored
213
	// to delivery stream to clients.
winlin authored
214
	std::vector<SrsConsumer*> consumers;
215 216 217 218
	// hls handler.
#ifdef SRS_HLS
	SrsHls* hls;
#endif
219 220 221 222
	// transcoding handler.
#ifdef SRS_FFMPEG
	SrsEncoder* encoder;
#endif
223 224
	// gop cache for client fast startup.
	SrsGopCache* gop_cache;
winlin authored
225 226
	// to forward stream to other servers
	std::vector<SrsForwarder*> forwarders;
winlin authored
227 228 229 230
private:
	/**
	* the sample rate of audio in metadata.
	*/
231
	int sample_rate;
winlin authored
232 233 234
	/**
	* the video frame rate in metadata.
	*/
235
	int frame_rate;
236 237 238 239
	/**
	* can publish, true when is not streaming
	*/
	bool _can_publish;
winlin authored
240 241 242 243 244 245 246
private:
	SrsSharedPtrMessage* cache_metadata;
	// the cached video sequence header.
	SrsSharedPtrMessage* cache_sh_video;
	// the cached audio sequence header.
	SrsSharedPtrMessage* cache_sh_audio;
public:
winlin authored
247 248 249 250 251
	/**
	* @param _req the client request object, 
	* 	this object will deep copy it for reload.
	*/
	SrsSource(SrsRequest* _req);
winlin authored
252
	virtual ~SrsSource();
winlin authored
253 254
// interface ISrsReloadHandler
public:
winlin authored
255
	virtual int on_reload_gop_cache(std::string vhost);
winlin authored
256
	virtual int on_reload_queue_length(std::string vhost);
winlin authored
257
	virtual int on_reload_forward(std::string vhost);
258 259
	virtual int on_reload_hls(std::string vhost);
	virtual int on_reload_transcode(std::string vhost);
260
public:
261
	// for the SrsForwarder to callback to request the sequence headers.
262
	virtual int on_forwarder_start(SrsForwarder* forwarder);
263 264
	// for the SrsHls to callback to request the sequence headers.
	virtual int on_hls_start();
winlin authored
265
public:
266
	virtual bool can_publish();
winlin authored
267 268 269
	virtual int on_meta_data(SrsCommonMessage* msg, SrsOnMetaDataPacket* metadata);
	virtual int on_audio(SrsCommonMessage* audio);
	virtual int on_video(SrsCommonMessage* video);
winlin authored
270 271 272 273 274 275
	/**
	* 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);
276
	virtual void on_unpublish();
winlin authored
277 278 279 280
public:
	virtual int create_consumer(SrsConsumer*& consumer);
	virtual void on_consumer_destroy(SrsConsumer* consumer);
	virtual void set_cache(bool enabled);
winlin authored
281 282 283
private:
	virtual int create_forwarders();
	virtual void destroy_forwarders();
winlin authored
284 285
};
winlin authored
286
#endif