简易实现linphone的SIP电话功能

​​​​​​最近公司由于发展需要,希望能通过SIP通话的方式来替代传统的电话模式,先简单介绍一个SIP。SIP(Session Initiation Protocol,会话初始协议)是由IETF(Internet Engineering Task Force,因特网工程任务组)制定的多媒体通信协议。它是一个基于文本的应用层控制协议,用于创建、修改和释放一个或多个参与者的会话。SIP 是一种源于互联网的IP 语音会话控制协议,具有灵活、易于实现、便于扩展等特点。

由于没有这方面的经验,在领导的推荐下,决定开始集成linphone到我们的项目里。开始也是万般艰辛,各种百度各种官网查资料,由于本地环境问题,官网下载的demo一直运行不了,然后在项目了引用也一直下载不成功,最后是通过maven仓库直接找到对应的linphoneSDK下载引用附上地址,再结合官网的demo成功的。先附上demo的截图:

公司是有现成的服务器的,所以我准备直接登陆可以实现了。

demo的主要流程就是注册

/**
     * 注册
     */
    private void configureAccount() {
        // At least the 3 below values are required
        mAccountCreator.setUsername(mUsername.getText().toString());
        mAccountCreator.setDomain(mDomain.getText().toString());
        mAccountCreator.setPassword(mPassword.getText().toString());

        // By default it will be UDP if not set, but TLS is strongly recommended
        switch (mTransport.getCheckedRadioButtonId()) {
            case R.id.transport_udp:
                mAccountCreator.setTransport(TransportType.Udp);
                break;
            case R.id.transport_tcp:
                mAccountCreator.setTransport(TransportType.Tcp);
                break;
            case R.id.transport_tls:
                mAccountCreator.setTransport(TransportType.Tls);
                break;
        }

        // This will automatically create the proxy config and auth info and add them to the Core
        ProxyConfig cfg = mAccountCreator.createProxyConfig();
        // Make sure the newly created one is the default
        //注册到服务器
        LinphoneService.getCore().setDefaultProxyConfig(cfg);
    }

根据回调的信息查看结果

        mCoreListener = new CoreListenerStub() {
            /**
             * 监听注册是否成功
             * @param core
             * @param cfg
             * @param state
             * @param message
             */
            @Override
            public void onRegistrationStateChanged(Core core, ProxyConfig cfg, RegistrationState state, String message) {
                if (state == RegistrationState.Ok) {
                    finish();
                } else if (state == RegistrationState.Failed) {
                    Toast.makeText(ConfigureAccountActivity.this, "Failure: " + message, Toast.LENGTH_LONG).show();
                }
            }
        };

拨打电话

      /**
         * 拨打电话
         */
        callButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Core core = LinphoneService.getCore();
                Address addressToCall = core.interpretUrl(mSipAddressToCall.getText().toString());
                CallParams params = core.createCallParams(null);

                Switch videoEnabled = findViewById(R.id.call_with_video);
                params.enableVideo(videoEnabled.isChecked());

                if (addressToCall != null) {
                    core.inviteAddressWithParams(addressToCall, params);
                }
            }
        });

多的不说附上demo,大家自行下载把~

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
要基于Linphone实现SIP电话通信,可以按照以下步骤: 1. 安装Linphone 在Linux系统中,可以使用包管理器安装Linphone。例如,在Ubuntu中,可以使用以下命令安装: ```shell sudo apt-get install linphone ``` 在Windows系统中,可以从Linphone的官方网站下载安装程序进行安装。 2. 创建SIP账户 要使用Linphone进行SIP电话通信,需要先创建一个SIP账户。可以使用Linphone自带的账户配置向导来创建账户,也可以手动创建。 3. 编写Python脚本 可以使用Python的Linphone模块来控制Linphone进行电话通信。以下是一个简单的Python脚本,可以实现拨打电话和接听电话功能。 ```python import linphone # 创建Linphone Core对象 lc = linphone.Factory.get().create_core() # 初始化Linphone lc.init(None, None) # 创建SIP账户对象 auth_info = linphone.AuthInfo.new( "sip_username", None, "sip_password", None, None, "sip_domain" ) lc.add_auth_info(auth_info) # 创建SIP代理对象 proxy_cfg = lc.create_proxy_config() proxy_cfg.identity_address = lc.create_address("sip:sip_username@sip_domain") proxy_cfg.server_addr = "sip:sip_domain" lc.add_proxy_config(proxy_cfg) lc.default_proxy_config = proxy_cfg # 监听电话事件 def on_call_state_changed(core, call, state, message): print("Call state:", state.name) lc.callback_call_state_changed = on_call_state_changed # 拨打电话 call_params = lc.create_call_params(None) call = lc.invite_address("sip:someone@sip_domain", call_params) # 接听电话 while True: lc.iterate() ``` 在这个脚本中,首先创建了一个Linphone Core对象,然后初始化Linphone。接着,创建了一个SIP账户对象和一个SIP代理对象,并将它们添加到Linphone中。然后,定义了一个回调函数来处理电话状态的变化,最后使用Linphone拨打电话和接听电话。 4. 运行Python脚本 在终端中运行Python脚本,即可开始使用Linphone进行SIP电话通信。 ```shell python3 sip_phone.py ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值