Skip to content

Commit

Permalink
Fixed the AudioDecoder class (#81)
Browse files Browse the repository at this point in the history
* fixed existing AudioDecoder unit tests

Signed-off-by: Ashton Larkin <ashton@openrobotics.org>

* handled possible seg fault by using AudioDecoder without calling SetFile

Signed-off-by: Ashton Larkin <ashton@openrobotics.org>
  • Loading branch information
adlarkin authored and mjcarroll committed Oct 14, 2020
1 parent f4f07ce commit b10d2e6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions av/include/ignition/common/AudioDecoder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ namespace ignition

/// \brief Get the sample rate from the latest decoded file.
/// \return Integer sample rate, such as 44100.
/// If no file is decoded, -1 is returned.
public: int SampleRate();

/// \brief Free audio object, close files, streams.
Expand Down
5 changes: 4 additions & 1 deletion av/src/AudioDecoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,10 @@ bool AudioDecoder::Decode(uint8_t **_outBuffer, unsigned int *_outBufferSize)
/////////////////////////////////////////////////
int AudioDecoder::SampleRate()
{
return this->data->codecCtx->sample_rate;
if (this->data->codecCtx)
return this->data->codecCtx->sample_rate;

return -1;
}

/////////////////////////////////////////////////
Expand Down
25 changes: 17 additions & 8 deletions av/src/AudioDecoder_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ TEST(AudioDecoder, FileNotSet)
unsigned int dataBufferSize;
uint8_t *dataBuffer = NULL;
EXPECT_FALSE(audio.Decode(&dataBuffer, &dataBufferSize));

EXPECT_EQ(audio.File(), "");
EXPECT_EQ(audio.SampleRate(), -1);
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -59,7 +62,7 @@ TEST(AudioDecoder, DataBuffer)
{
common::AudioDecoder audio;

std::string path = PROJECT_SOURCE_PATH;
std::string path = TEST_PATH;
path += "/data/cheer.wav";
EXPECT_TRUE(audio.SetFile(path));

Expand Down Expand Up @@ -110,8 +113,8 @@ TEST(AudioDecoder, CheerFile)
path = TEST_PATH;
path += "/data/cheer.wav";
EXPECT_TRUE(audio.SetFile(path));
EXPECT_EQ(audio.GetFile(), path);
EXPECT_EQ(audio.GetSampleRate(), 48000);
EXPECT_EQ(audio.File(), path);
EXPECT_EQ(audio.SampleRate(), 48000);

audio.Decode(&dataBuffer, &dataBufferSize);
EXPECT_EQ(dataBufferSize, 5428692u);
Expand All @@ -122,8 +125,8 @@ TEST(AudioDecoder, CheerFile)
path = TEST_PATH;
path += "/data/cheer.ogg";
EXPECT_TRUE(audio.SetFile(path));
EXPECT_EQ(audio.GetFile(), path);
EXPECT_EQ(audio.GetSampleRate(), 44100);
EXPECT_EQ(audio.File(), path);
EXPECT_EQ(audio.SampleRate(), 44100);

audio.Decode(&dataBuffer, &dataBufferSize);
// In Ubuntu trusty the buffer size double for ogg decoding.
Expand All @@ -137,11 +140,17 @@ TEST(AudioDecoder, CheerFile)
path = TEST_PATH;
path += "/data/cheer.mp3";
EXPECT_TRUE(audio.SetFile(path));
EXPECT_EQ(audio.GetFile(), path);
EXPECT_EQ(audio.GetSampleRate(), 44100);
EXPECT_EQ(audio.File(), path);
EXPECT_EQ(audio.SampleRate(), 44100);

audio.Decode(&dataBuffer, &dataBufferSize);
EXPECT_EQ(dataBufferSize, 4995072u);

// later versions of ffmpeg produces a different buffer size probably due to
// underlying changes in the decoder. The size of the first decoded frame
// is much smaller than all other frames.
EXPECT_TRUE(dataBufferSize == 4995072u ||
dataBufferSize == 4987612u ||
dataBufferSize == 4987612u * 2);
}
}

Expand Down
4 changes: 0 additions & 4 deletions av/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
ign_get_libsources_and_unittests(sources gtest_sources)

# FIXME: This class does not currently work
list(REMOVE_ITEM sources AudioDecoder.cc)
list(REMOVE_ITEM gtest_sources AudioDecoder_TEST.cc)

ign_add_component(av SOURCES ${sources} GET_TARGET_NAME av_target)

target_link_libraries(${av_target}
Expand Down

0 comments on commit b10d2e6

Please sign in to comment.