Bug 1749372 - Combine chunks in AppendAndConsumeChunk if possible r=pehrsons

Differential Revision: https://phabricator.services.mozilla.com/D135542
This commit is contained in:
Chun-Min Chang 2022-01-14 18:39:04 +00:00
Родитель 624a3640bf
Коммит 66498068fe
2 изменённых файлов: 76 добавлений и 0 удалений

Просмотреть файл

@ -445,6 +445,13 @@ class AudioSegment : public MediaSegmentBase<AudioSegment, AudioChunk> {
return;
}
if (!mChunks.IsEmpty() &&
mChunks.LastElement().CanCombineWithFollowing(aChunk)) {
mChunks.LastElement().mDuration += aChunk.GetDuration();
mDuration += aChunk.GetDuration();
return;
}
chunk = AppendChunk(aChunk.mDuration);
}
void ApplyVolume(float aVolume);

Просмотреть файл

@ -372,4 +372,73 @@ TEST(AudioSegment, AppendAndConsumeNonEmptyZeroDurationChunk)
EXPECT_FALSE(c.mBuffer->IsShared());
}
TEST(AudioSegment, CombineChunksInAppendAndConsumeChunk)
{
AudioChunk source;
fillChunk<float, 2>(&source, 10);
auto checkChunks = [&](const AudioSegment& aSegement,
const nsTArray<TrackTime>& aDurations) {
size_t i = 0;
for (AudioSegment::ConstChunkIterator iter(aSegement); !iter.IsEnded();
iter.Next()) {
EXPECT_EQ(iter->GetDuration(), aDurations[i++]);
}
EXPECT_EQ(i, aDurations.Length());
};
// The chunks can be merged if their duration are adjacent.
{
AudioChunk c1(source);
c1.SliceTo(2, 5);
AudioChunk c2(source);
c2.SliceTo(5, 9);
AudioSegment s;
s.AppendAndConsumeChunk(std::move(c1));
EXPECT_EQ(s.GetDuration(), 3);
s.AppendAndConsumeChunk(std::move(c2));
EXPECT_EQ(s.GetDuration(), 7);
checkChunks(s, {7});
}
// Otherwise, they cannot be merged.
{
// If durations of chunks are overlapped, they cannot be merged.
AudioChunk c1(source);
c1.SliceTo(2, 5);
AudioChunk c2(source);
c2.SliceTo(4, 9);
AudioSegment s;
s.AppendAndConsumeChunk(std::move(c1));
EXPECT_EQ(s.GetDuration(), 3);
s.AppendAndConsumeChunk(std::move(c2));
EXPECT_EQ(s.GetDuration(), 8);
checkChunks(s, {3, 5});
}
{
// If durations of chunks are discontinuous, they cannot be merged.
AudioChunk c1(source);
c1.SliceTo(2, 4);
AudioChunk c2(source);
c2.SliceTo(5, 9);
AudioSegment s;
s.AppendAndConsumeChunk(std::move(c1));
EXPECT_EQ(s.GetDuration(), 2);
s.AppendAndConsumeChunk(std::move(c2));
EXPECT_EQ(s.GetDuration(), 6);
checkChunks(s, {2, 4});
}
}
} // namespace audio_segment