caudiotransmission.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "caudiotransmission.h"
  2. #include "..\\..\\Other\\libaudiotransqueue\\libaudiotransqueue.h"
  3. Caudiotransmission::Caudiotransmission(asr_server_info_t* pAsrServerInfo)
  4. {
  5. LOG_FUNCTION();
  6. if (0 == InitAudioTransmission()){
  7. m_hEventStop= ::CreateEventA(NULL, TRUE, FALSE, 0);
  8. if (NULL == m_hEventStop) {
  9. Dbg("create m_hEventStop failed!");
  10. }
  11. if (NULL != pAsrServerInfo){
  12. sprintf_s(m_strMainServerURL, MAX_PATH, "%s", pAsrServerInfo->strMainServerURL);
  13. sprintf_s(m_strBackupServerURL, MAX_PATH, "%s", pAsrServerInfo->strBackupServerURL);
  14. if (NULL != pAsrServerInfo->fCreateObj){
  15. m_fCreateObj = pAsrServerInfo->fCreateObj;
  16. m_pIAudioTransObj = m_fCreateObj();
  17. if (NULL != m_pIAudioTransObj){
  18. Dbg("create audio transmission object success!");
  19. }
  20. }
  21. m_fDestoryObj = pAsrServerInfo->fDestoryObj;
  22. m_iTimeOut = pAsrServerInfo->iTimeOut;
  23. m_iMaxAudioSize = pAsrServerInfo->iMaxAudioSize;
  24. Dbg("init cpp restful success, connect timeout time is %d, max audio send size is %d.",m_iTimeOut, m_iMaxAudioSize);
  25. }
  26. }
  27. }
  28. int Caudiotransmission::InitAudioTransmission()
  29. {
  30. LOG_FUNCTION();
  31. int iRet = -1;
  32. memset(m_AudioQueueName, 0, MAX_PATH);
  33. memset(m_strMainServerURL, 0, MAX_PATH);
  34. memset(m_strBackupServerURL, 0, MAX_PATH);
  35. m_pIAudioTransObj = NULL;
  36. m_iTimeOut = DEFAULT_CONNECT_TIMEOUT;
  37. m_iMaxAudioSize = DEFAULT_MAX_AUDIO_SEND_SIZE;
  38. m_fCreateObj = NULL;
  39. m_fDestoryObj = NULL;
  40. m_salestransaudioqueue = NULL;
  41. m_hEventStop = NULL;
  42. m_hAudioTransThread = NULL;
  43. m_nAudioTransThreadId = 0;
  44. iRet = 0;
  45. return iRet;
  46. }
  47. void Caudiotransmission::HandleAudioTransException()
  48. {
  49. LOG_FUNCTION();
  50. Dbg("音频流传输失败.");
  51. LogEvent(Severity_High, LOG_EVT_AUDIOSTREAM_TRANSMISSION_FAILED, "音频流传输失败.");
  52. if (NULL != m_salestransaudioqueue) {
  53. if (m_salestransaudioqueue->ClearQueue()) {
  54. Dbg("clear audio transmission queue success.");
  55. }
  56. Dbg("delete sales trans audio queue, and it's addr is %0x.", m_salestransaudioqueue);
  57. delete m_salestransaudioqueue;
  58. m_salestransaudioqueue = NULL;
  59. }
  60. }
  61. eAudioFrameFlag Caudiotransmission::TansferAudioBlockFlag(eAudioFrameFlag eFlag)
  62. {
  63. eAudioFrameFlag eAudioFrameType = eAudioFrameComplete;
  64. if (eAudioFrameComplete != eFlag) {
  65. if (eAudioFrameInit == eFlag) {
  66. eAudioFrameType = eAudioFrameFirst;
  67. }
  68. else {
  69. eAudioFrameType = eAudioFrameCountinue;
  70. }
  71. }
  72. return eAudioFrameType;
  73. }
  74. int Caudiotransmission::AudioTransmissionThreadFunc()
  75. {
  76. LOG_FUNCTION();
  77. int iRet = -1;
  78. int iAudioWriteFailedCount = 0;
  79. bool bStopRecord = false;
  80. int iSendNum = 0;
  81. int iLastCopySeriNum = 0;
  82. audio_frame* audioframe = new audio_frame;
  83. memset(audioframe, 0, sizeof(audio_frame));
  84. eAudioFrameFlag eAudioFrameType = eAudioFrameInit;
  85. unsigned char audiodata[SINGLE_AUDIO_FRAME_SIZE] = {0};
  86. int iCopyBufferLen = 0;
  87. unsigned char* pSendBuffer = new unsigned char[m_iMaxAudioSize];
  88. memset(pSendBuffer, 0, m_iMaxAudioSize);
  89. while(true){
  90. DWORD dwRet = WaitForSingleObject(m_hEventStop, AUDIO_TRANS_INTERVAL_TIME);
  91. if (WAIT_OBJECT_0 == dwRet) {
  92. Dbg("exit circle for set event true.");
  93. bStopRecord = true;
  94. }
  95. if (NULL != m_salestransaudioqueue) {
  96. int nAudioLens = m_salestransaudioqueue->Count();
  97. Dbg("current sales trans audio queue length is %d.", nAudioLens);
  98. if (nAudioLens > 0){
  99. audioframe->data = (char*)audiodata;
  100. if (true == m_salestransaudioqueue->TryDequeue(audioframe)){
  101. if (false == m_salestransaudioqueue->DeleteQueueHead()){
  102. Dbg("sales trans audio queue delete head failed.");
  103. }
  104. if (iLastCopySeriNum == audioframe->iseriesnumber){
  105. Dbg("audio series number is %d has been succeed memcpy.", audioframe->iseriesnumber);
  106. continue;
  107. }
  108. memcpy((unsigned char*)pSendBuffer+iCopyBufferLen, audiodata, audioframe->framesize);
  109. iCopyBufferLen += SINGLE_AUDIO_FRAME_SIZE;
  110. iLastCopySeriNum = audioframe->iseriesnumber;
  111. Dbg("current memcpy audio series number is %d.",iLastCopySeriNum);
  112. }
  113. else{
  114. Dbg("sales trans audio queue length is not zero,try get audio from queue failed, continue.");
  115. continue;
  116. }
  117. }else{
  118. if (true != bStopRecord){
  119. Dbg("sales trans audio queue length is 0,and not stop record, continue.");
  120. Sleep(200);
  121. continue;
  122. }
  123. else{
  124. Dbg("bStopRecord is true and audio length is zero set audio frame eAudioFrameComplete,last buffer len is not %d.",m_iMaxAudioSize);
  125. eAudioFrameType = eAudioFrameComplete;
  126. if (0 == iCopyBufferLen){
  127. memcpy((unsigned char*)pSendBuffer, audiodata, SINGLE_AUDIO_FRAME_SIZE);
  128. iCopyBufferLen += SINGLE_AUDIO_FRAME_SIZE;
  129. }
  130. }
  131. }
  132. if (iCopyBufferLen < m_iMaxAudioSize){
  133. if (eAudioFrameComplete!= eAudioFrameType){
  134. continue;
  135. }
  136. }
  137. else{
  138. if (bStopRecord && 0 == m_salestransaudioqueue->Count()){
  139. Dbg("bStopRecord is true and audio length is zero set audio frame eAudioFrameComplete, last buffer len is %d.", m_iMaxAudioSize);
  140. eAudioFrameType = eAudioFrameComplete;
  141. }
  142. }
  143. if (NULL != m_pIAudioTransObj){
  144. eAudioFrameType = TansferAudioBlockFlag(eAudioFrameType);
  145. if (0 == m_pIAudioTransObj->SessionAudioWrite(pSendBuffer, iCopyBufferLen, eAudioFrameType, ++iSendNum)){
  146. Dbg("success write audio number is %d, send buffer len is %d, and audio frame type is %d.", iSendNum, iCopyBufferLen, eAudioFrameType);
  147. if (0 != iAudioWriteFailedCount){
  148. iAudioWriteFailedCount = 0;
  149. }
  150. }
  151. else{
  152. Dbg("audio write failed, audio block series number is %d, current block len is %d, and audio frame type is %d.", iSendNum, iCopyBufferLen, eAudioFrameType);
  153. iAudioWriteFailedCount++;
  154. if (iAudioWriteFailedCount > MAX_TRANSFAILED_COUNT){
  155. HandleAudioTransException();
  156. Dbg("more than %d time audio write failed, break.", MAX_TRANSFAILED_COUNT);
  157. break;
  158. }
  159. }
  160. }
  161. memset(pSendBuffer, 0, m_iMaxAudioSize);
  162. iCopyBufferLen = 0;
  163. }
  164. else{
  165. eAudioFrameType = eAudioFrameComplete;
  166. }
  167. if (eAudioFrameComplete == eAudioFrameType){
  168. Dbg("break");
  169. break;
  170. }
  171. }
  172. delete audioframe;
  173. audioframe = NULL;
  174. if (NULL != pSendBuffer){
  175. delete []pSendBuffer;
  176. pSendBuffer = NULL;
  177. }
  178. return iRet;
  179. }
  180. //音频包传输到AI识别服务器线程
  181. static unsigned int __stdcall AudioTransThread(LPVOID pPram)
  182. {
  183. LOG_FUNCTION();
  184. Caudiotransmission* pAudioTrans = (Caudiotransmission*)pPram;
  185. int iRet = -1;
  186. //__try
  187. {
  188. iRet = pAudioTrans->AudioTransmissionThreadFunc();
  189. }
  190. //__except(wmvrecorddump_exception(GetExceptionInformation(),WmvRecord), EXCEPTION_EXECUTE_HANDLER) {
  191. //}
  192. return iRet;
  193. }
  194. int Caudiotransmission::StartAudioTransmission(const char* strAudioQueue, void* pBlob, size_t uLen)
  195. {
  196. LOG_FUNCTION();
  197. int iRet = -1;
  198. if (NULL != strAudioQueue){
  199. sprintf_s(m_AudioQueueName, MAX_PATH, "%s", strAudioQueue);
  200. }
  201. Dbg("audio queue name is %s.", m_AudioQueueName);
  202. m_salestransaudioqueue = new Clibaudiotransqueue(m_AudioQueueName);
  203. if (NULL != m_salestransaudioqueue){
  204. Dbg("get sales trans audio queue success, and queue addr is %0x.", m_salestransaudioqueue);
  205. }
  206. //session create
  207. if (NULL != m_pIAudioTransObj){
  208. Dbg("m_strMainServerURL is %s.", m_strMainServerURL);
  209. iRet = m_pIAudioTransObj->SessionCreate(m_strMainServerURL, m_iTimeOut);
  210. if (0 != iRet){
  211. Dbg("session create failed!");
  212. return iRet;
  213. }
  214. else{
  215. Dbg("session create success!");
  216. }
  217. }
  218. else{
  219. Dbg("audio trans object is not valid.");
  220. return iRet;
  221. }
  222. //session begin
  223. if (NULL != pBlob){
  224. iRet = m_pIAudioTransObj->SessionBegin(pBlob, uLen);
  225. }
  226. else{
  227. Dbg("invalid session begin params.");
  228. return iRet;
  229. }
  230. if (0 != iRet){
  231. Dbg("session begin failed!");
  232. }
  233. else{
  234. Dbg("session begin success, and start audio transmission thread.");
  235. //启动音频包转发线程
  236. ResetEvent(m_hEventStop);
  237. m_hAudioTransThread = (HANDLE)_beginthreadex(NULL, 0, AudioTransThread, (LPVOID)this, 0, (unsigned int*)& m_nAudioTransThreadId);
  238. }
  239. return iRet;
  240. }
  241. int Caudiotransmission::StopAudioTransmission()
  242. {
  243. LOG_FUNCTION();
  244. int iRet = -1;
  245. SetEvent(m_hEventStop);
  246. WaitForSingleObject(m_hAudioTransThread, INFINITE);
  247. CloseHandle(m_hAudioTransThread);
  248. m_hAudioTransThread = NULL;
  249. if (NULL != m_pIAudioTransObj) {
  250. iRet = m_pIAudioTransObj->SessionEnd();
  251. }
  252. if (NULL != m_salestransaudioqueue) {
  253. Dbg("delete sales audio trans queue.");
  254. delete m_salestransaudioqueue;
  255. m_salestransaudioqueue = NULL;
  256. }
  257. else
  258. {
  259. Dbg("sales audio trans queue is null.");
  260. }
  261. return iRet;
  262. }
  263. Caudiotransmission::~Caudiotransmission()
  264. {
  265. LOG_FUNCTION();
  266. if (NULL != m_salestransaudioqueue){
  267. Dbg("delete sales audio trans queue.");
  268. delete m_salestransaudioqueue;
  269. m_salestransaudioqueue = NULL;
  270. }
  271. else
  272. {
  273. Dbg("sales audio trans queue is null.");
  274. }
  275. if (NULL != m_pIAudioTransObj){
  276. if (NULL != m_fDestoryObj){
  277. m_fDestoryObj(m_pIAudioTransObj);
  278. Dbg("delete audio trans object.");
  279. }
  280. else {
  281. Dbg("m_fDestoryObj interface is null.");
  282. }
  283. }
  284. else
  285. {
  286. Dbg("audio trans obj is null.");
  287. }
  288. return;
  289. }