Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the AudioDecoder class #81

Merged
merged 2 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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