Skip to content

Commit b781737

Browse files
Francois GaffieGenkzsz11
authored andcommitted
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. Signed-off-by: Francois Gaffie <francois.gaffie@renault.com> Change-Id: Iaa920c073885c2556a93a1b5ae23467fa6ffad4f Merged-In: Iaa920c073885c2556a93a1b5ae23467fa6ffad4f Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
1 parent dea9884 commit b781737

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

media/java/android/media/HwAudioSource.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ public class HwAudioSource extends PlayerBase {
3535
private final AudioDeviceInfo mAudioDeviceInfo;
3636
private final AudioAttributes mAudioAttributes;
3737

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

4046
/**
4147
* Class constructor for a hardware audio source based player.
@@ -127,29 +133,38 @@ void playerStop() {
127133

128134
/**
129135
* Starts the playback from {@link AudioDeviceInfo}.
136+
* Starts does not return any error code, caller must check {@link HwAudioSource#isPlaying} to
137+
* ensure the state of the HwAudioSource encoded in {@link mNativeHandle}.
130138
*/
131139
public void start() {
132140
Preconditions.checkState(!isPlaying(), "HwAudioSource is currently playing");
133-
baseStart();
134141
mNativeHandle = AudioSystem.startAudioSource(
135142
mAudioDeviceInfo.getPort().activeConfig(),
136143
mAudioAttributes);
144+
if (isPlaying()) {
145+
baseStart();
146+
}
137147
}
138148

139149
/**
140150
* Checks whether the HwAudioSource player is playing.
151+
* It checks the state of the HwAudioSource encoded in {@link HwAudioSource#isPlaying}.
152+
* 0 corresponds to a stopped or uninitialized HwAudioSource.
153+
* Negative value corresponds to a status reported by {@link AudioSystem#startAudioSource} to
154+
* indicate a failure when trying to start the HwAudioSource.
155+
*
141156
* @return true if currently playing, false otherwise
142157
*/
143158
public boolean isPlaying() {
144-
return mNativeHandle != 0;
159+
return mNativeHandle > 0;
145160
}
146161

147162
/**
148163
* Stops the playback from {@link AudioDeviceInfo}.
149164
*/
150165
public void stop() {
151-
baseStop();
152166
if (mNativeHandle > 0) {
167+
baseStop();
153168
AudioSystem.stopAudioSource(mNativeHandle);
154169
mNativeHandle = 0;
155170
}

0 commit comments

Comments
 (0)