乐趣区

关于android:修改linphonesdkandroid第四篇

背景

在应用 linphone-sdk-android 过程中,发现当有一起呼叫在通话中时,又收到一起呼叫,会莫名其妙的播报振铃声音,问题是曾经调用 linphone-sdk-android 提供的接口敞开了振铃声音

// 敞开 Ring
mCore.setRing(null);
mCore.setRingback(null);
mCore.setRemoteRingbackTone(null);
mCore.setNativeRingingEnabled(false);
mCore.setRingDuringIncomingEarlyMedia(false);
mCore.setVibrationOnIncomingCallEnabled(false);

// 敞开 CallErrorTone
Reason[] reasons = Reason.values();
for (Reason reason : reasons) {mCore.setCallErrorTone(reason, null);
}

// 敞开 ToneId
ToneID[] toneIds = ToneID.values();
for (ToneID toneId : toneIds) {mCore.setTone(toneId, "");
}

剖析

查看 Logcat 输入的日志,剖析发现有 ToneManagerdoStartRingtone 等关键词

13056-13056/com.guodong.android.linphone I/liblinphone: [ToneManager] [0x934e70ac] state changed : [None, LinphoneCallIncomingReceived]
13056-13056/com.guodong.android.linphone I/liblinphone: [ToneManager] add new session [0x934e70ac]
13056-13056/com.guodong.android.linphone I/liblinphone: [ToneManager] doStopToneToPlaySomethingElse
13056-13056/com.guodong.android.linphone I/liblinphone: [ToneManager] doStartRingtone
13056-13056/com.guodong.android.linphone I/liblinphone: [ToneManager] doStartNamedTone [2]

关上 IDE 去源码中搜寻一番,发现 tone-manager.cpp,在其中找到doStopToneToPlaySomethingElsedoStartRingtonedoStartNamedTone 办法,与 Logcat 日志输入吻合

void ToneManager::doStopToneToPlaySomethingElse(const std::shared_ptr<CallSession> &session) {lInfo() << "[ToneManager]" << __func__;
    if (isAnotherSessionInState(session, State::Tone)) {doStopTone();
    }
}

void ToneManager::doStartRingtone(const std::shared_ptr<CallSession> &session) {lInfo() << "[ToneManager]" << __func__;
    LinphoneCore *lc = getCore()->getCCore();
    // 如果有一个正在通话的呼叫就调用 `doStartNamedTone`
    if (isAnotherSessionInState(session, State::Call)) {
        /* play a tone within the context of the current call */
        doStartNamedTone(session, LinphoneToneCallWaiting);
    } else {
        MSSndCard *ringcard = lc->sound_conf.lsd_card ? lc->sound_conf.lsd_card : lc->sound_conf.ring_sndcard;
        if (ringcard && !linphone_core_is_native_ringing_enabled(lc)) {if (!linphone_core_callkit_enabled(lc)){ms_snd_card_set_stream_type(ringcard, MS_SND_CARD_STREAM_RING);
                linphone_ringtoneplayer_start(lc->factory, lc->ringtoneplayer, ringcard, lc->sound_conf.local_ring, 2000);
            }else{ms_message("Callkit is enabled, not playing ringtone.");
            }
        }
    }
}

void ToneManager::doStartNamedTone(const std::shared_ptr<CallSession> &session, LinphoneToneID toneId) {lInfo() << "[ToneManager]" << __func__ << "[" << Utils::toString(toneId) << "]";
    LinphoneToneDescription *tone = getToneFromId(toneId);

    // 在 Java 中已将 audiofile 置为 "",所以会走 else 分支
    if (tone && tone->audiofile) {playFile(tone->audiofile);
    } else {
        // 此处生成振铃声音
        MSDtmfGenCustomTone dtmfTone = generateToneFromId(toneId);
        playTone(session, dtmfTone);
    }
}

好的,当初先找到调用 doStopToneToPlaySomethingElse 的办法,在 IDE 中查找,发现 startRingtone 办法

void ToneManager::printDebugInfo(const std::shared_ptr<CallSession> &session) {auto callState = session->getState();
    auto toneState = getState(session);
    lInfo() << "[ToneManager] [" << session << "] state changed : [" << stateToString(toneState)  << "," << Utils::toString(callState) << "]";
}

void ToneManager::startRingtone(const std::shared_ptr<CallSession> &session) {printDebugInfo(session); // 对应 Logcat 第一行输入日志
    setState(session, State::Ringtone); // 对应 Logcat 第二行输入日志
    // 如果另外一个呼叫不在 Ringtone 且不在 Ringback 状态
    if (!isAnotherSessionInState(session, State::Ringtone) && !isAnotherSessionInState(session, State::Ringback)) {doStopToneToPlaySomethingElse(session);
        doStartRingtone(session);
        mStats->number_of_startRingtone++;
    }
}

查看 ToneManager::startRingtone 上面的 startErrorTone 办法,发现此办法是调用 linphone_core_tone_indications_enabled 判断是否能够播报 Tone,猜测能够在 startRingtone 办法中也减少此判断逻辑

void ToneManager::startErrorTone(const std::shared_ptr<CallSession> &session, LinphoneReason reason) {LinphoneCore *lc = getCore()->getCCore();
    // 此处判断是否能够播报 Tone
    if (linphone_core_tone_indications_enabled(lc)) {printDebugInfo(session);
        doStopToneToPlaySomethingElse(session);
        doStartErrorTone(session, reason);
        mStats->number_of_startErrorTone++;
    }
}

不过须要先理解下 linphone_core_tone_indications_enabled 办法的实现,此办法位于 msic.c 中,办法外部从 linphone_config 中读取 soundsection 下tone_indicationskey 的值转换为bool_t 类型,大于 0 为 Ture,小于等于 0 为 Flase,其中 linphone_config 能够从 Java 层配置

bool_t linphone_core_tone_indications_enabled(LinphoneCore*lc){return !!linphone_config_get_int(lc->config,"sound","tone_indications",1);
}

好的,问题剖析结束,能够在 startRingtone 办法中也减少此判断逻辑了

void ToneManager::startRingtone(const std::shared_ptr<CallSession> &session) {printDebugInfo(session);
    setState(session, State::Ringtone);
    // modified by guodongAndroid on 2022/04/24 批改有正在通话的呼叫时,又收到一起呼叫播报 Ringtone 问题
    // 减少 tone_indications 是否开启判断
    LinphoneCore *lc = getCore()->getCCore();
    if (!linphone_core_tone_indications_enabled(lc)) {return;}
    
    if (!isAnotherSessionInState(session, State::Ringtone) && !isAnotherSessionInState(session, State::Ringback)) {doStopToneToPlaySomethingElse(session);
        doStartRingtone(session);
        mStats->number_of_startRingtone++;
    }
}

保留从新编译,期待编译实现拷贝至 AS 中,当初只需在 Java 层初始化时配置 Config,批改 tone_indications 的值即可

// modified by guodongAndroid on 2022/04/24 批改有正在通话的呼叫时,又收到一起呼叫播报 Ringtone 问题
// 敞开 ToneIndications
mCore.getConfig().setInt("sound", "tone_indications", 0);

happy~

退出移动版