linphone-desktop 项目源代码很庞大,文件的关系也比较多,新手一般很难切入做二次开发:
这里我们以一个简单的例子,如何给账号增加一个属性的步骤让我们一起看看他是怎么串起来的
:
比如我们准备给账号加一个自动后台接听属性开关(这个虽有全局开关,但毕竟linphone支持多账号)
这里就涉及到 account-params:
1、首先,在核心C++类中添加新参数。修改文件 `linphone-sdk/liblinphone/src/account/account-params.h`:
class AccountParams : public bellesip::HybridObject<LinphoneAccountParams, AccountParams> {
public:
// ... 现有代码 ...
bool getAutoAnswerEnabled() const;
void setAutoAnswerEnabled(bool enable);
private:
// ... 现有代码 ...
bool mAutoAnswerEnabled = false;
};
2. 在对应的.cpp文件中实现getter和setter方法。修改 `linphone-sdk/liblinphone/src/account/account-params.cpp`:
bool AccountParams::getAutoAnswerEnabled() const {
return mAutoAnswerEnabled;
}
void AccountParams::setAutoAnswerEnabled(bool enable) {
mAutoAnswerEnabled = enable;
}
还需要保存到文件与读取文件的配置,这里要用到 : linphone_config_get_bool/linphone_config_set_bool
同样在linphone-sdk/liblinphone/src/account/account-params.cpp文件中找到
AccountParams::AccountParams(LinphoneCore *lc, int index) : AccountParams(nullptr) {
... 读取文件配置相关内容
mAutoAnswer = !!linphone_config_get_bool(config, key, "auto_answer", mAutoAnswer);
...
//写入
void AccountParams::writeToConfigFile(LinphoneConfig *config, int index) {
...
linphone_config_set_int(config, key, "auto_answer", mAutoAnswer);
3、更新C语言封装。在 `linphone-sdk/liblinphone/include/linphone/api/c-account-params.h` 中添加:(特别要注册参考现在的修改,增加注解要注意格式,以及空格空行,否则 *.hh代码无法生成)
LINPHONE_PUBLIC bool_t linphone_account_params_get_auto_answer_enabled(const LinphoneAccountParams *params);
LINPHONE_PUBLIC void linphone_account_params_set_auto_answer_enabled(LinphoneAccountParams *params, bool_t enable);
4、在 `linphone-sdk/liblinphone/src/c-wrapper/api/c-account-params.cpp` 中实现这些函数:
(特别要注册参考现在的修改,增加注解要注意格式,以及空格空行,否则 *.hh代码无法生成)
bool_t linphone_account_params_get_auto_answer_enabled(const LinphoneAccountParams *params) {
return params->getAutoAnswerEnabled();
}
void linphone_account_params_set_auto_answer_enabled(LinphoneAccountParams *params, bool_t enable) {
params->setAutoAnswerEnabled(!!enable);
}
5、当以上代码都完成后,先编译一下也很重要(如果没有生成相应的hh文件,可手动生成,参考文章最后说明),编译后如果我们要配置它,由于它使用QML,需要在AccountSettingsModel中暴露这个属性。修改 `src/components/settings/AccountSettingsModel.hpp`:
class AccountSettingsModel : public QObject {
Q_OBJECT
// ... 现有代码 ...
Q_PROPERTY(bool autoAnswerEnabled READ getAutoAnswerEnabled WRITE setAutoAnswerEnabled NOTIFY autoAnswerEnabledChanged)
public:
// ... 现有代码 ...
bool getAutoAnswerEnabled() const;
void setAutoAnswerEnabled(bool enable);
signals:
void autoAnswerEnabledChanged(bool enabled);
private:
// ... 现有代码 ...
};
6、在 `src/components/settings/AccountSettingsModel.cpp` 中实现这些方法:
bool AccountSettingsModel::getAutoAnswerEnabled() const {
return mAccountParams->getAutoAnswerEnabled();
}
void AccountSettingsModel::setAutoAnswerEnabled(bool enable) {
if (enable != getAutoAnswerEnabled()) {
mAccountParams->setAutoAnswerEnabled(enable);
emit autoAnswerEnabledChanged(enable);
}
}
7、最后,更新UI需要修改QML文件,比如 `ui/views/App/Settings/SettingsAccount.qml`:
import QtQuick 2.7
import QtQuick.Layouts 1.3
import Common 1.0
import Linphone 1.0
ColumnLayout {
// ... 现有代码 ...
Switch {
text: qsTr("settingsAccountAutoAnswerEnabled")
checked: AccountSettingsModel.autoAnswerEnabled
onCheckedChanged: AccountSettingsModel.autoAnswerEnabled = checked
}
// ... 现有代码 ...
}
以上只是做一个属性配置,真正的自动接听的逻辑这里就不加了,代码已经有现成的全局的了
为什么要这么做呢,主要是linphone的部分源代码是用生成方式,以上的代码就通过:
linphone-sdk\liblinphone\wrappers\cpp\genwrapper.py
生成 *.hh 到build目录下,感兴趣的可以去研究一下python脚本.
手动生成命令:(保存bat)
set src=C:/linphone-build/linphone-desktop
set build=%src%/build
set out=%build%/OUTPUT
set py=%src%/linphone-sdk/liblinphone/wrappers/cpp/genwrapper.py
set xml=%build%/linphone-sdk/liblinphone/coreapi/help/doc/doxygen/xml
python %py% -o %out% %xml%
注意这里build目录为cmake生成的build目录,这里要根据你实际情况修改.