# linphone_napi **Repository Path**: tybrave/linphone_napi ## Basic Information - **Project Name**: linphone_napi - **Description**: No description available - **Primary Language**: Unknown - **License**: LGPL-2.1 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 6 - **Created**: 2025-07-26 - **Last Updated**: 2025-09-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ohoslinphoneDemo #### 介绍 基于linphone_sdk(https://gitlab.linphone.org/BC/public/linphone-sdk)适配鸿蒙底层音视频驱动,支持音视频通话 代码编译so移植地址:https://gitee.com/openharmony-tpc-incubate/linphone-sdk/tree/ohos_5.4.18_dev/ #### 下载安装 ##### 编译安装教程 1. 搭建linux编译环境,搭建sip服务器,配置鸿蒙SDK路径 ``` export OHOS_SDK=xxxxxxx/linux 搭建sip服务器 参考:https://gitcode.com/openharmony-tpc/openharmony_tpc_samples/tree/master/ohos_pjsip#%E6%90%AD%E5%BB%BAsip%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%A6%82asterisk%E6%9C%8D%E5%8A%A1%E5%99%A8 ``` 2. 运行根目录的编译脚本 ``` ./build.sh ``` 3. 在生成的目录build-ohos下out目录,里面的lib及其include,copy到ohos_linphone的module中 ##### 下载安装教程 ``` ohpm install ohos_linphone ``` #### 使用说明 1. 配置插件路径,并进行初始化,参考demo中EntryAbility的initLinphone函数: ``` Factory.instance().setLogCollectionPath(this.context.filesDir); Factory.instance().enableLogCollection(true); Factory.instance().setCacheDir(this.context.cacheDir) Factory.instance().setData(this.context.filesDir); let preferences = CorePreferences.getInstance() preferences.setContext(this.context); await preferences.copyAssetsFromPackage(); let config = Factory.instance().createConfigWithFactory(preferences.configPath(), preferences.factoryConfigPath()); preferences.config = config; Factory.instance().enableLogcatLogs(true); CoreContext.getInstance().setContext(this.context); CoreContext.getInstance().start(); LongTask.getInstance(this.context).startTask(); ``` 2、输入账号进行注册登录 ``` login(name: string, pwd: string, address: string) { CoreContext.getInstance().getCore().loadConfigFromXml(CorePreferences.getInstance().thirdPartyDefaultValuesPath()) let serverAddress = address + ";transport=udp"; let server_addr: Address = Factory.instance().createAddress(serverAddress); let port = server_addr?.getPort(); if (port == -1) { throw new Error("incorrect port number setting") } let identity = `sip:${name}@${server_addr.getDomain()}:${port}` let identityAddress: Address = Factory.instance().createAddress(identity); if (!identityAddress) { promptAction.showToast({ message: "无效的服务器地址", duration: 200 }) return; } let authinfo: AuthInfo = Factory.instance().createAuthInfo(name, "", pwd, "", "", server_addr.getDomain()); if (!authinfo) { promptAction.showToast({ message: "验证账户失败", duration: 200 }) return; } CoreContext.getInstance().getCore().addAuthInfo(authinfo); let accountParams = CoreContext.getInstance().getCore().createAccountParams() if (name) { identityAddress.setDisplayName(name); } let code = accountParams.setIdentityAddress(identityAddress); code = accountParams.setServerAddress(server_addr); let newlyCreatedAccount = CoreContext.getInstance().getCore().createAccount(accountParams); CoreContext.getInstance().getCore().addAccount(newlyCreatedAccount) } ``` 3、拨打电话 ``` startCall(add: Address, isVideo: boolean) { let params = this.getCore().createCallParams(null); if (!params) { return } if (isVideo) { params.setVideoEnabled(true); this.core?.setPreferredVideoDefinitionByName("720p") } let currentCall = this.getCore().getCurrentCall(); if (currentCall) { let code = currentCall.pause(); } let callParams = params ? params : this.getCore().createCallParams(null) if (callParams == null) { let call = this.getCore().inviteAddress(add) return } let fileName = "" if (CorePreferences.getInstance().callRecordingUseSmffFormat) { fileName = CorePreferences.getInstance().ctx?.cacheDir + "/audio/" + add.getUsername + "_" + new Date().getTime() + CorePreferences.RECORDING_SMFF_FILE_EXTENSION } else { fileName = CorePreferences.getInstance().ctx?.cacheDir + "/audio/" + add.getUsername + "_" + new Date().getTime() + CorePreferences.RECORDING_MKV_FILE_EXTENSION } callParams.setRecordFile(fileName); let usernmae = add.getUsername(); let domian = add.getDomain(); let defaultAccount = callParams.getAccount() ? callParams.getAccount() : this.getCore().getDefaultAccount(); if (!defaultAccount) { return; } this.getCore().inviteAddressWithParams(add, callParams) params.unref(); } ``` 4、接听电话 ``` answerCall(isVideo: boolean) { // 获取来电中的call if (!this.core) { return; } let call: Call | undefined = undefined; let array = this.core.getCalls(); for (let index = 0; index < array.length; index++) { const element = array[index]; let status = element.getState(); if (status == 1 || status == 2 || status == 17) { call = element; break; } } if (call) { this.currentCall = call; let remoteParams = this.core.createCallParams(call); if (remoteParams == null) { this.currentCall.accept(); return } let remoteVideo = remoteParams.isVideoEnabled(); if (remoteVideo) { remoteParams.setVideoEnabled(true); remoteParams.setVideoDirection(3); this.core.setPreferredVideoDefinitionByName("720p") call.acceptWithParams(remoteParams); } else { this.currentCall.accept(); } } } ``` 5、挂断电话 ``` terminateCall() { // 1: Incoming ,0:Outgoing if (this.currentCall && this.currentCall.getDir() == 1 && (this.currentCall.getState() == 1 || this.currentCall.getState() == 17)) { this.currentCall.decline(3) // Declined 3 } else { this.currentCall?.terminate(); } } ``` #### 目录结构 ``` ohos_linphone/ ├── libs # linphone编译的so └── src └── main # 实现源码 ├── ets ├── Factory.ets #全局唯一工厂对象 ├── cpp # 主要代码实现 ├── account # 账号相关 ├── account_params # 账号参数相关 ├── address # 用户地址的封装 ├── audio # 音频 ├── authInfo # 账号验证 ├── call # 电话call ├── call_listener # call的一些监听 ├── call_params # call参数模块 ├── common # 公共工具类 ├── config # config ├── core # linphone core的封装 ├—— core_listener # linphone core的listener封装 ├—— factory # linphone工厂封装 ├—— search # 搜索 ├—— thirdparty # 依赖so的头文件 ├—— tool # 公共的tool ├—— types # 暴露ets层的接口 ├—— video # video ├—— CMakeLists.txt # napi工程编译文件 ├—— napi_init.cpp # napi入口文件 ``` #### 约束与限制 在下述版本验证通过: DevEco Studio: 5.0.4 Release, SDK: API16 Release(5.0.4.150) #### 贡献代码 使用过程中发现任何问题都可以提 [issue](https://gitee.com/openharmony-tpc-incubate/linphone_napi/issues) ,当然,也非常欢迎发 [PR](https://gitee.com/openharmony-tpc-incubate/linphone_napi/pulls) 共建。 #### 开源协议 本项目基于 [Apache License 2.0](https://gitee.com/openharmony-tpc-incubate/linphone_napi/blob/master/LICENSE) ,请自由的享受和参与开源。 #### 遗留问题 暂无