Commit 7db54bff authored by Andrea Gianarda's avatar Andrea Gianarda
Browse files

Get up to date device ID from AudioManager as it should be changed when the...

Get up to date device ID from AudioManager as it should be changed when the user decides to switch from bluetooth to earpiece/speaker
parent 4d186e77
...@@ -26,6 +26,8 @@ ...@@ -26,6 +26,8 @@
static const int flowControlIntervalMs = 5000; static const int flowControlIntervalMs = 5000;
static const int flowControlThresholdMs = 40; static const int flowControlThresholdMs = 40;
static void aaudio_player_callback_error(AAudioStream *stream, void *userData, aaudio_result_t error);
struct AAudioOutputContext { struct AAudioOutputContext {
AAudioOutputContext(MSFilter *f) { AAudioOutputContext(MSFilter *f) {
mFilter = f; mFilter = f;
...@@ -52,7 +54,46 @@ struct AAudioOutputContext { ...@@ -52,7 +54,46 @@ struct AAudioOutputContext {
ms_flow_controlled_bufferizer_set_flow_control_interval_ms(&buffer, flowControlIntervalMs); ms_flow_controlled_bufferizer_set_flow_control_interval_ms(&buffer, flowControlIntervalMs);
} }
void updateDeviceIdFromMsSndCard() { jobject getJVAudioManager(JNIEnv *env, std::string mgrJVCName) {
jclass linphoneManagerClass = env->FindClass("org/linphone/LinphoneManager");
jobject jPlayerObj = NULL;
if (linphoneManagerClass != NULL) {
std::string jVSig;
jVSig = "()L" + mgrJVCName + ";";
// Get AndroidAudioManager instance in Linphone Manager
jmethodID getAudioManagerId = env->GetStaticMethodID(linphoneManagerClass, "getAudioManager", jVSig.c_str());
if (getAudioManagerId != NULL) {
jPlayerObj = env->CallStaticObjectMethod(linphoneManagerClass, getAudioManagerId);
}
env->DeleteLocalRef(linphoneManagerClass);
}
return jPlayerObj;
}
void setDefaultDeviceId(std::string streamTypeStr) {
// env is an object in C++
JNIEnv *env = ms_get_jni_env();
std::string mgrJVCName ("org/linphone/call/AndroidAudioManager");
jobject jPlayerObj = getJVAudioManager(env, mgrJVCName);
jclass androidAudioManagerClass = env->FindClass(mgrJVCName.c_str());
if ((jPlayerObj != NULL) && (androidAudioManagerClass != NULL)) {
jmethodID getDefaultDeviceID = env->GetMethodID(androidAudioManagerClass, "getDefaultPlayerDeviceId", "(Ljava/lang/String;)V");
if (getDefaultDeviceID != NULL) {
// Convert C++ strings to jstrign in order to pass them to the JAVA code
jstring jStreamType = env->NewStringUTF(streamTypeStr.c_str());
env->CallVoidMethod(jPlayerObj, getDefaultDeviceID, jStreamType);
setDeviceId();
}
env->DeleteLocalRef(androidAudioManagerClass);
env->DeleteLocalRef(jPlayerObj);
}
}
void setDefaultDeviceIdFromMsSndCard() {
MSSndCardStreamType type = ms_snd_card_get_stream_type(soundCard); MSSndCardStreamType type = ms_snd_card_get_stream_type(soundCard);
std::string streamTypeStr (""); std::string streamTypeStr ("");
...@@ -77,40 +118,25 @@ struct AAudioOutputContext { ...@@ -77,40 +118,25 @@ struct AAudioOutputContext {
// If known conversion to stream type // If known conversion to stream type
if (!(streamTypeStr.empty())) { if (!(streamTypeStr.empty())) {
// env is an object in C++ setDefaultDeviceId(streamTypeStr);
JNIEnv *env = ms_get_jni_env(); }
}
std::string mgrJVCName ("org/linphone/call/AndroidAudioManager");
jclass linphoneManagerClass = env->FindClass("org/linphone/LinphoneManager");
jobject jPlayerObj = NULL;
if (linphoneManagerClass != NULL) {
std::string jVSig;
jVSig = "()L" + mgrJVCName + ";";
// Get AndroidAudioManager instance in Linphone Manager
jmethodID getAudioManagerId = env->GetStaticMethodID(linphoneManagerClass, "getAudioManager", jVSig.c_str());
if (getAudioManagerId != NULL) {
jPlayerObj = env->CallStaticObjectMethod(linphoneManagerClass, getAudioManagerId);
}
}
jclass androidAudioManagerClass = env->FindClass(mgrJVCName.c_str()); void setDeviceId() {
if ((jPlayerObj != NULL) && (androidAudioManagerClass != NULL)) { JNIEnv *env = ms_get_jni_env();
jmethodID getDefaultDeviceID = env->GetMethodID(androidAudioManagerClass, "getDefaultPlayerDeviceId", "(Ljava/lang/String;)I"); std::string mgrJVCName ("org/linphone/call/AndroidAudioManager");
if (getDefaultDeviceID != NULL) { jclass androidAudioManagerClass = env->FindClass(mgrJVCName.c_str());
// Convert C++ strings to jstrign in order to pass them to the JAVA code jobject jPlayerObj = getJVAudioManager(env, mgrJVCName);
jstring jStreamType = env->NewStringUTF(streamTypeStr.c_str()); if ((jPlayerObj != NULL) && (androidAudioManagerClass != NULL)) {
jint id = env->CallIntMethod(jPlayerObj, getDefaultDeviceID, jStreamType); jmethodID getDeviceIdID = env->GetMethodID(androidAudioManagerClass, "getDeviceId", "()I");
// id is -1 if an error occurred or no device was found if (getDeviceIdID != NULL) {
// In such scenario, do not change the device ID jint id = env->CallIntMethod(jPlayerObj, getDeviceIdID);
if (id != -1) deviceId = (int)id; // id is -1 if an error occurred or no device was found
} // In such scenario, do not change the device ID
env->DeleteLocalRef(androidAudioManagerClass); if (id != -1) deviceId = id;
env->DeleteLocalRef(jPlayerObj);
env->DeleteLocalRef(linphoneManagerClass);
} }
env->DeleteLocalRef(androidAudioManagerClass);
env->DeleteLocalRef(jPlayerObj);
} }
} }
...@@ -215,8 +241,6 @@ static aaudio_data_callback_result_t aaudio_player_callback(AAudioStream *stream ...@@ -215,8 +241,6 @@ static aaudio_data_callback_result_t aaudio_player_callback(AAudioStream *stream
return AAUDIO_CALLBACK_RESULT_CONTINUE; return AAUDIO_CALLBACK_RESULT_CONTINUE;
} }
static void aaudio_player_callback_error(AAudioStream *stream, void *userData, aaudio_result_t error);
static void aaudio_player_init(AAudioOutputContext *octx) { static void aaudio_player_init(AAudioOutputContext *octx) {
AAudioStreamBuilder *builder; AAudioStreamBuilder *builder;
aaudio_result_t result = AAudio_createStreamBuilder(&builder); aaudio_result_t result = AAudio_createStreamBuilder(&builder);
...@@ -225,7 +249,12 @@ static void aaudio_player_init(AAudioOutputContext *octx) { ...@@ -225,7 +249,12 @@ static void aaudio_player_init(AAudioOutputContext *octx) {
} }
octx->updateStreamTypeFromMsSndCard(); octx->updateStreamTypeFromMsSndCard();
octx->updateDeviceIdFromMsSndCard();
if (octx->deviceId == AAUDIO_UNSPECIFIED) {
octx->setDefaultDeviceIdFromMsSndCard();
}
// Update device ID in C++ class
octx->setDeviceId();
AAudioStreamBuilder_setDeviceId(builder, octx->deviceId); AAudioStreamBuilder_setDeviceId(builder, octx->deviceId);
AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_OUTPUT); AAudioStreamBuilder_setDirection(builder, AAUDIO_DIRECTION_OUTPUT);
AAudioStreamBuilder_setSampleRate(builder, octx->aaudio_context->samplerate); AAudioStreamBuilder_setSampleRate(builder, octx->aaudio_context->samplerate);
...@@ -325,7 +354,7 @@ static void android_snd_write_process(MSFilter *obj) { ...@@ -325,7 +354,7 @@ static void android_snd_write_process(MSFilter *obj) {
static void android_snd_write_postprocess(MSFilter *obj) { static void android_snd_write_postprocess(MSFilter *obj) {
AAudioOutputContext *octx = (AAudioOutputContext*)obj->data; AAudioOutputContext *octx = (AAudioOutputContext*)obj->data;
aaudio_player_close(octx); aaudio_player_close(octx);
} }
static MSFilterMethod android_snd_write_methods[] = { static MSFilterMethod android_snd_write_methods[] = {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment