Blame view

trunk/src/app/srs_app_source.hpp 8.3 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24 25
#ifndef SRS_APP_SOURCE_HPP
#define SRS_APP_SOURCE_HPP
winlin authored
26 27

/*
28
#include <srs_app_source.hpp>
winlin authored
29 30 31 32 33 34 35 36
*/

#include <srs_core.hpp>

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

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

/**
winlin authored
77 78 79 80 81 82
* 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:
83 84 85 86
    int64_t av_start_time;
    int64_t av_end_time;
    int queue_size_ms;
    std::vector<SrsSharedPtrMessage*> msgs;
winlin authored
87
public:
88 89
    SrsMessageQueue();
    virtual ~SrsMessageQueue();
winlin authored
90
public:
91 92 93 94 95
    /**
    * set the queue size
    * @param queue_size the queue size in seconds.
    */
    virtual void set_queue_size(double queue_size);
winlin authored
96
public:
97 98 99 100 101 102 103 104 105 106 107 108
    /**
    * 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);
    /**
    * 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);
winlin authored
109
private:
110 111 112 113 114 115
    /**
    * remove a gop from the front.
    * if no iframe found, clear it.
    */
    virtual void shrink();
    virtual void clear();
winlin authored
116 117 118
};

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

/**
160 161 162 163 164 165 166
* cache a gop of video/audio data,
* delivery at the connect of flash player,
* to enable it to fast startup.
*/
class SrsGopCache
{
private:
167 168 169 170 171 172 173 174 175 176 177 178 179 180
    /**
    * 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;
181
public:
182 183
    SrsGopCache();
    virtual ~SrsGopCache();
184
public:
185 186 187 188 189 190 191 192 193
    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);
194 195 196 197 198 199
    /**
    * used for atc to get the time of gop cache,
    * the atc will adjust the sequence header timestamp to gop cache.
    */
    virtual bool empty();
    virtual int64_t get_start_time();
200 201 202
};

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