libaudioqueue.cpp 10 KB

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