// libaudioqueue.cpp : 定义 DLL 应用程序的导出函数。 // //#include "stdafx.h" #include "libaudioqueue.h" #include "../libsharememory/libsharememory.h" #include #include //#include "SpBase.h" //#define TESTSHAREMEM 0 void Debug(const char* fmt, ...) { // char strfmt[512] = { 0 }; // va_list args; // va_start(args, fmt); // vsprintf(strfmt, fmt, args); //#ifdef TESTSHAREMEM // printf("%s\n", strfmt); //#else // Dbg("%s", strfmt); //#endif // va_end(args); // fflush(stdout); } 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_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]; 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_nFrameAddrLens = 0; m_nDataAddrlens = 0; for(int i=0;ifrontindex = m_pQueue->rearindex = 0; m_pQueue->queuelens = 0; m_ShareMem.Unlock(); bret = true; } else { Debug("%s:%d share memory lock m_lpMem addr is NULL", __FUNCTION__, __LINE__); } } else if(m_ShareMem.Open(szName)) { Debug("%s:%d share memory Create failed and try open share memory %s.", __FUNCTION__, __LINE__, szName); m_lpMem = m_ShareMem.Lock(1000); if(m_lpMem != NULL) { m_pQueue = (linkqueue *)m_lpMem; for(int i =0;iqueuelens; m_ShareMem.Unlock(); return num; } else { return 0; } } else { return 0; } } //往音频循环队列尾部插节点 bool InsertAudio(audio_frame* Audio) { if(m_ShareMem.IsValid()) { Debug("m_ShareMem is valid."); 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; memcpy(audiotmp->data,Audio->data,Audio->framesize); rearptrfront->nextqnodeindex = m_pQueue->rearindex; Debug("audio series number is %d, m_pQueue->queuelens = %d.", audiotmp->iseriesnumber, m_pQueue->queuelens); //delete Audio->data; //delete Audio; } m_ShareMem.Unlock(); return true; } else { Debug("m_lpMem is null"); return false; } } else { Debug("m_ShareMem is not valid."); return false; } } //读音频队列头部节点 bool GetAudio(audio_frame* Audio) { if(m_ShareMem.IsValid()) { m_lpMem = m_ShareMem.Lock(1000); if(m_lpMem != NULL) { Debug("%s:%d share memory Lock success!", __FUNCTION__, __LINE__); if (m_pQueue->queuelens == 0) { Debug("%s:%d m_pQueue queuelens is 0!", __FUNCTION__, __LINE__); 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 { Debug("%s:%d share memory Lock failed!", __FUNCTION__, __LINE__); return false; } } else { Debug("%s:%d m_ShareMem.Is not Valid()", __FUNCTION__, __LINE__); return false; } } //读视频队列头部节点,并删除头部节点 bool GetAudioAndDel(audio_frame* Audio) { if(m_ShareMem.IsValid()) { m_lpMem = m_ShareMem.Lock(1000); if(m_lpMem != NULL) { Debug("%s:%d share memory Lock success, m_pQueue->queuelens = %d.", __FUNCTION__, __LINE__, m_pQueue->queuelens); if (m_pQueue->queuelens == 0) { Debug("%s:%d m_pQueue queuelens is 0!", __FUNCTION__, __LINE__); 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(); Debug("%s:%d m_pQueue->queuelens = %d.", __FUNCTION__, __LINE__, m_pQueue->queuelens); return true; } } else { Debug("%s:%d share memory Lock failed!", __FUNCTION__, __LINE__); return false; } } else { Debug("%s:%d m_ShareMem.Is not Valid()", __FUNCTION__, __LINE__); 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; } } }; // 这是已导出类的构造函数。 // 有关类定义的信息,请参阅 libaudioqueue.h Clibaudioqueue::Clibaudioqueue(const char* audioqueuename,int framesize) { Debug("%s:%d before new Clibaudioqueue name is %s, and framesize is %d.", __FUNCTION__, __LINE__, audioqueuename, framesize); m_pImpl = new libaudioqueue_impl(audioqueuename,framesize); Debug("%s:%d after new Clibaudioqueue name is %s, and framesize is %d.", __FUNCTION__, __LINE__, audioqueuename, framesize); return; } Clibaudioqueue::~Clibaudioqueue() { ClearAudioQueue(); delete m_pImpl; return; } bool Clibaudioqueue::InsertAudio(audio_frame* Audio) { bool bRst = m_pImpl->InsertAudio(Audio); 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; }