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

Commit 170725ab authored by Jean-Michel Trivi's avatar Jean-Michel Trivi Committed by Android (Google) Code Review
Browse files

Merge "AudioTrack API for delay/padding in offload"

parents f75556a0 157cba49
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -23523,6 +23523,8 @@ package android.media {
    method public int setBufferSizeInFrames(int);
    method public int setLoopPoints(int, int, int);
    method public int setNotificationMarkerPosition(int);
    method public void setOffloadDelayPadding(int, int);
    method public void setOffloadEndOfStream();
    method public int setPlaybackHeadPosition(int);
    method public void setPlaybackParams(@NonNull android.media.PlaybackParams);
    method public void setPlaybackPositionUpdateListener(android.media.AudioTrack.OnPlaybackPositionUpdateListener);
+1 −0
Original line number Diff line number Diff line
@@ -275,6 +275,7 @@ cc_library_shared {
        "libselinux",
        "libandroidicu",
        "libmedia",
        "libmedia_helper",
        "libmediametrics",
        "libmeminfo",
        "libaudioclient",
+30 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include "core_jni_helpers.h"

#include <utils/Log.h>
#include <media/AudioParameter.h>
#include <media/AudioSystem.h>
#include <media/AudioTrack.h>

@@ -1310,6 +1311,33 @@ static jint android_media_AudioTrack_get_port_id(JNIEnv *env, jobject thiz) {
    return (jint)lpTrack->getPortId();
}

// ----------------------------------------------------------------------------
static void android_media_AudioTrack_set_delay_padding(JNIEnv *env,  jobject thiz,
        jint delayInFrames, jint paddingInFrames) {
    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
    if (lpTrack == NULL) {
        jniThrowException(env, "java/lang/IllegalStateException",
                "AudioTrack not initialized");
        return;
    }
    AudioParameter param = AudioParameter();
    param.addInt(String8(AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES), (int) delayInFrames);
    param.addInt(String8(AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES), (int) paddingInFrames);
    lpTrack->setParameters(param.toString());
}

static void android_media_AudioTrack_set_eos(JNIEnv *env,  jobject thiz) {
    sp<AudioTrack> lpTrack = getAudioTrack(env, thiz);
    if (lpTrack == NULL) {
        jniThrowException(env, "java/lang/IllegalStateException",
                          "AudioTrack not initialized");
        return;
    }
    AudioParameter param = AudioParameter();
    param.addInt(String8("EOS"), 1);
    lpTrack->setParameters(param.toString());
}

// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
static const JNINativeMethod gMethods[] = {
@@ -1385,6 +1413,8 @@ static const JNINativeMethod gMethods[] = {
                                        (void *)android_media_AudioTrack_get_volume_shaper_state},
    {"native_setPresentation", "(II)I", (void *)android_media_AudioTrack_setPresentation},
    {"native_getPortId", "()I", (void *)android_media_AudioTrack_get_port_id},
    {"native_set_delay_padding", "(II)V", (void *)android_media_AudioTrack_set_delay_padding},
    {"native_set_eos",        "()V",    (void *)android_media_AudioTrack_set_eos},
};


+47 −0
Original line number Diff line number Diff line
@@ -995,6 +995,50 @@ public class AudioTrack extends PlayerBase
        }
    }

    /**
     * Configures the delay and padding values for the current compressed stream playing
     * in offload mode.
     * This can only be used on a track successfully initialized with
     * {@link AudioTrack.Builder#setOffloadedPlayback(boolean)}. The unit is frames, where a
     * frame indicates the number of samples per channel, e.g. 100 frames for a stereo compressed
     * stream corresponds to 200 decoded interleaved PCM samples.
     * @param delayInFrames number of frames to be ignored at the beginning of the stream. A value
     *     of 0 indicates no padding is to be applied.
     * @param paddingInFrames number of frames to be ignored at the end of the stream. A value of 0
     *     of 0 indicates no delay is to be applied.
     */
    public void setOffloadDelayPadding(int delayInFrames, int paddingInFrames) {
        if (paddingInFrames < 0) {
            throw new IllegalArgumentException("Illegal negative padding");
        }
        if (delayInFrames < 0) {
            throw new IllegalArgumentException("Illegal negative delay");
        }
        if (!mOffloaded) {
            throw new IllegalStateException("Illegal use of delay/padding on non-offloaded track");
        }
        if (mState == STATE_UNINITIALIZED) {
            throw new IllegalStateException("Uninitialized track");
        }
        native_set_delay_padding(delayInFrames, paddingInFrames);
    }

    /**
     * Declares that the last write() operation on this track provided the last buffer of this
     * stream.
     * After the end of stream, previously set padding and delay values are ignored.
     * Use this method in the same thread as any write() operation.
     */
    public void setOffloadEndOfStream() {
        if (!mOffloaded) {
            throw new IllegalStateException("EOS not supported on non-offloaded track");
        }
        if (mState == STATE_UNINITIALIZED) {
            throw new IllegalStateException("Uninitialized track");
        }
        native_set_eos();
    }

    /**
     * Returns whether direct playback of an audio format with the provided attributes is
     * currently supported on the system.
@@ -3457,6 +3501,9 @@ public class AudioTrack extends PlayerBase

    private native int native_getPortId();

    private native void native_set_delay_padding(int delayInFrames, int paddingInFrames);
    private native void native_set_eos();

    //---------------------------------------------------------
    // Utility methods
    //------------------