• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 
17 package android.service.voice;
18 
19 import static android.Manifest.permission.CAPTURE_AUDIO_HOTWORD;
20 import static android.Manifest.permission.RECORD_AUDIO;
21 
22 import android.annotation.NonNull;
23 import android.annotation.Nullable;
24 import android.annotation.RequiresPermission;
25 import android.annotation.SystemApi;
26 import android.media.AudioFormat;
27 import android.os.ParcelFileDescriptor;
28 import android.os.PersistableBundle;
29 import android.os.SharedMemory;
30 
31 /**
32  * Basic functionality for hotword detectors.
33  *
34  * @hide
35  */
36 @SystemApi
37 public interface HotwordDetector {
38 
39     /**
40      * Indicates that it is a non-trusted hotword detector.
41      *
42      * @hide
43      */
44     int DETECTOR_TYPE_NORMAL = 0;
45 
46     /**
47      * Indicates that it is a DSP trusted hotword detector.
48      *
49      * @hide
50      */
51     int DETECTOR_TYPE_TRUSTED_HOTWORD_DSP = 1;
52 
53     /**
54      * Indicates that it is a software trusted hotword detector.
55      *
56      * @hide
57      */
58     int DETECTOR_TYPE_TRUSTED_HOTWORD_SOFTWARE = 2;
59 
60     /**
61      * Starts hotword recognition.
62      * <p>
63      * On calling this, the system streams audio from the device microphone to this application's
64      * {@link HotwordDetectionService}. Audio is streamed until {@link #stopRecognition()} is
65      * called.
66      * <p>
67      * On detection of a hotword,
68      * {@link AlwaysOnHotwordDetector.Callback#onDetected(AlwaysOnHotwordDetector.EventPayload)}
69      * is called on the callback provided when creating this {@link HotwordDetector}.
70      * <p>
71      * There is a noticeable impact on battery while recognition is active, so make sure to call
72      * {@link #stopRecognition()} when detection isn't needed.
73      * <p>
74      * Calling this again while recognition is active does nothing.
75      *
76      * @return true if the request to start recognition succeeded
77      */
78     @RequiresPermission(allOf = {RECORD_AUDIO, CAPTURE_AUDIO_HOTWORD})
startRecognition()79     boolean startRecognition();
80 
81     /**
82      * Stops hotword recognition.
83      *
84      * @return true if the request to stop recognition succeeded
85      */
stopRecognition()86     boolean stopRecognition();
87 
88     /**
89      * Starts hotword recognition on audio coming from an external connected microphone.
90      * <p>
91      * {@link #stopRecognition()} must be called before {@code audioStream} is closed.
92      *
93      * @param audioStream stream containing the audio bytes to run detection on
94      * @param audioFormat format of the encoded audio
95      * @param options options supporting detection, such as configuration specific to the
96      *         source of the audio. This will be provided to the {@link HotwordDetectionService}.
97      *         PersistableBundle does not allow any remotable objects or other contents that can be
98      *         used to communicate with other processes.
99      * @return true if the request to start recognition succeeded
100      */
startRecognition( @onNull ParcelFileDescriptor audioStream, @NonNull AudioFormat audioFormat, @Nullable PersistableBundle options)101     boolean startRecognition(
102             @NonNull ParcelFileDescriptor audioStream,
103             @NonNull AudioFormat audioFormat,
104             @Nullable PersistableBundle options);
105 
106     /**
107      * Set configuration and pass read-only data to hotword detection service.
108      *
109      * @param options Application configuration data to provide to the
110      * {@link HotwordDetectionService}. PersistableBundle does not allow any remotable objects or
111      * other contents that can be used to communicate with other processes.
112      * @param sharedMemory The unrestricted data blob to provide to the
113      * {@link HotwordDetectionService}. Use this to provide the hotword models data or other
114      * such data to the trusted process.
115      *
116      * @throws IllegalStateException if this HotwordDetector wasn't specified to use a
117      * {@link HotwordDetectionService} when it was created.
118      */
updateState(@ullable PersistableBundle options, @Nullable SharedMemory sharedMemory)119     void updateState(@Nullable PersistableBundle options, @Nullable SharedMemory sharedMemory);
120 
121     /**
122      * Invalidates this hotword detector so that any future calls to this result
123      * in an {@link IllegalStateException}.
124      *
125      * <p>If there are no other {@link HotwordDetector} instances linked to the
126      * {@link HotwordDetectionService}, the service will be shutdown.
127      */
destroy()128     default void destroy() {
129         throw new UnsupportedOperationException("Not implemented. Must override in a subclass.");
130     }
131 
132     /**
133      * @hide
134      */
detectorTypeToString(int detectorType)135     static String detectorTypeToString(int detectorType) {
136         switch (detectorType) {
137             case DETECTOR_TYPE_NORMAL:
138                 return "normal";
139             case DETECTOR_TYPE_TRUSTED_HOTWORD_DSP:
140                 return "trusted_hotword_dsp";
141             case DETECTOR_TYPE_TRUSTED_HOTWORD_SOFTWARE:
142                 return "trusted_hotword_software";
143             default:
144                 return Integer.toString(detectorType);
145         }
146     }
147 
148     /**
149      * The callback to notify of detection events.
150      */
151     interface Callback {
152 
153         /**
154          * Called when the keyphrase is spoken.
155          *
156          * @param eventPayload Payload data for the detection event.
157          */
158         // TODO: Consider creating a new EventPayload that the AOHD one subclasses.
onDetected(@onNull AlwaysOnHotwordDetector.EventPayload eventPayload)159         void onDetected(@NonNull AlwaysOnHotwordDetector.EventPayload eventPayload);
160 
161         /**
162          * Called when the detection fails due to an error.
163          */
onError()164         void onError();
165 
166         /**
167          * Called when the recognition is paused temporarily for some reason.
168          * This is an informational callback, and the clients shouldn't be doing anything here
169          * except showing an indication on their UI if they have to.
170          */
onRecognitionPaused()171         void onRecognitionPaused();
172 
173         /**
174          * Called when the recognition is resumed after it was temporarily paused.
175          * This is an informational callback, and the clients shouldn't be doing anything here
176          * except showing an indication on their UI if they have to.
177          */
onRecognitionResumed()178         void onRecognitionResumed();
179 
180         /**
181          * Called when the {@link HotwordDetectionService second stage detection} did not detect the
182          * keyphrase.
183          *
184          * @param result Info about the second stage detection result, provided by the
185          *         {@link HotwordDetectionService}.
186          */
onRejected(@onNull HotwordRejectedResult result)187         void onRejected(@NonNull HotwordRejectedResult result);
188 
189         /**
190          * Called when the {@link HotwordDetectionService} is created by the system and given a
191          * short amount of time to report it's initialization state.
192          *
193          * @param status Info about initialization state of {@link HotwordDetectionService}; the
194          * allowed values are {@link HotwordDetectionService#INITIALIZATION_STATUS_SUCCESS},
195          * 1<->{@link HotwordDetectionService#getMaxCustomInitializationStatus()},
196          * {@link HotwordDetectionService#INITIALIZATION_STATUS_UNKNOWN}.
197          */
onHotwordDetectionServiceInitialized(int status)198         void onHotwordDetectionServiceInitialized(int status);
199 
200         /**
201          * Called with the {@link HotwordDetectionService} is restarted.
202          *
203          * Clients are expected to call {@link HotwordDetector#updateState} to share the state with
204          * the newly created service.
205          */
onHotwordDetectionServiceRestarted()206         void onHotwordDetectionServiceRestarted();
207     }
208 }
209