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

for #512, write audio frame by frame for video+audio hls.

This commit is contained in:
winlin 2015-10-27 16:20:02 +08:00
parent e08beba89e
commit 3683f06e4d
6 changed files with 36 additions and 44 deletions

View file

@ -765,7 +765,9 @@ About the HLS overhead of SRS, we compare the overhead to FLV by remux the HLS t
| 5147kbps | 370s | 195040 | 200280 | 2.68% | | 5147kbps | 370s | 195040 | 200280 | 2.68% |
| 5158kbps | 1327s | 835664 | 858092 | 2.68% | | 5158kbps | 1327s | 835664 | 858092 | 2.68% |
The HLS overhead is calc by: (HLS - FLV) / FLV * 100% The HLS overhead is calc by: (HLS - FLV) / FLV * 100%.
The overhead is larger than this benchmark(48kbps audio is best overhead), for we fix the [#512][bug#512].
## Architecture ## Architecture
@ -1193,6 +1195,8 @@ Winlin
[bug #59]: https://github.com/simple-rtmp-server/srs/issues/59 [bug #59]: https://github.com/simple-rtmp-server/srs/issues/59
[bug #50]: https://github.com/simple-rtmp-server/srs/issues/50 [bug #50]: https://github.com/simple-rtmp-server/srs/issues/50
[bug #34]: https://github.com/simple-rtmp-server/srs/issues/34 [bug #34]: https://github.com/simple-rtmp-server/srs/issues/34
[bug #512]: https://github.com/simple-rtmp-server/srs/issues/512
[bug #xxxxxxxxxx]: https://github.com/simple-rtmp-server/srs/issues/xxxxxxxxxx
[r2.0a2]: https://github.com/simple-rtmp-server/srs/releases/tag/v2.0-a2 [r2.0a2]: https://github.com/simple-rtmp-server/srs/releases/tag/v2.0-a2
[r2.0a1]: https://github.com/simple-rtmp-server/srs/releases/tag/2.0a1 [r2.0a1]: https://github.com/simple-rtmp-server/srs/releases/tag/2.0a1

View file

@ -646,6 +646,11 @@ int SrsHlsMuxer::update_acodec(SrsCodecAudio ac)
return current->muxer->update_acodec(ac); return current->muxer->update_acodec(ac);
} }
bool SrsHlsMuxer::pure_audio()
{
return current && current->muxer && current->muxer->video_codec() == SrsCodecVideoDisabled;
}
int SrsHlsMuxer::flush_audio(SrsTsCache* cache) int SrsHlsMuxer::flush_audio(SrsTsCache* cache)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
@ -1049,25 +1054,6 @@ int SrsHlsCache::write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t
return ret; return ret;
} }
// flush if buffer exceed max size.
if (cache->audio->payload->length() > SRS_AUTO_HLS_AUDIO_CACHE_SIZE) {
if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) {
return ret;
}
}
// TODO: config it.
// in ms, audio delay to flush the audios.
int64_t audio_delay = SRS_CONF_DEFAULT_AAC_DELAY;
// flush if audio delay exceed
// cache->audio will be free in flush_audio
// so we must check whether it's null ptr.
if (cache->audio && pts - cache->audio->start_pts > audio_delay * 90) {
if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) {
return ret;
}
}
// reap when current source is pure audio. // reap when current source is pure audio.
// it maybe changed when stream info changed, // it maybe changed when stream info changed,
// for example, pure audio when start, audio/video when publishing, // for example, pure audio when start, audio/video when publishing,
@ -1083,6 +1069,14 @@ int SrsHlsCache::write_audio(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t
} }
} }
// directly write the audio frame by frame to ts,
// it's ok for the hls overload, or maybe cause the audio corrupt,
// which introduced by aggregate the audios to a big one.
// @see https://github.com/simple-rtmp-server/srs/issues/512
if ((ret = muxer->flush_audio(cache)) != ERROR_SUCCESS) {
return ret;
}
return ret; return ret;
} }
@ -1110,9 +1104,6 @@ int SrsHlsCache::write_video(SrsAvcAacCodec* codec, SrsHlsMuxer* muxer, int64_t
if ((ret = reap_segment("video", muxer, cache->video->dts)) != ERROR_SUCCESS) { if ((ret = reap_segment("video", muxer, cache->video->dts)) != ERROR_SUCCESS) {
return ret; return ret;
} }
// the video must be flushed, just return.
return ret;
} }
} }

View file

@ -309,6 +309,10 @@ public:
virtual bool is_segment_absolutely_overflow(); virtual bool is_segment_absolutely_overflow();
public: public:
virtual int update_acodec(SrsCodecAudio ac); virtual int update_acodec(SrsCodecAudio ac);
/**
* whether current hls muxer is pure audio mode.
*/
virtual bool pure_audio();
virtual int flush_audio(SrsTsCache* cache); virtual int flush_audio(SrsTsCache* cache);
virtual int flush_video(SrsTsCache* cache); virtual int flush_video(SrsTsCache* cache);
/** /**

View file

@ -246,12 +246,6 @@ extern int aac_sample_rates[];
#define SRS_SRS_MAX_CODEC_SAMPLE 128 #define SRS_SRS_MAX_CODEC_SAMPLE 128
#define SRS_AAC_SAMPLE_RATE_UNSET 15 #define SRS_AAC_SAMPLE_RATE_UNSET 15
// in ms, for HLS aac flush the audio
#define SRS_CONF_DEFAULT_AAC_DELAY 60
// max PES packets size to flush the video.
#define SRS_AUTO_HLS_AUDIO_CACHE_SIZE 128 * 1024
/** /**
* the FLV/RTMP supported audio sample size. * the FLV/RTMP supported audio sample size.
* Size of each audio sample. This parameter only pertains to * Size of each audio sample. This parameter only pertains to

View file

@ -2760,6 +2760,11 @@ void SrsTSMuxer::close()
writer->close(); writer->close();
} }
SrsCodecVideo SrsTSMuxer::video_codec()
{
return vcodec;
}
SrsTsCache::SrsTsCache() SrsTsCache::SrsTsCache()
{ {
audio = NULL; audio = NULL;
@ -3134,22 +3139,11 @@ int SrsTsEncoder::write_audio(int64_t timestamp, char* data, int size)
return ret; return ret;
} }
// flush if buffer exceed max size. // always flush audio frame by frame.
if (cache->audio->payload->length() > SRS_AUTO_HLS_AUDIO_CACHE_SIZE) { // @see https://github.com/simple-rtmp-server/srs/issues/512
return flush_video();
}
// TODO: config it.
// in ms, audio delay to flush the audios.
int64_t audio_delay = SRS_CONF_DEFAULT_AAC_DELAY;
// flush if audio delay exceed
if (dts - cache->audio->start_pts > audio_delay * 90) {
return flush_audio(); return flush_audio();
} }
return ret;
}
int SrsTsEncoder::write_video(int64_t timestamp, char* data, int size) int SrsTsEncoder::write_video(int64_t timestamp, char* data, int size)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;

View file

@ -1586,6 +1586,11 @@ public:
* close the writer. * close the writer.
*/ */
virtual void close(); virtual void close();
public:
/**
* get the video codec of ts muxer.
*/
virtual SrsCodecVideo video_codec();
}; };
/** /**