修改linphone-sdk-android-第四篇

背景

在使用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 &session) {
    lInfo() << "[ToneManager] " << __func__;
    if (isAnotherSessionInState(session, State::Tone)) {
        doStopTone();
    }
}

void ToneManager::doStartRingtone(const std::shared_ptr &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 &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 &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 &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 &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 &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~

你可能感兴趣的