Bug 1887774 delay mPacketizerInput initialization until required r=pehrsons

mPacketizerInput won't need to be re-created for every channel count change if
no processing is done for some channel counts.

More significantly, this will facilitate delaying, in a subsequent patch,
AudioProcessing construction while its parameters may still be changing.

Differential Revision: https://phabricator.services.mozilla.com/D206867
This commit is contained in:
Karl Tomlinson 2024-04-10 20:36:33 +00:00
parent c0cd86bb97
commit 4edafeaf28
3 changed files with 18 additions and 23 deletions

View File

@ -112,16 +112,12 @@ TEST(TestAudioInputProcessing, Buffering)
EXPECT_EQ(aip->NumBufferedFrames(graph), 0);
}
// aip has been started and set to processing mode so it will insert 80 frames
// into aip's internal buffer as pre-buffering.
// aip has been set to processing mode and is started.
aip->Start(graph);
{
// Need (nextTime - processedTime) = 256 - 256 = 0 frames this round.
// The Process() aip will take 0 frames from input, packetize and process
// these frames into 0 80-frame packet(0 frames left in packetizer), insert
// packets into aip's internal buffer, then move 0 frames the internal
// buffer to output, leaving 80 + 0 - 0 = 80 frames in aip's internal
// buffer.
// Process() will return early on 0 frames of input.
// Pre-buffering is not triggered.
processedTime = nextTime;
nextTime = MediaTrackGraphImpl::RoundUpToEndOfAudioBlock(3 * frames);
@ -131,12 +127,14 @@ TEST(TestAudioInputProcessing, Buffering)
aip->Process(graph, processedTime, nextTime, &input, &output);
EXPECT_EQ(input.GetDuration(), nextTime - processedTime);
EXPECT_EQ(output.GetDuration(), nextTime);
EXPECT_EQ(aip->NumBufferedFrames(graph), 80);
EXPECT_EQ(aip->NumBufferedFrames(graph), 0);
}
{
// Need (nextTime - processedTime) = 384 - 256 = 128 frames this round.
// The Process() aip will take 128 frames from input, packetize and process
// On receipt of the these first frames, aip will insert 80 frames
// into its internal buffer as pre-buffering.
// Process() will take 128 frames from input, packetize and process
// these frames into floor(128/80) = 1 80-frame packet (48 frames left in
// packetizer), insert packets into aip's internal buffer, then move 128
// frames the internal buffer to output, leaving 80 + 80 - 128 = 32 frames
@ -274,23 +272,22 @@ TEST(TestAudioInputProcessing, ProcessDataWithDifferentPrincipals)
EXPECT_EQ(aip->PassThrough(graph), false);
aip->Start(graph);
{
EXPECT_EQ(aip->NumBufferedFrames(graph), 480);
AudioSegment output;
{
// Trim the prebuffering silence.
AudioSegment data;
aip->Process(graph, 0, 4800, &input, &data);
EXPECT_EQ(input.GetDuration(), 4800);
EXPECT_EQ(data.GetDuration(), 4800);
// Extract another 480 frames to account for delay from pre-buffering.
EXPECT_EQ(aip->NumBufferedFrames(graph), 480);
AudioSegment dummy;
dummy.AppendNullData(480);
aip->Process(graph, 0, 480, &dummy, &data);
EXPECT_EQ(dummy.GetDuration(), 480);
EXPECT_EQ(data.GetDuration(), 480 + 4800);
// Ignore the pre-buffering data
// Ignore the pre-buffering silence.
output.AppendSlice(data, 480, 480 + 4800);
}

View File

@ -431,12 +431,13 @@ void AudioInputProcessing::SetPassThrough(MediaTrackGraph* aGraph,
}
if (aPassThrough) {
// Turn on pass-through
// Switching to pass-through. Clear state so that it doesn't affect any
// future processing, if re-enabled.
ResetAudioProcessing(aGraph);
} else {
// Turn off pass-through
// No longer pass-through. Processing will not use old state.
// Packetizer setup is deferred until needed.
MOZ_ASSERT(!mPacketizerInput);
EnsureAudioProcessing(aGraph, mRequestedInputChannelCount);
}
}
@ -465,7 +466,6 @@ void AudioInputProcessing::Start(MediaTrackGraph* aGraph) {
}
MOZ_ASSERT(!mPacketizerInput);
EnsureAudioProcessing(aGraph, mRequestedInputChannelCount);
}
void AudioInputProcessing::Stop(MediaTrackGraph* aGraph) {
@ -627,13 +627,11 @@ void AudioInputProcessing::Process(MediaTrackGraph* aGraph, GraphTime aFrom,
return;
}
// SetPassThrough(false) must be called before reaching here.
MOZ_ASSERT(mPacketizerInput);
// If mRequestedInputChannelCount is updated, create a new packetizer. No
// need to change the pre-buffering since the rate is always the same. The
// frames left in the packetizer would be replaced by null data and then
// transferred to mSegment.
EnsureAudioProcessing(aGraph, mRequestedInputChannelCount);
EnsurePacketizer(aGraph, mRequestedInputChannelCount);
// Preconditions of the audio-processing logic.
MOZ_ASSERT(static_cast<uint32_t>(mSegment.GetDuration()) +
@ -993,8 +991,8 @@ TrackTime AudioInputProcessing::NumBufferedFrames(
return mSegment.GetDuration();
}
void AudioInputProcessing::EnsureAudioProcessing(MediaTrackGraph* aGraph,
uint32_t aChannels) {
void AudioInputProcessing::EnsurePacketizer(MediaTrackGraph* aGraph,
uint32_t aChannels) {
aGraph->AssertOnGraphThread();
MOZ_ASSERT(aChannels > 0);
MOZ_ASSERT(mEnabled);

View File

@ -164,7 +164,7 @@ class AudioInputProcessing : public AudioDataListener {
~AudioInputProcessing() = default;
webrtc::AudioProcessing::Config ConfigForPrefs(
const MediaEnginePrefs& aPrefs);
void EnsureAudioProcessing(MediaTrackGraph* aGraph, uint32_t aChannels);
void EnsurePacketizer(MediaTrackGraph* aGraph, uint32_t aChannels);
void ResetAudioProcessing(MediaTrackGraph* aGraph);
PrincipalHandle GetCheckedPrincipal(const AudioSegment& aSegment);
// This implements the processing algoritm to apply to the input (e.g. a