Car-eye 开源团队在做JT/T视频转发平台的时候需要配置视频服务器的参数。用到TinyXML2,感觉非常好用,能快速完成自己的配置项目。
主要功能:实现对服务器的IP,端口,音视频参数的配置。可以采用一层节点完成设计。配置完成后达到如下效果:
-
实现代码如下:
#ifndef __CONFIGURE_H__
#define __CONFIGURE_H__
#include
#include
#include"XML\tinyxml2.h"
using namespace std;
using namespace tinyxml2;
class Configure
{
public:
Configure();
virtual ~Configure();
string m_Server_IP;
int m_Server_Port;
int m_Listen_Port;
int m_Http_port;
int m_SendPort;
string m_protocol;
string m_app;
int m_audio_channels;
int m_audio_Rate;
private:
int insertXMLNode(const char* xmlPath, const char* node, const char* value);
int Configure::createXML(const char* xmlPath);
XMLElement* queryUserNodeByName(XMLElement* root, const string& userName);
public:
unsigned char queryConfigureByName(const char* xmlPath, const string& userName, char* result);
unsigned char deleteUserByName(const char* xmlPath, const string& userName);
void WriteDefConfig(const char* xmlPath);
void LoadConfig(const char* xmlPath);
};
#endif
#include "Configure.h"
/*
* Car eye 车辆管理平台: www.car-eye.cn
* Car eye 开源网址: https://github.com/Car-eye-team
*
* Copyright (c) 2018 All rights reserved.
*
*/
//function:create a xml file
//param:xmlPath:xml文件路径
//return:0,成功,非0,失败
int Configure::createXML(const char* xmlPath)
{
const char* declaration = "";
XMLDocument doc;
doc.Parse(declaration);//会覆盖xml所有内容
//添加申明可以使用如下两行
//XMLDeclaration* declaration=doc.NewDeclaration();
//doc.InsertFirstChild(declaration);
XMLElement* root = doc.NewElement("configures");
doc.InsertEndChild(root);
return doc.SaveFile(xmlPath);
}
int Configure:: insertXMLNode(const char* xmlPath, const char* node, const char* value)
{
XMLDocument doc;
int res = doc.LoadFile(xmlPath);
if (res != 0)
{
printf("File is not existed");
return res;
}
XMLElement* root = doc.RootElement();
XMLElement* userNode = doc.NewElement(node);
userNode->SetText(value);
root->InsertEndChild(userNode);
return doc.SaveFile(xmlPath);
}
XMLElement* Configure::queryUserNodeByName(XMLElement* root, const string& userName)
{
XMLElement* userNode = root->FirstChildElement();
while (userNode != NULL)
{
if (userNode->Name() == userName)
break;
userNode = userNode->NextSiblingElement();//下一个兄弟节点
}
return userNode;
}
unsigned char Configure::queryConfigureByName(const char* xmlPath, const string& userName, char* result)
{
XMLDocument doc;
if (doc.LoadFile(xmlPath) != 0)
{
cout << "load xml file failed" << endl;
return NULL;
}
XMLElement* root = doc.RootElement();
XMLElement* userNode = queryUserNodeByName(root, userName);
if (userNode != NULL) //searched successfully
{
strcpy(result, userNode->GetText());
return 1;
}
return 0;
}
//function:删除指定节点内容
//param:xmlPath:xml文件路径;userName:用户名称
//return:bool
unsigned char Configure::deleteUserByName(const char* xmlPath, const string& userName)
{
XMLDocument doc;
if (doc.LoadFile(xmlPath) != 0)
{
cout << "load xml file failed" << endl;
return false;
}
XMLElement* root = doc.RootElement();
//doc.DeleteNode(root);//删除xml所有节点
XMLElement* userNode1 = root->FirstChildElement("configures");
XMLElement* userNode = queryUserNodeByName(root, userName);
if (userNode != NULL)
{
userNode1->DeleteChild(userNode);
if (doc.SaveFile(xmlPath) == 0)
return 1;
}
return 0;
}
Configure::Configure()
{
}
Configure::~Configure()
{
}
string& trim(string &s)
{
if (s.empty())
{
return s;
}
s.erase(0, s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
return s;
}
void Configure::LoadConfig(const char* xmlPath)
{
XMLDocument doc;
char value[100];
if (doc.LoadFile(xmlPath) != 0)
{
createXML(xmlPath);
WriteDefConfig(xmlPath);
}
if (queryConfigureByName(xmlPath, "Server_ip", value))
{
m_Server_IP = value;
m_Server_IP = trim(m_Server_IP);
}
else
{
m_Server_IP = "www.car-eye.cn";
}
if (queryConfigureByName(xmlPath, "Server_Port", value))
{
m_Server_Port = atoi(value);
}
else
{
m_Server_Port = 10085;
}
if (queryConfigureByName(xmlPath, "Linsten_Port", value))
{
m_Listen_Port = atoi(value);
}
else
{
m_Listen_Port = 9100;
}
if (queryConfigureByName(xmlPath, "Http_Port", value))
{
m_Http_port = atoi(value);
}
else
{
m_Http_port = 8090;
}
if (queryConfigureByName(xmlPath, "Send_Port", value))
{
m_SendPort = atoi(value);
}
else
{
m_SendPort = 1008;
}
if (queryConfigureByName(xmlPath, "Protocol", value))
{
m_protocol = value;
m_protocol = trim(m_protocol);
}
else
{
m_protocol = "rtmp";
}
if (queryConfigureByName(xmlPath, "Application", value))
{
m_app = value;
m_app = trim(m_app);
}
else
{
m_app = "live";
}
if (queryConfigureByName(xmlPath, "Audio_channel", value))
{
m_audio_channels = atoi(value);
}
else
{
m_audio_channels = 1;
}
if (queryConfigureByName(xmlPath, "Audio_SampleRate", value))
{
m_audio_Rate = atoi(value);
}
else
{
m_audio_Rate = 8000;
}
}
void Configure::WriteDefConfig(const char* xmlPath)
{
insertXMLNode(xmlPath,"Server_ip","XXX.XXX.XXX.XXX");
insertXMLNode(xmlPath, "Server_Port", "XXXX");
insertXMLNode(xmlPath, "Linsten_Port", "9100");
insertXMLNode(xmlPath, "Http_Port", "8090");
insertXMLNode(xmlPath, "Send_Port", "1008");
insertXMLNode(xmlPath, "Protocol", "rtmp");
insertXMLNode(xmlPath, "Application", "live");
insertXMLNode(xmlPath, "Audio_channel", "1");
insertXMLNode(xmlPath, "Audio_SampleRate", "8000");
}
car-eye开源官方网址:www.car-eye.cn
car-eye 流媒体平台网址:www.liveoss.com
car-eye 技术官方邮箱: [email protected]
car-eye技术交流QQ群: 590411159
CopyRight© car-eye 开源团队 2018