winlin

add time to write ts file

... ... @@ -46,8 +46,12 @@ void SrsCodecSample::clear()
cts = 0;
frame_type = SrsCodecVideoAVCFrameReserved;
codec_id = SrsCodecVideoReserved;
avc_packet_type = SrsCodecVideoAVCTypeReserved;
sound_rate = SrsCodecAudioSampleRateReserved;
sound_size = SrsCodecAudioSampleSizeReserved;
sound_type = SrsCodecAudioSoundTypeReserved;
aac_packet_type = SrsCodecAudioTypeReserved;
}
int SrsCodecSample::add_sample(char* bytes, int size)
... ... @@ -79,9 +83,6 @@ SrsCodec::SrsCodec()
video_codec_id = 0;
audio_data_rate = 0;
audio_codec_id = 0;
sound_rate = 0;
sound_size = 0;
sound_type = 0;
profile = 0;
level = 0;
avc_extra_size = 0;
... ... @@ -124,12 +125,15 @@ int SrsCodec::audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample)
int8_t sound_format = stream->read_1bytes();
sound_type = sound_format & 0x01;
sound_size = (sound_format >> 1) & 0x01;
sound_rate = (sound_format >> 2) & 0x01;
int8_t sound_type = sound_format & 0x01;
int8_t sound_size = (sound_format >> 1) & 0x01;
int8_t sound_rate = (sound_format >> 2) & 0x01;
sound_format = (sound_format >> 4) & 0x0f;
audio_codec_id = sound_format;
sample->sound_type = (SrsCodecAudioSoundType)sound_type;
sample->sound_rate = (SrsCodecAudioSampleRate)sound_rate;
sample->sound_size = (SrsCodecAudioSampleSize)sound_size;
// only support aac
if (audio_codec_id != SrsCodecAudioAAC) {
... ... @@ -145,6 +149,7 @@ int SrsCodec::audio_aac_demux(int8_t* data, int size, SrsCodecSample* sample)
}
int8_t aac_packet_type = stream->read_1bytes();
sample->aac_packet_type = (SrsCodecAudioType)aac_packet_type;
if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) {
// AudioSpecificConfig
... ... @@ -206,7 +211,6 @@ int SrsCodec::video_avc_demux(int8_t* data, int size, SrsCodecSample* sample)
frame_type = (frame_type >> 4) & 0x0f;
sample->frame_type = (SrsCodecVideoAVCFrame)frame_type;
sample->codec_id = (SrsCodecVideo)codec_id;
// only support h.264/avc
if (codec_id != SrsCodecVideoAVC) {
... ...
... ... @@ -132,6 +132,7 @@ enum SrsCodecAudio
// 1 = AAC raw
enum SrsCodecAudioType
{
SrsCodecAudioTypeReserved = -1,
SrsCodecAudioTypeSequenceHeader = 0,
SrsCodecAudioTypeRawData = 1,
};
... ... @@ -143,10 +144,12 @@ enum SrsCodecAudioType
// 3 = 44 kHz = 44100 Hz
enum SrsCodecAudioSampleRate
{
SrsCodecAudioSampleRate5512 = 0,
SrsCodecAudioSampleRate11025 = 1,
SrsCodecAudioSampleRate22050 = 2,
SrsCodecAudioSampleRate44100 = 3,
SrsCodecAudioSampleRateReserved = -1,
SrsCodecAudioSampleRate5512 = 0,
SrsCodecAudioSampleRate11025 = 1,
SrsCodecAudioSampleRate22050 = 2,
SrsCodecAudioSampleRate44100 = 3,
};
// Size of each audio sample. This parameter only pertains to
... ... @@ -156,8 +159,10 @@ enum SrsCodecAudioSampleRate
// 1 = 16-bit samples
enum SrsCodecAudioSampleSize
{
SrsCodecAudioSampleSize8bit = 0,
SrsCodecAudioSampleSize16bit = 1,
SrsCodecAudioSampleSizeReserved = -1,
SrsCodecAudioSampleSize8bit = 0,
SrsCodecAudioSampleSize16bit = 1,
};
// Mono or stereo sound
... ... @@ -165,8 +170,10 @@ enum SrsCodecAudioSampleSize
// 1 = Stereo sound
enum SrsCodecAudioSoundType
{
SrsCodecAudioSoundTypeMono = 0,
SrsCodecAudioSoundTypeStereo = 1,
SrsCodecAudioSoundTypeReserved = -1,
SrsCodecAudioSoundTypeMono = 0,
SrsCodecAudioSoundTypeStereo = 1,
};
/**
... ... @@ -188,12 +195,17 @@ public:
SrsCodecBuffer buffers[SRS_MAX_CODEC_SAMPLE];
public:
bool is_video;
// video specified
SrsCodecVideoAVCFrame frame_type;
SrsCodecVideoAVCType avc_packet_type;
// CompositionTime, video_file_format_spec_v10_1.pdf, page 78.
// cts = pts - dts, where dts = flvheader->timestamp.
int32_t cts;
SrsCodecVideoAVCFrame frame_type;
SrsCodecVideo codec_id;
SrsCodecVideoAVCType avc_packet_type;
// audio specified
SrsCodecAudioSampleRate sound_rate;
SrsCodecAudioSampleSize sound_size;
SrsCodecAudioSoundType sound_type;
SrsCodecAudioType aac_packet_type;
public:
SrsCodecSample();
virtual ~SrsCodecSample();
... ... @@ -230,12 +242,6 @@ public:
*/
// @see: SrsCodecAudioType
int audio_codec_id;
// @see: SrsCodecAudioSampleRate
int sound_rate;
// @see: SrsCodecAudioSampleSize
int sound_size;
// @see: SrsCodecAudioSoundType
int sound_type;
int audio_data_rate; // in bps
// the avc extra data, the AVC sequence header,
// without the flv codec header,
... ...
... ... @@ -139,41 +139,11 @@ int SrsHLS::on_meta_data(SrsOnMetaDataPacket* metadata)
if ((prop = obj->get_property("audiodatarate")) != NULL && prop->is_number()) {
codec->audio_data_rate = (int)(1000 * srs_amf0_convert<SrsAmf0Number>(prop)->value);
}
if ((prop = obj->get_property("audiosamplerate")) != NULL && prop->is_number()) {
int sound_rate = (int)srs_amf0_convert<SrsAmf0Number>(prop)->value;
if (sound_rate == 5512) {
codec->sound_rate = SrsCodecAudioSampleRate5512;
} else if (sound_rate == 11025) {
codec->sound_rate = SrsCodecAudioSampleRate11025;
} else if (sound_rate == 22050) {
codec->sound_rate = SrsCodecAudioSampleRate22050;
} else if (sound_rate == 44100) {
codec->sound_rate = SrsCodecAudioSampleRate44100;
} else {
ret = ERROR_HLS_METADATA;
srs_error("invalid sound_rate of metadata: %d, ret=%d", sound_rate, ret);
return ret;
}
}
if ((prop = obj->get_property("audiosamplesize")) != NULL && prop->is_number()) {
int sound_size = (int)srs_amf0_convert<SrsAmf0Number>(prop)->value;
if (sound_size == 16) {
codec->sound_size = SrsCodecAudioSampleSize16bit;
} else if (sound_size == 8) {
codec->sound_size = SrsCodecAudioSampleSize8bit;
} else {
ret = ERROR_HLS_METADATA;
srs_error("invalid sound_size of metadata: %d, ret=%d", sound_size, ret);
return ret;
}
}
if ((prop = obj->get_property("stereo")) != NULL && prop->is_number()) {
if (srs_amf0_convert<SrsAmf0Boolean>(prop)->value) {
codec->sound_type = SrsCodecAudioSoundTypeStereo;
} else {
codec->sound_type = SrsCodecAudioSoundTypeMono;
}
}
// ignore the following, for each flv/rtmp packet contains them:
// audiosamplerate, sample->sound_rate
// audiosamplesize, sample->sound_size
// stereo, sample->sound_type
return ret;
}
... ... @@ -196,7 +166,15 @@ int SrsHLS::on_audio(SrsCommonMessage* audio)
return ret;
}
if ((ret = muxer->write(codec, sample)) != ERROR_SUCCESS) {
// ignore sequence header
if (sample->aac_packet_type == SrsCodecAudioTypeSequenceHeader) {
return ret;
}
u_int32_t timestamp = audio->header.timestamp;
// TODO: correct the timestamp.
if ((ret = muxer->write_audio(timestamp, codec, sample)) != ERROR_SUCCESS) {
return ret;
}
... ... @@ -221,7 +199,15 @@ int SrsHLS::on_video(SrsCommonMessage* video)
return ret;
}
if ((ret = muxer->write(codec, sample)) != ERROR_SUCCESS) {
// ignore sequence header
if (sample->frame_type == SrsCodecVideoAVCFrameKeyFrame && sample->avc_packet_type == SrsCodecVideoAVCTypeSequenceHeader) {
return ret;
}
u_int32_t timestamp = video->header.timestamp;
// TODO: correct the timestamp.
if ((ret = muxer->write_video(timestamp, codec, sample)) != ERROR_SUCCESS) {
return ret;
}
... ... @@ -229,7 +215,7 @@ int SrsHLS::on_video(SrsCommonMessage* video)
}
// @see: ngx_rtmp_mpegts_header
static u_char mpegts_header[] = {
u_int8_t mpegts_header[] = {
/* TS */
0x47, 0x40, 0x00, 0x10, 0x00,
/* PSI */
... ... @@ -329,9 +315,21 @@ int SrsTSMuxer::open(std::string _path)
return ret;
}
int SrsTSMuxer::write(SrsCodec* codec, SrsCodecSample* sample)
int SrsTSMuxer::write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sample)
{
int ret = ERROR_SUCCESS;
static u_int8_t packet[188];
return ret;
}
int SrsTSMuxer::write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sample)
{
int ret = ERROR_SUCCESS;
static u_int8_t packet[188];
return ret;
}
... ...
... ... @@ -66,7 +66,8 @@ public:
virtual ~SrsTSMuxer();
public:
virtual int open(std::string _path);
virtual int write(SrsCodec* codec, SrsCodecSample* sample);
virtual int write_audio(u_int32_t time, SrsCodec* codec, SrsCodecSample* sample);
virtual int write_video(u_int32_t time, SrsCodec* codec, SrsCodecSample* sample);
virtual void close();
};
... ...
... ... @@ -199,7 +199,7 @@ struct SrsMessageHeader
* The 4 bytes are packed in the big-endian order.
* @remark, used as calc timestamp when decode and encode time.
*/
int32_t timestamp;
u_int32_t timestamp;
SrsMessageHeader();
virtual ~SrsMessageHeader();
... ...
... ... @@ -192,7 +192,7 @@ int SrsConsumer::jitter_correct(SrsSharedPtrMessage* msg, int audio_sample_rate,
* 3. last_pkt_correct_time: simply add the positive delta,
* and enforce the time monotonically.
*/
int32_t time = msg->header.timestamp;
u_int32_t time = msg->header.timestamp;
int32_t delta = time - last_pkt_time;
// if jitter detected, reset the delta.
... ... @@ -207,7 +207,7 @@ int SrsConsumer::jitter_correct(SrsSharedPtrMessage* msg, int audio_sample_rate,
}
// sometimes, the time is absolute time, so correct it again.
if (delta > CONST_MAX_JITTER_MS) {
if (delta < 0 || delta > CONST_MAX_JITTER_MS) {
delta = DEFAULT_FRAME_TIME_MS;
}
... ...
... ... @@ -46,8 +46,8 @@ class SrsHLS;
class SrsConsumer
{
private:
int32_t last_pkt_time;
int32_t last_pkt_correct_time;
u_int32_t last_pkt_time;
u_int32_t last_pkt_correct_time;
SrsSource* source;
std::vector<SrsSharedPtrMessage*> msgs;
bool paused;
... ...