From db57e4c048dc9d9d3fa5772190dd401a25fcaee1 Mon Sep 17 00:00:00 2001 From: Ashton Larkin Date: Wed, 8 Jul 2020 15:43:45 -0400 Subject: [PATCH 1/2] fixed existing AudioDecoder unit tests Signed-off-by: Ashton Larkin --- av/src/AudioDecoder_TEST.cc | 22 ++++++++++++++-------- av/src/CMakeLists.txt | 4 ---- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/av/src/AudioDecoder_TEST.cc b/av/src/AudioDecoder_TEST.cc index 60229f220..b81175ed4 100644 --- a/av/src/AudioDecoder_TEST.cc +++ b/av/src/AudioDecoder_TEST.cc @@ -59,7 +59,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 +110,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 +122,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 +137,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} From 00381026b878958de2a111c2723bce73638048ab Mon Sep 17 00:00:00 2001 From: Ashton Larkin Date: Wed, 8 Jul 2020 16:47:10 -0400 Subject: [PATCH 2/2] 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 | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) 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 b81175ed4..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); } /////////////////////////////////////////////////