libaudioqueue.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // libaudioqueue.cpp : 定义 DLL 应用程序的导出函数。
  2. //
  3. //#include "stdafx.h"
  4. #include "libaudioqueue.h"
  5. #include "../libsharememory/libsharememory.h"
  6. #include<stdlib.h>
  7. #include<string.h>
  8. //#include "SpBase.h"
  9. //#define TESTSHAREMEM 0
  10. void Debug(const char* fmt, ...)
  11. {
  12. // char strfmt[512] = { 0 };
  13. // va_list args;
  14. // va_start(args, fmt);
  15. // vsprintf(strfmt, fmt, args);
  16. //#ifdef TESTSHAREMEM
  17. // printf("%s\n", strfmt);
  18. //#else
  19. // Dbg("%s", strfmt);
  20. //#endif
  21. // va_end(args);
  22. // fflush(stdout);
  23. }
  24. typedef struct Qnode
  25. {
  26. unsigned int videoframeindex;
  27. unsigned int nextqnodeindex;
  28. }Qnode, *queueptr;
  29. typedef struct linkqueue
  30. {
  31. unsigned int frontindex;
  32. unsigned int rearindex;
  33. unsigned int queuelens;
  34. }linkqueue;
  35. class libaudioqueue_impl
  36. {
  37. private:
  38. Clibsharememory m_ShareMem;
  39. void* m_lpMem;
  40. linkqueue*m_pQueue;
  41. int m_nQueueAddrLens;
  42. int m_nQnodeAddrLens;
  43. int m_nFrameAddrLens;
  44. int m_nDataAddrlens;
  45. char* szShareMemName;
  46. unsigned long aQnodeAddr[MAX_AUDIOQUEUE_LENS];
  47. unsigned long aAudioFrameAddr[MAX_AUDIOQUEUE_LENS];
  48. unsigned long aAudioDataAddr[MAX_AUDIOQUEUE_LENS];
  49. public:
  50. libaudioqueue_impl(char* audioqueuename,int framesize=MAX_AUDIOQNODE_SIZE)
  51. {
  52. m_lpMem = NULL;
  53. m_pQueue = NULL;
  54. szShareMemName = NULL;
  55. m_nQueueAddrLens = 0;
  56. m_nQnodeAddrLens = 0;
  57. m_nFrameAddrLens = 0;
  58. m_nDataAddrlens = 0;
  59. for(int i=0;i<MAX_AUDIOQUEUE_LENS;i++)
  60. {
  61. aQnodeAddr[i] = 0;
  62. aAudioFrameAddr[i] = 0;
  63. aAudioDataAddr[i] = 0;
  64. }
  65. Debug("%s:%d before InitQueue name is %s, and framesize is %d.", __FUNCTION__, __LINE__, audioqueuename, framesize);
  66. InitQueue(audioqueuename,framesize);
  67. Debug("%s:%d after InitQueue name is %s, and framesize is %d.", __FUNCTION__, __LINE__, audioqueuename, framesize);
  68. }
  69. ~libaudioqueue_impl()
  70. {
  71. ClearAudioQueue();
  72. }
  73. //初始化队列
  74. bool InitQueue(char* szName,int framesize=MAX_AUDIOQNODE_SIZE)
  75. {
  76. bool bret = false;
  77. Debug("%s:%d InitQueue name is %s, and framesize is %d.", __FUNCTION__, __LINE__, szName, framesize);
  78. m_nQueueAddrLens = sizeof(linkqueue);
  79. m_nQnodeAddrLens = MAX_AUDIOQUEUE_LENS*sizeof(Qnode);
  80. m_nFrameAddrLens = MAX_AUDIOQUEUE_LENS*sizeof(audio_frame);
  81. m_nDataAddrlens = MAX_AUDIOQUEUE_LENS*framesize;
  82. int nMemTotalNum = m_nQueueAddrLens+m_nQnodeAddrLens+m_nFrameAddrLens+m_nDataAddrlens;
  83. Debug("%s:%d before share memory Create name is %s, and nMemTotalNum is %d.", __FUNCTION__, __LINE__, szName, nMemTotalNum);
  84. if (m_ShareMem.Create(szName,nMemTotalNum)&&(m_nDataAddrlens!=0))
  85. {
  86. Debug("%s:%d share memory Create success and name is %s, nMemTotalNum is %d.", __FUNCTION__, __LINE__, szName, nMemTotalNum);
  87. m_lpMem = m_ShareMem.Lock(1000);
  88. if(m_lpMem != NULL)
  89. {
  90. Debug("%s:%d share memory lock m_lpMem addr is not NULL", __FUNCTION__, __LINE__);
  91. memset(m_lpMem,0,nMemTotalNum);
  92. m_pQueue = (linkqueue *)m_lpMem;
  93. for(int i =0;i<MAX_AUDIOQUEUE_LENS;i++)
  94. {
  95. aQnodeAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+sizeof(Qnode)*i;
  96. aAudioFrameAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+sizeof(audio_frame)*i;
  97. aAudioDataAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+m_nFrameAddrLens+framesize*i;
  98. }
  99. m_pQueue->frontindex = m_pQueue->rearindex = 0;
  100. m_pQueue->queuelens = 0;
  101. m_ShareMem.Unlock();
  102. bret = true;
  103. }
  104. else
  105. {
  106. Debug("%s:%d share memory lock m_lpMem addr is NULL", __FUNCTION__, __LINE__);
  107. }
  108. }
  109. else if(m_ShareMem.Open(szName))
  110. {
  111. Debug("%s:%d share memory Create failed and try open share memory %s.", __FUNCTION__, __LINE__, szName);
  112. m_lpMem = m_ShareMem.Lock(1000);
  113. if(m_lpMem != NULL)
  114. {
  115. m_pQueue = (linkqueue *)m_lpMem;
  116. for(int i =0;i<MAX_AUDIOQUEUE_LENS;i++)
  117. {
  118. aQnodeAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+sizeof(Qnode)*i;
  119. aAudioFrameAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+sizeof(audio_frame)*i;
  120. aAudioDataAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+m_nFrameAddrLens+framesize*i;
  121. }
  122. m_ShareMem.Unlock();
  123. bret = true;
  124. }
  125. else {
  126. Debug("%s:%d share memory lock failed and return.", __FUNCTION__, __LINE__);
  127. }
  128. }
  129. Debug("InitQueue return value is %s, and share memory valid flag is %s.",bret ? "true" : "false", m_ShareMem.IsValid() ? "true" : "false");
  130. return bret;
  131. }
  132. //返回队列的元素个数,音频队列长度
  133. int GetAudioLens()
  134. {
  135. if(m_ShareMem.IsValid())
  136. {
  137. m_lpMem = m_ShareMem.Lock(1000);
  138. if(m_lpMem != NULL)
  139. {
  140. int num = m_pQueue->queuelens;
  141. m_ShareMem.Unlock();
  142. return num;
  143. }
  144. else
  145. {
  146. return 0;
  147. }
  148. }
  149. else
  150. {
  151. return 0;
  152. }
  153. }
  154. //往音频循环队列尾部插节点
  155. bool InsertAudio(audio_frame* Audio)
  156. {
  157. if(m_ShareMem.IsValid())
  158. {
  159. Debug("m_ShareMem is valid.");
  160. m_lpMem = m_ShareMem.Lock(1000);
  161. if(m_lpMem != NULL)
  162. {
  163. unsigned int nRearNextIndex = 0;
  164. //保存当前对位指针的序列号
  165. queueptr rearptrfront = (queueptr)aQnodeAddr[m_pQueue->rearindex];
  166. //如果队列已满
  167. if(m_pQueue->queuelens == MAX_AUDIOQUEUE_LENS)
  168. {
  169. m_pQueue->rearindex = (m_pQueue->rearindex+1)%MAX_AUDIOQUEUE_LENS;
  170. m_pQueue->frontindex = (m_pQueue->frontindex+1)%MAX_AUDIOQUEUE_LENS;
  171. m_pQueue->queuelens = MAX_AUDIOQUEUE_LENS;
  172. }
  173. else if (m_pQueue->queuelens == 0)
  174. {
  175. m_pQueue->rearindex = 0;
  176. m_pQueue->frontindex = 0;
  177. m_pQueue->queuelens++;
  178. }
  179. else
  180. {
  181. m_pQueue->rearindex = (m_pQueue->rearindex+1)%MAX_AUDIOQUEUE_LENS;
  182. m_pQueue->frontindex = m_pQueue->frontindex;
  183. m_pQueue->queuelens++;
  184. }
  185. if (Audio!=NULL)
  186. {
  187. queueptr rearqnodetmp = (queueptr)aQnodeAddr[m_pQueue->rearindex];
  188. rearqnodetmp->videoframeindex = m_pQueue->rearindex;
  189. rearqnodetmp->nextqnodeindex = 0;
  190. audio_frame*audiotmp = (audio_frame*)aAudioFrameAddr[m_pQueue->rearindex];
  191. audiotmp->data = (char*)aAudioDataAddr[m_pQueue->rearindex ];
  192. audiotmp->bitspersample = Audio->bitspersample;
  193. audiotmp->format = Audio->format;
  194. audiotmp->framesize = Audio->framesize;
  195. audiotmp->nchannels = Audio->nchannels;
  196. audiotmp->samplespersec = Audio->samplespersec;
  197. audiotmp->iseriesnumber = Audio->iseriesnumber;
  198. memcpy(audiotmp->data,Audio->data,Audio->framesize);
  199. rearptrfront->nextqnodeindex = m_pQueue->rearindex;
  200. Debug("audio series number is %d, m_pQueue->queuelens = %d.", audiotmp->iseriesnumber, m_pQueue->queuelens);
  201. //delete Audio->data;
  202. //delete Audio;
  203. }
  204. m_ShareMem.Unlock();
  205. return true;
  206. }
  207. else
  208. {
  209. Debug("m_lpMem is null");
  210. return false;
  211. }
  212. }
  213. else
  214. {
  215. Debug("m_ShareMem is not valid.");
  216. return false;
  217. }
  218. }
  219. //读音频队列头部节点
  220. bool GetAudio(audio_frame* Audio)
  221. {
  222. if(m_ShareMem.IsValid())
  223. {
  224. m_lpMem = m_ShareMem.Lock(1000);
  225. if(m_lpMem != NULL)
  226. {
  227. Debug("%s:%d share memory Lock success!", __FUNCTION__, __LINE__);
  228. if (m_pQueue->queuelens == 0)
  229. {
  230. Debug("%s:%d m_pQueue queuelens is 0!", __FUNCTION__, __LINE__);
  231. m_ShareMem.Unlock();
  232. return false;
  233. }
  234. else
  235. {
  236. audio_frame*audiotemp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];;
  237. Audio->format = audiotemp->format;
  238. Audio->framesize = audiotemp->framesize;
  239. Audio->bitspersample = audiotemp->bitspersample;
  240. Audio->nchannels = audiotemp->nchannels;
  241. Audio->samplespersec = audiotemp->samplespersec;
  242. Audio->iseriesnumber = audiotemp->iseriesnumber;
  243. memcpy(Audio->data,(char*)aAudioDataAddr[m_pQueue->frontindex],audiotemp->framesize);
  244. m_ShareMem.Unlock();
  245. return true;
  246. }
  247. }
  248. else
  249. {
  250. Debug("%s:%d share memory Lock failed!", __FUNCTION__, __LINE__);
  251. return false;
  252. }
  253. }
  254. else
  255. {
  256. Debug("%s:%d m_ShareMem.Is not Valid()", __FUNCTION__, __LINE__);
  257. return false;
  258. }
  259. }
  260. //读视频队列头部节点,并删除头部节点
  261. bool GetAudioAndDel(audio_frame* Audio)
  262. {
  263. if(m_ShareMem.IsValid())
  264. {
  265. m_lpMem = m_ShareMem.Lock(1000);
  266. if(m_lpMem != NULL)
  267. {
  268. Debug("%s:%d share memory Lock success, m_pQueue->queuelens = %d.", __FUNCTION__, __LINE__, m_pQueue->queuelens);
  269. if (m_pQueue->queuelens == 0)
  270. {
  271. Debug("%s:%d m_pQueue queuelens is 0!", __FUNCTION__, __LINE__);
  272. m_ShareMem.Unlock();
  273. return false;
  274. }
  275. else
  276. {
  277. audio_frame*audiotemp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];
  278. Audio->format = audiotemp->format;
  279. Audio->framesize = audiotemp->framesize;
  280. Audio->bitspersample = audiotemp->bitspersample;
  281. Audio->nchannels = audiotemp->nchannels;
  282. Audio->samplespersec = audiotemp->samplespersec;
  283. Audio->iseriesnumber = audiotemp->iseriesnumber;
  284. memcpy(Audio->data,(char*)aAudioDataAddr[m_pQueue->frontindex],audiotemp->framesize);
  285. char*data = (char*)aAudioDataAddr[m_pQueue->frontindex];
  286. memset(data,0,audiotemp->framesize);
  287. memset(audiotemp,0,sizeof(audio_frame));
  288. queueptr qnodetmp = (queueptr)aQnodeAddr[m_pQueue->frontindex];
  289. m_pQueue->frontindex = qnodetmp->nextqnodeindex;
  290. qnodetmp = (queueptr)aQnodeAddr[m_pQueue->rearindex];
  291. qnodetmp->nextqnodeindex = 0;
  292. m_pQueue->queuelens--;
  293. m_ShareMem.Unlock();
  294. Debug("%s:%d m_pQueue->queuelens = %d.", __FUNCTION__, __LINE__, m_pQueue->queuelens);
  295. return true;
  296. }
  297. }
  298. else
  299. {
  300. Debug("%s:%d share memory Lock failed!", __FUNCTION__, __LINE__);
  301. return false;
  302. }
  303. }
  304. else
  305. {
  306. Debug("%s:%d m_ShareMem.Is not Valid()", __FUNCTION__, __LINE__);
  307. return false;
  308. }
  309. }
  310. //清除队列
  311. bool ClearAudioQueue()
  312. {
  313. if(m_ShareMem.IsValid())
  314. {
  315. m_lpMem = m_ShareMem.Lock(1000);
  316. if(m_lpMem != NULL)
  317. {
  318. if (m_pQueue->queuelens != 0)
  319. {
  320. while(m_pQueue->queuelens != 0)
  321. {
  322. audio_frame*audiotemp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];
  323. char*data = (char*)aAudioDataAddr[m_pQueue->frontindex];
  324. memset(data,0,audiotemp->framesize);
  325. memset(audiotemp,0,sizeof(audio_frame));
  326. queueptr qnodetmp = (queueptr)aQnodeAddr[m_pQueue->frontindex];
  327. m_pQueue->frontindex = qnodetmp->nextqnodeindex;
  328. qnodetmp = (queueptr)aQnodeAddr[m_pQueue->rearindex];
  329. qnodetmp->nextqnodeindex = 0;
  330. m_pQueue->queuelens--;
  331. }
  332. }
  333. m_ShareMem.Unlock();
  334. return true;
  335. }
  336. else
  337. {
  338. return false;
  339. }
  340. }
  341. else
  342. {
  343. return false;
  344. }
  345. }
  346. //删除队头的数据
  347. bool DeleteHeadAudio()
  348. {
  349. if(m_ShareMem.IsValid())
  350. {
  351. m_lpMem = m_ShareMem.Lock(1000);
  352. if(m_lpMem != NULL)
  353. {
  354. if (m_pQueue->queuelens != 0)
  355. {
  356. audio_frame*audiotemp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];
  357. char*data = (char*)aAudioDataAddr[m_pQueue->frontindex];
  358. memset(data,0,audiotemp->framesize);
  359. memset(audiotemp,0,sizeof(audio_frame));
  360. queueptr qnodetmp = (queueptr)aQnodeAddr[m_pQueue->frontindex];
  361. m_pQueue->frontindex = qnodetmp->nextqnodeindex;
  362. qnodetmp = (queueptr)aQnodeAddr[m_pQueue->rearindex];
  363. qnodetmp->nextqnodeindex = 0;
  364. m_pQueue->queuelens--;
  365. }
  366. m_ShareMem.Unlock();
  367. return true;
  368. }
  369. else
  370. {
  371. return false;
  372. }
  373. }
  374. else
  375. {
  376. return false;
  377. }
  378. }
  379. //获取图像的大小
  380. int GetFrameSize()
  381. {
  382. if(m_ShareMem.IsValid())
  383. {
  384. m_lpMem = m_ShareMem.Lock(1000);
  385. if(m_lpMem != NULL)
  386. {
  387. int nFrameSize = 0;
  388. if (m_pQueue->queuelens != 0)
  389. {
  390. audio_frame*audiotmp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];
  391. nFrameSize = audiotmp->framesize;
  392. }
  393. m_ShareMem.Unlock();
  394. return nFrameSize;
  395. }
  396. else
  397. {
  398. return 0;
  399. }
  400. }
  401. else
  402. {
  403. return 0;
  404. }
  405. }
  406. };
  407. // 这是已导出类的构造函数。
  408. // 有关类定义的信息,请参阅 libaudioqueue.h
  409. Clibaudioqueue::Clibaudioqueue(char* audioqueuename,int framesize)
  410. {
  411. Debug("%s:%d before new Clibaudioqueue name is %s, and framesize is %d.", __FUNCTION__, __LINE__, audioqueuename, framesize);
  412. m_pImpl = new libaudioqueue_impl(audioqueuename,framesize);
  413. Debug("%s:%d after new Clibaudioqueue name is %s, and framesize is %d.", __FUNCTION__, __LINE__, audioqueuename, framesize);
  414. return;
  415. }
  416. Clibaudioqueue::~Clibaudioqueue()
  417. {
  418. ClearAudioQueue();
  419. delete m_pImpl;
  420. return;
  421. }
  422. bool Clibaudioqueue::InsertAudio(audio_frame* Audio)
  423. {
  424. bool bRst = m_pImpl->InsertAudio(Audio);
  425. return bRst;
  426. }
  427. bool Clibaudioqueue::GetAudio(audio_frame* Audio)
  428. {
  429. bool bRst = m_pImpl->GetAudio(Audio);
  430. return bRst;
  431. }
  432. bool Clibaudioqueue::GetAudioAndDel(audio_frame* Audio)
  433. {
  434. bool bRst = m_pImpl->GetAudioAndDel(Audio);
  435. return bRst;
  436. }
  437. int Clibaudioqueue::GetAudioLens(void)
  438. {
  439. int i = m_pImpl->GetAudioLens();
  440. return i;
  441. }
  442. void Clibaudioqueue::ClearAudioQueue()
  443. {
  444. m_pImpl->ClearAudioQueue();
  445. return;
  446. }
  447. int Clibaudioqueue::GetFrameSize()
  448. {
  449. int i = m_pImpl->GetFrameSize();
  450. return i;
  451. }
  452. void Clibaudioqueue::DeleteHeadAudio()
  453. {
  454. m_pImpl->DeleteHeadAudio();
  455. return;
  456. }