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

Commit e34b659f authored by jiabin's avatar jiabin
Browse files

Add offset for AV sync header.

AV sync header was 16 bytes, which will cause partial timestamp header
written when the framesize cannot be divided by 16.

Bug: 31688925
Test: Test with writing fake data to AudioTrack with timestamp
Change-Id: Idc818985b3e0de47665ce5a28c458c018aa69eb3
parent 4ed9a8a3
Loading
Loading
Loading
Loading
+20 −2
Original line number Diff line number Diff line
@@ -276,6 +276,9 @@ public class AudioTrack extends PlayerBase
    private static final int AUDIO_OUTPUT_FLAG_FAST = 0x4;
    private static final int AUDIO_OUTPUT_FLAG_DEEP_BUFFER = 0x8;

    // Size of HW_AV_SYNC track AV header.
    private static final float HEADER_V2_SIZE_BYTES = 20.0f;

    //--------------------------------------------------------------------------
    // Member variables
    //--------------------
@@ -364,6 +367,10 @@ public class AudioTrack extends PlayerBase
     * HW_AV_SYNC track audio data bytes remaining to write after current AV sync header
     */
    private int mAvSyncBytesRemaining = 0;
    /**
     * Offset of the first sample of the audio in byte from start of HW_AV_SYNC track AV header.
     */
    private int mOffset = 0;

    //--------------------------------
    // Used exclusively by native code
@@ -603,6 +610,16 @@ public class AudioTrack extends PlayerBase
        mSampleRate = sampleRate[0];
        mSessionId = session[0];

        if ((mAttributes.getFlags() & AudioAttributes.FLAG_HW_AV_SYNC) != 0) {
            int frameSizeInBytes;
            if (AudioFormat.isEncodingLinearFrames(mAudioFormat)) {
                frameSizeInBytes = mChannelCount * AudioFormat.getBytesPerSample(mAudioFormat);
            } else {
                frameSizeInBytes = 1;
            }
            mOffset = ((int) Math.ceil(HEADER_V2_SIZE_BYTES / frameSizeInBytes)) * frameSizeInBytes;
        }

        if (mDataLoadMode == MODE_STATIC) {
            mState = STATE_NO_STATIC_DATA;
        } else {
@@ -2520,14 +2537,15 @@ public class AudioTrack extends PlayerBase

        // create timestamp header if none exists
        if (mAvSyncHeader == null) {
            mAvSyncHeader = ByteBuffer.allocate(16);
            mAvSyncHeader = ByteBuffer.allocate(mOffset);
            mAvSyncHeader.order(ByteOrder.BIG_ENDIAN);
            mAvSyncHeader.putInt(0x55550001);
            mAvSyncHeader.putInt(0x55550002);
        }

        if (mAvSyncBytesRemaining == 0) {
            mAvSyncHeader.putInt(4, sizeInBytes);
            mAvSyncHeader.putLong(8, timestamp);
            mAvSyncHeader.putInt(16, mOffset);
            mAvSyncHeader.position(0);
            mAvSyncBytesRemaining = sizeInBytes;
        }