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

Commit 5253a31c authored by Francois Gaffie's avatar Francois Gaffie Committed by Eric Laurent
Browse files

HwAudioSource: wrong native handle check



Playing status of audio source is checked using the native handler
not 0.
However, if AudioSystem#startAudioSource fails, the native handler
returned is not 0, but an error code.
In fact AudioPolicyManager sets the PORT to NONE (0) and sets an error
status.
JNI layer transforms the failure status into a native handler returned
to the caller.

Bug: 186088557
Test: make

Signed-off-by: default avatarFrancois Gaffie <francois.gaffie@renault.com>
Change-Id: Iaa920c073885c2556a93a1b5ae23467fa6ffad4f
parent 81b0519f
Loading
Loading
Loading
Loading
+20 −5
Original line number Diff line number Diff line
@@ -37,7 +37,13 @@ public class HwAudioSource extends PlayerBase {
    private final AudioDeviceInfo mAudioDeviceInfo;
    private final AudioAttributes mAudioAttributes;

    private int mNativeHandle;
    /**
     * The value of the native handle encodes the HwAudioSource state.
     * The native handle returned by {@link AudioSystem#startAudioSource} is either valid
     * (aka > 0, so successfully started) or hosting an error code (negative).
     * 0 corresponds to an untialized or stopped HwAudioSource.
     */
    private int mNativeHandle = 0;

    /**
     * Class constructor for a hardware audio source based player.
@@ -129,15 +135,19 @@ public class HwAudioSource extends PlayerBase {

    /**
     * Starts the playback from {@link AudioDeviceInfo}.
     * Starts does not return any error code, caller must check {@link HwAudioSource#isPlaying} to
     * ensure the state of the HwAudioSource encoded in {@link mNativeHandle}.
     */
    public void start() {
        Preconditions.checkState(!isPlaying(), "HwAudioSource is currently playing");
        mNativeHandle = AudioSystem.startAudioSource(
                mAudioDeviceInfo.getPort().activeConfig(),
                mAudioAttributes);
        if (isPlaying()) {
            // FIXME: b/174876389 clean up device id reporting
            baseStart(getDeviceId());
        }
    }

    private int getDeviceId() {
        ArrayList<AudioPatch> patches = new ArrayList<AudioPatch>();
@@ -162,18 +172,23 @@ public class HwAudioSource extends PlayerBase {

    /**
     * Checks whether the HwAudioSource player is playing.
     * It checks the state of the HwAudioSource encoded in {@link HwAudioSource#isPlaying}.
     * 0 corresponds to a stopped or uninitialized HwAudioSource.
     * Negative value corresponds to a status reported by {@link AudioSystem#startAudioSource} to
     * indicate a failure when trying to start the HwAudioSource.
     *
     * @return true if currently playing, false otherwise
     */
    public boolean isPlaying() {
        return mNativeHandle != 0;
        return mNativeHandle > 0;
    }

    /**
     * Stops the playback from {@link AudioDeviceInfo}.
     */
    public void stop() {
        baseStop();
        if (mNativeHandle > 0) {
            baseStop();
            AudioSystem.stopAudioSource(mNativeHandle);
            mNativeHandle = 0;
        }