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