• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.media.musicrecognition;
18 
19 import static java.util.Objects.requireNonNull;
20 
21 import android.annotation.CallbackExecutor;
22 import android.annotation.IntDef;
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.annotation.RequiresPermission;
26 import android.annotation.SuppressLint;
27 import android.annotation.SystemApi;
28 import android.annotation.SystemService;
29 import android.content.Context;
30 import android.media.MediaMetadata;
31 import android.os.Bundle;
32 import android.os.RemoteException;
33 
34 import java.lang.annotation.Retention;
35 import java.lang.annotation.RetentionPolicy;
36 import java.util.concurrent.Executor;
37 
38 /**
39  * System service that manages music recognition.
40  *
41  * @hide
42  */
43 @SystemApi
44 @SystemService(Context.MUSIC_RECOGNITION_SERVICE)
45 public class MusicRecognitionManager {
46 
47     /**
48      * Error code provided by RecognitionCallback#onRecognitionFailed()
49      *
50      * @hide
51      */
52     @Retention(RetentionPolicy.SOURCE)
53     @IntDef(prefix = {"RECOGNITION_FAILED_"},
54             value = {RECOGNITION_FAILED_UNKNOWN,
55                     RECOGNITION_FAILED_NOT_FOUND,
56                     RECOGNITION_FAILED_NO_CONNECTIVITY,
57                     RECOGNITION_FAILED_SERVICE_UNAVAILABLE,
58                     RECOGNITION_FAILED_SERVICE_KILLED,
59                     RECOGNITION_FAILED_TIMEOUT,
60                     RECOGNITION_FAILED_AUDIO_UNAVAILABLE})
61     public @interface RecognitionFailureCode {
62     }
63 
64     /** Catchall error code. */
65     public static final int RECOGNITION_FAILED_UNKNOWN = -1;
66     /** Recognition was performed but no result could be identified. */
67     public static final int RECOGNITION_FAILED_NOT_FOUND = 1;
68     /** Recognition failed because the server couldn't be reached. */
69     public static final int RECOGNITION_FAILED_NO_CONNECTIVITY = 2;
70     /**
71      * Recognition was not possible because the application which provides it is not available (for
72      * example, disabled).
73      */
74     public static final int RECOGNITION_FAILED_SERVICE_UNAVAILABLE = 3;
75     /** Recognition failed because the recognizer was killed. */
76     public static final int RECOGNITION_FAILED_SERVICE_KILLED = 5;
77     /** Recognition attempt timed out. */
78     public static final int RECOGNITION_FAILED_TIMEOUT = 6;
79     /** Recognition failed due to an issue with obtaining an audio stream. */
80     public static final int RECOGNITION_FAILED_AUDIO_UNAVAILABLE = 7;
81 
82     /** Callback interface for the caller of this api. */
83     public interface RecognitionCallback {
84         /**
85          * Should be invoked by receiving app with the result of the search.
86          *
87          * @param recognitionRequest original request that started the recognition
88          * @param result result of the search
89          * @param extras extra data to be supplied back to the caller. Note that all
90          *               executable parameters and file descriptors would be removed from the
91          *               supplied bundle
92          */
onRecognitionSucceeded(@onNull RecognitionRequest recognitionRequest, @NonNull MediaMetadata result, @SuppressLint("NullableCollection") @Nullable Bundle extras)93         void onRecognitionSucceeded(@NonNull RecognitionRequest recognitionRequest,
94                 @NonNull MediaMetadata result,
95                 @SuppressLint("NullableCollection")
96                 @Nullable Bundle extras);
97 
98         /**
99          * Invoked when the search is not successful (possibly but not necessarily due to error).
100          *
101          * @param recognitionRequest original request that started the recognition
102          * @param failureCode failure code describing reason for failure
103          */
onRecognitionFailed(@onNull RecognitionRequest recognitionRequest, @RecognitionFailureCode int failureCode)104         void onRecognitionFailed(@NonNull RecognitionRequest recognitionRequest,
105                 @RecognitionFailureCode int failureCode);
106 
107         /**
108          * Invoked by the system once the audio stream is closed either due to error, reaching the
109          * limit, or the remote service closing the stream.  Always called per
110          * #beingStreamingSearch() invocation.
111          */
onAudioStreamClosed()112         void onAudioStreamClosed();
113     }
114 
115     private final IMusicRecognitionManager mService;
116 
117     /** @hide */
MusicRecognitionManager(IMusicRecognitionManager service)118     public MusicRecognitionManager(IMusicRecognitionManager service) {
119         mService = service;
120     }
121 
122     /**
123      * Constructs an {@link android.media.AudioRecord} from the given parameters and streams the
124      * audio bytes to the designated cloud lookup service.  After the lookup is done, the given
125      * callback will be invoked by the system with the result or lack thereof.
126      *
127      * @param recognitionRequest audio parameters for the stream to search
128      * @param callbackExecutor where the callback is invoked
129      * @param callback invoked when the result is available
130      *
131      * @hide
132      */
133     @SystemApi
134     @RequiresPermission(android.Manifest.permission.MANAGE_MUSIC_RECOGNITION)
beginStreamingSearch( @onNull RecognitionRequest recognitionRequest, @NonNull @CallbackExecutor Executor callbackExecutor, @NonNull RecognitionCallback callback)135     public void beginStreamingSearch(
136             @NonNull RecognitionRequest recognitionRequest,
137             @NonNull @CallbackExecutor Executor callbackExecutor,
138             @NonNull RecognitionCallback callback) {
139         try {
140             mService.beginRecognition(
141                     requireNonNull(recognitionRequest),
142                     new MusicRecognitionCallbackWrapper(
143                             requireNonNull(recognitionRequest),
144                             requireNonNull(callback),
145                             requireNonNull(callbackExecutor)));
146         } catch (RemoteException e) {
147             throw e.rethrowFromSystemServer();
148         }
149     }
150 
151     private final class MusicRecognitionCallbackWrapper extends
152             IMusicRecognitionManagerCallback.Stub {
153 
154         @NonNull
155         private final RecognitionRequest mRecognitionRequest;
156         @NonNull
157         private final RecognitionCallback mCallback;
158         @NonNull
159         private final Executor mCallbackExecutor;
160 
MusicRecognitionCallbackWrapper( RecognitionRequest recognitionRequest, RecognitionCallback callback, Executor callbackExecutor)161         MusicRecognitionCallbackWrapper(
162                 RecognitionRequest recognitionRequest,
163                 RecognitionCallback callback,
164                 Executor callbackExecutor) {
165             mRecognitionRequest = recognitionRequest;
166             mCallback = callback;
167             mCallbackExecutor = callbackExecutor;
168         }
169 
170         @Override
onRecognitionSucceeded(MediaMetadata result, Bundle extras)171         public void onRecognitionSucceeded(MediaMetadata result, Bundle extras) {
172             mCallbackExecutor.execute(
173                     () -> mCallback.onRecognitionSucceeded(mRecognitionRequest, result, extras));
174         }
175 
176         @Override
onRecognitionFailed(@ecognitionFailureCode int failureCode)177         public void onRecognitionFailed(@RecognitionFailureCode int failureCode) {
178             mCallbackExecutor.execute(
179                     () -> mCallback.onRecognitionFailed(mRecognitionRequest, failureCode));
180         }
181 
182         @Override
onAudioStreamClosed()183         public void onAudioStreamClosed() {
184             mCallbackExecutor.execute(mCallback::onAudioStreamClosed);
185         }
186     }
187 }
188