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.

198 lines
6.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolONVIF.h"
  2. #define LOG( X ) m_pLog->Log( "ONVIFproto", this, X )
  3. #define LOG_ERROR( X ) m_pLog->Log( "ONVIFproto", this, _LOG_ERROR, X )
  4. protocolONVIF::protocolONVIF(){
  5. m_oDataTransport = NULL;
  6. m_pRequestMutex = NULL;
  7. m_pStreamURI = NULL;
  8. m_bProtoIsBusy = false;
  9. };
  10. protocolONVIF::protocolONVIF(basicTransport *SocketTransport) :protocolHTTP( NULL ){
  11. m_pRequestMutex = new basicMutex();
  12. m_pStreamURI = (CSTR)malloc(1024);
  13. memset(m_pStreamURI, 0, 1024);
  14. m_oDataTransport = SocketTransport;
  15. m_bProtoIsBusy = false;
  16. if (m_oDataTransport != NULL) {
  17. m_oDataTransport->addRecipient((protocolParser *) this);
  18. };
  19. };
  20. protocolONVIF::~protocolONVIF(){
  21. if (m_pStreamURI) free(m_pStreamURI);
  22. if (m_pRequestMutex) delete m_pRequestMutex;
  23. };
  24. //int protocolONVIF::packetChecker(unsigned char *Buffer, size_t BufferSize){
  25. // return 0;
  26. //};
  27. //int protocolONVIF::packetExtractor(unsigned char *Buffer, size_t BufferSize){
  28. // return BufferSize;
  29. //};
  30. bool protocolONVIF::packetHandler(protocolPackage *Package){
  31. LOG("PACKET HANDLER");
  32. if (Package == NULL) return true;
  33. if (Package->m_nSize == 0){
  34. LOG_ERROR("No data 1 PACKET HANDLER END");
  35. if ( Package->m_pData != NULL ) free( Package->m_pData );
  36. free( Package );
  37. return true;
  38. };
  39. if (Package->m_pData == NULL){
  40. LOG_ERROR("No data 2 PACKET HANDLER END");
  41. free( Package );
  42. return true;
  43. };
  44. if (Package->m_nCode != 200){
  45. LOG_ERROR("Bad Code PACKET HANDLER END");
  46. free( Package->m_pData );
  47. free( Package );
  48. return true;
  49. };
  50. try{
  51. XMLNode nodeRoot = XMLNode::parseString((const char *)Package->m_pData, "root");
  52. if (nodeRoot.isEmpty()){
  53. char BUBU[128];
  54. 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);
  55. LOG_ERROR(BUBU);
  56. } else {
  57. if (strstr(nodeRoot.getName(), "Envelope") != NULL){
  58. XMLNode nodeBody = nodeRoot.getChildNode("Body", 0);
  59. XMLNode nodeType = nodeBody.getChildNode(0);
  60. LOG(nodeType.getName());
  61. if (strstr(nodeType.getName(), "GetStreamUriResponse")!= NULL){
  62. XMLNode nodeMedia = nodeType.getChildNode("MediaUri", 0);
  63. XMLNode nodeUri = nodeMedia.getChildNode("Uri", 0);
  64. sprintf( m_pStreamURI,"%s", nodeUri.getText());
  65. m_bProtoIsBusy = false;
  66. } else if (strstr(nodeType.getName(), "GetProfilesResponse") != NULL){
  67. for (int index = 0; index < nodeType.nChildNode(); index++){
  68. XMLNode nodeProfile = nodeType.getChildNode("Profiles", index);
  69. profileONVIF *pProfile;
  70. pProfile = (profileONVIF*)malloc(sizeof(profileONVIF));
  71. if (pProfile != NULL){
  72. pProfile->pToken = (CSTR)malloc(strlen(nodeProfile.getAttribute("token")+1));
  73. sprintf(pProfile->pToken, "%s", nodeProfile.getAttribute("token"));
  74. XMLNode nodeName = nodeProfile.getChildNode("Name");
  75. pProfile->pName = (CSTR)malloc(strlen(nodeName.getText() + 1));
  76. sprintf(pProfile->pName, "%s", nodeName.getText());
  77. XMLNode nodeEncoder = nodeProfile.getChildNode("VideoEncoderConfiguration");
  78. XMLNode nodeNcode = nodeEncoder.getChildNode("Encoding");
  79. pProfile->pNcode = (CSTR)malloc(strlen(nodeNcode.getText() + 1));
  80. sprintf(pProfile->pNcode, "%s", nodeNcode.getText());
  81. XMLNode nodeResolution = nodeEncoder.getChildNode("Resolution");
  82. XMLNode nodeWidth = nodeResolution.getChildNode("Width");
  83. XMLNode nodeHeight = nodeResolution.getChildNode("Height");
  84. pProfile->nWidth = atoi(nodeWidth.getText());
  85. pProfile->nHeight = atoi(nodeHeight.getText());
  86. XMLNode nodeRate = nodeEncoder.getChildNode("RateControl");
  87. pProfile->nBitrate = -1;
  88. pProfile->nFramerate = -1;
  89. m_vProfiles.push_back(pProfile);
  90. m_bProtoIsBusy = false;
  91. };
  92. };
  93. };
  94. };
  95. };
  96. }
  97. catch (exception e){
  98. };
  99. LOG("PACKET HANDLER FINISHED");
  100. free( Package->m_pData );
  101. free( Package );
  102. return true;
  103. };
  104. bool protocolONVIF::GetProfiles(CSTR URL){
  105. char Buffer[1024];
  106. int size;
  107. int nRequestTimeOut;
  108. nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
  109. while ( nRequestTimeOut--){
  110. if (!m_bProtoIsBusy)break;
  111. SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
  112. };
  113. if (nRequestTimeOut == 0) return false;
  114. m_bProtoIsBusy = true;
  115. if (!m_vProfiles.empty()){
  116. for (vectorProfileOnvif::iterator index = m_vProfiles.begin(); index < m_vProfiles.end(); index++){
  117. if (*index != NULL){
  118. if ((*index)->pToken != NULL) free((*index)->pToken);
  119. if ((*index)->pNcode != NULL) free((*index)->pNcode);
  120. if ((*index)->pName != NULL) free((*index)->pName);
  121. free(*index);
  122. };
  123. };
  124. m_vProfiles.clear();
  125. };
  126. sprintf(Buffer, ONVIF_GetProfiles );
  127. size = strlen(Buffer);
  128. issuePost( URL, Buffer, size);
  129. nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
  130. while (nRequestTimeOut--){
  131. if (!m_bProtoIsBusy)break;
  132. SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
  133. };
  134. m_bProtoIsBusy = false;
  135. if (nRequestTimeOut){
  136. return true;
  137. };
  138. return false;
  139. };
  140. CSTR protocolONVIF::GetStreamURI(CSTR URL, int profileID ){
  141. char Buffer[1024];
  142. int size;
  143. int nRequestTimeOut;
  144. nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
  145. while (nRequestTimeOut--){
  146. //if (m_pRequestMutex->try_lock()) break;
  147. if (!m_bProtoIsBusy)break;
  148. SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
  149. };
  150. if (nRequestTimeOut == 0) return NULL;
  151. m_bProtoIsBusy = true;
  152. if (m_vProfiles.empty()){
  153. m_bProtoIsBusy = false;
  154. return NULL;
  155. };
  156. sprintf(Buffer, ONVIF_GetStreamURI, m_vProfiles.at(profileID)->pToken );
  157. printf(Buffer);
  158. size = strlen(Buffer);
  159. issuePost(URL, Buffer, size);
  160. m_nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
  161. while (m_nRequestTimeOut--){
  162. if (!m_bProtoIsBusy)break;
  163. SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
  164. };
  165. m_bProtoIsBusy = false;
  166. if (m_nRequestTimeOut){
  167. return m_pStreamURI;
  168. };
  169. return NULL;
  170. };
  171. CSTR protocolONVIF::buildOneWayRequest(CSTR OPName, CSTR Prefix){
  172. XMLNode tXMLNode;
  173. tXMLNode = XMLNode::createXMLTopNode();
  174. tXMLNode.addAttribute(OPName, Prefix);
  175. return tXMLNode.createXMLString(0, NULL);
  176. };
  177. #undef LOG
  178. #undef LOG_ERROR