Merge "DO NOT MERGE setDeviceVolume: better logs, clears routing cache" into tm-qpr-dev

This commit is contained in:
Jean-Michel Trivi
2023-01-10 20:22:01 +00:00
committed by Android (Google) Code Review
5 changed files with 79 additions and 22 deletions

View File

@@ -82,6 +82,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
private final @NonNull AudioService mAudioService; private final @NonNull AudioService mAudioService;
private final @NonNull Context mContext; private final @NonNull Context mContext;
private final @NonNull AudioSystemAdapter mAudioSystem;
/** ID for Communication strategy retrieved form audio policy manager */ /** ID for Communication strategy retrieved form audio policy manager */
private int mCommunicationStrategyId = -1; private int mCommunicationStrategyId = -1;
@@ -156,12 +157,14 @@ import java.util.concurrent.atomic.AtomicBoolean;
public static final long USE_SET_COMMUNICATION_DEVICE = 243827847L; public static final long USE_SET_COMMUNICATION_DEVICE = 243827847L;
//------------------------------------------------------------------- //-------------------------------------------------------------------
/*package*/ AudioDeviceBroker(@NonNull Context context, @NonNull AudioService service) { /*package*/ AudioDeviceBroker(@NonNull Context context, @NonNull AudioService service,
@NonNull AudioSystemAdapter audioSystem) {
mContext = context; mContext = context;
mAudioService = service; mAudioService = service;
mBtHelper = new BtHelper(this); mBtHelper = new BtHelper(this);
mDeviceInventory = new AudioDeviceInventory(this); mDeviceInventory = new AudioDeviceInventory(this);
mSystemServer = SystemServerAdapter.getDefaultAdapter(mContext); mSystemServer = SystemServerAdapter.getDefaultAdapter(mContext);
mAudioSystem = audioSystem;
init(); init();
} }
@@ -170,12 +173,14 @@ import java.util.concurrent.atomic.AtomicBoolean;
* in system_server */ * in system_server */
AudioDeviceBroker(@NonNull Context context, @NonNull AudioService service, AudioDeviceBroker(@NonNull Context context, @NonNull AudioService service,
@NonNull AudioDeviceInventory mockDeviceInventory, @NonNull AudioDeviceInventory mockDeviceInventory,
@NonNull SystemServerAdapter mockSystemServer) { @NonNull SystemServerAdapter mockSystemServer,
@NonNull AudioSystemAdapter audioSystem) {
mContext = context; mContext = context;
mAudioService = service; mAudioService = service;
mBtHelper = new BtHelper(this); mBtHelper = new BtHelper(this);
mDeviceInventory = mockDeviceInventory; mDeviceInventory = mockDeviceInventory;
mSystemServer = mockSystemServer; mSystemServer = mockSystemServer;
mAudioSystem = audioSystem;
init(); init();
} }
@@ -450,7 +455,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
AudioAttributes attr = AudioAttributes attr =
AudioProductStrategy.getAudioAttributesForStrategyWithLegacyStreamType( AudioProductStrategy.getAudioAttributesForStrategyWithLegacyStreamType(
AudioSystem.STREAM_VOICE_CALL); AudioSystem.STREAM_VOICE_CALL);
List<AudioDeviceAttributes> devices = AudioSystem.getDevicesForAttributes( List<AudioDeviceAttributes> devices = mAudioSystem.getDevicesForAttributes(
attr, false /* forVolume */); attr, false /* forVolume */);
if (devices.isEmpty()) { if (devices.isEmpty()) {
if (mAudioService.isPlatformVoice()) { if (mAudioService.isPlatformVoice()) {
@@ -1225,7 +1230,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
Log.v(TAG, "onSetForceUse(useCase<" + useCase + ">, config<" + config + ">, fromA2dp<" Log.v(TAG, "onSetForceUse(useCase<" + useCase + ">, config<" + config + ">, fromA2dp<"
+ fromA2dp + ">, eventSource<" + eventSource + ">)"); + fromA2dp + ">, eventSource<" + eventSource + ">)");
} }
AudioSystem.setForceUse(useCase, config); mAudioSystem.setForceUse(useCase, config);
} }
private void onSendBecomingNoisyIntent() { private void onSendBecomingNoisyIntent() {
@@ -1863,9 +1868,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
if (preferredCommunicationDevice == null if (preferredCommunicationDevice == null
|| preferredCommunicationDevice.getType() != AudioDeviceInfo.TYPE_BLUETOOTH_SCO) { || preferredCommunicationDevice.getType() != AudioDeviceInfo.TYPE_BLUETOOTH_SCO) {
AudioSystem.setParameters("BT_SCO=off"); mAudioSystem.setParameters("BT_SCO=off");
} else { } else {
AudioSystem.setParameters("BT_SCO=on"); mAudioSystem.setParameters("BT_SCO=on");
} }
if (preferredCommunicationDevice == null) { if (preferredCommunicationDevice == null) {
AudioDeviceAttributes defaultDevice = getDefaultCommunicationDevice(); AudioDeviceAttributes defaultDevice = getDefaultCommunicationDevice();

View File

@@ -183,6 +183,7 @@ import com.android.server.LocalServices;
import com.android.server.SystemService; import com.android.server.SystemService;
import com.android.server.audio.AudioServiceEvents.DeviceVolumeEvent; import com.android.server.audio.AudioServiceEvents.DeviceVolumeEvent;
import com.android.server.audio.AudioServiceEvents.PhoneStateEvent; import com.android.server.audio.AudioServiceEvents.PhoneStateEvent;
import com.android.server.audio.AudioServiceEvents.VolChangedBroadcastEvent;
import com.android.server.audio.AudioServiceEvents.VolumeEvent; import com.android.server.audio.AudioServiceEvents.VolumeEvent;
import com.android.server.pm.UserManagerInternal; import com.android.server.pm.UserManagerInternal;
import com.android.server.pm.UserManagerInternal.UserRestrictionsListener; import com.android.server.pm.UserManagerInternal.UserRestrictionsListener;
@@ -1205,7 +1206,7 @@ public class AudioService extends IAudioService.Stub
mUseFixedVolume = mContext.getResources().getBoolean( mUseFixedVolume = mContext.getResources().getBoolean(
com.android.internal.R.bool.config_useFixedVolume); com.android.internal.R.bool.config_useFixedVolume);
mDeviceBroker = new AudioDeviceBroker(mContext, this); mDeviceBroker = new AudioDeviceBroker(mContext, this, mAudioSystem);
mRecordMonitor = new RecordingActivityMonitor(mContext); mRecordMonitor = new RecordingActivityMonitor(mContext);
mRecordMonitor.registerRecordingCallback(mVoiceRecordingActivityMonitor, true); mRecordMonitor.registerRecordingCallback(mVoiceRecordingActivityMonitor, true);
@@ -1637,7 +1638,7 @@ public class AudioService extends IAudioService.Stub
synchronized (mSettingsLock) { synchronized (mSettingsLock) {
final int forDock = mDockAudioMediaEnabled ? final int forDock = mDockAudioMediaEnabled ?
AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE; AudioSystem.FORCE_DIGITAL_DOCK : AudioSystem.FORCE_NONE;
mDeviceBroker.setForceUse_Async(AudioSystem.FOR_DOCK, forDock, "onAudioServerDied"); mDeviceBroker.setForceUse_Async(AudioSystem.FOR_DOCK, forDock, "onAudioServerDied");
sendEncodedSurroundMode(mContentResolver, "onAudioServerDied"); sendEncodedSurroundMode(mContentResolver, "onAudioServerDied");
sendEnabledSurroundFormats(mContentResolver, true); sendEnabledSurroundFormats(mContentResolver, true);
@@ -2258,9 +2259,10 @@ public class AudioService extends IAudioService.Stub
SENDMSG_QUEUE, SENDMSG_QUEUE,
AudioSystem.FOR_DOCK, AudioSystem.FOR_DOCK,
mDockAudioMediaEnabled ? mDockAudioMediaEnabled ?
AudioSystem.FORCE_ANALOG_DOCK : AudioSystem.FORCE_NONE, AudioSystem.FORCE_DIGITAL_DOCK : AudioSystem.FORCE_NONE,
new String("readDockAudioSettings"), new String("readDockAudioSettings"),
0); 0);
} }
@@ -3741,19 +3743,30 @@ public class AudioService extends IAudioService.Stub
Objects.requireNonNull(ada); Objects.requireNonNull(ada);
Objects.requireNonNull(callingPackage); Objects.requireNonNull(callingPackage);
AudioService.sVolumeLogger.loglogi("setDeviceVolume" + " from:" + callingPackage + " "
+ vi + " " + ada, TAG);
if (!vi.hasStreamType()) { if (!vi.hasStreamType()) {
Log.e(TAG, "Unsupported non-stream type based VolumeInfo", new Exception()); Log.e(TAG, "Unsupported non-stream type based VolumeInfo", new Exception());
return; return;
} }
int index = vi.getVolumeIndex(); int index = vi.getVolumeIndex();
if (index == VolumeInfo.INDEX_NOT_SET && !vi.hasMuteCommand()) { if (index == VolumeInfo.INDEX_NOT_SET && !vi.hasMuteCommand()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"changing device volume requires a volume index or mute command"); "changing device volume requires a volume index or mute command");
} }
// force a cache clear to force reevaluating stream type to audio device selection
// that can interfere with the sending of the VOLUME_CHANGED_ACTION intent
// TODO change cache management to not rely only on invalidation, but on "do not trust"
// moments when routing is in flux.
mAudioSystem.clearRoutingCache();
// log the current device that will be used when evaluating the sending of the
// VOLUME_CHANGED_ACTION intent to see if the current device is the one being modified
final int currDev = getDeviceForStream(vi.getStreamType());
AudioService.sVolumeLogger.log(new DeviceVolumeEvent(vi.getStreamType(), index, ada,
currDev, callingPackage));
// TODO handle unmuting of current audio device // TODO handle unmuting of current audio device
// if a stream is not muted but the VolumeInfo is for muting, set the volume index // if a stream is not muted but the VolumeInfo is for muting, set the volume index
// for the device to min volume // for the device to min volume
@@ -3837,11 +3850,11 @@ public class AudioService extends IAudioService.Stub
return; return;
} }
final AudioEventLogger.Event event = (device == null) if (device == null) {
? new VolumeEvent(VolumeEvent.VOL_SET_STREAM_VOL, streamType, // call was already logged in setDeviceVolume()
index/*val1*/, flags/*val2*/, callingPackage) sVolumeLogger.log(new VolumeEvent(VolumeEvent.VOL_SET_STREAM_VOL, streamType,
: new DeviceVolumeEvent(streamType, index, device, callingPackage); index/*val1*/, flags/*val2*/, callingPackage));
sVolumeLogger.log(event); }
setStreamVolume(streamType, index, flags, device, setStreamVolume(streamType, index, flags, device,
callingPackage, callingPackage, attributionTag, callingPackage, callingPackage, attributionTag,
Binder.getCallingUid(), callingOrSelfHasAudioSettingsPermission()); Binder.getCallingUid(), callingOrSelfHasAudioSettingsPermission());
@@ -4242,7 +4255,11 @@ public class AudioService extends IAudioService.Stub
maybeSendSystemAudioStatusCommand(false); maybeSendSystemAudioStatusCommand(false);
} }
} }
sendVolumeUpdate(streamType, oldIndex, index, flags, device); if (ada == null) {
// only non-null when coming here from setDeviceVolume
// TODO change test to check early if device is current device or not
sendVolumeUpdate(streamType, oldIndex, index, flags, device);
}
} }
@@ -7982,6 +7999,8 @@ public class AudioService extends IAudioService.Stub
mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex); mVolumeChanged.putExtra(AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, oldIndex);
mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS, mVolumeChanged.putExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE_ALIAS,
mStreamVolumeAlias[mStreamType]); mStreamVolumeAlias[mStreamType]);
AudioService.sVolumeLogger.log(new VolChangedBroadcastEvent(
mStreamType, mStreamVolumeAlias[mStreamType], index));
sendBroadcastToAll(mVolumeChanged); sendBroadcastToAll(mVolumeChanged);
} }
} }
@@ -10155,7 +10174,7 @@ public class AudioService extends IAudioService.Stub
static final int LOG_NB_EVENTS_PHONE_STATE = 20; static final int LOG_NB_EVENTS_PHONE_STATE = 20;
static final int LOG_NB_EVENTS_DEVICE_CONNECTION = 50; static final int LOG_NB_EVENTS_DEVICE_CONNECTION = 50;
static final int LOG_NB_EVENTS_FORCE_USE = 20; static final int LOG_NB_EVENTS_FORCE_USE = 20;
static final int LOG_NB_EVENTS_VOLUME = 40; static final int LOG_NB_EVENTS_VOLUME = 100;
static final int LOG_NB_EVENTS_DYN_POLICY = 10; static final int LOG_NB_EVENTS_DYN_POLICY = 10;
static final int LOG_NB_EVENTS_SPATIAL = 30; static final int LOG_NB_EVENTS_SPATIAL = 30;

View File

@@ -147,19 +147,42 @@ public class AudioServiceEvents {
} }
} }
static final class VolChangedBroadcastEvent extends AudioEventLogger.Event {
final int mStreamType;
final int mAliasStreamType;
final int mIndex;
VolChangedBroadcastEvent(int stream, int alias, int index) {
mStreamType = stream;
mAliasStreamType = alias;
mIndex = index;
}
@Override
public String eventToString() {
return new StringBuilder("sending VOLUME_CHANGED stream:")
.append(AudioSystem.streamToString(mStreamType))
.append(" index:").append(mIndex)
.append(" alias:").append(AudioSystem.streamToString(mAliasStreamType))
.toString();
}
}
static final class DeviceVolumeEvent extends AudioEventLogger.Event { static final class DeviceVolumeEvent extends AudioEventLogger.Event {
final int mStream; final int mStream;
final int mVolIndex; final int mVolIndex;
final String mDeviceNativeType; final String mDeviceNativeType;
final String mDeviceAddress; final String mDeviceAddress;
final String mCaller; final String mCaller;
final int mDeviceForStream;
DeviceVolumeEvent(int streamType, int index, @NonNull AudioDeviceAttributes device, DeviceVolumeEvent(int streamType, int index, @NonNull AudioDeviceAttributes device,
String callingPackage) { int deviceForStream, String callingPackage) {
mStream = streamType; mStream = streamType;
mVolIndex = index; mVolIndex = index;
mDeviceNativeType = "0x" + Integer.toHexString(device.getInternalType()); mDeviceNativeType = "0x" + Integer.toHexString(device.getInternalType());
mDeviceAddress = device.getAddress(); mDeviceAddress = device.getAddress();
mDeviceForStream = deviceForStream;
mCaller = callingPackage; mCaller = callingPackage;
// log metrics // log metrics
new MediaMetrics.Item(MediaMetrics.Name.AUDIO_VOLUME_EVENT) new MediaMetrics.Item(MediaMetrics.Name.AUDIO_VOLUME_EVENT)
@@ -180,7 +203,9 @@ public class AudioServiceEvents {
.append(" index:").append(mVolIndex) .append(" index:").append(mVolIndex)
.append(" device:").append(mDeviceNativeType) .append(" device:").append(mDeviceNativeType)
.append(" addr:").append(mDeviceAddress) .append(" addr:").append(mDeviceAddress)
.append(") from ").append(mCaller).toString(); .append(") from ").append(mCaller)
.append(" currDevForStream:Ox").append(Integer.toHexString(mDeviceForStream))
.toString();
} }
} }

View File

@@ -105,6 +105,13 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback,
} }
} }
public void clearRoutingCache() {
if (DEBUG_CACHE) {
Log.d(TAG, "---- routing cache clear (from java) ----------");
}
invalidateRoutingCache();
}
/** /**
* Implementation of AudioSystem.VolumeRangeInitRequestCallback * Implementation of AudioSystem.VolumeRangeInitRequestCallback
*/ */
@@ -337,6 +344,7 @@ public class AudioSystemAdapter implements AudioSystem.RoutingUpdateCallback,
* @return * @return
*/ */
public int setParameters(String keyValuePairs) { public int setParameters(String keyValuePairs) {
invalidateRoutingCache();
return AudioSystem.setParameters(keyValuePairs); return AudioSystem.setParameters(keyValuePairs);
} }

View File

@@ -74,7 +74,7 @@ public class AudioDeviceBrokerTest {
mSpyDevInventory = spy(new AudioDeviceInventory(mSpyAudioSystem)); mSpyDevInventory = spy(new AudioDeviceInventory(mSpyAudioSystem));
mSpySystemServer = spy(new NoOpSystemServerAdapter()); mSpySystemServer = spy(new NoOpSystemServerAdapter());
mAudioDeviceBroker = new AudioDeviceBroker(mContext, mMockAudioService, mSpyDevInventory, mAudioDeviceBroker = new AudioDeviceBroker(mContext, mMockAudioService, mSpyDevInventory,
mSpySystemServer); mSpySystemServer, mSpyAudioSystem);
mSpyDevInventory.setDeviceBroker(mAudioDeviceBroker); mSpyDevInventory.setDeviceBroker(mAudioDeviceBroker);
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();