aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/cubeb-jni.cpp
blob: 5580d4eaf7ceb838b86132a4c867f2a2298d960d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* clang-format off */
#include "cubeb-jni.h"
#include "cubeb-jni-instances.h"
#include <assert.h>
#include <atomic>
#include <initializer_list>
#include <jni.h>
/* clang-format on */

#define AUDIO_STREAM_TYPE_MUSIC 3

struct cubeb_jni {
  jobject s_audio_manager_obj = nullptr;
  jclass s_audio_manager_class = nullptr;
  jmethodID s_audio_manager_get_output_latency_id = nullptr;
  jmethodID s_audio_manager_set_mode_id = nullptr;
  struct cubeb_fx_def {
    jclass clazz = nullptr;
    jmethodID static_function_is_available = nullptr;
  } s_fxs[CUBEB_FX_SENTINEL__]{};
};

static constexpr const char *
cubeb_fx_name(cubeb_fx_type type)
{
  switch (type) {
  case CUBEB_FX_ACOUSTIC_ECHO_CANCELER:
    return "android/media/audiofx/AcousticEchoCanceler";
  case CUBEB_FX_AUTOMATIC_GAIN_CONTROL:
    return "android/media/audiofx/AutomaticGainControl";
  case CUBEB_FX_NOISE_SUPPRESSOR:
    return "android/media/audiofx/NoiseSuppressor";
  case CUBEB_FX_SENTINEL__:
    return nullptr;
  }
  return nullptr;
}

extern "C" cubeb_jni *
cubeb_jni_init()
{
  jobject ctx_obj = cubeb_jni_get_context_instance();
  JNIEnv * jni_env = cubeb_get_jni_env_for_thread();
  if (!jni_env || !ctx_obj) {
    return nullptr;
  }

  cubeb_jni * cubeb_jni_ptr = new cubeb_jni;
  assert(cubeb_jni_ptr);

  {
    // Find the audio manager object and make it global to call it from another
    // method
    jclass context_class = jni_env->FindClass("android/content/Context");
    jfieldID audio_service_field = jni_env->GetStaticFieldID(
        context_class, "AUDIO_SERVICE", "Ljava/lang/String;");
    jstring jstr = (jstring)jni_env->GetStaticObjectField(context_class,
                                                          audio_service_field);
    jmethodID get_system_service_id =
        jni_env->GetMethodID(context_class, "getSystemService",
                             "(Ljava/lang/String;)Ljava/lang/Object;");
    jobject audio_manager_obj =
        jni_env->CallObjectMethod(ctx_obj, get_system_service_id, jstr);
    cubeb_jni_ptr->s_audio_manager_obj =
        reinterpret_cast<jobject>(jni_env->NewGlobalRef(audio_manager_obj));
    jni_env->DeleteLocalRef(ctx_obj);
    jni_env->DeleteLocalRef(context_class);
    jni_env->DeleteLocalRef(jstr);
    jni_env->DeleteLocalRef(audio_manager_obj);
  }

  {
    // Make the audio manager class a global reference in order to preserve
    // method id
    jclass audio_manager_class =
        jni_env->FindClass("android/media/AudioManager");
    cubeb_jni_ptr->s_audio_manager_class =
        reinterpret_cast<jclass>(jni_env->NewGlobalRef(audio_manager_class));
    cubeb_jni_ptr->s_audio_manager_get_output_latency_id =
        jni_env->GetMethodID(audio_manager_class, "getOutputLatency", "(I)I");
    cubeb_jni_ptr->s_audio_manager_set_mode_id =
        jni_env->GetMethodID(audio_manager_class, "setMode", "(I)V");
    jni_env->DeleteLocalRef(audio_manager_class);
  }

  auto populate_fx_class = [&](cubeb_fx_type type) {
    jclass c = jni_env->FindClass(cubeb_fx_name(type));
    cubeb_jni::cubeb_fx_def & fx = cubeb_jni_ptr->s_fxs[type];
    fx.clazz = reinterpret_cast<jclass>(jni_env->NewGlobalRef(c));
    fx.static_function_is_available =
        jni_env->GetStaticMethodID(c, "isAvailable", "()Z");
    jni_env->DeleteLocalRef(c);
  };
  populate_fx_class(CUBEB_FX_ACOUSTIC_ECHO_CANCELER);
  populate_fx_class(CUBEB_FX_AUTOMATIC_GAIN_CONTROL);
  populate_fx_class(CUBEB_FX_NOISE_SUPPRESSOR);

  return cubeb_jni_ptr;
}

extern "C" bool
cubeb_fx_is_available(cubeb_jni * cubeb_jni_ptr, cubeb_fx_type type)
{
  JNIEnv * jni_env = cubeb_get_jni_env_for_thread();
  cubeb_jni::cubeb_fx_def & fx = cubeb_jni_ptr->s_fxs[type];
  return JNI_FALSE != jni_env->CallStaticBooleanMethod(
                          fx.clazz, fx.static_function_is_available);
}

extern "C" int
cubeb_audio_manager_get_output_latency(cubeb_jni * cubeb_jni_ptr)
{
  assert(cubeb_jni_ptr);
  JNIEnv * jni_env = cubeb_get_jni_env_for_thread();
  return jni_env->CallIntMethod(
      cubeb_jni_ptr->s_audio_manager_obj,
      cubeb_jni_ptr->s_audio_manager_get_output_latency_id,
      AUDIO_STREAM_TYPE_MUSIC); // param: AudioManager.STREAM_MUSIC
}

extern "C" void
cubeb_jni_destroy(cubeb_jni * cubeb_jni_ptr)
{
  assert(cubeb_jni_ptr);

  JNIEnv * jni_env = cubeb_get_jni_env_for_thread();
  assert(jni_env);

  jni_env->DeleteGlobalRef(cubeb_jni_ptr->s_audio_manager_obj);
  jni_env->DeleteGlobalRef(cubeb_jni_ptr->s_audio_manager_class);
  for (size_t i = 0; i < CUBEB_FX_SENTINEL__; ++i) {
    jni_env->DeleteGlobalRef(cubeb_jni_ptr->s_fxs[i].clazz);
  }

  delete cubeb_jni_ptr;
}