You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

199 lines
6.1 KiB

#include "protocolONVIF.h"
#define LOG( X ) m_pLog->Log( "ONVIFproto", this, X )
#define LOG_ERROR( X ) m_pLog->Log( "ONVIFproto", this, _LOG_ERROR, X )
protocolONVIF::protocolONVIF(){
m_oDataTransport = NULL;
m_pRequestMutex = NULL;
m_pStreamURI = NULL;
m_bProtoIsBusy = false;
};
protocolONVIF::protocolONVIF(basicTransport *SocketTransport) :protocolHTTP( NULL ){
m_pRequestMutex = new basicMutex();
m_pStreamURI = (CSTR)malloc(1024);
memset(m_pStreamURI, 0, 1024);
m_oDataTransport = SocketTransport;
m_bProtoIsBusy = false;
if (m_oDataTransport != NULL) {
m_oDataTransport->addRecipient((protocolParser *) this);
};
};
protocolONVIF::~protocolONVIF(){
if (m_pStreamURI) free(m_pStreamURI);
if (m_pRequestMutex) delete m_pRequestMutex;
};
//int protocolONVIF::packetChecker(unsigned char *Buffer, size_t BufferSize){
// return 0;
//};
//int protocolONVIF::packetExtractor(unsigned char *Buffer, size_t BufferSize){
// return BufferSize;
//};
bool protocolONVIF::packetHandler(protocolPackage *Package){
LOG("PACKET HANDLER");
if (Package == NULL) return true;
if (Package->m_nSize == 0){
LOG_ERROR("No data 1 PACKET HANDLER END");
if ( Package->m_pData != NULL ) free( Package->m_pData );
free( Package );
return true;
};
if (Package->m_pData == NULL){
LOG_ERROR("No data 2 PACKET HANDLER END");
free( Package );
return true;
};
if (Package->m_nCode != 200){
LOG_ERROR("Bad Code PACKET HANDLER END");
free( Package->m_pData );
free( Package );
return true;
};
try{
XMLNode nodeRoot = XMLNode::parseString((const char *)Package->m_pData, "root");
if (nodeRoot.isEmpty()){
char BUBU[128];
sprintf(BUBU, "The package[%d:%d] XML data is not a valid XML data. Cann't find root element.", strlen((CSTR)Package->m_pData), Package->m_nSize);
LOG_ERROR(BUBU);
} else {
if (strstr(nodeRoot.getName(), "Envelope") != NULL){
XMLNode nodeBody = nodeRoot.getChildNode("Body", 0);
XMLNode nodeType = nodeBody.getChildNode(0);
LOG(nodeType.getName());
if (strstr(nodeType.getName(), "GetStreamUriResponse")!= NULL){
XMLNode nodeMedia = nodeType.getChildNode("MediaUri", 0);
XMLNode nodeUri = nodeMedia.getChildNode("Uri", 0);
sprintf( m_pStreamURI,"%s", nodeUri.getText());
m_bProtoIsBusy = false;
} else if (strstr(nodeType.getName(), "GetProfilesResponse") != NULL){
for (int index = 0; index < nodeType.nChildNode(); index++){
XMLNode nodeProfile = nodeType.getChildNode("Profiles", index);
profileONVIF *pProfile;
pProfile = (profileONVIF*)malloc(sizeof(profileONVIF));
if (pProfile != NULL){
pProfile->pToken = (CSTR)malloc(strlen(nodeProfile.getAttribute("token")+1));
sprintf(pProfile->pToken, "%s", nodeProfile.getAttribute("token"));
XMLNode nodeName = nodeProfile.getChildNode("Name");
pProfile->pName = (CSTR)malloc(strlen(nodeName.getText() + 1));
sprintf(pProfile->pName, "%s", nodeName.getText());
XMLNode nodeEncoder = nodeProfile.getChildNode("VideoEncoderConfiguration");
XMLNode nodeNcode = nodeEncoder.getChildNode("Encoding");
pProfile->pNcode = (CSTR)malloc(strlen(nodeNcode.getText() + 1));
sprintf(pProfile->pNcode, "%s", nodeNcode.getText());
XMLNode nodeResolution = nodeEncoder.getChildNode("Resolution");
XMLNode nodeWidth = nodeResolution.getChildNode("Width");
XMLNode nodeHeight = nodeResolution.getChildNode("Height");
pProfile->nWidth = atoi(nodeWidth.getText());
pProfile->nHeight = atoi(nodeHeight.getText());
XMLNode nodeRate = nodeEncoder.getChildNode("RateControl");
pProfile->nBitrate = -1;
pProfile->nFramerate = -1;
m_vProfiles.push_back(pProfile);
m_bProtoIsBusy = false;
};
};
};
};
};
}
catch (exception e){
};
LOG("PACKET HANDLER FINISHED");
free( Package->m_pData );
free( Package );
return true;
};
bool protocolONVIF::GetProfiles(CSTR URL){
char Buffer[1024];
int size;
int nRequestTimeOut;
nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
while ( nRequestTimeOut--){
if (!m_bProtoIsBusy)break;
SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
};
if (nRequestTimeOut == 0) return false;
m_bProtoIsBusy = true;
if (!m_vProfiles.empty()){
for (vectorProfileOnvif::iterator index = m_vProfiles.begin(); index < m_vProfiles.end(); index++){
if (*index != NULL){
if ((*index)->pToken != NULL) free((*index)->pToken);
if ((*index)->pNcode != NULL) free((*index)->pNcode);
if ((*index)->pName != NULL) free((*index)->pName);
free(*index);
};
};
m_vProfiles.clear();
};
sprintf(Buffer, ONVIF_GetProfiles );
size = strlen(Buffer);
issuePost( URL, Buffer, size);
nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
while (nRequestTimeOut--){
if (!m_bProtoIsBusy)break;
SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
};
m_bProtoIsBusy = false;
if (nRequestTimeOut){
return true;
};
return false;
};
CSTR protocolONVIF::GetStreamURI(CSTR URL, int profileID ){
char Buffer[1024];
int size;
int nRequestTimeOut;
nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
while (nRequestTimeOut--){
//if (m_pRequestMutex->try_lock()) break;
if (!m_bProtoIsBusy)break;
SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
};
if (nRequestTimeOut == 0) return NULL;
m_bProtoIsBusy = true;
if (m_vProfiles.empty()){
m_bProtoIsBusy = false;
return NULL;
};
sprintf(Buffer, ONVIF_GetStreamURI, m_vProfiles.at(profileID)->pToken );
printf(Buffer);
size = strlen(Buffer);
issuePost(URL, Buffer, size);
m_nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
while (m_nRequestTimeOut--){
if (!m_bProtoIsBusy)break;
SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
};
m_bProtoIsBusy = false;
if (m_nRequestTimeOut){
return m_pStreamURI;
};
return NULL;
};
CSTR protocolONVIF::buildOneWayRequest(CSTR OPName, CSTR Prefix){
XMLNode tXMLNode;
tXMLNode = XMLNode::createXMLTopNode();
tXMLNode.addAttribute(OPName, Prefix);
return tXMLNode.createXMLString(0, NULL);
};
#undef LOG
#undef LOG_ERROR