• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

修改linphonesdkandroid第四篇

android 搞java代码 3年前 (2022-05-21) 22次浏览 已收录 0个评论
文章目录[隐藏]

背景

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

<code class="java">// 敞开Ring
mCore.setRing(<a href="https://www.gaodaima.com/tag/null" title="查看更多关于null的文章" target="_blank">null</a>);
mCore.setRingback(null);
mCore.setRemoteRingbackTone(null);
mCore.setNativeRingingEnabled(<a href="https://www.gaodaima.com/tag/false" title="查看更多关于false的文章" target="_blank">false</a>);
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等关键词

<code class="logcat">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日志输入吻合

<code class="c++">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办法

<code class="c++">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办法中也减少此判断逻辑

<code class="c++">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层配置

<code class="c++">bool_t linphone_core_tone_indications_enabled(LinphoneCore*lc){
    return !!linphone_config_get_int(lc->config,"sound","tone_indications",1);
}

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

<code class="c++">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的值即可

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

happy~


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:修改linphonesdkandroid第四篇

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址