winlin

add time to write ts file

@@ -46,8 +46,12 @@ void SrsCodecSample::clear() @@ -46,8 +46,12 @@ void SrsCodecSample::clear()
46 46
47 cts = 0; 47 cts = 0;
48 frame_type = SrsCodecVideoAVCFrameReserved; 48 frame_type = SrsCodecVideoAVCFrameReserved;
49 - codec_id = SrsCodecVideoReserved;  
50 avc_packet_type = SrsCodecVideoAVCTypeReserved; 49 avc_packet_type = SrsCodecVideoAVCTypeReserved;
  50 +
  51 + sound_rate = SrsCodecAudioSampleRateReserved;
  52 + sound_size = SrsCodecAudioSampleSizeReserved;
  53 + sound_type = SrsCodecAudioSoundTypeReserved;
  54 + aac_packet_type = SrsCodecAudioTypeReserved;
51 } 55 }
52 56
53 int SrsCodecSample::add_sample(char* bytes, int size) 57 int SrsCodecSample::add_sample(char* bytes, int size)
@@ -79,9 +83,6 @@ SrsCodec::SrsCodec() @@ -79,9 +83,6 @@ SrsCodec::SrsCodec()
79 video_codec_id = 0; 83 video_codec_id = 0;
80 audio_data_rate = 0; 84 audio_data_rate = 0;
81 audio_codec_id = 0; 85 audio_codec_id = 0;
82 - sound_rate = 0;  
83 - sound_size = 0;  
84 - sound_type = 0;  
85 profile = 0; 86 profile = 0;
86 level = 0; 87 level = 0;
87 avc_extra_size = 0; 88 avc_extra_size = 0;
@@ -124,12 +125,15 @@ int SrsCodec::audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample) @@ -124,12 +125,15 @@ int SrsCodec::audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample)
124 125
125 int8_t sound_format = stream->read_1bytes(); 126 int8_t sound_format = stream->read_1bytes();
126 127
127 - sound_type = sound_format & 0x01;  
128 - sound_size = (sound_format >> 1) & 0x01;  
129 - sound_rate = (sound_format >> 2) & 0x01; 128 + int8_t sound_type = sound_format & 0x01;
  129 + int8_t sound_size = (sound_format >> 1) & 0x01;
  130 + int8_t sound_rate = (sound_format >> 2) & 0x01;
130 sound_format = (sound_format >> 4) & 0x0f; 131 sound_format = (sound_format >> 4) & 0x0f;
131 132
132 audio_codec_id = sound_format; 133 audio_codec_id = sound_format;
  134 + sample->sound_type = (SrsCodecAudioSoundType)sound_type;
  135 + sample->sound_rate = (SrsCodecAudioSampleRate)sound_rate;
  136 + sample->sound_size = (SrsCodecAudioSampleSize)sound_size;
133 137
134 // only support aac 138 // only support aac
135 if (audio_codec_id != SrsCodecAudioAAC) { 139 if (audio_codec_id != SrsCodecAudioAAC) {
@@ -145,6 +149,7 @@ int SrsCodec::audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample) @@ -145,6 +149,7 @@ int SrsCodec::audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample)
145 } 149 }
146 150
147 int8_t aac_packet_type = stream->read_1bytes(); 151 int8_t aac_packet_type = stream->read_1bytes();
  152 + sample->aac_packet_type = (SrsCodecAudioType)aac_packet_type;
148 153
149 if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) { 154 if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) {
150 // AudioSpecificConfig 155 // AudioSpecificConfig
@@ -206,7 +211,6 @@ int SrsCodec::video_avc_demux(int8_t* data, int size, SrsCodecSample* sample) @@ -206,7 +211,6 @@ int SrsCodec::video_avc_demux(int8_t* data, int size, SrsCodecSample* sample)
206 frame_type = (frame_type >> 4) & 0x0f; 211 frame_type = (frame_type >> 4) & 0x0f;
207 212
208 sample->frame_type = (SrsCodecVideoAVCFrame)frame_type; 213 sample->frame_type = (SrsCodecVideoAVCFrame)frame_type;
209 - sample->codec_id = (SrsCodecVideo)codec_id;  
210 214
211 // only support h.264/avc 215 // only support h.264/avc
212 if (codec_id != SrsCodecVideoAVC) { 216 if (codec_id != SrsCodecVideoAVC) {
@@ -132,6 +132,7 @@ enum SrsCodecAudio @@ -132,6 +132,7 @@ enum SrsCodecAudio
132 // 1 = AAC raw 132 // 1 = AAC raw
133 enum SrsCodecAudioType 133 enum SrsCodecAudioType
134 { 134 {
  135 + SrsCodecAudioTypeReserved = -1,
135 SrsCodecAudioTypeSequenceHeader = 0, 136 SrsCodecAudioTypeSequenceHeader = 0,
136 SrsCodecAudioTypeRawData = 1, 137 SrsCodecAudioTypeRawData = 1,
137 }; 138 };
@@ -143,10 +144,12 @@ enum SrsCodecAudioType @@ -143,10 +144,12 @@ enum SrsCodecAudioType
143 // 3 = 44 kHz = 44100 Hz 144 // 3 = 44 kHz = 44100 Hz
144 enum SrsCodecAudioSampleRate 145 enum SrsCodecAudioSampleRate
145 { 146 {
146 - SrsCodecAudioSampleRate5512 = 0,  
147 - SrsCodecAudioSampleRate11025 = 1,  
148 - SrsCodecAudioSampleRate22050 = 2,  
149 - SrsCodecAudioSampleRate44100 = 3, 147 + SrsCodecAudioSampleRateReserved = -1,
  148 +
  149 + SrsCodecAudioSampleRate5512 = 0,
  150 + SrsCodecAudioSampleRate11025 = 1,
  151 + SrsCodecAudioSampleRate22050 = 2,
  152 + SrsCodecAudioSampleRate44100 = 3,
150 }; 153 };
151 154
152 // Size of each audio sample. This parameter only pertains to 155 // Size of each audio sample. This parameter only pertains to
@@ -156,8 +159,10 @@ enum SrsCodecAudioSampleRate @@ -156,8 +159,10 @@ enum SrsCodecAudioSampleRate
156 // 1 = 16-bit samples 159 // 1 = 16-bit samples
157 enum SrsCodecAudioSampleSize 160 enum SrsCodecAudioSampleSize
158 { 161 {
159 - SrsCodecAudioSampleSize8bit = 0,  
160 - SrsCodecAudioSampleSize16bit = 1, 162 + SrsCodecAudioSampleSizeReserved = -1,
  163 +
  164 + SrsCodecAudioSampleSize8bit = 0,
  165 + SrsCodecAudioSampleSize16bit = 1,
161 }; 166 };
162 167
163 // Mono or stereo sound 168 // Mono or stereo sound
@@ -165,8 +170,10 @@ enum SrsCodecAudioSampleSize @@ -165,8 +170,10 @@ enum SrsCodecAudioSampleSize
165 // 1 = Stereo sound 170 // 1 = Stereo sound
166 enum SrsCodecAudioSoundType 171 enum SrsCodecAudioSoundType
167 { 172 {
168 - SrsCodecAudioSoundTypeMono = 0,  
169 - SrsCodecAudioSoundTypeStereo = 1, 173 + SrsCodecAudioSoundTypeReserved = -1,
  174 +
  175 + SrsCodecAudioSoundTypeMono = 0,
  176 + SrsCodecAudioSoundTypeStereo = 1,
170 }; 177 };
171 178
172 /** 179 /**
@@ -188,12 +195,17 @@ public: @@ -188,12 +195,17 @@ public:
188 SrsCodecBuffer buffers[SRS_MAX_CODEC_SAMPLE]; 195 SrsCodecBuffer buffers[SRS_MAX_CODEC_SAMPLE];
189 public: 196 public:
190 bool is_video; 197 bool is_video;
  198 + // video specified
  199 + SrsCodecVideoAVCFrame frame_type;
  200 + SrsCodecVideoAVCType avc_packet_type;
191 // CompositionTime, video_file_format_spec_v10_1.pdf, page 78. 201 // CompositionTime, video_file_format_spec_v10_1.pdf, page 78.
192 // cts = pts - dts, where dts = flvheader->timestamp. 202 // cts = pts - dts, where dts = flvheader->timestamp.
193 int32_t cts; 203 int32_t cts;
194 - SrsCodecVideoAVCFrame frame_type;  
195 - SrsCodecVideo codec_id;  
196 - SrsCodecVideoAVCType avc_packet_type; 204 + // audio specified
  205 + SrsCodecAudioSampleRate sound_rate;
  206 + SrsCodecAudioSampleSize sound_size;
  207 + SrsCodecAudioSoundType sound_type;
  208 + SrsCodecAudioType aac_packet_type;
197 public: 209 public:
198 SrsCodecSample(); 210 SrsCodecSample();
199 virtual ~SrsCodecSample(); 211 virtual ~SrsCodecSample();
@@ -230,12 +242,6 @@ public: @@ -230,12 +242,6 @@ public:
230 */ 242 */
231 // @see: SrsCodecAudioType 243 // @see: SrsCodecAudioType
232 int audio_codec_id; 244 int audio_codec_id;
233 - // @see: SrsCodecAudioSampleRate  
234 - int sound_rate;  
235 - // @see: SrsCodecAudioSampleSize  
236 - int sound_size;  
237 - // @see: SrsCodecAudioSoundType  
238 - int sound_type;  
239 int audio_data_rate; // in bps 245 int audio_data_rate; // in bps
240 // the avc extra data, the AVC sequence header, 246 // the avc extra data, the AVC sequence header,
241 // without the flv codec header, 247 // without the flv codec header,
@@ -139,41 +139,11 @@ int SrsHLS::on_meta_data(SrsOnMetaDataPacket* metadata) @@ -139,41 +139,11 @@ int SrsHLS::on_meta_data(SrsOnMetaDataPacket* metadata)
139 if ((prop = obj->get_property("audiodatarate")) != NULL && prop->is_number()) { 139 if ((prop = obj->get_property("audiodatarate")) != NULL && prop->is_number()) {
140 codec->audio_data_rate = (int)(1000 * srs_amf0_convert<SrsAmf0Number>(prop)->value); 140 codec->audio_data_rate = (int)(1000 * srs_amf0_convert<SrsAmf0Number>(prop)->value);
141 } 141 }
142 - if ((prop = obj->get_property("audiosamplerate")) != NULL && prop->is_number()) {  
143 - int sound_rate = (int)srs_amf0_convert<SrsAmf0Number>(prop)->value;  
144 - if (sound_rate == 5512) {  
145 - codec->sound_rate = SrsCodecAudioSampleRate5512;  
146 - } else if (sound_rate == 11025) {  
147 - codec->sound_rate = SrsCodecAudioSampleRate11025;  
148 - } else if (sound_rate == 22050) {  
149 - codec->sound_rate = SrsCodecAudioSampleRate22050;  
150 - } else if (sound_rate == 44100) {  
151 - codec->sound_rate = SrsCodecAudioSampleRate44100;  
152 - } else {  
153 - ret = ERROR_HLS_METADATA;  
154 - srs_error("invalid sound_rate of metadata: %d, ret=%d", sound_rate, ret);  
155 - return ret;  
156 - }  
157 - }  
158 - if ((prop = obj->get_property("audiosamplesize")) != NULL && prop->is_number()) {  
159 - int sound_size = (int)srs_amf0_convert<SrsAmf0Number>(prop)->value;  
160 - if (sound_size == 16) {  
161 - codec->sound_size = SrsCodecAudioSampleSize16bit;  
162 - } else if (sound_size == 8) {  
163 - codec->sound_size = SrsCodecAudioSampleSize8bit;  
164 - } else {  
165 - ret = ERROR_HLS_METADATA;  
166 - srs_error("invalid sound_size of metadata: %d, ret=%d", sound_size, ret);  
167 - return ret;  
168 - }  
169 - }  
170 - if ((prop = obj->get_property("stereo")) != NULL && prop->is_number()) {  
171 - if (srs_amf0_convert<SrsAmf0Boolean>(prop)->value) {  
172 - codec->sound_type = SrsCodecAudioSoundTypeStereo;  
173 - } else {  
174 - codec->sound_type = SrsCodecAudioSoundTypeMono;  
175 - }  
176 - } 142 +
  143 + // ignore the following, for each flv/rtmp packet contains them:
  144 + // audiosamplerate, sample->sound_rate
  145 + // audiosamplesize, sample->sound_size
  146 + // stereo, sample->sound_type
177 147
178 return ret; 148 return ret;
179 } 149 }
@@ -196,7 +166,15 @@ int SrsHLS::on_audio(SrsCommonMessage* audio) @@ -196,7 +166,15 @@ int SrsHLS::on_audio(SrsCommonMessage* audio)
196 return ret; 166 return ret;
197 } 167 }
198 168
199 - if ((ret = muxer->write(codec, sample)) != ERROR_SUCCESS) { 169 + // ignore sequence header
  170 + if (sample->aac_packet_type == SrsCodecAudioTypeSequenceHeader) {
  171 + return ret;
  172 + }
  173 +
  174 + u_int32_t timestamp = audio->header.timestamp;
  175 + // TODO: correct the timestamp.
  176 +
  177 + if ((ret = muxer->write_audio(timestamp, codec, sample)) != ERROR_SUCCESS) {
200 return ret; 178 return ret;
201 } 179 }
202 180
@@ -221,7 +199,15 @@ int SrsHLS::on_video(SrsCommonMessage* video) @@ -221,7 +199,15 @@ int SrsHLS::on_video(SrsCommonMessage* video)
221 return ret; 199 return ret;
222 } 200 }
223 201
224 - if ((ret = muxer->write(codec, sample)) != ERROR_SUCCESS) { 202 + // ignore sequence header
  203 + if (sample->frame_type == SrsCodecVideoAVCFrameKeyFrame && sample->avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) {
  204 + return ret;
  205 + }
  206 +
  207 + u_int32_t timestamp = video->header.timestamp;
  208 + // TODO: correct the timestamp.
  209 +
  210 + if ((ret = muxer->write_video(timestamp, codec, sample)) != ERROR_SUCCESS) {
225 return ret; 211 return ret;
226 } 212 }
227 213
@@ -229,7 +215,7 @@ int SrsHLS::on_video(SrsCommonMessage* video) @@ -229,7 +215,7 @@ int SrsHLS::on_video(SrsCommonMessage* video)
229 } 215 }
230 216
231 // @see: ngx_rtmp_mpegts_header 217 // @see: ngx_rtmp_mpegts_header
232 -static u_char mpegts_header[] = { 218 +u_int8_t mpegts_header[] = {
233 /* TS */ 219 /* TS */
234 0x47, 0x40, 0x00, 0x10, 0x00, 220 0x47, 0x40, 0x00, 0x10, 0x00,
235 /* PSI */ 221 /* PSI */
@@ -329,9 +315,21 @@ int SrsTSMuxer::open(std::string _path) @@ -329,9 +315,21 @@ int SrsTSMuxer::open(std::string _path)
329 return ret; 315 return ret;
330 } 316 }
331 317
332 -int SrsTSMuxer::write(SrsCodec* codec, SrsCodecSample* sample) 318 +int SrsTSMuxer::write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sample)
  319 +{
  320 + int ret = ERROR_SUCCESS;
  321 +
  322 + static u_int8_t packet[188];
  323 +
  324 + return ret;
  325 +}
  326 +
  327 +int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sample)
333 { 328 {
334 int ret = ERROR_SUCCESS; 329 int ret = ERROR_SUCCESS;
  330 +
  331 + static u_int8_t packet[188];
  332 +
335 return ret; 333 return ret;
336 } 334 }
337 335
@@ -66,7 +66,8 @@ public: @@ -66,7 +66,8 @@ public:
66 virtual ~SrsTSMuxer(); 66 virtual ~SrsTSMuxer();
67 public: 67 public:
68 virtual int open(std::string _path); 68 virtual int open(std::string _path);
69 - virtual int write(SrsCodec* codec, SrsCodecSample* sample); 69 + virtual int write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sample);
  70 + virtual int write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sample);
70 virtual void close(); 71 virtual void close();
71 }; 72 };
72 73
@@ -199,7 +199,7 @@ struct SrsMessageHeader @@ -199,7 +199,7 @@ struct SrsMessageHeader
199 * The 4 bytes are packed in the big-endian order. 199 * The 4 bytes are packed in the big-endian order.
200 * @remark, used as calc timestamp when decode and encode time. 200 * @remark, used as calc timestamp when decode and encode time.
201 */ 201 */
202 - int32_t timestamp; 202 + u_int32_t timestamp;
203 203
204 SrsMessageHeader(); 204 SrsMessageHeader();
205 virtual ~SrsMessageHeader(); 205 virtual ~SrsMessageHeader();
@@ -192,7 +192,7 @@ int SrsConsumer::jitter_correct(SrsSharedPtrMessage* msg, int audio_sample_rate, @@ -192,7 +192,7 @@ int SrsConsumer::jitter_correct(SrsSharedPtrMessage* msg, int audio_sample_rate,
192 * 3. last_pkt_correct_time: simply add the positive delta, 192 * 3. last_pkt_correct_time: simply add the positive delta,
193 * and enforce the time monotonically. 193 * and enforce the time monotonically.
194 */ 194 */
195 - int32_t time = msg->header.timestamp; 195 + u_int32_t time = msg->header.timestamp;
196 int32_t delta = time - last_pkt_time; 196 int32_t delta = time - last_pkt_time;
197 197
198 // if jitter detected, reset the delta. 198 // if jitter detected, reset the delta.
@@ -207,7 +207,7 @@ int SrsConsumer::jitter_correct(SrsSharedPtrMessage* msg, int audio_sample_rate, @@ -207,7 +207,7 @@ int SrsConsumer::jitter_correct(SrsSharedPtrMessage* msg, int audio_sample_rate,
207 } 207 }
208 208
209 // sometimes, the time is absolute time, so correct it again. 209 // sometimes, the time is absolute time, so correct it again.
210 - if (delta > CONST_MAX_JITTER_MS) { 210 + if (delta < 0 || delta > CONST_MAX_JITTER_MS) {
211 delta = DEFAULT_FRAME_TIME_MS; 211 delta = DEFAULT_FRAME_TIME_MS;
212 } 212 }
213 213
@@ -46,8 +46,8 @@ class SrsHLS; @@ -46,8 +46,8 @@ class SrsHLS;
46 class SrsConsumer 46 class SrsConsumer
47 { 47 {
48 private: 48 private:
49 - int32_t last_pkt_time;  
50 - int32_t last_pkt_correct_time; 49 + u_int32_t last_pkt_time;
  50 + u_int32_t last_pkt_correct_time;
51 SrsSource* source; 51 SrsSource* source;
52 std::vector<SrsSharedPtrMessage*> msgs; 52 std::vector<SrsSharedPtrMessage*> msgs;
53 bool paused; 53 bool paused;