1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

add time to write ts file

This commit is contained in:
winlin 2013-11-24 18:55:39 +08:00
parent 385394315d
commit ca6720aab2
7 changed files with 79 additions and 70 deletions

View file

@ -46,8 +46,12 @@ void SrsCodecSample::clear()
cts = 0; cts = 0;
frame_type = SrsCodecVideoAVCFrameReserved; frame_type = SrsCodecVideoAVCFrameReserved;
codec_id = SrsCodecVideoReserved;
avc_packet_type = SrsCodecVideoAVCTypeReserved; 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) int SrsCodecSample::add_sample(char* bytes, int size)
@ -79,9 +83,6 @@ SrsCodec::SrsCodec()
video_codec_id = 0; video_codec_id = 0;
audio_data_rate = 0; audio_data_rate = 0;
audio_codec_id = 0; audio_codec_id = 0;
sound_rate = 0;
sound_size = 0;
sound_type = 0;
profile = 0; profile = 0;
level = 0; level = 0;
avc_extra_size = 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(); int8_t sound_format = stream->read_1bytes();
sound_type = sound_format & 0x01; int8_t sound_type = sound_format & 0x01;
sound_size = (sound_format >> 1) & 0x01; int8_t sound_size = (sound_format >> 1) & 0x01;
sound_rate = (sound_format >> 2) & 0x01; int8_t sound_rate = (sound_format >> 2) & 0x01;
sound_format = (sound_format >> 4) & 0x0f; sound_format = (sound_format >> 4) & 0x0f;
audio_codec_id = sound_format; 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 // only support aac
if (audio_codec_id != SrsCodecAudioAAC) { 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(); int8_t aac_packet_type = stream->read_1bytes();
sample->aac_packet_type = (SrsCodecAudioType)aac_packet_type;
if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) { if (aac_packet_type == SrsCodecAudioTypeSequenceHeader) {
// AudioSpecificConfig // AudioSpecificConfig
@ -206,7 +211,6 @@ int SrsCodec::video_avc_demux(int8_t* data, int size, SrsCodecSample* sample)
frame_type = (frame_type >> 4) & 0x0f; frame_type = (frame_type >> 4) & 0x0f;
sample->frame_type = (SrsCodecVideoAVCFrame)frame_type; sample->frame_type = (SrsCodecVideoAVCFrame)frame_type;
sample->codec_id = (SrsCodecVideo)codec_id;
// only support h.264/avc // only support h.264/avc
if (codec_id != SrsCodecVideoAVC) { if (codec_id != SrsCodecVideoAVC) {

View file

@ -132,6 +132,7 @@ enum SrsCodecAudio
// 1 = AAC raw // 1 = AAC raw
enum SrsCodecAudioType enum SrsCodecAudioType
{ {
SrsCodecAudioTypeReserved = -1,
SrsCodecAudioTypeSequenceHeader = 0, SrsCodecAudioTypeSequenceHeader = 0,
SrsCodecAudioTypeRawData = 1, SrsCodecAudioTypeRawData = 1,
}; };
@ -143,10 +144,12 @@ enum SrsCodecAudioType
// 3 = 44 kHz = 44100 Hz // 3 = 44 kHz = 44100 Hz
enum SrsCodecAudioSampleRate enum SrsCodecAudioSampleRate
{ {
SrsCodecAudioSampleRate5512 = 0, SrsCodecAudioSampleRateReserved = -1,
SrsCodecAudioSampleRate11025 = 1,
SrsCodecAudioSampleRate22050 = 2, SrsCodecAudioSampleRate5512 = 0,
SrsCodecAudioSampleRate44100 = 3, SrsCodecAudioSampleRate11025 = 1,
SrsCodecAudioSampleRate22050 = 2,
SrsCodecAudioSampleRate44100 = 3,
}; };
// Size of each audio sample. This parameter only pertains to // Size of each audio sample. This parameter only pertains to
@ -156,8 +159,10 @@ enum SrsCodecAudioSampleRate
// 1 = 16-bit samples // 1 = 16-bit samples
enum SrsCodecAudioSampleSize enum SrsCodecAudioSampleSize
{ {
SrsCodecAudioSampleSize8bit = 0, SrsCodecAudioSampleSizeReserved = -1,
SrsCodecAudioSampleSize16bit = 1,
SrsCodecAudioSampleSize8bit = 0,
SrsCodecAudioSampleSize16bit = 1,
}; };
// Mono or stereo sound // Mono or stereo sound
@ -165,8 +170,10 @@ enum SrsCodecAudioSampleSize
// 1 = Stereo sound // 1 = Stereo sound
enum SrsCodecAudioSoundType enum SrsCodecAudioSoundType
{ {
SrsCodecAudioSoundTypeMono = 0, SrsCodecAudioSoundTypeReserved = -1,
SrsCodecAudioSoundTypeStereo = 1,
SrsCodecAudioSoundTypeMono = 0,
SrsCodecAudioSoundTypeStereo = 1,
}; };
/** /**
@ -188,12 +195,17 @@ public:
SrsCodecBuffer buffers[SRS_MAX_CODEC_SAMPLE]; SrsCodecBuffer buffers[SRS_MAX_CODEC_SAMPLE];
public: public:
bool is_video; bool is_video;
// video specified
SrsCodecVideoAVCFrame frame_type;
SrsCodecVideoAVCType avc_packet_type;
// CompositionTime, video_file_format_spec_v10_1.pdf, page 78. // CompositionTime, video_file_format_spec_v10_1.pdf, page 78.
// cts = pts - dts, where dts = flvheader->timestamp. // cts = pts - dts, where dts = flvheader->timestamp.
int32_t cts; int32_t cts;
SrsCodecVideoAVCFrame frame_type; // audio specified
SrsCodecVideo codec_id; SrsCodecAudioSampleRate sound_rate;
SrsCodecVideoAVCType avc_packet_type; SrsCodecAudioSampleSize sound_size;
SrsCodecAudioSoundType sound_type;
SrsCodecAudioType aac_packet_type;
public: public:
SrsCodecSample(); SrsCodecSample();
virtual ~SrsCodecSample(); virtual ~SrsCodecSample();
@ -230,12 +242,6 @@ public:
*/ */
// @see: SrsCodecAudioType // @see: SrsCodecAudioType
int audio_codec_id; 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 int audio_data_rate; // in bps
// the avc extra data, the AVC sequence header, // the avc extra data, the AVC sequence header,
// without the flv codec header, // without the flv codec header,

View file

@ -139,41 +139,11 @@ int SrsHLS::on_meta_data(SrsOnMetaDataPacket* metadata)
if ((prop = obj->get_property("audiodatarate")) != NULL && prop->is_number()) { if ((prop = obj->get_property("audiodatarate")) != NULL && prop->is_number()) {
codec->audio_data_rate = (int)(1000 * srs_amf0_convert<SrsAmf0Number>(prop)->value); 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; // ignore the following, for each flv/rtmp packet contains them:
if (sound_rate == 5512) { // audiosamplerate, sample->sound_rate
codec->sound_rate = SrsCodecAudioSampleRate5512; // audiosamplesize, sample->sound_size
} else if (sound_rate == 11025) { // stereo, sample->sound_type
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;
}
}
return ret; return ret;
} }
@ -196,7 +166,15 @@ int SrsHLS::on_audio(SrsCommonMessage* audio)
return ret; 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; return ret;
} }
@ -221,7 +199,15 @@ int SrsHLS::on_video(SrsCommonMessage* video)
return ret; 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; return ret;
} }
@ -229,7 +215,7 @@ int SrsHLS::on_video(SrsCommonMessage* video)
} }
// @see: ngx_rtmp_mpegts_header // @see: ngx_rtmp_mpegts_header
static u_char mpegts_header[] = { u_int8_t mpegts_header[] = {
/* TS */ /* TS */
0x47, 0x40, 0x00, 0x10, 0x00, 0x47, 0x40, 0x00, 0x10, 0x00,
/* PSI */ /* PSI */
@ -329,9 +315,21 @@ int SrsTSMuxer::open(std::string _path)
return ret; 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; 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; return ret;
} }

View file

@ -66,7 +66,8 @@ public:
virtual ~SrsTSMuxer(); virtual ~SrsTSMuxer();
public: public:
virtual int open(std::string _path); 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(); virtual void close();
}; };

View file

@ -199,7 +199,7 @@ struct SrsMessageHeader
* The 4 bytes are packed in the big-endian order. * The 4 bytes are packed in the big-endian order.
* @remark, used as calc timestamp when decode and encode time. * @remark, used as calc timestamp when decode and encode time.
*/ */
int32_t timestamp; u_int32_t timestamp;
SrsMessageHeader(); SrsMessageHeader();
virtual ~SrsMessageHeader(); virtual ~SrsMessageHeader();

View file

@ -192,7 +192,7 @@ int SrsConsumer::jitter_correct(SrsSharedPtrMessage* msg, int audio_sample_rate,
* 3. last_pkt_correct_time: simply add the positive delta, * 3. last_pkt_correct_time: simply add the positive delta,
* and enforce the time monotonically. * 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; int32_t delta = time - last_pkt_time;
// if jitter detected, reset the delta. // 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. // 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; delta = DEFAULT_FRAME_TIME_MS;
} }

View file

@ -46,8 +46,8 @@ class SrsHLS;
class SrsConsumer class SrsConsumer
{ {
private: private:
int32_t last_pkt_time; u_int32_t last_pkt_time;
int32_t last_pkt_correct_time; u_int32_t last_pkt_correct_time;
SrsSource* source; SrsSource* source;
std::vector<SrsSharedPtrMessage*> msgs; std::vector<SrsSharedPtrMessage*> msgs;
bool paused; bool paused;