Blame view

trunk/src/core/srs_core_hls.hpp 5.9 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
/*
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_HLS_HPP
#define SRS_CORE_HLS_HPP

/*
#include <srs_core_hls.hpp>
*/
#include <srs_core.hpp>
32 33
#ifdef SRS_HLS
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
* 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:
	int64_t base_pts;
	int64_t nb_samples;
	int sync_ms;
public:
	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();
};
winlin authored
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
//TODO: refine the ts muxer, do more jobs.
class SrsTSMuxer
{
private:
	int fd;
	std::string path;
public:
	SrsTSMuxer();
	virtual ~SrsTSMuxer();
public:
	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();
};
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 123
* 3.3.2.  EXTINF
* The EXTINF tag specifies the duration of a media segment.
*/
struct SrsM3u8Segment
{
	// 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;
	
	SrsM3u8Segment();
	virtual ~SrsM3u8Segment();
	
	/**
	* update the segment duration.
	*/
	virtual double update_duration(int64_t video_stream_dts);
};

/**
winlin authored
124
* muxer the m3u8 and ts files.
winlin authored
125
*/
winlin authored
126
class SrsM3u8Muxer
127
{
128
private:
129
	std::string app;
winlin authored
130 131
	std::string stream;
private:
winlin authored
132
	std::string hls_path;
133 134
	int hls_fragment;
	int hls_window;
winlin authored
135 136 137 138 139 140 141 142 143 144 145 146 147
private:
	int file_index;
	std::string m3u8;
private:
	/**
	* m3u8 segments.
	*/
	std::vector<SrsM3u8Segment*> segments;
	/**
	* current writing segment.
	*/
	SrsM3u8Segment* current;
	// last known dts
148
	int64_t video_stream_dts;
winlin authored
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
public:
	SrsM3u8Muxer();
	virtual ~SrsM3u8Muxer();
public:
	virtual int update_config(std::string _app, std::string _stream, std::string path, int fragment, int window);
	virtual int segment_open();
	virtual int flush_audio(SrsMpegtsFrame* af, SrsCodecBuffer* ab);
	virtual int flush_video(SrsMpegtsFrame* af, SrsCodecBuffer* ab, SrsMpegtsFrame* vf, SrsCodecBuffer* vb);
	virtual int segment_close();
private:
	virtual int refresh_m3u8();
	virtual int _refresh_m3u8(int& fd, std::string m3u8_file);
	virtual int create_dir();
};

/**
165
* ts need to cache some audio then flush
winlin authored
166
*/
167
class SrsTSCache
winlin authored
168 169 170 171 172 173 174
{
private:
	// current frame and buffer
	SrsMpegtsFrame* af;
	SrsCodecBuffer* ab;
	SrsMpegtsFrame* vf;
	SrsCodecBuffer* vb;
175
private:
winlin authored
176
	// the audio cache buffer start pts, to flush audio if full.
177
	int64_t audio_buffer_start_pts;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	// time jitter for aac
	SrsHlsAacJitter* aac_jitter;
public:
	SrsTSCache();
	virtual ~SrsTSCache();
public:
	/**
	* write audio to cache, if need to flush, flush to muxer.
	*/
	virtual int write_audio(SrsCodec* codec, SrsM3u8Muxer* muxer, int64_t pts, SrsCodecSample* sample);
	/**
	* write video to muxer.
	*/
	virtual int write_video(SrsCodec* codec, SrsM3u8Muxer* muxer, int64_t dts, SrsCodecSample* sample);
	/**
	* flush audio in cache to muxer.
	*/
	virtual int flush_audio(SrsM3u8Muxer* muxer);
private:
	virtual int cache_audio(SrsCodec* codec, SrsCodecSample* sample);
	virtual int cache_video(SrsCodec* codec, SrsCodecSample* sample);
};

/**
* write m3u8 hls.
*/
class SrsHls
{
private:
	SrsM3u8Muxer* muxer;
	SrsTSCache* ts_cache;
winlin authored
209
private:
210
	bool hls_enabled;
211
	SrsSource* source;
212
	SrsCodec* codec;
213
	SrsCodecSample* sample;
214
	SrsRtmpJitter* jitter;
winlin authored
215
	SrsPithyPrint* pithy_print;
216
public:
217
	SrsHls(SrsSource* _source);
218
	virtual ~SrsHls();
219
public:
220 221 222 223
	/**
	* publish stream event, continue to write the m3u8,
	* for the muxer object not destroyed.
	*/
224
	virtual int on_publish(SrsRequest* req);
225 226 227 228
	/**
	* the unpublish event, only close the muxer, donot destroy the 
	* muxer, for when we continue to publish, the m3u8 will continue.
	*/
229
	virtual void on_unpublish();
230 231 232
	/**
	* get some information from metadata, it's optinal.
	*/
233
	virtual int on_meta_data(SrsAmf0Object* metadata);
234 235 236
	/**
	* mux the audio packets to ts.
	*/
237
	virtual int on_audio(SrsSharedPtrMessage* audio);
238 239 240
	/**
	* mux the video packets to ts.
	*/
241
	virtual int on_video(SrsSharedPtrMessage* video);
winlin authored
242
private:
243
	virtual void hls_mux();
244 245
};
246 247
#endif
winlin authored
248
#endif