Blame view

trunk/src/app/srs_app_hls.hpp 9.2 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_HLS_HPP
#define SRS_APP_HLS_HPP
winlin authored
26 27

/*
28
#include <srs_app_hls.hpp>
winlin authored
29 30 31
*/
#include <srs_core.hpp>
32
#ifdef SRS_AUTO_HLS
33
34
#include <string>
winlin authored
35
#include <vector>
36
37
class SrsSharedPtrMessage;
38
class SrsCodecSample;
winlin authored
39
class SrsCodecBuffer;
40
class SrsMpegtsFrame;
41
class SrsAmf0Object;
winlin authored
42
class SrsRtmpJitter;
43
class SrsTSMuxer;
44
class SrsCodec;
45
class SrsRequest;
winlin authored
46
class SrsPithyPrint;
47
class SrsSource;
48
winlin authored
49
/**
50 51 52 53 54 55 56 57 58
* jitter correct for audio,
* the sample rate 44100/32000 will lost precise,
* when mp4/ts(tbn=90000) covert to flv/rtmp(1000),
* so the Hls on ipad or iphone will corrupt,
* @see nginx-rtmp: est_pts
*/
class SrsHlsAacJitter
{
private:
59 60 61
    int64_t base_pts;
    int64_t nb_samples;
    int sync_ms;
62
public:
63 64 65 66 67 68 69 70 71 72 73 74 75 76
    SrsHlsAacJitter();
    virtual ~SrsHlsAacJitter();
    /**
    * when buffer start, calc the "correct" pts for ts,
    * @param flv_pts, the flv pts calc from flv header timestamp,
    * @return the calc correct pts.
    */
    virtual int64_t on_buffer_start(int64_t flv_pts, int sample_rate);
    /**
    * when buffer continue, muxer donot write to file,
    * the audio buffer continue grow and donot need a pts,
    * for the ts audio PES packet only has one pts at the first time.
    */
    virtual void on_buffer_continue();
77 78
};
79 80 81 82
/**
* write data from frame(header info) and buffer(data) to ts file.
* it's a simple object wrapper for utility from nginx-rtmp: SrsMpegtsWriter
*/
winlin authored
83 84 85
class SrsTSMuxer
{
private:
86 87
    int fd;
    std::string path;
winlin authored
88
public:
89 90
    SrsTSMuxer();
    virtual ~SrsTSMuxer();
winlin authored
91
public:
92 93 94 95
    virtual int open(std::string _path);
    virtual int write_audio(SrsMpegtsFrame* af, SrsCodecBuffer* ab);
    virtual int write_video(SrsMpegtsFrame* vf, SrsCodecBuffer* vb);
    virtual void close();
winlin authored
96 97
};
98
/**
99 100
* the wrapper of m3u8 segment from specification:
*
101 102 103
* 3.3.2.  EXTINF
* The EXTINF tag specifies the duration of a media segment.
*/
104
class SrsHlsSegment
105
{
106
public:
107 108 109 110 111 112 113 114 115 116 117 118
    // duration in seconds in m3u8.
    double duration;
    // sequence number in m3u8.
    int sequence_no;
    // ts uri in m3u8.
    std::string uri;
    // ts full file to write.
    std::string full_path;
    // the muxer to write ts.
    SrsTSMuxer* muxer;
    // current segment start dts for m3u8
    int64_t segment_start_dts;
winlin authored
119 120
    // whether current segement is sequence header.
    bool is_sequence_header;
121
    
122 123
    SrsHlsSegment();
    virtual ~SrsHlsSegment();
124 125 126
    
    /**
    * update the segment duration.
127
    * @current_frame_dts the dts of frame, in tbn of ts.
128
    */
129
    virtual void update_duration(int64_t current_frame_dts);
130 131 132
};

/**
133 134 135 136 137 138
* muxer the HLS stream(m3u8 and ts files).
* generally, the m3u8 muxer only provides methods to open/close segments,
* to flush video/audio, without any mechenisms.
* 
* that is, user must use HlsCache, which will control the methods of muxer,
* and provides HLS mechenisms.
winlin authored
139
*/
140
class SrsHlsMuxer
141
{
142
private:
143 144
    std::string app;
    std::string stream;
winlin authored
145
private:
146 147 148
    std::string hls_path;
    int hls_fragment;
    int hls_window;
winlin authored
149
private:
150 151
    int file_index;
    std::string m3u8;
winlin authored
152
private:
153 154 155
    /**
    * m3u8 segments.
    */
156
    std::vector<SrsHlsSegment*> segments;
157 158 159
    /**
    * current writing segment.
    */
160
    SrsHlsSegment* current;
winlin authored
161
public:
162 163
    SrsHlsMuxer();
    virtual ~SrsHlsMuxer();
winlin authored
164
public:
165
    virtual int update_config(std::string _app, std::string _stream, std::string path, int fragment, int window);
166 167 168 169 170 171
    /**
    * open a new segment(a new ts file),
    * @param segment_start_dts use to calc the segment duration,
    *       use 0 for the first segment of HLS.
    */
    virtual int segment_open(int64_t segment_start_dts);
winlin authored
172
    virtual int on_sequence_header();
173 174 175 176 177
    /**
    * whether video overflow,
    * that is whether the current segment duration >= the segment in config
    */
    virtual bool is_segment_overflow();
178 179
    virtual int flush_audio(SrsMpegtsFrame* af, SrsCodecBuffer* ab);
    virtual int flush_video(SrsMpegtsFrame* af, SrsCodecBuffer* ab, SrsMpegtsFrame* vf, SrsCodecBuffer* vb);
180 181 182 183 184
    /**
    * close segment(ts).
    * @param log_desc the description for log.
    */
    virtual int segment_close(std::string log_desc);
winlin authored
185
private:
186 187 188
    virtual int refresh_m3u8();
    virtual int _refresh_m3u8(int& fd, std::string m3u8_file);
    virtual int create_dir();
winlin authored
189 190 191
};

/**
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
* hls stream cache, 
* use to cache hls stream and flush to hls muxer.
* 
* when write stream to ts file:
* video frame will directly flush to M3u8Muxer,
* audio frame need to cache, because it's small and flv tbn problem.
* 
* whatever, the Hls cache used to cache video/audio,
* and flush video/audio to m3u8 muxer if needed.
* 
* about the flv tbn problem:
*   flv tbn is 1/1000, ts tbn is 1/90000,
*   when timestamp convert to flv tbn, it will loose precise,
*   so we must gather audio frame together, and recalc the timestamp @see SrsHlsAacJitter,
*   we use a aac jitter to correct the audio pts.
winlin authored
207
*/
208
class SrsHlsCache
winlin authored
209 210
{
private:
211 212 213 214 215
    // current frame and buffer
    SrsMpegtsFrame* af;
    SrsCodecBuffer* ab;
    SrsMpegtsFrame* vf;
    SrsCodecBuffer* vb;
216
private:
217 218 219 220
    // the audio cache buffer start pts, to flush audio if full.
    int64_t audio_buffer_start_pts;
    // time jitter for aac
    SrsHlsAacJitter* aac_jitter;
221 222 223 224 225 226 227 228
private:
    /**
    * for pure audio HLS application,
    * the video count used to count the video,
    * if zero and audio buffer overflow, reap the ts,
    * just like we got a keyframe.
    */
    u_int32_t video_count;
229
public:
230 231
    SrsHlsCache();
    virtual ~SrsHlsCache();
232
public:
233
    /**
234
    * when publish or unpublish stream.
235
    */
236 237
    virtual int on_publish(SrsHlsMuxer* muxer, SrsRequest* req, int64_t segment_start_dts);
    virtual int on_unpublish(SrsHlsMuxer* muxer);
238
    /**
winlin authored
239 240 241 242 243 244 245
    * when get sequence header, 
    * must write a #EXT-X-DISCONTINUITY to m3u8.
    * @see: hls-m3u8-draft-pantos-http-live-streaming-12.txt
    * @see: 3.4.11.  EXT-X-DISCONTINUITY
    */
    virtual int on_sequence_header(SrsHlsMuxer* muxer);
    /**
246
    * write audio to cache, if need to flush, flush to muxer.
247
    */
248
    virtual int write_audio(SrsCodec* codec, SrsHlsMuxer* muxer, int64_t pts, SrsCodecSample* sample);
249
    /**
250
    * write video to muxer.
251
    */
252
    virtual int write_video(SrsCodec* codec, SrsHlsMuxer* muxer, int64_t dts, SrsCodecSample* sample);
253
private:
254 255 256 257 258 259
    /**
    * reopen the muxer for a new hls segment,
    * close current segment, open a new segment,
    * then write the key frame to the new segment.
    * so, user must reap_segment then flush_video to hls muxer.
    */
260
    virtual int reap_segment(std::string log_desc, SrsHlsMuxer* muxer, int64_t segment_start_dts);
261 262
    virtual int cache_audio(SrsCodec* codec, SrsCodecSample* sample);
    virtual int cache_video(SrsCodec* codec, SrsCodecSample* sample);
263 264 265
};

/**
266 267
* delivery RTMP stream to HLS(m3u8 and ts),
* SrsHls provides interface with SrsSource.
268
* TODO: FIXME: add utest for hls.
269 270 271 272
*/
class SrsHls
{
private:
273 274
    SrsHlsMuxer* muxer;
    SrsHlsCache* hls_cache;
winlin authored
275
private:
276 277 278 279 280 281
    bool hls_enabled;
    SrsSource* source;
    SrsCodec* codec;
    SrsCodecSample* sample;
    SrsRtmpJitter* jitter;
    SrsPithyPrint* pithy_print;
282 283 284 285 286 287 288 289 290 291 292 293 294 295
    /**
    * we store the stream dts,
    * for when we notice the hls cache to publish,
    * it need to know the segment start dts.
    * 
    * for example. when republish, the stream dts will 
    * monotonically increase, and the ts dts should start 
    * from current dts.
    * 
    * or, simply because the HlsCache never free when unpublish,
    * so when publish or republish it must start at stream dts,
    * not zero dts.
    */
    int64_t stream_dts;
296
public:
297 298
    SrsHls(SrsSource* _source);
    virtual ~SrsHls();
299
public:
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
    /**
    * publish stream event, continue to write the m3u8,
    * for the muxer object not destroyed.
    */
    virtual int on_publish(SrsRequest* req);
    /**
    * the unpublish event, only close the muxer, donot destroy the 
    * muxer, for when we continue to publish, the m3u8 will continue.
    */
    virtual void on_unpublish();
    /**
    * get some information from metadata, it's optinal.
    */
    virtual int on_meta_data(SrsAmf0Object* metadata);
    /**
    * mux the audio packets to ts.
    */
317
    virtual int on_audio(SrsSharedPtrMessage* audio);
318 319 320
    /**
    * mux the video packets to ts.
    */
321
    virtual int on_video(SrsSharedPtrMessage* video);
winlin authored
322
private:
323
    virtual void hls_mux();
324 325
};
326 327
#endif
winlin authored
328
#endif