Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fd8859d0 authored by Andy Hung's avatar Andy Hung
Browse files

AudioFormat: Add getFrameSizeInBytes

Test: Check docs, CTS AudioFormatTest
Bug: 116723648
Change-Id: I87a2c34df5fa36a4ebce61942c7654419068d6a9
parent d0e81d2f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -22915,6 +22915,7 @@ package android.media {
    method public int getChannelIndexMask();
    method public int getChannelMask();
    method public int getEncoding();
    method public int getFrameSizeInBytes();
    method public int getSampleRate();
    method public void writeToParcel(android.os.Parcel, int);
    field public static final deprecated int CHANNEL_CONFIGURATION_DEFAULT = 1; // 0x1
+32 −0
Original line number Diff line number Diff line
@@ -213,6 +213,8 @@ import java.util.Objects;
 * (e.g.{@link AudioTrack#getPlaybackHeadPosition()
 * AudioTrack.getPlaybackHeadPosition()}),
 * depending on the context where audio frame is used.
 * For the purposes of {@link AudioFormat#getFrameSizeInBytes()}, a compressed data format
 * returns a frame size of 1 byte.
 */
public final class AudioFormat implements Parcelable {

@@ -707,6 +709,16 @@ public final class AudioFormat implements Parcelable {
            channelCount = 0; // position and index channel count mismatch
        }
        mChannelCount = channelCount;

        int frameSizeInBytes = 1;
        try {
            frameSizeInBytes = getBytesPerSample(mEncoding) * channelCount;
        } catch (IllegalArgumentException iae) {
            // ignored
        }
        // it is possible that channel count is 0, so ensure we return 1 for
        // mFrameSizeInBytes for consistency.
        mFrameSizeInBytes = frameSizeInBytes != 0 ? frameSizeInBytes : 1;
    }

    /** @hide */
@@ -734,6 +746,7 @@ public final class AudioFormat implements Parcelable {

    // Derived values computed in the constructor, cached here.
    private final int mChannelCount;
    private final int mFrameSizeInBytes;

    /**
     * Return the encoding.
@@ -788,6 +801,25 @@ public final class AudioFormat implements Parcelable {
        return mChannelCount;
    }

    /**
     * Return the frame size in bytes.
     *
     * For PCM or PCM packed compressed data this is the size of a sample multiplied
     * by the channel count. For all other cases, including invalid/unset channel masks,
     * this will return 1 byte.
     * As an example, a stereo 16-bit PCM format would have a frame size of 4 bytes,
     * an 8 channel float PCM format would have a frame size of 32 bytes,
     * and a compressed data format (not packed in PCM) would have a frame size of 1 byte.
     *
     * Both {@link AudioRecord} or {@link AudioTrack} process data in multiples of
     * this frame size.
     *
     * @return The audio frame size in bytes corresponding to the encoding and the channel mask.
     */
    public int getFrameSizeInBytes() {
        return mFrameSizeInBytes;
    }

    /** @hide */
    public int getPropertySetMask() {
        return mPropertySetMask;