使用配置文件打造可配置的视频服务器转发平台

Car-eye 开源团队在做JT/T视频转发平台的时候需要配置视频服务器的参数。用到TinyXML2,感觉非常好用,能快速完成自己的配置项目。

主要功能:实现对服务器的IP,端口,音视频参数的配置。可以采用一层节点完成设计。配置完成后达到如下效果:

  <?xml version="1.0" encoding="UTF-8" standalone="no" ?>

- <configures>

  <Server_ip>XXX.XXX.XXX.XXX</Server_ip>

  <Server_Port>10085</Server_Port>

  <Linsten_Port>9100</Linsten_Port>

  <Http_Port>8090</Http_Port>

  <Send_Port>1008</Send_Port>

  <Protocol>rtmp</Protocol>

  <Application>live</Application>

  <Audio_channel>1</Audio_channel>

  <Audio_SampleRate>8000</Audio_SampleRate>

  </configures>

实现代码如下:

#ifndef __CONFIGURE_H__
#define __CONFIGURE_H__
 
 
 
#include <string.h>
#include<iostream>
#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 = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>";
	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 技术官方邮箱: support@car-eye.cn    
car-eye技术交流QQ群: 590411159     

CopyRight©  car-eye 开源团队 2018

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值