Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.audio; |
| 18 | |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 19 | import android.media.AudioFormat; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 20 | import android.media.AudioManager; |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 21 | import android.media.AudioRecordingConfiguration; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 22 | import android.media.AudioSystem; |
| 23 | import android.media.IRecordingConfigDispatcher; |
Jean-Michel Trivi | dd2772a | 2016-02-17 12:30:52 -0800 | [diff] [blame] | 24 | import android.media.MediaRecorder; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 25 | import android.os.IBinder; |
| 26 | import android.os.RemoteException; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.HashMap; |
| 31 | import java.util.Iterator; |
| 32 | |
| 33 | /** |
| 34 | * Class to receive and dispatch updates from AudioSystem about recording configurations. |
| 35 | */ |
| 36 | public final class RecordingActivityMonitor implements AudioSystem.AudioRecordingCallback { |
| 37 | |
| 38 | public final static String TAG = "AudioService.RecordingActivityMonitor"; |
| 39 | |
| 40 | private ArrayList<RecMonitorClient> mClients = new ArrayList<RecMonitorClient>(); |
| 41 | |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 42 | private HashMap<Integer, AudioRecordingConfiguration> mRecordConfigs = |
| 43 | new HashMap<Integer, AudioRecordingConfiguration>(); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 44 | |
| 45 | RecordingActivityMonitor() { |
| 46 | RecMonitorClient.sMonitor = this; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Implementation of android.media.AudioSystem.AudioRecordingCallback |
| 51 | */ |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 52 | public void onRecordingConfigurationChanged(int event, int session, int source, |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 53 | int[] recordingInfo) { |
Jean-Michel Trivi | dd2772a | 2016-02-17 12:30:52 -0800 | [diff] [blame] | 54 | if (MediaRecorder.isSystemOnlyAudioSource(source)) { |
| 55 | return; |
| 56 | } |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 57 | final AudioRecordingConfiguration[] configs = |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 58 | updateSnapshot(event, session, source, recordingInfo); |
| 59 | if (configs != null){ |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 60 | synchronized(mClients) { |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 61 | final Iterator<RecMonitorClient> clientIterator = mClients.iterator(); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 62 | while (clientIterator.hasNext()) { |
| 63 | try { |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 64 | clientIterator.next().mDispatcherCb.dispatchRecordingConfigChange( |
| 65 | configs); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 66 | } catch (RemoteException e) { |
| 67 | Log.w(TAG, "Could not call dispatchRecordingConfigChange() on client", e); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void initMonitor() { |
| 75 | AudioSystem.setRecordingCallback(this); |
| 76 | } |
| 77 | |
| 78 | void registerRecordingCallback(IRecordingConfigDispatcher rcdb) { |
| 79 | if (rcdb == null) { |
| 80 | return; |
| 81 | } |
| 82 | synchronized(mClients) { |
| 83 | final RecMonitorClient rmc = new RecMonitorClient(rcdb); |
| 84 | if (rmc.init()) { |
| 85 | mClients.add(rmc); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void unregisterRecordingCallback(IRecordingConfigDispatcher rcdb) { |
| 91 | if (rcdb == null) { |
| 92 | return; |
| 93 | } |
| 94 | synchronized(mClients) { |
| 95 | final Iterator<RecMonitorClient> clientIterator = mClients.iterator(); |
| 96 | while (clientIterator.hasNext()) { |
| 97 | RecMonitorClient rmc = clientIterator.next(); |
| 98 | if (rcdb.equals(rmc.mDispatcherCb)) { |
| 99 | rmc.release(); |
| 100 | clientIterator.remove(); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 107 | AudioRecordingConfiguration[] getActiveRecordingConfigurations() { |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 108 | synchronized(mRecordConfigs) { |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 109 | return mRecordConfigs.values().toArray(new AudioRecordingConfiguration[0]); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Update the internal "view" of the active recording sessions |
| 115 | * @param event |
| 116 | * @param session |
| 117 | * @param source |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 118 | * @param recordingFormat see |
| 119 | * {@link AudioSystem.AudioRecordingCallback#onRecordingConfigurationChanged(int, int, int, int[])} |
| 120 | * for the definition of the contents of the array |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 121 | * @return null if the list of active recording sessions has not been modified, an array |
| 122 | * with the current active configurations otherwise. |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 123 | */ |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 124 | private AudioRecordingConfiguration[] updateSnapshot(int event, int session, int source, |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 125 | int[] recordingInfo) { |
| 126 | final boolean configChanged; |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 127 | final AudioRecordingConfiguration[] configs; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 128 | synchronized(mRecordConfigs) { |
| 129 | switch (event) { |
| 130 | case AudioManager.RECORD_CONFIG_EVENT_STOP: |
| 131 | // return failure if an unknown recording session stopped |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 132 | configChanged = (mRecordConfigs.remove(new Integer(session)) != null); |
| 133 | break; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 134 | case AudioManager.RECORD_CONFIG_EVENT_START: |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 135 | final AudioFormat clientFormat = new AudioFormat.Builder() |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 136 | .setEncoding(recordingInfo[0]) |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 137 | // FIXME this doesn't support index-based masks |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 138 | .setChannelMask(recordingInfo[1]) |
| 139 | .setSampleRate(recordingInfo[2]) |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 140 | .build(); |
| 141 | final AudioFormat deviceFormat = new AudioFormat.Builder() |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 142 | .setEncoding(recordingInfo[3]) |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 143 | // FIXME this doesn't support index-based masks |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 144 | .setChannelMask(recordingInfo[4]) |
| 145 | .setSampleRate(recordingInfo[5]) |
Jean-Michel Trivi | 33fd816 | 2016-02-22 10:52:41 -0800 | [diff] [blame] | 146 | .build(); |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 147 | final int patchHandle = recordingInfo[6]; |
| 148 | final Integer sessionKey = new Integer(session); |
| 149 | if (mRecordConfigs.containsKey(sessionKey)) { |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 150 | final AudioRecordingConfiguration updatedConfig = |
| 151 | new AudioRecordingConfiguration(session, source, |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 152 | clientFormat, deviceFormat, patchHandle); |
| 153 | if (updatedConfig.equals(mRecordConfigs.get(sessionKey))) { |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 154 | configChanged = false; |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 155 | } else { |
| 156 | // config exists but has been modified |
| 157 | mRecordConfigs.remove(sessionKey); |
| 158 | mRecordConfigs.put(sessionKey, updatedConfig); |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 159 | configChanged = true; |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 160 | } |
| 161 | } else { |
| 162 | mRecordConfigs.put(sessionKey, |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 163 | new AudioRecordingConfiguration(session, source, |
Jean-Michel Trivi | 8ab7280 | 2016-02-25 16:31:45 -0800 | [diff] [blame] | 164 | clientFormat, deviceFormat, patchHandle)); |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 165 | configChanged = true; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 166 | } |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 167 | break; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 168 | default: |
| 169 | Log.e(TAG, String.format("Unknown event %d for session %d, source %d", |
| 170 | event, session, source)); |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 171 | configChanged = false; |
| 172 | } |
| 173 | if (configChanged) { |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame^] | 174 | configs = mRecordConfigs.values().toArray(new AudioRecordingConfiguration[0]); |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 175 | } else { |
| 176 | configs = null; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 179 | return configs; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Inner class to track clients that want to be notified of recording updates |
| 184 | */ |
| 185 | private final static class RecMonitorClient implements IBinder.DeathRecipient { |
| 186 | |
| 187 | // can afford to be static because only one RecordingActivityMonitor ever instantiated |
| 188 | static RecordingActivityMonitor sMonitor; |
| 189 | |
| 190 | final IRecordingConfigDispatcher mDispatcherCb; |
| 191 | |
| 192 | RecMonitorClient(IRecordingConfigDispatcher rcdb) { |
| 193 | mDispatcherCb = rcdb; |
| 194 | } |
| 195 | |
| 196 | public void binderDied() { |
| 197 | Log.w(TAG, "client died"); |
| 198 | sMonitor.unregisterRecordingCallback(mDispatcherCb); |
| 199 | } |
| 200 | |
| 201 | boolean init() { |
| 202 | try { |
| 203 | mDispatcherCb.asBinder().linkToDeath(this, 0); |
| 204 | return true; |
| 205 | } catch (RemoteException e) { |
| 206 | Log.w(TAG, "Could not link to client death", e); |
| 207 | return false; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void release() { |
| 212 | mDispatcherCb.asBinder().unlinkToDeath(this, 0); |
| 213 | } |
| 214 | } |
| 215 | } |