Skip to content

Commit

Permalink
For ossrs#1636, muxing sh, use sound_rate if aac sr not set.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Mar 13, 2020
1 parent 7036f83 commit a7c8980
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
23 changes: 17 additions & 6 deletions trunk/src/protocol/srs_raw_avc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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
Expand Down
32 changes: 31 additions & 1 deletion trunk/src/utest/srs_utest_avc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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));
Expand All @@ -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));
}
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit a7c8980

Please sign in to comment.