1 /*
2  * Copyright (C) 2019 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 #include <sstream>
18 
19 #include "core_jni_helpers.h"
20 #include <media/AudioSystem.h>
21 
22 namespace android {
23 
24 namespace {
25 
26 #define PACKAGE "com/android/server/soundtrigger_middleware"
27 #define CLASSNAME PACKAGE "/AudioSessionProviderImpl"
28 #define SESSION_CLASSNAME PACKAGE "/SoundTriggerMiddlewareImpl$AudioSessionProvider$AudioSession"
29 
acquireAudioSession(JNIEnv * env,jobject clazz)30 jobject acquireAudioSession(
31         JNIEnv* env,
32         jobject clazz) {
33 
34     audio_session_t session;
35     audio_io_handle_t ioHandle;
36     audio_devices_t device;
37 
38     status_t status = AudioSystem::acquireSoundTriggerSession(&session,
39                                                               &ioHandle,
40                                                               &device);
41     if (status != 0) {
42         std::ostringstream message;
43         message
44                 << "AudioSystem::acquireSoundTriggerSession returned an error code: "
45                 << status;
46         env->ThrowNew(FindClassOrDie(env, "java/lang/RuntimeException"),
47                       message.str().c_str());
48         return nullptr;
49     }
50 
51     jclass cls = FindClassOrDie(env, SESSION_CLASSNAME);
52     jmethodID ctor = GetMethodIDOrDie(env, cls, "<init>", "(III)V");
53     return env->NewObject(cls,
54                           ctor,
55                           static_cast<int>(session),
56                           static_cast<int>(ioHandle),
57                           static_cast<int>(device));
58 }
59 
releaseAudioSession(JNIEnv * env,jobject clazz,jint handle)60 void releaseAudioSession(JNIEnv* env, jobject clazz, jint handle) {
61     status_t status =
62             AudioSystem::releaseSoundTriggerSession(static_cast<audio_session_t>(handle));
63 
64     if (status != 0) {
65         std::ostringstream message;
66         message
67                 << "AudioSystem::releaseAudioSystemSession returned an error code: "
68                 << status;
69         env->ThrowNew(FindClassOrDie(env, "java/lang/RuntimeException"),
70                       message.str().c_str());
71     }
72 }
73 
74 const JNINativeMethod g_methods[] = {
75         {"acquireSession", "()L" SESSION_CLASSNAME ";",
76          reinterpret_cast<void*>(acquireAudioSession)},
77         {"releaseSession", "(I)V",
78          reinterpret_cast<void*>(releaseAudioSession)},
79 };
80 
81 }  // namespace
82 
register_com_android_server_soundtrigger_middleware_AudioSessionProviderImpl(JNIEnv * env)83 int register_com_android_server_soundtrigger_middleware_AudioSessionProviderImpl(
84         JNIEnv* env) {
85     return RegisterMethodsOrDie(env,
86                                 CLASSNAME,
87                                 g_methods,
88                                 NELEM(g_methods));
89 }
90 
91 } // namespace android
92