blob: 4b0a14241d6b6378df0244e3758883db13e58080 [file] [log] [blame]
Jean-Michel Trivid3c71f02015-12-07 11:59:31 -08001/*
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
17package com.android.server.audio;
18
Jean-Michel Trivi33fd8162016-02-22 10:52:41 -080019import android.media.AudioFormat;
Jean-Michel Trivid3c71f02015-12-07 11:59:31 -080020import android.media.AudioManager;
21import android.media.AudioRecordConfiguration;
22import android.media.AudioSystem;
23import android.media.IRecordingConfigDispatcher;
Jean-Michel Trividd2772a2016-02-17 12:30:52 -080024import android.media.MediaRecorder;
Jean-Michel Trivid3c71f02015-12-07 11:59:31 -080025import android.os.IBinder;
26import android.os.RemoteException;
27import android.util.Log;
28
29import java.util.ArrayList;
30import java.util.HashMap;
31import java.util.Iterator;
32
33/**
34 * Class to receive and dispatch updates from AudioSystem about recording configurations.
35 */
36public 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
42 private HashMap<Integer, AudioRecordConfiguration> mRecordConfigs =
43 new HashMap<Integer, AudioRecordConfiguration>();
44
45 RecordingActivityMonitor() {
46 RecMonitorClient.sMonitor = this;
47 }
48
49 /**
50 * Implementation of android.media.AudioSystem.AudioRecordingCallback
51 */
Jean-Michel Trivi33fd8162016-02-22 10:52:41 -080052 public void onRecordingConfigurationChanged(int event, int session, int source,
53 int[] recordingFormat) {
Jean-Michel Trividd2772a2016-02-17 12:30:52 -080054 if (MediaRecorder.isSystemOnlyAudioSource(source)) {
55 return;
56 }
Jean-Michel Trivi33fd8162016-02-22 10:52:41 -080057 if (updateSnapshot(event, session, source, recordingFormat)) {
Jean-Michel Trivid3c71f02015-12-07 11:59:31 -080058 final Iterator<RecMonitorClient> clientIterator = mClients.iterator();
59 synchronized(mClients) {
60 while (clientIterator.hasNext()) {
61 try {
62 clientIterator.next().mDispatcherCb.dispatchRecordingConfigChange();
63 } catch (RemoteException e) {
64 Log.w(TAG, "Could not call dispatchRecordingConfigChange() on client", e);
65 }
66 }
67 }
68 }
69 }
70
71 void initMonitor() {
72 AudioSystem.setRecordingCallback(this);
73 }
74
75 void registerRecordingCallback(IRecordingConfigDispatcher rcdb) {
76 if (rcdb == null) {
77 return;
78 }
79 synchronized(mClients) {
80 final RecMonitorClient rmc = new RecMonitorClient(rcdb);
81 if (rmc.init()) {
82 mClients.add(rmc);
83 }
84 }
85 }
86
87 void unregisterRecordingCallback(IRecordingConfigDispatcher rcdb) {
88 if (rcdb == null) {
89 return;
90 }
91 synchronized(mClients) {
92 final Iterator<RecMonitorClient> clientIterator = mClients.iterator();
93 while (clientIterator.hasNext()) {
94 RecMonitorClient rmc = clientIterator.next();
95 if (rcdb.equals(rmc.mDispatcherCb)) {
96 rmc.release();
97 clientIterator.remove();
98 break;
99 }
100 }
101 }
102 }
103
104 AudioRecordConfiguration[] getActiveRecordConfigurations() {
105 synchronized(mRecordConfigs) {
106 return mRecordConfigs.values().toArray(new AudioRecordConfiguration[0]);
107 }
108 }
109
110 /**
111 * Update the internal "view" of the active recording sessions
112 * @param event
113 * @param session
114 * @param source
Jean-Michel Trivi33fd8162016-02-22 10:52:41 -0800115 * @param recordingFormat see
116 * {@link AudioSystem.AudioRecordingCallback#onRecordingConfigurationChanged(int, int, int, int[])}
117 * for the definition of the contents of the array
Jean-Michel Trivid3c71f02015-12-07 11:59:31 -0800118 * @return true if the list of active recording sessions has been modified, false otherwise.
119 */
Jean-Michel Trivi33fd8162016-02-22 10:52:41 -0800120 private boolean updateSnapshot(int event, int session, int source, int[] recordingFormat) {
Jean-Michel Trivid3c71f02015-12-07 11:59:31 -0800121 synchronized(mRecordConfigs) {
122 switch (event) {
123 case AudioManager.RECORD_CONFIG_EVENT_STOP:
124 // return failure if an unknown recording session stopped
125 return (mRecordConfigs.remove(new Integer(session)) != null);
126 case AudioManager.RECORD_CONFIG_EVENT_START:
Jean-Michel Trivi33fd8162016-02-22 10:52:41 -0800127 final AudioFormat clientFormat = new AudioFormat.Builder()
128 .setEncoding(recordingFormat[0])
129 // FIXME this doesn't support index-based masks
130 .setChannelMask(recordingFormat[1])
131 .setSampleRate(recordingFormat[2])
132 .build();
133 final AudioFormat deviceFormat = new AudioFormat.Builder()
134 .setEncoding(recordingFormat[3])
135 // FIXME this doesn't support index-based masks
136 .setChannelMask(recordingFormat[4])
137 .setSampleRate(recordingFormat[5])
138 .build();
Jean-Michel Trivid3c71f02015-12-07 11:59:31 -0800139 if (mRecordConfigs.containsKey(new Integer(session))) {
140 // start of session that's already tracked, not worth an update
141 // TO DO in the future when tracking record format: there might be a record
142 // format change during a recording that requires reporting
143 return false;
144 } else {
145 mRecordConfigs.put(new Integer(session),
Jean-Michel Trivi33fd8162016-02-22 10:52:41 -0800146 new AudioRecordConfiguration(session, source,
147 clientFormat, deviceFormat));
Jean-Michel Trivid3c71f02015-12-07 11:59:31 -0800148 return true;
149 }
150 default:
151 Log.e(TAG, String.format("Unknown event %d for session %d, source %d",
152 event, session, source));
153 return false;
154 }
155 }
156 }
157
158 /**
159 * Inner class to track clients that want to be notified of recording updates
160 */
161 private final static class RecMonitorClient implements IBinder.DeathRecipient {
162
163 // can afford to be static because only one RecordingActivityMonitor ever instantiated
164 static RecordingActivityMonitor sMonitor;
165
166 final IRecordingConfigDispatcher mDispatcherCb;
167
168 RecMonitorClient(IRecordingConfigDispatcher rcdb) {
169 mDispatcherCb = rcdb;
170 }
171
172 public void binderDied() {
173 Log.w(TAG, "client died");
174 sMonitor.unregisterRecordingCallback(mDispatcherCb);
175 }
176
177 boolean init() {
178 try {
179 mDispatcherCb.asBinder().linkToDeath(this, 0);
180 return true;
181 } catch (RemoteException e) {
182 Log.w(TAG, "Could not link to client death", e);
183 return false;
184 }
185 }
186
187 void release() {
188 mDispatcherCb.asBinder().unlinkToDeath(this, 0);
189 }
190 }
191}