Skip to content

Commit 614d830

Browse files
committed
Minor style and formatting fixes
This change is in preparation of github.com//pull/2259.
1 parent 94f5947 commit 614d830

File tree

7 files changed

+120
-110
lines changed

7 files changed

+120
-110
lines changed

libraries/common/src/main/java/androidx/media3/common/util/Util.java

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@
118118
import java.lang.reflect.Method;
119119
import java.math.BigDecimal;
120120
import java.math.RoundingMode;
121-
import java.nio.BufferOverflowException;
122121
import java.nio.ByteBuffer;
123122
import java.nio.ByteOrder;
124-
import java.nio.ReadOnlyBufferException;
125123
import java.nio.charset.StandardCharsets;
126124
import java.util.ArrayDeque;
127125
import java.util.ArrayList;
@@ -3462,51 +3460,51 @@ public static long getNowUnixTimeMs(long elapsedRealtimeEpochOffsetMs) {
34623460
}
34633461

34643462
/**
3465-
* Absolute <i>get</i> method for reading a sign-extended 24-bit integer value.
3463+
* Returns the sign-extended 24-bit integer value at {@code index}.
34663464
*
3467-
* <p>Reads three bytes at the given index, composing them into a 24-bit integer value according
3468-
* to the current byte order.
3465+
* <p>This method uses {@link ByteBuffer#order()} for the integer's byte order.
34693466
*
3470-
* @param buf The buffer to operate on
3471-
* @param index The index from which the bytes will be read
3472-
* @return The 24-bit integer value at the given index
3473-
* @throws IndexOutOfBoundsException If {@code index} is negative or not smaller than the buffer's
3474-
* limit, minus two
3467+
* @param buffer The buffer from which to read the 24-bit integer.
3468+
* @param index The index of the 24-bit integer.
34753469
*/
3476-
public static int getInt24(ByteBuffer buf, int index) {
3477-
byte component1 = buf.get(buf.order() == ByteOrder.BIG_ENDIAN ? index : index + 2);
3478-
byte component2 = buf.get(index + 1);
3479-
byte component3 = buf.get(buf.order() == ByteOrder.BIG_ENDIAN ? index + 2 : index);
3470+
@UnstableApi
3471+
public static int getInt24(ByteBuffer buffer, int index) {
3472+
byte component1 = buffer.get(buffer.order() == ByteOrder.BIG_ENDIAN ? index : index + 2);
3473+
byte component2 = buffer.get(index + 1);
3474+
byte component3 = buffer.get(buffer.order() == ByteOrder.BIG_ENDIAN ? index + 2 : index);
34803475
return ((component1 << 24) & 0xff000000
34813476
| (component2 << 16) & 0xff0000
34823477
| (component3 << 8) & 0xff00)
34833478
>> 8;
34843479
}
34853480

34863481
/**
3487-
* Relative <i>put</i> method for writing a 24-bit integer value.
3482+
* Writes a 24-bit integer value to a buffer at its current {@link ByteBuffer#position()}.
3483+
*
3484+
* <p>This method uses {@link ByteBuffer#order()} for the integer's byte order.
34883485
*
3489-
* <p>Writes three bytes containing the given int value, in the current byte order, into this
3490-
* buffer at the current position, and then increments the position by three.
3486+
* <p>This is a relative operation that affects the buffer's position.
34913487
*
3492-
* @param val The short value to be written
3493-
* @param buf The buffer to operate on
3494-
* @throws BufferOverflowException If there are fewer than two bytes remaining in this buffer
3495-
* @throws ReadOnlyBufferException If this buffer is read-only
3496-
* @throws IllegalArgumentException If the value is out of range for a 24-bit integer
3488+
* @param buffer The buffer on which to write the integer.
3489+
* @param value The integer value to write.
3490+
* @throws IllegalArgumentException If {@code value} is out of range for a 24-bit integer.
34973491
*/
3498-
public static void putInt24(ByteBuffer buf, int val) {
3499-
if ((val & ~0xffffff) != 0 && (val & ~0x7fffff) != 0xff800000) {
3500-
throw new IllegalArgumentException("value out of range: " + Integer.toHexString(val));
3501-
}
3492+
@UnstableApi
3493+
public static void putInt24(ByteBuffer buffer, int value) {
3494+
checkArgument(
3495+
(value & ~0xffffff) == 0 || (value & ~0x7fffff) == 0xff800000,
3496+
"Value out of range of 24-bit integer: " + Integer.toHexString(value));
3497+
checkArgument(buffer.remaining() >= 3);
35023498
byte component1 =
3503-
buf.order() == ByteOrder.BIG_ENDIAN ? (byte) ((val & 0xFF0000) >> 16) : (byte) (val & 0xFF);
3504-
byte component2 = (byte) ((val & 0xFF00) >> 8);
3499+
buffer.order() == ByteOrder.BIG_ENDIAN
3500+
? (byte) ((value & 0xFF0000) >> 16)
3501+
: (byte) (value & 0xFF);
3502+
byte component2 = (byte) ((value & 0xFF00) >> 8);
35053503
byte component3 =
3506-
buf.order() == ByteOrder.BIG_ENDIAN ? (byte) (val & 0xFF) : (byte) ((val & 0xFF0000) >> 16);
3507-
buf.put(component1);
3508-
buf.put(component2);
3509-
buf.put(component3);
3504+
buffer.order() == ByteOrder.BIG_ENDIAN
3505+
? (byte) (value & 0xFF)
3506+
: (byte) ((value & 0xFF0000) >> 16);
3507+
buffer.put(component1).put(component2).put(component3);
35103508
}
35113509

35123510
/**

libraries/common/src/test/java/androidx/media3/common/util/UtilTest.java

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,49 +1680,55 @@ public void contentHashCode_sparseArraysWithDifferentContent_returnsDifferentCon
16801680
}
16811681

16821682
@Test
1683-
public void putInt24_littleEndian() {
1684-
ByteBuffer buf = ByteBuffer.allocateDirect(2 * 3).order(ByteOrder.LITTLE_ENDIAN);
1685-
assertThrows(IllegalArgumentException.class, () -> putInt24(buf, 0xFF000000));
1686-
assertThrows(IllegalArgumentException.class, () -> putInt24(buf, 0x8FFFFFFF));
1687-
assertThrows(IllegalArgumentException.class, () -> putInt24(buf, 0x01FFFFFF));
1688-
putInt24(buf, -1);
1689-
putInt24(buf, 0x123456);
1690-
buf.rewind();
1691-
assertThat(createByteArray(buf))
1683+
public void putInt24_withOutOfRangeInts_throws() {
1684+
ByteBuffer buffer = ByteBuffer.allocateDirect(6).order(ByteOrder.LITTLE_ENDIAN);
1685+
assertThrows(IllegalArgumentException.class, () -> putInt24(buffer, 0xFF000000));
1686+
assertThrows(IllegalArgumentException.class, () -> putInt24(buffer, 0x8FFFFFFF));
1687+
assertThrows(IllegalArgumentException.class, () -> putInt24(buffer, 0x01FFFFFF));
1688+
}
1689+
1690+
@Test
1691+
public void putInt24_littleEndianBuffer_respectsOrdering() {
1692+
ByteBuffer buffer = ByteBuffer.allocateDirect(6).order(ByteOrder.LITTLE_ENDIAN);
1693+
putInt24(buffer, -1);
1694+
putInt24(buffer, 0x123456);
1695+
buffer.rewind();
1696+
assertThat(createByteArray(buffer))
16921697
.isEqualTo(new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, 0x56, 0x34, 0x12});
1693-
assertThat(getInt24(buf, 0)).isEqualTo(0xffffffff);
1694-
assertThat(getInt24(buf, 0) & 0xffffff).isEqualTo(0xffffff);
1695-
assertThat(getInt24(buf, 0)).isEqualTo(-1);
1696-
assertThat(getInt24(buf, 3)).isEqualTo(0x00123456);
1697-
1698-
buf.rewind();
1699-
putInt24(buf, 0xff0001);
1700-
putInt24(buf, 0x00ff02);
1701-
assertThat(getInt24(buf, 0) & 0xffffff).isEqualTo(0xff0001);
1702-
assertThat(getInt24(buf, 3) & 0xffffff).isEqualTo(0x00ff02);
1703-
}
1704-
1705-
@Test
1706-
public void putInt24_bigEndian() {
1707-
ByteBuffer buf = ByteBuffer.allocateDirect(2 * 3).order(ByteOrder.BIG_ENDIAN);
1708-
assertThrows(IllegalArgumentException.class, () -> putInt24(buf, 0xFF000000));
1709-
assertThrows(IllegalArgumentException.class, () -> putInt24(buf, 0x8FFFFFFF));
1710-
assertThrows(IllegalArgumentException.class, () -> putInt24(buf, 0x01FFFFFF));
1711-
putInt24(buf, -1);
1712-
putInt24(buf, 0x123456);
1713-
buf.rewind();
1714-
assertThat(createByteArray(buf))
1698+
}
1699+
1700+
@Test
1701+
public void getInt24_littleEndianBuffer_returnsExpectedValues() {
1702+
ByteBuffer buffer = ByteBuffer.allocateDirect(9).order(ByteOrder.LITTLE_ENDIAN);
1703+
buffer.put(
1704+
new byte[] {
1705+
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x56, 0x34, 0x12, 0x01, 0x00, (byte) 0xFF
1706+
});
1707+
assertThat(getInt24(buffer, 0)).isEqualTo(-1);
1708+
assertThat(getInt24(buffer, 3)).isEqualTo(0x00123456);
1709+
assertThat(getInt24(buffer, 6)).isEqualTo(0xFFFF0001);
1710+
}
1711+
1712+
@Test
1713+
public void putInt24_bigEndianBuffer_respectsOrdering() {
1714+
ByteBuffer buffer = ByteBuffer.allocateDirect(6).order(ByteOrder.BIG_ENDIAN);
1715+
putInt24(buffer, -1);
1716+
putInt24(buffer, 0x123456);
1717+
buffer.rewind();
1718+
assertThat(createByteArray(buffer))
17151719
.isEqualTo(new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, 0x12, 0x34, 0x56});
1716-
assertThat(getInt24(buf, 0)).isEqualTo(0xffffffff);
1717-
assertThat(getInt24(buf, 0) & 0xffffff).isEqualTo(0xffffff);
1718-
assertThat(getInt24(buf, 0)).isEqualTo(-1);
1719-
assertThat(getInt24(buf, 3)).isEqualTo(0x00123456);
1720-
1721-
buf.rewind();
1722-
putInt24(buf, 0xff0001);
1723-
putInt24(buf, 0x00ff02);
1724-
assertThat(getInt24(buf, 0) & 0xffffff).isEqualTo(0xff0001);
1725-
assertThat(getInt24(buf, 3) & 0xffffff).isEqualTo(0x00ff02);
1720+
}
1721+
1722+
@Test
1723+
public void getInt24_bigEndianBuffer_returnsExpectedValues() {
1724+
ByteBuffer buffer = ByteBuffer.allocateDirect(9).order(ByteOrder.BIG_ENDIAN);
1725+
buffer.put(
1726+
new byte[] {
1727+
(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x12, 0x34, 0x56, (byte) 0xFF, 0x00, 0x01
1728+
});
1729+
assertThat(getInt24(buffer, 0)).isEqualTo(-1);
1730+
assertThat(getInt24(buffer, 3)).isEqualTo(0x00123456);
1731+
assertThat(getInt24(buffer, 6)).isEqualTo(0xFFFF0001);
17261732
}
17271733

17281734
private static void assertEscapeUnescapeFileName(String fileName, String escapedFileName) {

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/ChannelMappingAudioProcessor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package androidx.media3.exoplayer.audio;
1717

1818
import static androidx.media3.common.util.Util.getByteDepth;
19+
import static androidx.media3.common.util.Util.getInt24;
20+
import static androidx.media3.common.util.Util.isEncodingLinearPcm;
21+
import static androidx.media3.common.util.Util.putInt24;
1922

2023
import androidx.annotation.Nullable;
2124
import androidx.media3.common.C;
@@ -24,7 +27,6 @@
2427
import androidx.media3.common.audio.BaseAudioProcessor;
2528
import androidx.media3.common.util.Assertions;
2629
import androidx.media3.common.util.UnstableApi;
27-
import androidx.media3.common.util.Util;
2830
import java.nio.ByteBuffer;
2931
import java.util.Arrays;
3032

@@ -59,7 +61,7 @@ public AudioFormat onConfigure(AudioFormat inputAudioFormat)
5961
return AudioFormat.NOT_SET;
6062
}
6163

62-
if (!Util.isEncodingLinearPcm(inputAudioFormat.encoding)) {
64+
if (!isEncodingLinearPcm(inputAudioFormat.encoding)) {
6365
throw new UnhandledAudioFormatException(inputAudioFormat);
6466
}
6567

@@ -102,7 +104,7 @@ public void queueInput(ByteBuffer inputBuffer) {
102104
break;
103105
case C.ENCODING_PCM_24BIT:
104106
case C.ENCODING_PCM_24BIT_BIG_ENDIAN:
105-
Util.putInt24(buffer, Util.getInt24(inputBuffer, inputIndex));
107+
putInt24(buffer, getInt24(inputBuffer, inputIndex));
106108
break;
107109
case C.ENCODING_PCM_32BIT:
108110
case C.ENCODING_PCM_32BIT_BIG_ENDIAN:

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/DefaultAudioSink.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,9 @@ public DefaultAudioSink build() {
526526
private final boolean enableFloatOutput;
527527
private final ChannelMappingAudioProcessor channelMappingAudioProcessor;
528528
private final TrimmingAudioProcessor trimmingAudioProcessor;
529-
private final ImmutableList<AudioProcessor> toIntPcmAvailableAudioProcessors;
530-
private final ImmutableList<AudioProcessor> toFloatPcmAvailableAudioProcessors;
531-
private final ImmutableList<AudioProcessor> forPreProcessingAvailableAudioProcessors;
529+
private final ToInt16PcmAudioProcessor toInt16PcmAudioProcessor;
530+
private final ToFloatPcmAudioProcessor toFloatPcmAudioProcessor;
531+
private final ImmutableList<AudioProcessor> availableAudioProcessors;
532532
private final AudioTrackPositionTracker audioTrackPositionTracker;
533533
private final ArrayDeque<MediaPositionParameters> mediaPositionParametersCheckpoints;
534534
private final boolean preferAudioTrackPlaybackParams;
@@ -607,9 +607,9 @@ private DefaultAudioSink(Builder builder) {
607607
audioTrackPositionTracker = new AudioTrackPositionTracker(new PositionTrackerListener());
608608
channelMappingAudioProcessor = new ChannelMappingAudioProcessor();
609609
trimmingAudioProcessor = new TrimmingAudioProcessor();
610-
toIntPcmAvailableAudioProcessors = ImmutableList.of(new ToInt16PcmAudioProcessor());
611-
toFloatPcmAvailableAudioProcessors = ImmutableList.of(new ToFloatPcmAudioProcessor());
612-
forPreProcessingAvailableAudioProcessors =
610+
toInt16PcmAudioProcessor = new ToInt16PcmAudioProcessor();
611+
toFloatPcmAudioProcessor = new ToFloatPcmAudioProcessor();
612+
availableAudioProcessors =
613613
ImmutableList.of(trimmingAudioProcessor, channelMappingAudioProcessor);
614614
volume = 1f;
615615
audioSessionId = C.AUDIO_SESSION_ID_UNSET;
@@ -708,11 +708,11 @@ public void configure(Format inputFormat, int specifiedBufferSize, @Nullable int
708708
inputPcmFrameSize = Util.getPcmFrameSize(inputFormat.pcmEncoding, inputFormat.channelCount);
709709

710710
ImmutableList.Builder<AudioProcessor> pipelineProcessors = new ImmutableList.Builder<>();
711-
pipelineProcessors.addAll(forPreProcessingAvailableAudioProcessors);
711+
pipelineProcessors.addAll(availableAudioProcessors);
712712
if (shouldUseFloatOutput(inputFormat.pcmEncoding)) {
713-
pipelineProcessors.addAll(toFloatPcmAvailableAudioProcessors);
713+
pipelineProcessors.add(toFloatPcmAudioProcessor);
714714
} else {
715-
pipelineProcessors.addAll(toIntPcmAvailableAudioProcessors);
715+
pipelineProcessors.add(toInt16PcmAudioProcessor);
716716
pipelineProcessors.add(audioProcessorChain.getAudioProcessors());
717717
}
718718
audioProcessingPipeline = new AudioProcessingPipeline(pipelineProcessors.build());
@@ -1581,12 +1581,12 @@ public void flush() {
15811581
@Override
15821582
public void reset() {
15831583
flush();
1584-
for (AudioProcessor audioProcessor : toIntPcmAvailableAudioProcessors) {
1585-
audioProcessor.reset();
1586-
}
1587-
for (AudioProcessor audioProcessor : toFloatPcmAvailableAudioProcessors) {
1584+
for (AudioProcessor audioProcessor : availableAudioProcessors) {
15881585
audioProcessor.reset();
15891586
}
1587+
toInt16PcmAudioProcessor.reset();
1588+
toFloatPcmAudioProcessor.reset();
1589+
15901590
if (audioProcessingPipeline != null) {
15911591
audioProcessingPipeline.reset();
15921592
}

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/audio/TrimmingAudioProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package androidx.media3.exoplayer.audio;
1717

18+
import static androidx.media3.common.util.Util.isEncodingLinearPcm;
1819
import static java.lang.Math.min;
1920

2021
import androidx.media3.common.Format;
@@ -79,7 +80,7 @@ public long getDurationAfterProcessorApplied(long durationUs) {
7980
@Override
8081
public AudioFormat onConfigure(AudioFormat inputAudioFormat)
8182
throws UnhandledAudioFormatException {
82-
if (!Util.isEncodingLinearPcm(inputAudioFormat.encoding)) {
83+
if (!isEncodingLinearPcm(inputAudioFormat.encoding)) {
8384
throw new UnhandledAudioFormatException(inputAudioFormat);
8485
}
8586
reconfigurationPending = true;

libraries/exoplayer/src/test/java/androidx/media3/exoplayer/audio/ChannelMappingAudioProcessorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void channelMap_withPcm24Samples_mapsOutputCorrectly()
9292

9393
processor.queueInput(createInt24ByteBuffer(new int[] {0xff0001, 0x00ff02, 0x0300ff, 0x40ff00}));
9494
int[] output = createInt24Array(processor.getOutput());
95-
assertThat(output).isEqualTo(new int[] {0x00ff02, 0xff0001, 0x40ff00, 0x0300ff});
95+
assertThat(output).isEqualTo(new int[] {0x00ff02, 0xffff0001, 0x40ff00, 0x0300ff});
9696
}
9797

9898
@Test

libraries/test_utils/src/main/java/androidx/media3/test/utils/TestUtil.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static android.os.Build.VERSION.SDK_INT;
1919
import static androidx.media3.common.util.Assertions.checkNotNull;
2020
import static androidx.media3.common.util.Assertions.checkState;
21+
import static androidx.media3.common.util.Util.putInt24;
2122
import static com.google.common.truth.Truth.assertThat;
2223
import static org.mockito.Mockito.mock;
2324
import static org.mockito.Mockito.verify;
@@ -175,6 +176,13 @@ public static byte[] createByteArray(int... bytes) {
175176
return array;
176177
}
177178

179+
/** Gets the underlying data of the {@link ByteBuffer} as a {@code byte[]}. */
180+
public static byte[] createByteArray(ByteBuffer byteBuffer) {
181+
byte[] content = new byte[byteBuffer.remaining()];
182+
byteBuffer.get(content);
183+
return content;
184+
}
185+
178186
/** Gets the underlying data of the {@link ByteBuffer} as a {@code float[]}. */
179187
public static float[] createFloatArray(ByteBuffer byteBuffer) {
180188
FloatBuffer buffer = byteBuffer.asFloatBuffer();
@@ -191,11 +199,13 @@ public static int[] createIntArray(ByteBuffer byteBuffer) {
191199
return content;
192200
}
193201

194-
/** Gets the underlying data of the {@link ByteBuffer} as int24 values in {@code int[]}. */
202+
/**
203+
* Gets the underlying data of the {@link ByteBuffer} as 24-bit integer values in {@code int[]}.
204+
*/
195205
public static int[] createInt24Array(ByteBuffer byteBuffer) {
196206
int[] content = new int[byteBuffer.remaining() / 3];
197207
for (int i = 0; i < content.length; i++) {
198-
content[i] = Util.getInt24(byteBuffer, byteBuffer.position() + i * 3) & 0xffffff;
208+
content[i] = Util.getInt24(byteBuffer, byteBuffer.position() + i * 3);
199209
}
200210
return content;
201211
}
@@ -208,13 +218,6 @@ public static short[] createShortArray(ByteBuffer byteBuffer) {
208218
return content;
209219
}
210220

211-
/** Gets the underlying data of the {@link ByteBuffer} as a {@code byte[]}. */
212-
public static byte[] createByteArray(ByteBuffer byteBuffer) {
213-
byte[] content = new byte[byteBuffer.remaining()];
214-
byteBuffer.get(content);
215-
return content;
216-
}
217-
218221
/** Creates a {@link ByteBuffer} containing the {@code data}. */
219222
public static ByteBuffer createByteBuffer(float[] data) {
220223
ByteBuffer buffer =
@@ -230,16 +233,6 @@ public static ByteBuffer createByteBuffer(int[] data) {
230233
return buffer;
231234
}
232235

233-
/** Creates a {@link ByteBuffer} containing the {@code data}. */
234-
public static ByteBuffer createInt24ByteBuffer(int[] data) {
235-
ByteBuffer buffer = ByteBuffer.allocateDirect(data.length * 3).order(ByteOrder.nativeOrder());
236-
for (int i : data) {
237-
Util.putInt24(buffer, i);
238-
}
239-
buffer.rewind();
240-
return buffer;
241-
}
242-
243236
/** Creates a {@link ByteBuffer} containing the {@code data}. */
244237
public static ByteBuffer createByteBuffer(short[] data) {
245238
ByteBuffer buffer = ByteBuffer.allocateDirect(data.length * 2).order(ByteOrder.nativeOrder());
@@ -255,6 +248,16 @@ public static ByteBuffer createByteBuffer(byte[] data) {
255248
return buffer;
256249
}
257250

251+
/** Creates a {@link ByteBuffer} with the contents of {@code data} as 24-bit integers. */
252+
public static ByteBuffer createInt24ByteBuffer(int[] data) {
253+
ByteBuffer buffer = ByteBuffer.allocateDirect(data.length * 3).order(ByteOrder.nativeOrder());
254+
for (int i : data) {
255+
putInt24(buffer, i);
256+
}
257+
buffer.rewind();
258+
return buffer;
259+
}
260+
258261
/**
259262
* Converts an array of integers in the range [0, 255] into an equivalent byte list.
260263
*

0 commit comments

Comments
 (0)