blob: 2a3fc644ee6d76c0afd7ab8838d3bb57308c92ee [file] [log] [blame]
Ytai Ben-Tsvifb557022021-03-05 17:05:19 -08001/*
2 * Copyright 2021 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 android.hardware.soundtrigger3;
18
19import android.hardware.soundtrigger3.ISoundTriggerHwCallback;
20import android.hardware.soundtrigger3.ISoundTriggerHwGlobalCallback;
21
22import android.media.soundtrigger.PhraseSoundModel;
23import android.media.soundtrigger.Properties;
24import android.media.soundtrigger.RecognitionConfig;
25import android.media.soundtrigger.SoundModel;
26import android.media.soundtrigger.ModelParameter;
27import android.media.soundtrigger.ModelParameterRange;
28import android.media.soundtrigger.Properties;
29import android.media.soundtrigger.RecognitionConfig;
30
31/**
32 * SoundTrigger HAL interface. Used for hardware recognition of hotwords
33 * and other sounds.
34 *
35 * Basic usage:
36 * ============
37 * ISoundTriggerHw supports the ability to have one of more detection sessions running at a given
38 * time, and listening to acoustic events. The basic flow of setting up such a session is:
39 * - Load a model using loadSoundModel() or loadPhraseSoundModel(). The provided model object would
40 * indicate the (implementation-specific) detection algorithm (engine) to use, as well as any
41 * parameters applicable for this agorithm. Upon success, those methods would return a handle
42 * which will be used to reference this model in subsequent calls.
43 * - Once the model had been successfully loaded, detection can begin by calling startRecognition().
44 * - Recognition will continue running in the background until one of the following events occurs:
45 * - stopRecognition() has been called on this model.
46 * - A detection has occurred.
47 * - Detection was aborted, typically for resource constraints, for example, when a higher-
48 * priority use-case has been initiated.
49 * - In the latter two cases, a recognition event will be sent via a the callback interface that was
50 * registered by the client upon loading. In either case, after any of these events occur, the
51 * detection becomes inactive and no more recognition callbacks are allowed.
52 * - The same model maybe started again at a later time, and this process may repeat as many times
53 * as needed.
54 * - Finally, an inactive model that is no longer needed may be unloaded via unloadModel().
55 *
56 * Important notes about the threading model:
57 * ==========================================
58 * Both this interface and the corresponding callback interface use a synchronous calling
59 * convention. This model comes with some advantages, but also with some risks of deadlocks if the
60 * implementation does not handle this correctly. Please consider the following:
61 * - After stopRecognition() returns, no more recognition events for that model may be sent. This
62 * implies that any queues holding such events must be flushed before the call returns and that
63 * may imply that callback from the HAL to the client are done while stopRecognition() is blocked.
64 * This is OK, and supported by the framework.
65 * - Similarly, the same relationship applies between unloadModel() and subsequent callbacks to
66 * modelUnloaded().
67 * - Other than these two cases, calls into the HAL *MAY NOT* block on callbacks from the HAL, or
68 * else deadlock conditions may result, which may be handled by rebooting of the HAL process and
69 * cause service outages.
70 *
71 * Due to the asynchronous nature of recognition events and preemptive model unloading, the HAL must
72 * correctly handle requests that would have been valid before an event has been delivered, but
73 * became moot as result of the event. Namely:
74 * - stopRecognition() may be called on a model that has already delivered an event and became
75 * inactive as a result. The HAL must return a successful return code in this case.
76 * - Furthermore, if a model is preemptively unloaded after it triggers (typically, this would
77 * happen when it is first aborted and immediately preemptively unloaded), stopRecognition() may
78 * be called on it. The HAL must return successfully in this case.
79 * - startRecognition() may be called on a model that has been preemptively unloaded. In this case,
80 * the HAL must signal a ServiceSpecificException(RESOURCE_CONTENTION) to indicate that the
81 * operation is temporarily unsuccessful.
82 * - unloadSoundModel() may be called on a model that has been preemptively unloaded. The HAL must
83 * return a successful return code in this case. This also implies that model handles should
84 * generally not be reused until explicitly unloaded. To avoid the rare possibility of running out
85 * of handles, the framework may call unloadModel() on models that have been preemptively unloaded
86 * by the HAL.
87 *
88 * Important notes about resource constraints and concurrency
89 * =========================================================
90 * Up until this version, the framework would enforce concurrency constraints expressed by the
91 * Properties presented by the soundtrigger instance. These include constraints on the maximum
92 * amount of models that can be loaded at the same time and on running recognition while capturing
93 * from the microphone.
94 * This version changes the approach for how these constraints are modeled, both offering the HAL
95 * implementation more flexibility and simplifying the framework's job in enforcing these
96 * limitations. Note that there is no change for how the framework behaves with earlier versions,
97 * everything described below only applies to this version and onward.
98 * The way this is achieved is as following:
99 * - The framework will no longer enforce constraints on concurrent loading of models, as expressed
100 * in the Properties.maxSoundModels field (this property is merely a hint at this point and may be
101 * deprecated in the future), or any other implicit constraints.
102 * - The framework will no longer enforce constraints on concurrency of audio recording and
103 * soundtrigger operation, as expressed in the Properties.concurrentCapture field (this property
104 * is merely a hint at this point and may be deprecated in the future).
105 * - The HAL implementation is free to reject loading of any model at any time by having the
106 * respective load*() method signal a ServiceSpecificException(RESOURCE_CONTENTION).
107 * - The HAL implementation is free to reject starting of any model at any time by having the
108 * respective start*() method signal a ServiceSpecificException(RESOURCE_CONTENTION).
109 * - The HAL implementation is free to preemptively stop a previously started model at its own
110 * discretion (for example, if a higher priority use-case which cannot coexist with detection
111 * has been requested). The HAL must notify the framework of the preemption by sending a
112 * recognition event with an `ABORTED` status. The implementation must NOT attempt to restart the
113 * recognition automatically when conditions change.
114 * - The HAL implementation is free to preemptively unload a previously loaded model at its own
115 * discretion (for example, if a higher-priority model is being loaded and the two cannot
116 * coexist). When doing so, it must first abort the detection if active (as per above) and then
117 * notify the framework of the unload using the modelUnloaded() callback.
118 * - When conditions change, such that a model that couldn't previously load or start or that had
119 * previously been preemptively stopped or unloaded, the HAL must notify the framework via the
120 * newly added onResourcesAvailable() callback. This callback is not a guarantee that any
121 * operation would now succeed, but merely a hint that retrying something that had previously
122 * failed, now MAY succeed. Until this callback is invoked, the client may assume that any
123 * operation that had previously failed or aborted would still fail if retried, so the
124 * implementation should not forget to deliver it.
125 * There are no guarantees regarding how the framework may respond to this event and the order in
126 * which it may choose to reload/restart its models. Typically, as result of this event the
127 * framework will make a single attempt per model to bring this model to its desired state
128 * (loaded, started).
129 */
130@VintfStability
131interface ISoundTriggerHw {
132 /**
133 * Retrieve implementation properties.
134 *
135 * @return A Properties structure containing implementation description and capabilities.
136 */
137 Properties getProperties();
138
139 /**
140 * This will get called at most once per every attachment to the service.
141 *
142 * All events not tied to a specific model should go through this callback.
143 *
144 * @param callback An interface to receive global event callbacks.
145 */
146 void registerGlobalCallback(in ISoundTriggerHwGlobalCallback callback);
147
148 /**
149 * Load a sound model. Once loaded, recognition of this model can be started and stopped.
150 *
151 * @param soundModel A SoundModel structure describing the sound model to load.
152 * @param callback The callback interface on which the recognitionCallback()
153 * method will be called upon completion and modelUnloaded() upon preemptive unload.
154 * @return A unique handle assigned by the HAL for use by the client when controlling
155 * activity for this sound model.
156 * @throws ServiceSpecificException(RESOURCE_CONTENTION) if the model cannot be loaded due
157 * to resource constraints. This is typically a temporary condition and the client may
158 * retry after the onResourcesAvailable() global callback is invoked.
159 */
160 int loadSoundModel(in SoundModel soundModel, in ISoundTriggerHwCallback callback);
161
162 /**
163 * Load a key phrase sound model. Once loaded, recognition of this model can be started and
164 * stopped.
165 *
166 * @param soundModel A PhraseSoundModel structure describing the sound model to load.
167 * @param callback The callback interface on which the phraseRecognitionCallback() method will
168 * be called upon completion and modelUnloaded() upon preempted unload.
169 * @return A unique handle assigned by the HAL for use by the framework when controlling
170 * activity for this sound model.
171 * @throws ServiceSpecificException(RESOURCE_CONTENTION) if the model cannot be loaded due
172 * to resource constraints. This is typically a temporary condition and the client may
173 * retry after the onResourcesAvailable() global callback is invoked.
174 */
175 int loadPhraseSoundModel(in PhraseSoundModel soundModel, in ISoundTriggerHwCallback callback);
176
177 /**
178 * Unload a sound model. A sound model may be unloaded to free up resources and make room for a
179 * new one to overcome implementation limitations.
180 * This call is idempotent, to avoid any race conditions.
181 *
182 * @param modelHandle the handle of the sound model to unload.
183 */
184 void unloadSoundModel(in int modelHandle);
185
186 /**
187 * Start recognition on a given model.
188 * This must be called on a model that is in the stopped state.
189 * The state of this model becomes active and will remain so until explicitly stopped, or a
190 * recognition event had been delivered to the client.
191 *
192 * @param modelHandle the handle of the sound model to use for recognition
193 * @param deviceHandle The handle of the audio device to be used for recognition, as declared by
194 * the audio subsystem.
195 * @param ioHandle A handle assigned by the framework, which will later be used to retrieve
196 * an audio stream associated with this recognition session.
197 * @param config A RecognitionConfig structure containing attributes of the recognition to
198 * perform.
199 * @throws ServiceSpecificException(RESOURCE_CONTENTION) if the model cannot be started due
200 * to resource constraints. This is typically a temporary condition and the client may
201 * retry after the onResourcesAvailable() global callback is invoked.
202 */
203 void startRecognition(in int modelHandle, in int deviceHandle,
204 in int ioHandle, in RecognitionConfig config);
205
206 /**
207 * Stop recognition on a given model.
208 * This call is idempotent, to avoid any race conditions.
209 *
210 * @param modelHandle The handle of the sound model to use for recognition
211 */
212 void stopRecognition(in int modelHandle);
213
214 /**
215 * Request a recognition event to be generated.
216 * The model must be in the started state and will remain started after the event is sent.
217 * The model state is returned asynchronously as a RecognitionEvent via the callback that was
218 * registered upon loading. That event must have a RecognitionStatus.FORCED status.
219 *
220 * @param modelHandle The handle of the sound model whose state is being
221 * queried.
222 */
223 void forceRecognitionEvent(in int modelHandle);
224
225 /**
226 * Get supported parameter attributes with respect to the provided model handle.
227 * Model parameters are used to query/control model-specific detection behavior during a
228 * detection session.
229 * Along with determining the valid range, this API is also used to determine if a given
230 * parameter ID is supported at all by the modelHandle for use with getParameter() and
231 * setParameter() APIs.
232 *
233 * @param modelHandle The sound model handle indicating which model to query.
234 * @param modelParam Parameter to set which will be validated against the ModelParameter type.
235 * @return This structure indicates supported attributes of the parameter for the given model
236 * handle. If the parameter is not supported, null is returned.
237 */
238 @nullable ModelParameterRange queryParameter(in int modelHandle, in ModelParameter modelParam);
239
240 /**
241 * Get a model specific parameter.
242 * If the value has not been set, a default value is returned. See ModelParameter for parameter
243 * default values.
244 * The caller must check if the handle supports the parameter via the queryParameter API prior
245 * to calling this method.
246 *
247 * @param modelHandle The sound model associated with given modelParam
248 * @param modelParam Parameter to set which will be validated against the ModelParameter type.
249 * Not putting ModelParameter type directly in the definition and validating internally
250 * allows for forward compatibility.
251 * @return Value set to the requested parameter.
252 */
253 int getParameter(in int modelHandle, in ModelParameter modelParam);
254
255 /**
256 * Set a model specific parameter with the given value.
257 * This parameter will keep its value for the duration the model is loaded regardless of
258 * starting and stopping recognition. Once the model is unloaded, the value will be lost.
259 * The caller must check if the handle supports the parameter via the queryParameter API prior
260 * to calling this method.
261 *
262 * @param modelHandle The sound model handle indicating which model to modify parameters
263 * @param modelParam Parameter to set which will be validated against the ModelParameter type.
264 * Not putting ModelParameter type directly in the definition and validating internally
265 * allows for forward compatibility.
266 * @param value The value to set for the given model parameter.
267 */
268 void setParameter(in int modelHandle, in ModelParameter modelParam, in int value);
269}