From a98c9e04e304ecf67183aaff6777d04e5c24eaab Mon Sep 17 00:00:00 2001 From: winlin Date: Sun, 16 Apr 2017 21:33:39 +0800 Subject: [PATCH] Fix #547, support HLS audio in TS. 3.0.22 --- README.md | 4 +++- trunk/src/app/srs_app_hls.cpp | 21 +++++++++++++++++++-- trunk/src/app/srs_app_hls.hpp | 6 ++++++ trunk/src/core/srs_core.hpp | 2 +- trunk/src/kernel/srs_kernel_codec.hpp | 1 + 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8b29a91f8..411716b82 100755 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ Please select your language: - [x] [experiment] Support push POST FLV over HTTP, please read [wiki]([CN][v2_CN_Streamer2], [EN][v2_EN_Streamer2]). - [x] [experiment] Support multiple processes by [dolphin][srs-dolphin] or [oryx][oryx]. - [x] [experiment] Support [mgmt console][console], please read [srs-ngb][srs-ngb]. -- [ ] Enhanced HLS audio-only use aac instead of ts. +- [x] Enhanced HLS audio-only use ts, see [#547][bug #547]. - [ ] Enhanced forward with vhost and url variables. - [ ] Support source or idle stream cleanup. - [ ] Support origin cluster, please read [#464][bug #464], [RTMP 302][bug #92]. @@ -186,6 +186,7 @@ Please select your language: ### V3 changes +* v3.0, 2017-04-16, Fix [#547][bug #547], support HLS audio in TS. 3.0.22 * v3.0, 2017-03-26, Fix [#820][bug #820], extract service for modules. 3.0.21 * v3.0, 2017-03-02, Fix [#786][bug #786], simply don't reuse object. 3.0.20 * v3.0, 2017-03-01, For [#110][bug #110], refine thread object. 3.0.19 @@ -1400,6 +1401,7 @@ Winlin [bug #738]: https://github.com/ossrs/srs/issues/738 [bug #786]: https://github.com/ossrs/srs/issues/786 [bug #820]: https://github.com/ossrs/srs/issues/820 +[bug #547]: https://github.com/ossrs/srs/issues/547 [bug #xxxxxxxxxxxxx]: https://github.com/ossrs/srs/issues/xxxxxxxxxxxxx [exo #828]: https://github.com/google/ExoPlayer/pull/828 diff --git a/trunk/src/app/srs_app_hls.cpp b/trunk/src/app/srs_app_hls.cpp index 1d4681d7c..f481168ba 100644 --- a/trunk/src/app/srs_app_hls.cpp +++ b/trunk/src/app/srs_app_hls.cpp @@ -969,6 +969,9 @@ SrsHls::SrsHls() disposable = false; last_update_time = 0; + previous_audio_dts = 0; + aac_samples = 0; + jitter = new SrsRtmpJitter(); controller = new SrsHlsController(); @@ -1115,8 +1118,22 @@ int SrsHls::on_audio(SrsSharedPtrMessage* shared_audio, SrsFormat* format) return ret; } - // the dts calc from rtmp/flv header. - int64_t dts = audio->timestamp * 90; + // Reset the aac samples counter when DTS jitter. + if (previous_audio_dts > audio->timestamp) { + previous_audio_dts = audio->timestamp; + aac_samples = 0; + } + + // Use the diff to guess whether the samples is 1024 or 960. + int nb_samples_per_frame = 1024; + int diff = ::abs((int)(audio->timestamp - previous_audio_dts)) * srs_flv_srates[format->acodec->sound_rate]; + if (diff > 100 && diff < 950) { + nb_samples_per_frame = 960; + } + + // Recalc the DTS by the samples of AAC. + int64_t dts = 90000 * aac_samples / srs_flv_srates[format->acodec->sound_rate]; + aac_samples += nb_samples_per_frame; if ((ret = controller->write_audio(format->audio, dts)) != ERROR_SUCCESS) { srs_error("hls cache write audio failed. ret=%d", ret); diff --git a/trunk/src/app/srs_app_hls.hpp b/trunk/src/app/srs_app_hls.hpp index f374b78c5..a175b2b93 100644 --- a/trunk/src/app/srs_app_hls.hpp +++ b/trunk/src/app/srs_app_hls.hpp @@ -298,6 +298,12 @@ private: bool enabled; bool disposable; int64_t last_update_time; +private: + // If the diff=dts-previous_audio_dts is about 23, + // that's the AAC samples is 1024, and we use the samples to calc the dts. + int64_t previous_audio_dts; + // The total aac samples. + uint64_t aac_samples; private: SrsOriginHub* hub; SrsRtmpJitter* jitter; diff --git a/trunk/src/core/srs_core.hpp b/trunk/src/core/srs_core.hpp index 56a178a2c..1e062dade 100644 --- a/trunk/src/core/srs_core.hpp +++ b/trunk/src/core/srs_core.hpp @@ -27,7 +27,7 @@ // current release version #define VERSION_MAJOR 3 #define VERSION_MINOR 0 -#define VERSION_REVISION 21 +#define VERSION_REVISION 22 // generated by configure, only macros. #include diff --git a/trunk/src/kernel/srs_kernel_codec.hpp b/trunk/src/kernel/srs_kernel_codec.hpp index fad7322be..1d18ed957 100644 --- a/trunk/src/kernel/srs_kernel_codec.hpp +++ b/trunk/src/kernel/srs_kernel_codec.hpp @@ -183,6 +183,7 @@ enum SrsAudioAacFrameTrait * 2 = 22 kHz = 22050 Hz * 3 = 44 kHz = 44100 Hz * However, we can extends this table. + * @remark Use srs_flv_srates to convert it. */ enum SrsAudioSampleRate {