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.

269 lines
6.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. {
  6. m_oDataTransport = NULL;
  7. m_pRequestMutex = NULL;
  8. m_pStreamURI = NULL;
  9. m_bProtoIsBusy = false;
  10. }
  11. protocolONVIF::protocolONVIF(basicTransport * SocketTransport) :
  12. protocolHTTP(NULL)
  13. {
  14. m_pRequestMutex = new basicMutex();
  15. m_pStreamURI = (CSTR) malloc(1024);
  16. memset(m_pStreamURI, 0, 1024);
  17. m_oDataTransport = SocketTransport;
  18. m_bProtoIsBusy = false;
  19. if (m_oDataTransport != NULL)
  20. {
  21. m_oDataTransport->addRecipient((protocolParser *) this);
  22. };
  23. }
  24. protocolONVIF::~protocolONVIF()
  25. {
  26. if (m_pStreamURI)
  27. {
  28. free(m_pStreamURI);
  29. }
  30. if (m_pRequestMutex)
  31. {
  32. delete m_pRequestMutex;
  33. }
  34. }
  35. // int protocolONVIF::packetChecker(unsigned char *Buffer, size_t BufferSize){
  36. // return 0;
  37. // };
  38. // int protocolONVIF::packetExtractor(unsigned char *Buffer, size_t BufferSize){
  39. // return BufferSize;
  40. // };
  41. bool protocolONVIF::packetHandler(protocolPackage * Package)
  42. {
  43. LOG("PACKET HANDLER");
  44. if (Package == NULL)
  45. {
  46. return true;
  47. }
  48. if (Package->m_nSize == 0)
  49. {
  50. LOG_ERROR("No data 1 PACKET HANDLER END");
  51. if (Package->m_pData != NULL)
  52. {
  53. free(Package->m_pData);
  54. }
  55. free(Package);
  56. return true;
  57. };
  58. if (Package->m_pData == NULL)
  59. {
  60. LOG_ERROR("No data 2 PACKET HANDLER END");
  61. free(Package);
  62. return true;
  63. };
  64. if (Package->m_nCode != 200)
  65. {
  66. LOG_ERROR("Bad Code PACKET HANDLER END");
  67. free(Package->m_pData);
  68. free(Package);
  69. return true;
  70. };
  71. try
  72. {
  73. XMLNode nodeRoot = XMLNode::parseString((const char *) Package->m_pData, "root");
  74. if (nodeRoot.isEmpty())
  75. {
  76. char BUBU[128];
  77. sprintf(BUBU, "The package[%ld:%ld] XML data is not a valid XML data. Cann't find root element.", strlen((CSTR) Package->m_pData), Package->m_nSize);
  78. LOG_ERROR(BUBU);
  79. }
  80. else
  81. {
  82. if (strstr(nodeRoot.getName(), "Envelope") != NULL)
  83. {
  84. XMLNode nodeBody = nodeRoot.getChildNode("Body", 0);
  85. XMLNode nodeType = nodeBody.getChildNode(0);
  86. LOG(nodeType.getName());
  87. if (strstr(nodeType.getName(), "GetStreamUriResponse") != NULL)
  88. {
  89. XMLNode nodeMedia = nodeType.getChildNode("MediaUri", 0);
  90. XMLNode nodeUri = nodeMedia.getChildNode("Uri", 0);
  91. sprintf(m_pStreamURI, "%s", nodeUri.getText());
  92. m_bProtoIsBusy = false;
  93. }
  94. else if (strstr(nodeType.getName(), "GetProfilesResponse") != NULL)
  95. {
  96. for (int index = 0; index < nodeType.nChildNode(); index++)
  97. {
  98. XMLNode nodeProfile = nodeType.getChildNode("Profiles", index);
  99. profileONVIF * pProfile;
  100. pProfile = (profileONVIF *) malloc(sizeof(profileONVIF));
  101. if (pProfile != NULL)
  102. {
  103. pProfile->pToken = (CSTR) malloc(strlen(nodeProfile.getAttribute("token") + 1));
  104. sprintf(pProfile->pToken, "%s", nodeProfile.getAttribute("token"));
  105. XMLNode nodeName = nodeProfile.getChildNode("Name");
  106. pProfile->pName = (CSTR) malloc(strlen(nodeName.getText() + 1));
  107. sprintf(pProfile->pName, "%s", nodeName.getText());
  108. XMLNode nodeEncoder = nodeProfile.getChildNode("VideoEncoderConfiguration");
  109. XMLNode nodeNcode = nodeEncoder.getChildNode("Encoding");
  110. pProfile->pNcode = (CSTR) malloc(strlen(nodeNcode.getText() + 1));
  111. sprintf(pProfile->pNcode, "%s", nodeNcode.getText());
  112. XMLNode nodeResolution = nodeEncoder.getChildNode("Resolution");
  113. XMLNode nodeWidth = nodeResolution.getChildNode("Width");
  114. XMLNode nodeHeight = nodeResolution.getChildNode("Height");
  115. pProfile->nWidth = atoi(nodeWidth.getText());
  116. pProfile->nHeight = atoi(nodeHeight.getText());
  117. XMLNode nodeRate = nodeEncoder.getChildNode("RateControl");
  118. pProfile->nBitrate = -1;
  119. pProfile->nFramerate = -1;
  120. m_vProfiles.push_back(pProfile);
  121. m_bProtoIsBusy = false;
  122. };
  123. };
  124. };
  125. };
  126. };
  127. }
  128. catch (const std::exception& e)
  129. {
  130. };
  131. LOG("PACKET HANDLER FINISHED");
  132. free(Package->m_pData);
  133. free(Package);
  134. return true;
  135. }
  136. bool protocolONVIF::GetProfiles(CSTR URL)
  137. {
  138. char Buffer[1024];
  139. int size;
  140. int nRequestTimeOut;
  141. nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
  142. while (nRequestTimeOut--)
  143. {
  144. if (! m_bProtoIsBusy)
  145. {
  146. break;
  147. }
  148. SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
  149. };
  150. if (nRequestTimeOut == 0)
  151. {
  152. return false;
  153. }
  154. m_bProtoIsBusy = true;
  155. if (! m_vProfiles.empty())
  156. {
  157. for (vectorProfileOnvif::iterator index = m_vProfiles.begin(); index < m_vProfiles.end(); index++)
  158. {
  159. if (*index != NULL)
  160. {
  161. if ((*index)->pToken != NULL)
  162. {
  163. free((*index)->pToken);
  164. }
  165. if ((*index)->pNcode != NULL)
  166. {
  167. free((*index)->pNcode);
  168. }
  169. if ((*index)->pName != NULL)
  170. {
  171. free((*index)->pName);
  172. }
  173. free(*index);
  174. };
  175. };
  176. m_vProfiles.clear();
  177. };
  178. sprintf(Buffer, ONVIF_GetProfiles);
  179. size = strlen(Buffer);
  180. issuePost(URL, Buffer, size);
  181. nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
  182. while (nRequestTimeOut--)
  183. {
  184. if (! m_bProtoIsBusy)
  185. {
  186. break;
  187. }
  188. SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
  189. };
  190. m_bProtoIsBusy = false;
  191. if (nRequestTimeOut)
  192. {
  193. return true;
  194. };
  195. return false;
  196. }
  197. CSTR protocolONVIF::GetStreamURI(CSTR URL, int profileID)
  198. {
  199. char Buffer[1024];
  200. int size;
  201. int nRequestTimeOut;
  202. nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
  203. while (nRequestTimeOut--)
  204. {
  205. // if (m_pRequestMutex->try_lock()) break;
  206. if (! m_bProtoIsBusy)
  207. {
  208. break;
  209. }
  210. SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
  211. };
  212. if (nRequestTimeOut == 0)
  213. {
  214. return NULL;
  215. }
  216. m_bProtoIsBusy = true;
  217. if (m_vProfiles.empty())
  218. {
  219. m_bProtoIsBusy = false;
  220. return NULL;
  221. };
  222. sprintf(Buffer, ONVIF_GetStreamURI, m_vProfiles.at(profileID)->pToken);
  223. printf(Buffer);
  224. size = strlen(Buffer);
  225. issuePost(URL, Buffer, size);
  226. m_nRequestTimeOut = ONVIF_REQUEST_TIMEOUT_COUNT;
  227. while (m_nRequestTimeOut--)
  228. {
  229. if (! m_bProtoIsBusy)
  230. {
  231. break;
  232. }
  233. SLEEP(ONVIF_REQUEST_TIMEOUT_TIME);
  234. };
  235. m_bProtoIsBusy = false;
  236. if (m_nRequestTimeOut)
  237. {
  238. return m_pStreamURI;
  239. };
  240. return NULL;
  241. }
  242. CSTR protocolONVIF::buildOneWayRequest(CSTR OPName, CSTR Prefix)
  243. {
  244. XMLNode tXMLNode;
  245. tXMLNode = XMLNode::createXMLTopNode();
  246. tXMLNode.addAttribute(OPName, Prefix);
  247. return tXMLNode.createXMLString(0, NULL);
  248. }
  249. #undef LOG
  250. #undef LOG_ERROR