BlueberryServer: Always play audio track
In some runs of PtsBot cases a2dp is already playing
(`isA2dpPlaying` returns true) when we start. In this
case we don't call AudioTrack#play making the playback
AudioTrack#write calls fails.
We should investigate why a2dp is playing instead but
in the meantime this is a simple and effective fix.
Test: atest pts-bot
Change-Id: I67d81723fb0c468f8629e0bfe4c43adac59a709c
diff --git a/android/blueberry/server/src/com/android/blueberry/A2dp.kt b/android/blueberry/server/src/com/android/blueberry/A2dp.kt
index ebac7ce..67fde15 100644
--- a/android/blueberry/server/src/com/android/blueberry/A2dp.kt
+++ b/android/blueberry/server/src/com/android/blueberry/A2dp.kt
@@ -168,17 +168,18 @@
throw Status.UNKNOWN.asException()
}
- if (!bluetoothA2dp.isA2dpPlaying(device)) {
- val a2dpPlayingStateFlow =
- flow
- .filter { it.getAction() == BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED }
- .filter {
- it.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE).address == address
- }
- .map { it.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothAdapter.ERROR) }
+ audioTrack.play()
- audioTrack.play()
- a2dpPlayingStateFlow.filter { it == BluetoothA2dp.STATE_PLAYING }.first()
+ // If A2dp is not already playing, wait for it
+ if (!bluetoothA2dp.isA2dpPlaying(device)) {
+ flow
+ .filter { it.getAction() == BluetoothA2dp.ACTION_PLAYING_STATE_CHANGED }
+ .filter {
+ it.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE).address == address
+ }
+ .map { it.getIntExtra(BluetoothA2dp.EXTRA_STATE, BluetoothAdapter.ERROR) }
+ .filter { it == BluetoothA2dp.STATE_PLAYING }
+ .first()
}
StartResponse.getDefaultInstance()
}