123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472 |
- // libaudioqueue.cpp : 定义 DLL 应用程序的导出函数。
- //
- #ifdef _WIN32
- #include "stdafx.h"
- #endif
- #include "libaudioqueue.h"
- #include "libsharememory.h"
- #include <string.h>
- typedef struct Qnode
- {
- unsigned int videoframeindex;
- unsigned int nextqnodeindex;
- }Qnode, *queueptr;
- typedef struct linkqueue
- {
- unsigned int frontindex;
- unsigned int rearindex;
- unsigned int queuelens;
- }linkqueue;
- class libaudioqueue_impl
- {
- private:
- Clibsharememory m_ShareMem;
- void* m_lpMem;
- linkqueue*m_pQueue;
- int m_nTimeSignLens;
- int m_nQueueAddrLens;
- int m_nQnodeAddrLens;
- int m_nFrameAddrLens;
- int m_nDataAddrlens;
- char* szShareMemName;
- unsigned long aQnodeAddr[MAX_AUDIOQUEUE_LENS];
- unsigned long aAudioFrameAddr[MAX_AUDIOQUEUE_LENS];
- unsigned long aAudioDataAddr[MAX_AUDIOQUEUE_LENS];
- unsigned long nTimeSignAddr;
- public:
- libaudioqueue_impl(const char* audioqueuename,int framesize=MAX_AUDIOQNODE_SIZE)
- {
- m_lpMem = NULL;
- m_pQueue = NULL;
- szShareMemName = NULL;
- m_nQueueAddrLens = 0;
- m_nQnodeAddrLens = 0;
- m_nTimeSignLens = 0;
- m_nFrameAddrLens = 0;
- m_nDataAddrlens = 0;
- for(int i=0;i<MAX_AUDIOQUEUE_LENS;i++)
- {
- aQnodeAddr[i] = 0;
- aAudioFrameAddr[i] = 0;
- aAudioDataAddr[i] = 0;
- }
- nTimeSignAddr = 0;
- InitQueue(audioqueuename,framesize);
- }
- ~libaudioqueue_impl()
- {
- ClearAudioQueue();
- }
- //初始化队列
- bool InitQueue(const char* szName,int framesize=MAX_AUDIOQNODE_SIZE)
- {
- bool bret = false;
- m_nQueueAddrLens = sizeof(linkqueue);
- m_nQnodeAddrLens = MAX_AUDIOQUEUE_LENS*sizeof(Qnode);
- m_nFrameAddrLens = MAX_AUDIOQUEUE_LENS*sizeof(audio_frame);
- m_nDataAddrlens = MAX_AUDIOQUEUE_LENS*framesize;
- m_nTimeSignLens = sizeof(unsigned int);
- int nMemTotalNum = m_nTimeSignLens+m_nQueueAddrLens+m_nQnodeAddrLens+m_nFrameAddrLens+m_nDataAddrlens;
- if (m_ShareMem.Create(szName,nMemTotalNum)&&(m_nDataAddrlens!=0))
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- memset(m_lpMem,0,nMemTotalNum);
- m_pQueue = (linkqueue *)m_lpMem;
- for(int i =0;i<MAX_AUDIOQUEUE_LENS;i++)
- {
- aQnodeAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+sizeof(Qnode)*i;
- aAudioFrameAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+sizeof(audio_frame)*i;
- aAudioDataAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+m_nFrameAddrLens+framesize*i;
- }
- nTimeSignAddr = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+m_nFrameAddrLens+m_nDataAddrlens;
- m_pQueue->frontindex = m_pQueue->rearindex = 0;
- m_pQueue->queuelens = 0;
- m_ShareMem.Unlock();
- bret = true;
- }
- }
- else if(m_ShareMem.Open(szName))
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- m_pQueue = (linkqueue *)m_lpMem;
- for(int i =0;i<MAX_AUDIOQUEUE_LENS;i++)
- {
- aQnodeAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+sizeof(Qnode)*i;
- aAudioFrameAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+sizeof(audio_frame)*i;
- aAudioDataAddr[i] = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+m_nFrameAddrLens+framesize*i;
- }
- nTimeSignAddr = (unsigned long)m_pQueue+m_nQueueAddrLens+m_nQnodeAddrLens+m_nFrameAddrLens+m_nDataAddrlens;
- m_ShareMem.Unlock();
- bret = true;
- }
- }
- return bret;
- }
- //返回队列的元素个数,音频队列长度
- int GetAudioLens()
- {
- if(m_ShareMem.IsValid())
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- int num = m_pQueue->queuelens;
- m_ShareMem.Unlock();
- return num;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- return 0;
- }
- }
- //往音频循环队列尾部插节点
- bool InsertAudio(audio_frame* Audio,unsigned int nowtime)
- {
- if(m_ShareMem.IsValid())
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- unsigned int nRearNextIndex = 0;
- //保存当前对位指针的序列号
- queueptr rearptrfront = (queueptr)aQnodeAddr[m_pQueue->rearindex];
- //如果队列已满
- if(m_pQueue->queuelens == MAX_AUDIOQUEUE_LENS)
- {
- m_pQueue->rearindex = (m_pQueue->rearindex+1)%MAX_AUDIOQUEUE_LENS;
- m_pQueue->frontindex = (m_pQueue->frontindex+1)%MAX_AUDIOQUEUE_LENS;
- m_pQueue->queuelens = MAX_AUDIOQUEUE_LENS;
- }
- else if (m_pQueue->queuelens == 0)
- {
- m_pQueue->rearindex = 0;
- m_pQueue->frontindex = 0;
- m_pQueue->queuelens++;
- }
- else
- {
- m_pQueue->rearindex = (m_pQueue->rearindex+1)%MAX_AUDIOQUEUE_LENS;
- m_pQueue->frontindex = m_pQueue->frontindex;
- m_pQueue->queuelens++;
- }
- if (Audio!=NULL)
- {
- queueptr rearqnodetmp = (queueptr)aQnodeAddr[m_pQueue->rearindex];
- rearqnodetmp->videoframeindex = m_pQueue->rearindex;
- rearqnodetmp->nextqnodeindex = 0;
- audio_frame*audiotmp = (audio_frame*)aAudioFrameAddr[m_pQueue->rearindex];
- audiotmp->data = (char*)aAudioDataAddr[m_pQueue->rearindex];
- audiotmp->bitspersample = Audio->bitspersample;
- audiotmp->format = Audio->format;
- audiotmp->framesize = Audio->framesize;
- audiotmp->nchannels = Audio->nchannels;
- audiotmp->samplespersec = Audio->samplespersec;
- audiotmp->iseriesnumber = Audio->iseriesnumber;
- unsigned int*Ptr = (unsigned int*)nTimeSignAddr;
- *Ptr = nowtime;
- memcpy(audiotmp->data,Audio->data,Audio->framesize);
- rearptrfront->nextqnodeindex = m_pQueue->rearindex;
- //delete Audio->data;
- //delete Audio;
- }
- m_ShareMem.Unlock();
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- //读音频队列头部节点
- bool GetAudio(audio_frame* Audio)
- {
- if(m_ShareMem.IsValid())
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- if (m_pQueue->queuelens == 0)
- {
- m_ShareMem.Unlock();
- return false;
- }
- else
- {
- audio_frame*audiotemp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];;
- Audio->format = audiotemp->format;
- Audio->framesize = audiotemp->framesize;
- Audio->bitspersample = audiotemp->bitspersample;
- Audio->nchannels = audiotemp->nchannels;
- Audio->samplespersec = audiotemp->samplespersec;
- Audio->iseriesnumber = audiotemp->iseriesnumber;
- memcpy(Audio->data,(char*)aAudioDataAddr[m_pQueue->frontindex],audiotemp->framesize);
- m_ShareMem.Unlock();
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- //读视频队列头部节点,并删除头部节点
- bool GetAudioAndDel(audio_frame* Audio)
- {
- if(m_ShareMem.IsValid())
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- if (m_pQueue->queuelens == 0)
- {
- m_ShareMem.Unlock();
- return false;
- }
- else
- {
- audio_frame*audiotemp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];
- Audio->format = audiotemp->format;
- Audio->framesize = audiotemp->framesize;
- Audio->bitspersample = audiotemp->bitspersample;
- Audio->nchannels = audiotemp->nchannels;
- Audio->samplespersec = audiotemp->samplespersec;
- Audio->iseriesnumber = audiotemp->iseriesnumber;
- memcpy(Audio->data,(char*)aAudioDataAddr[m_pQueue->frontindex],audiotemp->framesize);
- char*data = (char*)aAudioDataAddr[m_pQueue->frontindex];
- memset(data,0,audiotemp->framesize);
- memset(audiotemp,0,sizeof(audio_frame));
- queueptr qnodetmp = (queueptr)aQnodeAddr[m_pQueue->frontindex];
- m_pQueue->frontindex = qnodetmp->nextqnodeindex;
- qnodetmp = (queueptr)aQnodeAddr[m_pQueue->rearindex];
- qnodetmp->nextqnodeindex = 0;
- m_pQueue->queuelens--;
- m_ShareMem.Unlock();
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- //清除队列
- bool ClearAudioQueue()
- {
- if(m_ShareMem.IsValid())
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- if (m_pQueue->queuelens != 0)
- {
- while(m_pQueue->queuelens != 0)
- {
- audio_frame*audiotemp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];
- char*data = (char*)aAudioDataAddr[m_pQueue->frontindex];
- memset(data,0,audiotemp->framesize);
- memset(audiotemp,0,sizeof(audio_frame));
- queueptr qnodetmp = (queueptr)aQnodeAddr[m_pQueue->frontindex];
- m_pQueue->frontindex = qnodetmp->nextqnodeindex;
- qnodetmp = (queueptr)aQnodeAddr[m_pQueue->rearindex];
- qnodetmp->nextqnodeindex = 0;
- m_pQueue->queuelens--;
- }
- }
- m_ShareMem.Unlock();
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
-
- //删除队头的数据
- bool DeleteHeadAudio()
- {
- if(m_ShareMem.IsValid())
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- if (m_pQueue->queuelens != 0)
- {
- audio_frame*audiotemp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];
- char*data = (char*)aAudioDataAddr[m_pQueue->frontindex];
- memset(data,0,audiotemp->framesize);
- memset(audiotemp,0,sizeof(audio_frame));
- queueptr qnodetmp = (queueptr)aQnodeAddr[m_pQueue->frontindex];
- m_pQueue->frontindex = qnodetmp->nextqnodeindex;
- qnodetmp = (queueptr)aQnodeAddr[m_pQueue->rearindex];
- qnodetmp->nextqnodeindex = 0;
- m_pQueue->queuelens--;
- }
- m_ShareMem.Unlock();
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- //获取音频包大小
- int GetFrameSize()
- {
- if(m_ShareMem.IsValid())
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- int nFrameSize = 0;
- if (m_pQueue->queuelens != 0)
- {
- audio_frame*audiotmp = (audio_frame*)aAudioFrameAddr[m_pQueue->frontindex];
- nFrameSize = audiotmp->framesize;
- }
- m_ShareMem.Unlock();
- return nFrameSize;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- return 0;
- }
- }
- unsigned int GetLastFrameTime()
- {
- if(m_ShareMem.IsValid())
- {
- m_lpMem = m_ShareMem.Lock(1000);
- if(m_lpMem != NULL)
- {
- unsigned int nLastFrameTime = 0;
- nLastFrameTime = *(unsigned int *)nTimeSignAddr;
- m_ShareMem.Unlock();
- return nLastFrameTime;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- return 0;
- }
- }
- };
- // 这是已导出类的构造函数。
- // 有关类定义的信息,请参阅 libaudioqueue.h
- Clibaudioqueue::Clibaudioqueue(const char* audioqueuename,int framesize)
- {
- m_pImpl = new libaudioqueue_impl(audioqueuename,framesize);
- return;
- }
- Clibaudioqueue::~Clibaudioqueue()
- {
- ClearAudioQueue();
- delete m_pImpl;
- return;
- }
- bool Clibaudioqueue::InsertAudio(audio_frame* Audio,unsigned int nowtime)
- {
- bool bRst = m_pImpl->InsertAudio(Audio, nowtime);
- return bRst;
- }
- bool Clibaudioqueue::GetAudio(audio_frame* Audio)
- {
- bool bRst = m_pImpl->GetAudio(Audio);
- return bRst;
- }
- bool Clibaudioqueue::GetAudioAndDel(audio_frame* Audio)
- {
- bool bRst = m_pImpl->GetAudioAndDel(Audio);
- return bRst;
- }
- int Clibaudioqueue::GetAudioLens(void)
- {
- int i = m_pImpl->GetAudioLens();
- return i;
- }
- void Clibaudioqueue::ClearAudioQueue()
- {
- m_pImpl->ClearAudioQueue();
- return;
- }
- int Clibaudioqueue::GetFrameSize()
- {
- int i = m_pImpl->GetFrameSize();
- return i;
- }
- void Clibaudioqueue::DeleteHeadAudio()
- {
- m_pImpl->DeleteHeadAudio();
- return;
- }
- unsigned int Clibaudioqueue::GetLastFrameTime()
- {
- return m_pImpl->GetLastFrameTime();
- }
|