关于PJSIP的onIncomingCall函数异常的测试方法咨询
测试pjsip-pjsua2-cs中
onIncomingCall函数的有效方案 问题描述
在代码开发中遇到问题,onIncomingCall函数未按预期工作,求提供有效测试该函数是否正常运行的指导。
环境信息
- 编程语言:C#
- 框架/库:pjsip-pjsua2-cs
- 版本号:2.10.1
现有代码
Main函数
static async Task Main() { try { #region Initialize PJSIP Endpoint ep = new Endpoint(); ep.libCreate(); EpConfig epConfig = new EpConfig(); ep.libInit(epConfig); TransportConfig transportConfig = new TransportConfig(); transportConfig.port = 5060; // Create a TCP transport with your SIP server settings TransportConfig tcfg = new TransportConfig(); tcfg.port = 5060; ep.transportCreate(pjsip_transport_type_e.PJSIP_TRANSPORT_UDP, tcfg); ep.transportCreate(pjsip_transport_type_e.PJSIP_TRANSPORT_TCP, tcfg); #endregion #region Configure a SIP account // Load your SIP settings from configuration SipSettings config = LoadSipSettingsFromConfiguration(); // Create and configure a SIP account AccountConfig accCfg = CreateSipAccountConfig(config); Account acc = new Account(); acc.create(accCfg); #endregion Console.WriteLine("SIP account created. Press Enter to exit."); Console.ReadLine(); ep.Dispose(); accCfg.Dispose(); } catch (Exception ex) { Console.WriteLine("Error: " + ex.Message); } }
onIncomingCall函数
#region onIncomingCall function public override void onIncomingCall(OnIncomingCallParam prm) { Console.WriteLine("Incoming call received."); // Handle the incoming call here, e.g., answer the call MyCall call = new MyCall(this, prm.callId); CallOpParam callOpParam = new CallOpParam(true); call.answer(callOpParam); // Now, integrate Direct Line bot conversation logic here HandleBotConversation(call); } #endregion
测试与排查步骤
1. 强化日志输出,确认事件触发
扩展onIncomingCall的日志,输出关键参数信息,明确来电事件是否被触发:
public override void onIncomingCall(OnIncomingCallParam prm) { Console.WriteLine($"Incoming call received. Call ID: {prm.callId}, Remote URI: {prm.remoteUri}"); // 原有逻辑... }
同时在Account类的onRegState回调中添加日志,确认SIP账号注册成功,排除注册失败导致收不到来电的问题。
2. 模拟来电验证
- 使用SIP客户端工具(如Linphone、X-Lite)向你的SIP账号发起呼叫,观察控制台是否有日志输出。
- 检查本地防火墙和网络端口:确保5060端口(UDP/TCP)对外开放,避免端口被拦截导致来电请求无法到达。
3. 验证代码逻辑正确性
- 确认
MyCall类正确继承自pjsip的Call类,构造函数逻辑无误:public class MyCall : Call { public MyCall(Account acc, int callId) : base(acc, callId) {} // 按需重写其他Call回调方法 } - 检查
HandleBotConversation方法是否存在阻塞逻辑,建议改为异步处理避免阻塞来电流程:private async void HandleBotConversation(MyCall call) { await Task.Run(() => { // 你的机器人对话逻辑 }); }
4. 捕获异常,排查静默失败
在onIncomingCall中添加异常捕获,避免异常导致函数静默终止:
public override void onIncomingCall(OnIncomingCallParam prm) { try { Console.WriteLine($"Incoming call received. Call ID: {prm.callId}, Remote URI: {prm.remoteUri}"); MyCall call = new MyCall(this, prm.callId); CallOpParam callOpParam = new CallOpParam(true); call.answer(callOpParam); HandleBotConversation(call); } catch (Exception ex) { Console.WriteLine($"Error handling incoming call: {ex.Message}\n{ex.StackTrace}"); } }
5. 检查账号配置与端点初始化
打印SIP账号配置信息,验证服务器地址、用户名、密码等参数是否正确:
// 在账号创建后添加 Console.WriteLine($"SIP ID URI: {accCfg.idUri}, Registrar URI: {accCfg.regConfig.registrarUri}");
内容的提问来源于stack exchange,提问作者kirubalan
相关产品推荐
相关产品推荐

