Blame view

trunk/src/app/srs_app_hls.hpp 11.9 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2015 SRS(simple-rtmp-server)
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 33 34
/**
* the HLS section, only available when HLS enabled.
*/
35
#ifdef SRS_AUTO_HLS
36
37
#include <string>
winlin authored
38
#include <vector>
39
40
#include <srs_kernel_codec.hpp>
41
#include <srs_kernel_file.hpp>
42
#include <srs_app_async_call.hpp>
43
44
class SrsSharedPtrMessage;
45
class SrsCodecSample;
46
class SrsAmf0Object;
winlin authored
47
class SrsRtmpJitter;
48
class SrsTSMuxer;
49
class SrsAvcAacCodec;
50
class SrsRequest;
winlin authored
51
class SrsPithyPrint;
52
class SrsSource;
53
class SrsFileWriter;
54
class SrsSimpleBuffer;
55 56
class SrsTsAacJitter;
class SrsTsCache;
57
class SrsHlsSegment;
58
class SrsTsCache;
59
class SrsTsContext;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

/**
* the handler for hls event.
* for example, we use memory only hls for
*/
class ISrsHlsHandler
{
public:
    ISrsHlsHandler();
    virtual ~ISrsHlsHandler();
public:
    /**
    * when publish stream
    */
    virtual int on_hls_publish(SrsRequest* req) = 0;
    /**
    * when update the m3u8 file.
    */
    virtual int on_update_m3u8(SrsRequest* r, std::string m3u8) = 0;
    /**
    * when reap new ts file.
    */
    virtual int on_update_ts(SrsRequest* r, std::string uri, std::string ts) = 0;
    /**
    * when unpublish stream
    */
    virtual int on_hls_unpublish(SrsRequest* req) = 0;
};

/**
* write to file and cache.
*/
class SrsHlsCacheWriter : public SrsFileWriter
{
private:
    SrsFileWriter impl;
    std::string data;
    bool should_write_cache;
    bool should_write_file;
public:
    SrsHlsCacheWriter(bool write_cache, bool write_file);
    virtual ~SrsHlsCacheWriter();
public:
    /**
    * open file writer, can open then close then open...
    */
    virtual int open(std::string file);
    virtual void close();
public:
    virtual bool is_open();
    virtual int64_t tellg();
public:
    /**
    * write to file. 
    * @param pnwrite the output nb_write, NULL to ignore.
    */
    virtual int write(void* buf, size_t count, ssize_t* pnwrite);
public:
    /**
    * get the string cache.
    */
    virtual std::string cache();
};
123
124 125 126
/**
* the wrapper of m3u8 segment from specification:
*
127 128 129
* 3.3.2.  EXTINF
* The EXTINF tag specifies the duration of a media segment.
*/
130
class SrsHlsSegment
131
{
132
public:
133 134 135 136 137 138 139 140 141
    // 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.
142
    SrsHlsCacheWriter* writer;
143 144 145
    SrsTSMuxer* muxer;
    // current segment start dts for m3u8
    int64_t segment_start_dts;
winlin authored
146 147
    // whether current segement is sequence header.
    bool is_sequence_header;
148
public:
149
    SrsHlsSegment(SrsTsContext* c, bool write_cache, bool write_file, SrsCodecAudio ac, SrsCodecVideo vc);
150
    virtual ~SrsHlsSegment();
151
public:
152 153
    /**
    * update the segment duration.
154
    * @current_frame_dts the dts of frame, in tbn of ts.
155
    */
156
    virtual void update_duration(int64_t current_frame_dts);
157 158 159
};

/**
160
 * the hls async call: on_hls
161 162 163 164 165
 */
class SrsDvrAsyncCallOnHls : public ISrsDvrAsyncCall
{
private:
    std::string path;
166 167 168
    std::string ts_url;
    std::string m3u8;
    std::string m3u8_url;
169 170
    int seq_no;
    SrsRequest* req;
171
    double duration;
172
public:
173
    SrsDvrAsyncCallOnHls(SrsRequest* r, std::string p, std::string t, std::string m, std::string mu, int s, double d);
174 175 176 177 178 179 180
    virtual ~SrsDvrAsyncCallOnHls();
public:
    virtual int call();
    virtual std::string to_string();
};

/**
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
 * the hls async call: on_hls_notify
 */
class SrsDvrAsyncCallOnHlsNotify : public ISrsDvrAsyncCall
{
private:
    std::string ts_url;
    SrsRequest* req;
public:
    SrsDvrAsyncCallOnHlsNotify(SrsRequest* r, std::string u);
    virtual ~SrsDvrAsyncCallOnHlsNotify();
public:
    virtual int call();
    virtual std::string to_string();
};

/**
197 198 199 200 201 202
* 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
203
*/
204
class SrsHlsMuxer
205
{
206
private:
207
    SrsRequest* req;
winlin authored
208
private:
209
    std::string hls_entry_prefix;
210
    std::string hls_path;
211
    std::string hls_ts_file;
212
    bool hls_cleanup;
213
    bool hls_wait_keyframe;
214
    std::string m3u8_dir;
215
    double hls_aof_ratio;
216 217
    double hls_fragment;
    double hls_window;
218
    SrsDvrAsyncCallThread* async;
219 220 221
private:
    // whether use floor algorithm for timestamp.
    bool hls_ts_floor;
222
    // the deviation in piece to adjust the fragment to be more
223
    // bigger or smaller.
224
    int deviation_ts;
225 226
    // the previous reap floor timestamp,
    // used to detect the dup or jmp or ts.
winlin authored
227
    int64_t accept_floor_ts;
228
    int64_t previous_floor_ts;
winlin authored
229
private:
230
    int _sequence_no;
231
    int target_duration;
232
    std::string m3u8;
233
    std::string m3u8_url;
winlin authored
234
private:
235
    ISrsHlsHandler* handler;
236
    // TODO: FIXME: supports reload.
237 238 239
    bool should_write_cache;
    bool should_write_file;
private:
240 241 242
    /**
    * m3u8 segments.
    */
243
    std::vector<SrsHlsSegment*> segments;
244 245 246
    /**
    * current writing segment.
    */
247
    SrsHlsSegment* current;
248 249 250
    /**
    * the current audio codec, when open new muxer,
    * set the muxer audio codec.
251
    * @see https://github.com/simple-rtmp-server/srs/issues/301
252 253
    */
    SrsCodecAudio acodec;
254 255
    /**
     * the ts context, to keep cc continous between ts.
256
     * @see https://github.com/simple-rtmp-server/srs/issues/375
257 258
     */
    SrsTsContext* context;
winlin authored
259
public:
260
    SrsHlsMuxer();
261
    virtual ~SrsHlsMuxer();
winlin authored
262
public:
263
    virtual int sequence_no();
264 265
    virtual std::string ts_url();
    virtual double duration();
266
    virtual int deviation();
267
public:
268
    /**
269 270 271 272
    * initialize the hls muxer.
    */
    virtual int initialize(ISrsHlsHandler* h);
    /**
273 274
    * when publish, update the config for muxer.
    */
275 276
    virtual int update_config(SrsRequest* r, std::string entry_prefix,
        std::string path, std::string m3u8_file, std::string ts_file,
277
        double fragment, double window, bool ts_floor, double aof_ratio,
278
        bool cleanup, bool wait_keyframe);
279 280 281 282 283 284
    /**
    * 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
285
    virtual int on_sequence_header();
286
    /**
287 288
    * whether segment overflow,
    * that is whether the current segment duration>=(the segment in config)
289 290
    */
    virtual bool is_segment_overflow();
291
    /**
292 293 294 295
     * whether wait keyframe to reap the ts.
     */
    virtual bool wait_keyframe();
    /**
296 297
    * whether segment absolutely overflow, for pure audio to reap segment,
    * that is whether the current segment duration>=2*(the segment in config)
298
    * @see https://github.com/simple-rtmp-server/srs/issues/151#issuecomment-71155184
299 300
    */
    virtual bool is_segment_absolutely_overflow();
301
public:
302
    virtual int update_acodec(SrsCodecAudio ac);
303 304
    virtual int flush_audio(SrsTsCache* cache);
    virtual int flush_video(SrsTsCache* cache);
305 306 307 308 309
    /**
    * close segment(ts).
    * @param log_desc the description for log.
    */
    virtual int segment_close(std::string log_desc);
winlin authored
310
private:
311
    virtual int refresh_m3u8();
312
    virtual int _refresh_m3u8(std::string m3u8_file);
winlin authored
313 314 315
};

/**
316 317 318 319 320 321 322 323 324 325 326 327 328
* 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,
329
*   so we must gather audio frame together, and recalc the timestamp @see SrsTsAacJitter,
330
*   we use a aac jitter to correct the audio pts.
winlin authored
331
*/
332
class SrsHlsCache
winlin authored
333 334
{
private:
335
    SrsTsCache* cache;
336
public:
337 338
    SrsHlsCache();
    virtual ~SrsHlsCache();
339
public:
340
    /**
341
    * when publish or unpublish stream.
342
    */
343 344
    virtual int on_publish(SrsHlsMuxer* muxer, SrsRequest* req, int64_t segment_start_dts);
    virtual int on_unpublish(SrsHlsMuxer* muxer);
345
    /**
winlin authored
346 347 348 349 350 351 352
    * 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);
    /**
353
    * write audio to cache, if need to flush, flush to muxer.
354
    */
355
    virtual int write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t pts, SrsCodecSample* sample);
356
    /**
357
    * write video to muxer.
358
    */
359
    virtual int write_video(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t dts, SrsCodecSample* sample);
360
private:
361 362 363 364 365 366
    /**
    * 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.
    */
367
    virtual int reap_segment(std::string log_desc, SrsHlsMuxer* muxer, int64_t segment_start_dts);
368 369 370
};

/**
371 372
* delivery RTMP stream to HLS(m3u8 and ts),
* SrsHls provides interface with SrsSource.
373
* TODO: FIXME: add utest for hls.
374 375 376 377
*/
class SrsHls
{
private:
378 379
    SrsHlsMuxer* muxer;
    SrsHlsCache* hls_cache;
380
    ISrsHlsHandler* handler;
winlin authored
381
private:
382 383
    bool hls_enabled;
    SrsSource* source;
384
    SrsAvcAacCodec* codec;
385 386
    SrsCodecSample* sample;
    SrsRtmpJitter* jitter;
387
    SrsPithyPrint* pprint;
388 389 390 391 392 393 394 395 396 397 398 399 400 401
    /**
    * 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;
402
public:
403
    SrsHls();
404
    virtual ~SrsHls();
405
public:
406
    /**
407 408 409 410
    * initialize the hls by handler and source.
    */
    virtual int initialize(SrsSource* s, ISrsHlsHandler* h);
    /**
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    * 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.
426
    * @param shared_audio, directly ptr, copy it if need to save it.
427
    */
428
    virtual int on_audio(SrsSharedPtrMessage* shared_audio);
429 430
    /**
    * mux the video packets to ts.
431
    * @param shared_video, directly ptr, copy it if need to save it.
432
    */
433
    virtual int on_video(SrsSharedPtrMessage* shared_video);
winlin authored
434
private:
435
    virtual void hls_show_mux_log();
436 437
};
438 439
#endif
440
#endif