diff --git a/trunk/src/protocol/srs_raw_avc.cpp b/trunk/src/protocol/srs_raw_avc.cpp index a7c0155967..da98c6c67c 100644 --- a/trunk/src/protocol/srs_raw_avc.cpp +++ b/trunk/src/protocol/srs_raw_avc.cpp @@ -454,9 +454,23 @@ srs_error_t SrsRawAacStream::mux_sequence_header(SrsRawAacStreamCodec* codec, st // For example, AAC sampling_frequency_index is 3(48000HZ) or 4(44100HZ), // the sound_rate is always 3(44100HZ), if we covert sound_rate to // sampling_frequency_index, we may make mistake. - char samplingFrequencyIndex = codec->sampling_frequency_index; - if (samplingFrequencyIndex >= SrsAAcSampleRateNumbers) { - samplingFrequencyIndex = 4; // Default to 44100 + uint8_t samplingFrequencyIndex = (uint8_t)codec->sampling_frequency_index; + if (samplingFrequencyIndex >= SrsAacSampleRateUnset) { + switch (codec->sound_rate) { + case SrsAudioSampleRate5512: + samplingFrequencyIndex = 0x0c; break; + case SrsAudioSampleRate11025: + samplingFrequencyIndex = 0x0a; break; + case SrsAudioSampleRate22050: + samplingFrequencyIndex = 0x07; break; + case SrsAudioSampleRate44100: + samplingFrequencyIndex = 0x04; break; + default: + break; + } + } + if (samplingFrequencyIndex >= SrsAacSampleRateUnset) { + return srs_error_new(ERROR_AAC_DATA_INVALID, "invalid sample index %d", samplingFrequencyIndex); } char chs[2]; @@ -470,9 +484,6 @@ srs_error_t SrsRawAacStream::mux_sequence_header(SrsRawAacStreamCodec* codec, st // samplingFrequencyIndex; 4 bslbf chs[0] |= (samplingFrequencyIndex >> 1) & 0x07; chs[1] = (samplingFrequencyIndex << 7) & 0x80; - if (samplingFrequencyIndex == 0x0f) { - return srs_error_new(ERROR_AAC_DATA_INVALID, "invalid sampling frequency index"); - } // 7bits left. // channelConfiguration; 4 bslbf diff --git a/trunk/src/utest/srs_utest_avc.cpp b/trunk/src/utest/srs_utest_avc.cpp index 15b34f5902..9380632a99 100644 --- a/trunk/src/utest/srs_utest_avc.cpp +++ b/trunk/src/utest/srs_utest_avc.cpp @@ -405,6 +405,7 @@ VOID TEST(SrsAVCTest, AACMuxSequenceHeader) codec.aac_object = SrsAacObjectTypeAacMain; codec.channel_configuration = 1; codec.sound_rate = SrsAudioSampleRate22050; + codec.sampling_frequency_index = 7; HELPER_ASSERT_SUCCESS(h.mux_sequence_header(&codec, sh)); EXPECT_EQ(2, sh.length()); EXPECT_EQ(0x0b, (uint8_t)sh.at(0)); @@ -417,6 +418,7 @@ VOID TEST(SrsAVCTest, AACMuxSequenceHeader) codec.aac_object = SrsAacObjectTypeAacMain; codec.channel_configuration = 1; codec.sound_rate = SrsAudioSampleRate11025; + codec.sampling_frequency_index = 0xa; HELPER_ASSERT_SUCCESS(h.mux_sequence_header(&codec, sh)); EXPECT_EQ(2, sh.length()); EXPECT_EQ(0x0d, (uint8_t)sh.at(0)); @@ -426,7 +428,8 @@ VOID TEST(SrsAVCTest, AACMuxSequenceHeader) // Fail for invalid sampling rate. if (true) { SrsRawAacStream h; string sh; SrsRawAacStreamCodec codec; - codec.sampling_frequency_index = 0xf; + codec.aac_object = SrsAacObjectTypeAacMain; + codec.sampling_frequency_index = SrsAacSampleRateUnset; codec.sound_rate = SrsAudioSampleRateReserved; HELPER_EXPECT_FAILED(h.mux_sequence_header(&codec, sh)); } @@ -457,6 +460,33 @@ VOID TEST(SrsAVCTest, AACMuxSequenceHeader) codec.aac_object = SrsAacObjectTypeAacMain; codec.channel_configuration = 1; codec.sound_rate = SrsAudioSampleRate44100; + codec.sampling_frequency_index = 4; + HELPER_ASSERT_SUCCESS(h.mux_sequence_header(&codec, sh)); + EXPECT_EQ(2, sh.length()); + EXPECT_EQ(0x0a, (uint8_t)sh.at(0)); + EXPECT_EQ(0x08, (uint8_t)sh.at(1)); + } + + // We ignored the sound_rate. + if (true) { + SrsRawAacStream h; string sh; SrsRawAacStreamCodec codec; + codec.aac_object = SrsAacObjectTypeAacMain; + codec.channel_configuration = 1; + codec.sound_rate = SrsAudioSampleRate22050; + codec.sampling_frequency_index = 4; + HELPER_ASSERT_SUCCESS(h.mux_sequence_header(&codec, sh)); + EXPECT_EQ(2, sh.length()); + EXPECT_EQ(0x0a, (uint8_t)sh.at(0)); + EXPECT_EQ(0x08, (uint8_t)sh.at(1)); + } + + // Use sound_rate if sampling_frequency_index not set. + if (true) { + SrsRawAacStream h; string sh; SrsRawAacStreamCodec codec; + codec.aac_object = SrsAacObjectTypeAacMain; + codec.channel_configuration = 1; + codec.sound_rate = SrsAudioSampleRate44100; + codec.sampling_frequency_index = SrsAacSampleRateUnset; HELPER_ASSERT_SUCCESS(h.mux_sequence_header(&codec, sh)); EXPECT_EQ(2, sh.length()); EXPECT_EQ(0x0a, (uint8_t)sh.at(0));