Refactor readBytes to readFrames in SpeedChangingAudioProcessor

Using frames instead of bytes helps simplify the processing logic for
the component and will help with the move towards sample-based
`SpeedProvider`s. By using frames, we also abstract away the complexity
related to sample encoding.

This is a non-functional refactor.

PiperOrigin-RevId: 691444106
This commit is contained in:
ivanbuper 2024-10-30 09:18:52 -07:00 committed by Copybara-Service
parent 15583f7c64
commit 2a9963424b

View File

@ -17,6 +17,7 @@
package androidx.media3.common.audio; package androidx.media3.common.audio;
import static androidx.media3.common.util.Assertions.checkArgument; import static androidx.media3.common.util.Assertions.checkArgument;
import static androidx.media3.common.util.Util.sampleCountToDurationUs;
import static java.lang.Math.min; import static java.lang.Math.min;
import static java.lang.Math.round; import static java.lang.Math.round;
@ -87,7 +88,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor {
@GuardedBy("lock") @GuardedBy("lock")
private float currentSpeed; private float currentSpeed;
private long bytesRead; private long framesRead;
private boolean endOfStreamQueuedToSonic; private boolean endOfStreamQueuedToSonic;
@ -114,11 +115,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor {
@Override @Override
public void queueInput(ByteBuffer inputBuffer) { public void queueInput(ByteBuffer inputBuffer) {
long currentTimeUs = long currentTimeUs = sampleCountToDurationUs(framesRead, inputAudioFormat.sampleRate);
Util.scaleLargeTimestamp(
/* timestamp= */ bytesRead,
/* multiplier= */ C.MICROS_PER_SECOND,
/* divisor= */ (long) inputAudioFormat.sampleRate * inputAudioFormat.bytesPerFrame);
float newSpeed = speedProvider.getSpeed(currentTimeUs); float newSpeed = speedProvider.getSpeed(currentTimeUs);
long nextSpeedChangeTimeUs = speedProvider.getNextSpeedChangeTimeUs(currentTimeUs); long nextSpeedChangeTimeUs = speedProvider.getNextSpeedChangeTimeUs(currentTimeUs);
long sampleRateAlignedNextSpeedChangeTimeUs = long sampleRateAlignedNextSpeedChangeTimeUs =
@ -129,7 +126,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor {
// one or more mid-sample speed changes. // one or more mid-sample speed changes.
if (sampleRateAlignedNextSpeedChangeTimeUs == currentTimeUs) { if (sampleRateAlignedNextSpeedChangeTimeUs == currentTimeUs) {
long sampleDuration = long sampleDuration =
Util.sampleCountToDurationUs(/* sampleCount= */ 1, inputAudioFormat.sampleRate); sampleCountToDurationUs(/* sampleCount= */ 1, inputAudioFormat.sampleRate);
newSpeed = speedProvider.getSpeed(currentTimeUs + sampleDuration); newSpeed = speedProvider.getSpeed(currentTimeUs + sampleDuration);
nextSpeedChangeTimeUs = nextSpeedChangeTimeUs =
speedProvider.getNextSpeedChangeTimeUs(currentTimeUs + sampleDuration); speedProvider.getNextSpeedChangeTimeUs(currentTimeUs + sampleDuration);
@ -173,7 +170,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor {
} }
buffer.flip(); buffer.flip();
} }
bytesRead += inputBuffer.position() - startPosition; framesRead += (inputBuffer.position() - startPosition) / inputAudioFormat.bytesPerFrame;
updateLastProcessedInputTime(); updateLastProcessedInputTime();
inputBuffer.limit(inputBufferLimit); inputBuffer.limit(inputBufferLimit);
} }
@ -385,11 +382,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor {
inputSegmentStartTimesUs.get(inputSegmentStartTimesUs.size() - 1) inputSegmentStartTimesUs.get(inputSegmentStartTimesUs.size() - 1)
+ currentProcessedInputDurationUs; + currentProcessedInputDurationUs;
} else { } else {
lastProcessedInputTimeUs = lastProcessedInputTimeUs = sampleCountToDurationUs(framesRead, inputAudioFormat.sampleRate);
Util.scaleLargeTimestamp(
/* timestamp= */ bytesRead,
/* multiplier= */ C.MICROS_PER_SECOND,
/* divisor= */ (long) inputAudioFormat.sampleRate * inputAudioFormat.bytesPerFrame);
} }
} }
} }
@ -414,7 +407,7 @@ public final class SpeedChangingAudioProcessor extends BaseAudioProcessor {
currentSpeed = 1f; currentSpeed = 1f;
} }
bytesRead = 0; framesRead = 0;
endOfStreamQueuedToSonic = false; endOfStreamQueuedToSonic = false;
// TODO: b/339842724 - This should ideally also reset speedAdjustedTimeAsyncInputTimeUs and // TODO: b/339842724 - This should ideally also reset speedAdjustedTimeAsyncInputTimeUs and
// clear pendingCallbacks and pendingCallbacksInputTimes. We can't do this at the moment // clear pendingCallbacks and pendingCallbacksInputTimes. We can't do this at the moment