From 851aae0e47b77e747c265c46cd710e674707a530 Mon Sep 17 00:00:00 2001 From: adlarkin <42042756+adlarkin@users.noreply.github.com> Date: Fri, 10 Jul 2020 10:43:01 -0400 Subject: [PATCH] Fixed the AudioDecoder class (#81) * fixed existing AudioDecoder unit tests Signed-off-by: Ashton Larkin * handled possible seg fault by using AudioDecoder without calling SetFile Signed-off-by: Ashton Larkin --- av/include/ignition/common/AudioDecoder.hh | 1 + av/src/AudioDecoder.cc | 5 ++++- av/src/AudioDecoder_TEST.cc | 25 +++++++++++++++------- av/src/CMakeLists.txt | 4 ---- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/av/include/ignition/common/AudioDecoder.hh b/av/include/ignition/common/AudioDecoder.hh index 6b920fcd8..da9479a87 100644 --- a/av/include/ignition/common/AudioDecoder.hh +++ b/av/include/ignition/common/AudioDecoder.hh @@ -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. diff --git a/av/src/AudioDecoder.cc b/av/src/AudioDecoder.cc index 8729a952a..e0d2db9cf 100644 --- a/av/src/AudioDecoder.cc +++ b/av/src/AudioDecoder.cc @@ -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; } ///////////////////////////////////////////////// diff --git a/av/src/AudioDecoder_TEST.cc b/av/src/AudioDecoder_TEST.cc index 60229f220..89fb765c5 100644 --- a/av/src/AudioDecoder_TEST.cc +++ b/av/src/AudioDecoder_TEST.cc @@ -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); } ///////////////////////////////////////////////// @@ -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)); @@ -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); @@ -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. @@ -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); } } diff --git a/av/src/CMakeLists.txt b/av/src/CMakeLists.txt index 471e8e90b..fb78e8c7a 100644 --- a/av/src/CMakeLists.txt +++ b/av/src/CMakeLists.txt @@ -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}