EasyPusher直播推送中用到的缓冲区设计和丢帧原理
问题描述
我们在开发直播过程中,会需要用到直播推送端,推送端将直播的音视频数据推送到流媒体服务器或者cdn,再由流媒体服务器/CDN进行视频的转发和分发,提供给客户端进行观看。由于直播推送端会存在于各种不同的网络环境下面:有线、无线、3G、4G、卫星信号等等,在这些网络条件下,如何做到能够做到灵活、低延时直播,我们这个时候就需要引入发送缓冲区和丢帧策略两种功能,保证推送的实时和数据的有效;
环形缓冲区(引用)
环形缓冲区(ring buffer),是一种数据结构用于表示一个固定尺寸、头尾相连的缓冲区,适合缓存数据流。
用法
圆形缓冲区的一个有用特性是:当一个数据元素被用掉后,其余数据元素不需要移动其存储位置。相反,一个非圆形缓冲区(例如一个普通的队列)在用掉一个数据元素后,其余数据元素需要向前搬移。换句话说,圆形缓冲区适合实现先进先出缓冲区,而非圆形缓冲区适合后进先出缓冲区。
圆形缓冲区适合于事先明确了缓冲区的最大容量的情形。扩展一个圆形缓冲区的容量,需要搬移其中的数据。因此一个缓冲区如果需要经常调整其容量,用链表实现更为合适。
写操作覆盖圆形缓冲区中未被处理的数据在某些情况下是允许的。特别是在多媒体处理时。例如,音频的生产者可以覆盖掉声卡尚未来得及处理的音频数据。
工作过程
一个圆形缓冲区最初为空并有预定的长度。例如,这是一个具有七个元素空间的圆形缓冲区,其中底部的单线与箭头表示“头尾相接”形成一个圆形地址空间:
假定1被写入缓冲区中部(对于圆形缓冲区来说,最初的写入位置在哪里是无关紧要的):
再写入2个元素,分别是2 & 3 — 被追加在1之后:
如果两个元素被处理,那么是缓冲区中最老的两个元素被卸载。在本例中,1 & 2被卸载,缓冲区中只剩下3:
如果缓冲区中有7个元素,则是满的:
如果缓冲区是满的,又要写入新的数据,一种策略是覆盖掉最老的数据。此例中,2个新数据— A & B — 写入,覆盖了3 & 4:
也可以采取其他策略,禁止覆盖缓冲区的数据,采取返回一个错误码或者抛出异常。
最终,如果从缓冲区中卸载2个数据,不是3 & 4 而是 5 & 6 。因为 A & B 已经覆盖了3 & 4:
圆形缓冲区工作机制
由于计算机内存是线性地址空间,因此圆形缓冲区需要特别的设计才可以从逻辑上实现。
读指针与写指针
一般的,圆形缓冲区需要4个指针:
- 在内存中实际开始位置;
- 在内存中实际结束位置,也可以用缓冲区长度代替;
- 存储在缓冲区中的有效数据的开始位置(读指针);
- 存储在缓冲区中的有效数据的结尾位置(写指针)。
读指针、写指针可以用整型值来表示。
下例为一个未满的缓冲区的读写指针:
下例为一个满的缓冲区的读写指针:
EasyPusher的推送缓冲区设计
EasyPusher内部也同样采用的环形缓冲的设计方法,将音视频数据都同时存入缓冲区,再由发送者从缓冲区中获取数据进行发送,这样就形成了一个异步、生产者、消费者的过程,上层调用者只需要将采集、编码后的音视频Frame数据Push到SDK的缓冲区中,即可返回继续进行上层逻辑操作,SDK内部的发送线程则从缓冲区中不断获取音视频数据推送到流媒体服务器;
缓冲区丢帧策略
有缓冲区,势必会出现缓冲区满的情况,当这个时候,就需要合理的丢帧策略了,我们这里采用的非关键帧丢帧策略跟《EasyDarwin开源流媒体服务器低延时直播之转发缓存跟进算法》博客中的原理是一致的,当缓冲区新增一帧进入缓冲区时,此时缓冲区已经满了,那么我们就要从缓冲区的读指针部分开始丢弃非关键帧P帧,直到遇到I帧就停止丢帧,再将新来的数据帧增加进入缓冲区,这样既可以保证一个较合理的直播推送缓冲区,确保推送延时可控,又能够保证不会出现花屏的问题;
EasyPusher推送缓冲区实现
/*
Copyright (c) 2012-2016 EasyDarwin.ORG. All rights reserved.
Github: https://github.com/EasyDarwin
WEChat: EasyDarwin
Website: http://www.easydarwin.org
*/
#include "ssqueue.h"
#include <time.h>
#include <stdarg.h>
#include "trace.h"
int SSQ_Init(SS_QUEUE_OBJ_T *pObj, unsigned int sharememory, unsigned int channelid, wchar_t *sharename, unsigned int bufsize, unsigned int prerecordsecs, unsigned int createsharememory)
{
wchar_t wszHeaderName[36] = {0,};
wchar_t wszFramelistName[36] = {0,};
wchar_t wszDataName[36] = {0,};
if (NULL==pObj) return -1;
if (createsharememory==0x01 && bufsize<1) return -1;
if ( (sharememory==0x01) && (NULL==sharename || (0==wcscmp(sharename, TEXT("\0")))) ) return -1;
memset(pObj, 0x00, sizeof(SS_QUEUE_OBJ_T));
pObj->channelid = channelid;
pObj->shareinfo.id = channelid;
wcscpy(pObj->shareinfo.name, sharename);
wchar_t wszMutexName[36] = {0,};
wsprintf(wszMutexName, TEXT("%s%d_mutex"), sharename, channelid);
pObj->hMutex = OpenMutex(NULL, FALSE, wszMutexName);
if (NULL == pObj->hMutex)
{
pObj->hMutex = CreateMutex(NULL, FALSE, wszMutexName);
if (NULL == pObj->hMutex) return -1;
}
//Create Header map
#ifdef _WIN32
if (sharememory == 0x01)
{
wsprintf(wszHeaderName, TEXT("%s%d_h"), sharename, channelid);
pObj->hSSHeader = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, wszHeaderName);
if (NULL==pObj->hSSHeader && createsharememory==0x01)
{
pObj->hSSHeader = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT, 0, sizeof(SS_HEADER_T), wszHeaderName);
if (NULL==pObj->hSSHeader || pObj->hSSHeader==INVALID_HANDLE_VALUE)
{
return -1;
}
}
pObj->pQueHeader = (SS_HEADER_T*)MapViewOfFile(pObj->hSSHeader, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
if (createsharememory==0x01)
{
if (pObj->pQueHeader->bufsize < 1)
{
memset(pObj->pQueHeader, 0x00, sizeof(SS_HEADER_T));
pObj->pQueHeader->bufsize = bufsize;
}
}
else if (NULL==pObj->pQueHeader)
{
return -1;
}
else
{
bufsize = pObj->pQueHeader->bufsize;
}
}
else
{
pObj->pQueHeader = new SS_HEADER_T;
memset(pObj->pQueHeader, 0x00, sizeof(SS_HEADER_T));
}
//==========================================
//Create frame list map
if (prerecordsecs > 0)
{
wsprintf(wszFramelistName, TEXT("%s%d_f"), sharename, channelid);
unsigned int nFramelistNum = prerecordsecs * 30; //每秒30帧
unsigned int nFrameQueSize = nFramelistNum*sizeof(FRAMEINFO_LIST_T);
if (sharememory == 0x01)
{
pObj->hSSFrameList = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, wszFramelistName);
if (NULL==pObj->hSSFrameList && createsharememory==0x01)
{
pObj->hSSFrameList = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT, 0, nFrameQueSize, wszFramelistName);
if (NULL==pObj->hSSFrameList || pObj->hSSFrameList==INVALID_HANDLE_VALUE)
{
return -1;
}
}
pObj->pFrameinfoList = (FRAMEINFO_LIST_T*)MapViewOfFile(pObj->hSSFrameList, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
if (createsharememory==0x01)
{
memset(pObj->pFrameinfoList, 0x00, nFrameQueSize);
pObj->pQueHeader->framelistNum = nFramelistNum;
}
else if (NULL==pObj->hSSFrameList)
{
return -1;
}
}
else
{
pObj->pFrameinfoList = new FRAMEINFO_LIST_T[nFramelistNum];
memset(&pObj->pFrameinfoList[0], 0x00, sizeof(FRAMEINFO_LIST_T)*nFramelistNum);
pObj->pQueHeader->framelistNum = nFramelistNum;
}
}
//Create data map
if (sharememory == 0x01)
{
wsprintf(wszDataName, TEXT("%s%d_b"), sharename, channelid);
pObj->hSSData = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, wszDataName);
if (NULL==pObj->hSSData && createsharememory==0x01)
{
pObj->hSSData = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE|SEC_COMMIT, 0, bufsize, wszDataName);
}
if (NULL == pObj->hSSData || pObj->hSSData==INVALID_HANDLE_VALUE)
{
return -1;
}
pObj->pQueData = (char*)MapViewOfFile(pObj->hSSData, FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, 0);
}
else
{
pObj->pQueData = new char [bufsize];
pObj->pQueHeader->bufsize = bufsize;
}
if (createsharememory==0x01)
{
//memset(pQueHeader, 0x00, sizeof(SS_HEADER_T));
memset(pObj->pQueData, 0x00, bufsize);
}
#else
int ret = shm_create((SYNC_VID_SHM_KEY<<8)|channelid, &pObj->shmHdrid, sizeof(SS_HEADER_T), (char**)&pObj->pQueHeader);
if (ret < 0)
{
return -1;
}
SSQ_TRACE("[%d]pQueHeader: %d\n", (SYNC_VID_SHM_KEY<<8)|channelid, pObj->shmHdrid);
ret = shm_create((SYNC_VID_SHM_KEY<<16)|channelid, &pObj->shmDatid, bufsize, (char**)&pObj->pQueData);
if (ret < 0)
{
shm_delete(&pObj->shmHdrid, (char*)pObj->pQueHeader);
return -1;
}
pObj->pQueHeader->bufsize = bufsize;
SSQ_TRACE("[%d]pQueData: %d\n", (SYNC_VID_SHM_KEY<<16)|channelid, pObj->shmDatid);
#endif
return 0;
}
int SSQ_Deinit(SS_QUEUE_OBJ_T *pObj)
{
if (NULL == pObj) return -1;
#ifdef _WIN32
if (NULL != pObj->hMutex)
{
CloseHandle(pObj->hMutex);
pObj->hMutex = NULL;
}
if (NULL != pObj->hSSHeader)
{
if (! UnmapViewOfFile(pObj->pQueHeader))
{
}
pObj->pQueHeader = NULL;
CloseHandle(pObj->hSSHeader);
pObj->hSSHeader = NULL;
}
if (NULL != pObj->pQueHeader)
{
delete []pObj->pQueHeader;
pObj->pQueHeader = NULL;
}
if (NULL != pObj->hSSData)
{
if (! UnmapViewOfFile(pObj->pQueData))
{
}
pObj->pQueData = NULL;
CloseHandle(pObj->hSSData);
pObj->hSSData = NULL;
}
if (NULL != pObj->pQueData)
{
delete []pObj->pQueData;
pObj->pQueData = NULL;
}
if (NULL != pObj->hSSFrameList)
{
if (! UnmapViewOfFile(pObj->pFrameinfoList))
{
}
pObj->pFrameinfoList = NULL;
CloseHandle(pObj->hSSFrameList);
pObj->hSSFrameList = NULL;
}
if (NULL != pObj->pFrameinfoList)
{
delete []pObj->pFrameinfoList;
pObj->pFrameinfoList = NULL;
}
#else
if (pObj->shmHdrid>0 && pObj->pQueHeader!=NULL)
{
shm_delete(&pObj->shmHdrid, (char*)pObj->pQueHeader);
}
if (pObj->shmDatid>0 && pObj->pQueData!=NULL)
{
shm_delete(&pObj->shmDatid, (char*)pObj->pQueData);
pObj->pQueData = NULL;
}
#endif
return 0;
}
int SSQ_SetClearFlag(SS_QUEUE_OBJ_T *pObj, unsigned int _flag)
{
if (NULL == pObj) return -1;
if (NULL==pObj->pQueData) return -1;
pObj->pQueHeader->clear_flag = _flag;
return 0;
}
int SSQ_Clear(SS_QUEUE_OBJ_T *pObj)
{
if (NULL == pObj) return -1;
if (NULL==pObj->pQueData) return -1;
//WaitForSingleObject(pObj->hMutex, INFINITE);
//Lock();
pObj->pQueHeader->writepos = 0;
pObj->pQueHeader->readpos = 0;
pObj->pQueHeader->totalsize= 0;
pObj->pQueHeader->videoframes = 0; //视频帧数
pObj->pQueHeader->maxframeno = 0;
pObj->pQueHeader->frameno = 0;
memset(pObj->pQueData, 0x00, pObj->pQueHeader->bufsize);
//ReleaseMutex(pObj->hMutex);
//Unlock();
return 0;
}
int SSQ_AddFrameInfo(SS_QUEUE_OBJ_T *pObj, unsigned int _pos, MEDIA_FRAME_INFO *frameinfo)
{
if (NULL == pObj) return -1;
if (NULL == pObj->pQueHeader) return -1;
if (NULL == pObj->pFrameinfoList) return -1;
if ( pObj->pQueHeader->frameno+1 > pObj->pQueHeader->framelistNum)
{
memmove((char*)pObj->pFrameinfoList, (char*)pObj->pFrameinfoList+sizeof(FRAMEINFO_LIST_T), sizeof(FRAMEINFO_LIST_T)*pObj->pQueHeader->framelistNum-1);
pObj->pQueHeader->frameno --;
pObj->pQueHeader->maxframeno = pObj->pQueHeader->frameno+1;
}
pObj->pFrameinfoList[pObj->pQueHeader->frameno].pos = pObj->pQueHeader->writepos;
#ifdef _DEBUG1
static int nTimestamp = 0;
pObj->pQueHeader->pFrameinfoList[pObj->pQueHeader->frameno].timestamp = ++nTimestamp;
#else
pObj->pFrameinfoList[pObj->pQueHeader->frameno].timestamp_sec = frameinfo->timestamp_sec;
pObj->pFrameinfoList[pObj->pQueHeader->frameno].rtp_timestamp = frameinfo->timestamp_sec*1000+frameinfo->timestamp_usec/1000;
#endif
//SSQ_TRACE("帧号: %d\n", pObj->pQueHeader->frameno);
pObj->pQueHeader->frameno ++;
pObj->pQueHeader->maxframeno = pObj->pQueHeader->frameno;
/*
if ( pObj->pQueHeader->frameno >= pObj->pQueHeader->framelistNum)
{
SSQ_TRACE("max frame: %d\n", pObj->pQueHeader->maxframeno);
pObj->pQueHeader->frameno = 0;
}
if (pObj->pQueHeader->frameno > pObj->pQueHeader->maxframeno)
{
pObj->pQueHeader->maxframeno = pObj->pQueHeader->frameno;
}
*/
return 0;
}
int SSQ_AddData(SS_QUEUE_OBJ_T *pObj, unsigned int channelid, unsigned int mediatype, MEDIA_FRAME_INFO *frameinfo, char *pbuf)
{
int ret = 0;
SS_BUF_T bufNode;
if (NULL==pObj || NULL==frameinfo || NULL==pbuf) return -1;
if (NULL == pObj->pQueData) return -1;
if (NULL == pObj->pQueHeader) return -1;
if (frameinfo->length < 1)
{
#ifdef _DEBUG
SSQ_TRACE("frame length < 1: %d\n", frameinfo->length);
#endif
return -1;
}
#ifdef _DEBUG1
SSQ_TRACE("frame: %02x%02x%02x%02x%02x\n", (unsigned char)pbuf[0],
(unsigned char)pbuf[1],
(unsigned char)pbuf[2],
(unsigned char)pbuf[3],
(unsigned char)pbuf[4]);
#endif
if (frameinfo->length > pObj->pQueHeader->bufsize)
{
SSQ_TRACE("Buffer too low.. Current Frame Size: %d\tBuffer Size: %d\n", frameinfo->length, pObj->pQueHeader->bufsize);
return -1;
}
WaitForSingleObject(pObj->hMutex, INFINITE); //Lock
if (pObj->pQueHeader->clear_flag == 0x01)
{
//SSQ_TRACE("Received clear signal... Clear Buffer..\n");
SSQ_TRACE("clear cache.. WritePos: %d\n", pObj->pQueHeader->writepos);
SSQ_Clear(pObj);
pObj->pQueHeader->clear_flag = 0x00;
}
if (pObj->pQueHeader->writepos == pObj->pQueHeader->readpos)
{
if (pObj->pQueHeader->totalsize < 0 || pObj->pQueHeader->videoframes < 0)
{
SSQ_TRACE("write speed==read speed... data length exception: %d video frames:%d\n", pObj->pQueHeader->totalsize, pObj->pQueHeader->videoframes);
}
//Clear();
}
if (sizeof(SS_BUF_T) + frameinfo->length + pObj->pQueHeader->totalsize > pObj->pQueHeader->bufsize)
{
SSQ_TRACE("beyond ssqueue length.. len:%d\ttotalsize:%d\tbufsize:%d cache frames:%d\n", frameinfo->length, pObj->pQueHeader->totalsize, pObj->pQueHeader->bufsize, pObj->pQueHeader->videoframes);
ReleaseMutex(pObj->hMutex);
pObj->pQueHeader->isfull = 0x01;
return -1;
}
pObj->pQueHeader->isfull = 0x00;
memset(&bufNode, 0x00, sizeof(SS_BUF_T));
memcpy(&bufNode.frameinfo, frameinfo, sizeof(MEDIA_FRAME_INFO));
bufNode.channelid = channelid;//++m_FrameTally;
bufNode.mediatype = mediatype;
bufNode.flag = BUF_QUE_FLAG;
//_TRACE("WritePos: %d totalSize: %d\n", pQueHeader->writepos, pQueHeader->totalsize);
//Lock();
if ((pObj->pQueHeader->writepos + sizeof(SS_BUF_T) + frameinfo->length) <= pObj->pQueHeader->bufsize)
{
//copy to queue
if (mediatype==MEDIA_TYPE_VIDEO) SSQ_AddFrameInfo(pObj, pObj->pQueHeader->writepos, frameinfo);
unsigned int nAdd = pObj->pQueHeader->writepos;
memcpy(pObj->pQueData+nAdd, &bufNode, sizeof(SS_BUF_T));
nAdd += sizeof(SS_BUF_T);
memcpy(pObj->pQueData+nAdd, pbuf, frameinfo->length);
nAdd += frameinfo->length;
pObj->pQueHeader->writepos = nAdd;
pObj->pQueHeader->totalsize+= sizeof(SS_BUF_T);
pObj->pQueHeader->totalsize+= frameinfo->length;
if (mediatype==MEDIA_TYPE_VIDEO) pObj->pQueHeader->videoframes ++;
//_TRACE("顺序新增.. writepos: %d / %d\n", pQueHeader->writepos, pQueHeader->size);
}
else if (pObj->pQueHeader->writepos == pObj->pQueHeader->bufsize) //从头开始
{
//记录帧位置
if (mediatype==MEDIA_TYPE_VIDEO) SSQ_AddFrameInfo(pObj, 0, frameinfo);
memcpy(pObj->pQueData, &bufNode, sizeof(SS_BUF_T));
pObj->pQueHeader->writepos = sizeof(SS_BUF_T);
memcpy(pObj->pQueData+pObj->pQueHeader->writepos, pbuf, frameinfo->length);
pObj->pQueHeader->writepos += frameinfo->length;
pObj->pQueHeader->totalsize= sizeof(SS_BUF_T);
pObj->pQueHeader->totalsize+= frameinfo->length;
if (mediatype==MEDIA_TYPE_VIDEO) pObj->pQueHeader->videoframes ++;
SSQ_TRACE("从头开始写.. writepos: %d\n", pObj->pQueHeader->writepos);
}
//else if (pQueHeader->size - pQueHeader->writepos+pQueHeader->readpos >= (int)(frameinfo->length+sizeof(SS_BUF_T))) //从尾写到头
//else if (pObj->pQueHeader->bufsize - pObj->pQueHeader->writepos+pObj->pQueHeader->readpos >= (int)(frameinfo->length+sizeof(SS_BUF_T))) //从尾写到头
else// if (pObj->pQueHeader->bufsize - pObj->pQueHeader->writepos >= (int)(frameinfo->length+sizeof(SS_BUF_T))) //从尾写到头
{
unsigned int remain = pObj->pQueHeader->bufsize - pObj->pQueHeader->writepos; //剩余空间
if (remain>=sizeof(SS_BUF_T))
{
unsigned int nAdd = pObj->pQueHeader->writepos;
//记录帧位置
if (mediatype==MEDIA_TYPE_VIDEO) SSQ_AddFrameInfo(pObj, nAdd, frameinfo);
//_TRACE("WritePos111: %d\n", pQueHeader->writepos);
memcpy(pObj->pQueData+nAdd, &bufNode, sizeof(SS_BUF_T));
nAdd += sizeof(SS_BUF_T);
//pQueHeader->totalsize+= sizeof(SS_BUF_T);
//_TRACE("WritePos222: %d\n", pQueHeader->writepos+sizeof(SS_BUF_T));
remain = pObj->pQueHeader->bufsize - nAdd;//pQueHeader->writepos;
//_TRACE("WritePos: %d\n", pQueHeader->writepos+sizeof(SS_BUF_T)+remain);
if (remain>0)
{
memcpy(pObj->pQueData+nAdd, pbuf, remain);
memcpy(pObj->pQueData, pbuf+remain, frameinfo->length-remain);
nAdd = frameinfo->length-remain;
pObj->pQueHeader->writepos = nAdd;
pObj->pQueHeader->totalsize+= sizeof(SS_BUF_T);
pObj->pQueHeader->totalsize+= frameinfo->length;
if (pObj->pQueHeader->totalsize>pObj->pQueHeader->bufsize)
{
//原因: rtsp server已停止读该队列(当前没有客户端访问)
SSQ_TRACE("[RTSP Server已停止读取该队列]错误 %d > %d frameinfo->length:%d...\n", pObj->pQueHeader->totalsize, pObj->pQueHeader->bufsize, frameinfo->length);
//SSQ_Clear(pObj);
}
else
{
if (mediatype==MEDIA_TYPE_VIDEO) pObj->pQueHeader->videoframes ++;
//SSQ_TRACE("111Header及部分帧数据位于缓存尾, 部分帧数据位于缓存首.. remain: %d writepos: %d totalsize: %d\n", remain, pObj->pQueHeader->writepos, pObj->pQueHeader->totalsize);
}
}
else if (remain==0)
{
memcpy(pObj->pQueData, pbuf, frameinfo->length);
nAdd = frameinfo->length;
pObj->pQueHeader->writepos = nAdd;
pObj->pQueHeader->totalsize+= sizeof(SS_BUF_T);
pObj->pQueHeader->totalsize+= frameinfo->length;
if (mediatype==MEDIA_TYPE_VIDEO) pObj->pQueHeader->videoframes ++;
if (pObj->pQueHeader->totalsize>pObj->pQueHeader->bufsize)
{
SSQ_TRACE("错误222 %d > %d frameinfo->length:%d...\n", pObj->pQueHeader->totalsize, pObj->pQueHeader->bufsize, frameinfo->length);
//SSQ_Clear(pObj);
}
else
{
SSQ_TRACE("22222Header位于缓存尾,帧数据位于缓存首.. writepos: %d\n", pObj->pQueHeader->writepos);
}
}
else
{
SSQ_TRACE("错误... 剩余空间小于0: %d\n", remain);
ret = -1;
}
}
else if (remain>0)
{
char *tmpbuf = (char *)&bufNode;
unsigned int nAdd = pObj->pQueHeader->writepos;
//记录帧位置
if (mediatype==MEDIA_TYPE_VIDEO) SSQ_AddFrameInfo(pObj, nAdd, frameinfo);
//SSQ_TRACE("ADD DATA...%d\n", nAdd);
memcpy(pObj->pQueData+nAdd, tmpbuf, remain);
//SSQ_TRACE("ADD DATA222...%d\n", sizeof(SS_BUF_T)-remain);
memcpy(pObj->pQueData, tmpbuf+remain, sizeof(SS_BUF_T)-remain);
nAdd = sizeof(SS_BUF_T)-remain;
memcpy(pObj->pQueData+nAdd, pbuf, frameinfo->length);
nAdd += frameinfo->length;
pObj->pQueHeader->writepos = nAdd;
pObj->pQueHeader->totalsize+= sizeof(SS_BUF_T);
pObj->pQueHeader->totalsize+= frameinfo->length;
if (pObj->pQueHeader->totalsize>pObj->pQueHeader->bufsize)
{
SSQ_TRACE("错误333 %d > %d frameinfo->length:%d...\n", pObj->pQueHeader->totalsize, pObj->pQueHeader->bufsize, frameinfo->length);
//SSQ_Clear(pObj);
}
if (mediatype==MEDIA_TYPE_VIDEO) pObj->pQueHeader->videoframes ++;
}
else
{
ret = -1;
SSQ_TRACE("ERROR...\n");
}
}
//else
{
//else if (pObj->pQueHeader->bufsize - pObj->pQueHeader->writepos+pObj->pQueHeader->readpos >= (int)(frameinfo->length+sizeof(SS_BUF_T))) //从尾写到头
//SSQ_TRACE("写尾错误.. 未处理. 写位置:%d / pObj->pQueHeader->bufsize 读位置:%d\n", pObj->pQueHeader->writepos, pObj->pQueHeader->bufsize, pObj->pQueHeader->readpos);
}
//Unlock();
ReleaseMutex(pObj->hMutex);
//SSQ_TRACE("writepos: %d\ttotalsize: %d\n", pObj->pQueHeader->writepos, pObj->pQueHeader->totalsize);
#ifdef _DEBUG1
if (mediatype==MEDIA_TYPE_VIDEO)
{
SSQ_TRACE("==========================\n");
for (int i=0; i<pObj->pQueHeader->maxframeno; i++)
{
SSQ_TRACE("[%d] times: %d pos: %d %02X%02X%02X%02X%02X\n", i, pObj->pQueHeader->pFrameinfoList[i].timestamp, pObj->pQueHeader->pFrameinfoList[i].pos,
(unsigned char)pObj->pQueData[pObj->pQueHeader->pFrameinfoList[i].pos+sizeof(SS_BUF_T)+0], (unsigned char)pObj->pQueData[pObj->pQueHeader->pFrameinfoList[i].pos+sizeof(SS_BUF_T)+1], (unsigned char)pObj->pQueData[pObj->pQueHeader->pFrameinfoList[i].pos+sizeof(SS_BUF_T)+2],
(unsigned char)pObj->pQueData[pObj->pQueHeader->pFrameinfoList[i].pos+sizeof(SS_BUF_T)+3], (unsigned char)pObj->pQueData[pObj->pQueHeader->pFrameinfoList[i].pos+sizeof(SS_BUF_T)+4]);
}
}
#endif
return ret;
}
int SSQ_GetData(SS_QUEUE_OBJ_T *pObj, unsigned int *channelid, unsigned int *mediatype, MEDIA_FRAME_INFO *frameinfo, char *pbuf)
{
int ret = 0;
unsigned int remain = 0;
if (NULL == pObj) return -1;
if (NULL == pObj->pQueHeader) return -1;
if (NULL == pObj->pFrameinfoList) return -1;
WaitForSingleObject(pObj->hMutex, INFINITE); //Lock
if (pObj->pQueHeader->totalsize < 0)
{
SSQ_TRACE("pObj->pQueHeader->totalsize<0: %d\n", pObj->pQueHeader->totalsize);
ReleaseMutex(pObj->hMutex);
return -1;
}
if (pObj->pQueHeader->totalsize <= sizeof(SS_BUF_T))
{
ReleaseMutex(pObj->hMutex);
return -1;
}
//_TRACE("读位置: %d\n", pQueHeader->readpos);
//if (NULL != chid) *chid = m_chid;
//Lock();
#if 0
ret = -1;
for (unsigned int i=0; i<pObj->pQueHeader->maxframeno; i++)
{
if (pObj->pFrameinfoList[i].rtp_timestamp > frameinfo->rtptimestamp)
{
pObj->pQueHeader->readpos = pObj->pFrameinfoList[i].pos;
ret = SSQ_GetDataByPosition(pObj, pObj->pFrameinfoList[i].pos, channelid, mediatype, frameinfo, pbuf);
break;
}
}
#else
if (pObj->pQueHeader->readpos == pObj->pQueHeader->bufsize)
{
SSQ_TRACE("重置读位置[%d / %d]..\n", pObj->pQueHeader->readpos, pObj->pQueHeader->bufsize);
pObj->pQueHeader->readpos = 0;
}
if ((pObj->pQueHeader->readpos + sizeof(SS_BUF_T)) <= pObj->pQueHeader->bufsize)
{
SS_BUF_T *pNode = (SS_BUF_T *)(pObj->pQueData + pObj->pQueHeader->readpos);
//if (pNode->id<1)
if (pNode->flag != BUF_QUE_FLAG)
{
SSQ_TRACE("标志位错误... 缓存视频帧:%d 字节数:%d 清空队列\n", pObj->pQueHeader->videoframes, pObj->pQueHeader->totalsize);
if (pObj->hSSHeader == NULL) //同一个进程内使用
{
SSQ_Clear(pObj);
}
else
{
pObj->pQueHeader->clear_flag = 0x01;
#ifdef _WIN32
while (pObj->pQueHeader->clear_flag!=0x00) {Sleep(1);}
#else
while (pObj->pQueHeader->clear_flag!=0x00) {usleep(100);}
#endif
}
//Unlock();
SSQ_TRACE("111标志位错误... 缓存视频帧:%d 字节数:%d\n", pObj->pQueHeader->videoframes, pObj->pQueHeader->totalsize);
SSQ_TRACE("标志位错误.. 清空队列.. readpos: %d\n", pObj->pQueHeader->readpos);
//pObj->pQueHeader->clear_flag = 0x01;
//_TRACE("标志位错误.. 清空队列完成..\n");
ReleaseMutex(pObj->hMutex);
return -1;
}
if (NULL!=mediatype) *mediatype = pNode->mediatype;
if (NULL != channelid) *channelid = pNode->channelid;
memcpy(frameinfo, &pNode->frameinfo, sizeof(MEDIA_FRAME_INFO));
if ( (pObj->pQueHeader->readpos + pNode->frameinfo.length+sizeof(SS_BUF_T)) <= pObj->pQueHeader->bufsize)
{
//从头到尾读
if (pObj->pQueHeader->totalsize < frameinfo->length+sizeof(SS_BUF_T))
{
//数据量不够
SSQ_TRACE("数据量不够... 总字节数[%d]<帧长[%d]. 读位置:%d\n", pObj->pQueHeader->totalsize, frameinfo->length+sizeof(SS_BUF_T), pObj->pQueHeader->readpos);
ReleaseMutex(pObj->hMutex);
return -1;
}
if (frameinfo->length+sizeof(SS_BUF_T) > pObj->pQueHeader->totalsize)
{
SSQ_TRACE("总字节数[%d]<帧长[%d]. 读位置:%d\n", pObj->pQueHeader->totalsize, frameinfo->length+sizeof(SS_BUF_T), pObj->pQueHeader->readpos);
}
//_TRACE("读位置00000000...: %d / %d\n", pQueHeader->readpos, pQueHeader->size);
pObj->pQueHeader->readpos += sizeof(SS_BUF_T);
unsigned int total1 = pObj->pQueHeader->totalsize;
pObj->pQueHeader->totalsize -= sizeof(SS_BUF_T);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData+pObj->pQueHeader->readpos, frameinfo->length);
//memset(pObj->pQueData+pObj->pQueHeader->readpos, 0x00, frameinfo->length); //clear
pObj->pQueHeader->readpos += frameinfo->length;
unsigned int total2 = pObj->pQueHeader->totalsize;
pObj->pQueHeader->totalsize -= (frameinfo->length);
if (pObj->pQueHeader->readpos == pObj->pQueHeader->bufsize)
{
pObj->pQueHeader->readpos = 0;
}
if (pObj->pQueHeader->readpos > pObj->pQueHeader->bufsize)
{
SSQ_TRACE("读位置错误11111...: %d / %d\n", pObj->pQueHeader->readpos, pObj->pQueHeader->bufsize);
}
if (pObj->pQueHeader->totalsize < 0)
{
SSQ_TRACE("读位置: %d\t写位置: %d 当前帧大小:%d total1:%d\ttotal2:%d\ttotal3:%d\n", pObj->pQueHeader->readpos, pObj->pQueHeader->writepos, frameinfo->length, total1, total2, pObj->pQueHeader->totalsize);
SSQ_TRACE("总字节数错误: %d\n", pObj->pQueHeader->totalsize);
}
}
else
{
if (pObj->pQueHeader->totalsize < (pNode->frameinfo.length+sizeof(SS_BUF_T)))
{
SSQ_TRACE("总字节数<帧长+sizeof(SS_BUF_T)..\n");
//Unlock();
ReleaseMutex(pObj->hMutex);
return -1;
}
remain = pObj->pQueHeader->bufsize-pObj->pQueHeader->readpos;
if (remain>=sizeof(SS_BUF_T))
{
pObj->pQueHeader->readpos += sizeof(SS_BUF_T);
remain = pObj->pQueHeader->bufsize - pObj->pQueHeader->readpos;
if (remain>0)
{
//SSQ_TRACE("111尾有部分数据... 首有部分数据... remain>0: %d\n", remain);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData+pObj->pQueHeader->readpos, remain);
//memset(pObj->pQueData+pObj->pQueHeader->readpos, 0x00, remain); //clear
if (NULL!=pbuf) memcpy(pbuf+remain, pObj->pQueData, frameinfo->length-remain);
//memset(pObj->pQueData, 0x00, frameinfo->length-remain); //clear
pObj->pQueHeader->readpos = frameinfo->length-remain;
pObj->pQueHeader->totalsize -= frameinfo->length;
pObj->pQueHeader->totalsize -= sizeof(SS_BUF_T);
if (pObj->pQueHeader->totalsize < 0)
{
SSQ_TRACE("3333pObj->pQueHeader->totalsize<0: %d\n", pObj->pQueHeader->totalsize);
}
}
else
{
if (remain < 0)
{
SSQ_TRACE("位移错误: 剩余字节数<0:%d\n", remain);
}
else
{
SSQ_TRACE("111尾有部分数据... 首有部分数据... remain<=0: %d\n", remain);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData, frameinfo->length);
//memset(pObj->pQueData, 0x00, frameinfo->length-remain); //clear
pObj->pQueHeader->readpos = frameinfo->length;
pObj->pQueHeader->totalsize -= frameinfo->length;
pObj->pQueHeader->totalsize -= sizeof(SS_BUF_T);
if (pObj->pQueHeader->totalsize < 0)
{
SSQ_TRACE("4444pObj->pQueHeader->totalsize<0: %d\n", pObj->pQueHeader->totalsize);
}
}
}
//SSQ_TRACE("remain > sizeof(SS_BUF_T): %d\t\treadpos: %d\n", remain, pObj->pQueHeader->readpos);
}
else
{
//SSQ_TRACE("remain < sizeof(SS_BUF_T): %d\n", remain);
//执行到此处,说明异常
//remain = pObj->pQueHeader->bufsize - pObj->pQueHeader->readpos;
if (remain>0)
{
SSQ_TRACE("222尾有部分数据... 首有部分数据... remain>0: %d\n", remain);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData+pObj->pQueHeader->readpos, remain);
//memset(pObj->pQueData+pObj->pQueHeader->readpos, 0x00, remain); //clear
if (NULL!=pbuf) memcpy(pbuf+remain, pObj->pQueData, frameinfo->length-remain);
//memset(pObj->pQueData, 0x00, frameinfo->length-remain); //clear
pObj->pQueHeader->readpos = frameinfo->length-remain;
pObj->pQueHeader->totalsize -= frameinfo->length;
if (pObj->pQueHeader->totalsize < 0)
{
SSQ_TRACE("555pObj->pQueHeader->totalsize<0: %d\n", pObj->pQueHeader->totalsize);
}
}
else
{
SSQ_TRACE("222尾有部分数据... 首有部分数据... remain<=0: %d\n", remain);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData, frameinfo->length);
//memset(pObj->pQueData, 0x00, frameinfo->length-remain); //clear
pObj->pQueHeader->readpos = frameinfo->length;
pObj->pQueHeader->totalsize -= frameinfo->length;
if (pObj->pQueHeader->totalsize < 0)
{
SSQ_TRACE("666pObj->pQueHeader->totalsize<0: %d\n", pObj->pQueHeader->totalsize);
}
}
pObj->pQueHeader->totalsize -= sizeof(SS_BUF_T);
if (pObj->pQueHeader->totalsize < 0)
{
SSQ_TRACE("777pObj->pQueHeader->totalsize<0: %d\n", pObj->pQueHeader->totalsize);
}
}
//pObj->pQueHeader->readpos += sizeof(SS_BUF_T);
//pObj->pQueHeader->totalsize -= sizeof(SS_BUF_T);
if (pObj->pQueHeader->readpos>pObj->pQueHeader->bufsize)
{
SSQ_TRACE("读位置错误...: %d / %d\n", pObj->pQueHeader->readpos, pObj->pQueHeader->bufsize);
}
/*
if (NULL!=pbuf) memcpy(pbuf, pShareMemoryBuff+pQueHeader->readpos, remain);
memset(pShareMemoryBuff+pQueHeader->readpos, 0x00, remain); //clear
if (NULL!=pbuf) memcpy(pbuf+remain, pShareMemoryBuff, frameinfo->length-remain);
memset(pShareMemoryBuff, 0x00, frameinfo->length-remain); //clear
pQueHeader->readpos = frameinfo->length-remain;
pQueHeader->totalsize -= (frameinfo->length-remain);
*/
}
if (MEDIA_TYPE_VIDEO==pNode->mediatype) pObj->pQueHeader->videoframes --;
//memset(pNode, 0x00, sizeof(SS_BUF_T));
}
else
{
unsigned int remain = pObj->pQueHeader->bufsize-pObj->pQueHeader->readpos;
SS_BUF_T bufnode;
char *pp = (char *)&bufnode;
memset(&bufnode, 0x00, sizeof(SS_BUF_T));
//SSQ_TRACE("GET DATA...\n");
//SSQ_TRACE("1 REMAIN: %d\n", remain);
if (remain>0)
{
memcpy(pp, pObj->pQueData+pObj->pQueHeader->readpos, remain);
//memset(pObj->pQueData+pObj->pQueHeader->readpos, 0x00, remain); //clear
//SSQ_TRACE("2 read: %d\n", sizeof(SS_BUF_T)-remain);
memcpy(pp+remain, pObj->pQueData, sizeof(SS_BUF_T)-remain);
//memset(pObj->pQueData, 0x00, sizeof(SS_BUF_T)-remain); //clear
memcpy(frameinfo, &bufnode.frameinfo, sizeof(MEDIA_FRAME_INFO));
if (NULL != channelid) *channelid = bufnode.channelid;
//if (bufnode.id<1)
if (bufnode.flag != BUF_QUE_FLAG)
{
//Unlock();
SSQ_Clear(pObj);
ReleaseMutex(pObj->hMutex);
SSQ_TRACE("SSQ_标志符错误...\n");
return -1;
}
pObj->pQueHeader->readpos = sizeof(SS_BUF_T)-remain;
if (NULL!=mediatype) *mediatype = bufnode.mediatype;
//SSQ_TRACE("3 frame length: %d\n", bufnode.frameinfo.length);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData+pObj->pQueHeader->readpos, bufnode.frameinfo.length);
//memset(pObj->pQueData+pObj->pQueHeader->readpos, 0x00, bufnode.frameinfo.length); //clear
pObj->pQueHeader->readpos += bufnode.frameinfo.length;
pObj->pQueHeader->totalsize -= sizeof(SS_BUF_T);
//pObj->pQueHeader->totalsize -= (frameinfo->length-remain);
pObj->pQueHeader->totalsize -= (frameinfo->length);//20140521
if (pObj->pQueHeader->totalsize < 0)
{
SSQ_TRACE("888pObj->pQueHeader->totalsize<0: %d\n", pObj->pQueHeader->totalsize);
}
//SSQ_TRACE("GET DATA OK..\n");
if (MEDIA_TYPE_VIDEO==bufnode.mediatype) pObj->pQueHeader->videoframes --;
}
else
{
SSQ_TRACE("异常... REMAIN <= 0....\n");
}
ret = 1000;
}
#endif
//Unlock();
ReleaseMutex(pObj->hMutex);
return ret;
}
//===========================================
//根据位置获取对应的帧数据
int SSQ_GetDataByPosition(SS_QUEUE_OBJ_T *pObj, unsigned int position, unsigned int clearflag, unsigned int *channelid, unsigned int *mediatype, MEDIA_FRAME_INFO *frameinfo, char *pbuf)
{
int ret = 0;
unsigned int remain = 0;
if (NULL == pObj) return -1;
if (NULL == pObj->pQueHeader) return -1;
if (NULL == pObj->pFrameinfoList) return -1;
unsigned int *pOffset = (unsigned int *)&position;
unsigned int totalsize = pObj->pQueHeader->totalsize;
unsigned int *pTotalSize = (unsigned int*)&totalsize;
if (clearflag == 0x01)
{
pOffset = (unsigned int*)&pObj->pQueHeader->readpos;
pTotalSize = (unsigned int*)&pObj->pQueHeader->totalsize;
}
WaitForSingleObject(pObj->hMutex, INFINITE); //Lock
if (*pOffset == pObj->pQueHeader->bufsize)
{
SSQ_TRACE("重置读位置[%d / %d]..\n", *pOffset, pObj->pQueHeader->bufsize);
*pOffset = 0;
}
if (clearflag==0x01)
{
if (pObj->pQueHeader->totalsize <= sizeof(SS_BUF_T))
{
ReleaseMutex(pObj->hMutex);
return -1;
}
if (pObj->pQueHeader->readpos == pObj->pQueHeader->bufsize)
{
pObj->pQueHeader->readpos = 0;
}
}
if ((*pOffset + sizeof(SS_BUF_T)) <= pObj->pQueHeader->bufsize)
{
SS_BUF_T *pNode = (SS_BUF_T *)(pObj->pQueData + *pOffset);
if (pNode->flag != BUF_QUE_FLAG)
{
SSQ_TRACE("[SSQ_GetDataByPosition]标志位错误...\n");
if (clearflag == 0x01)
{
if (pObj->hSSHeader==NULL) //同一进程
{
SSQ_Clear(pObj);
}
else
{
pObj->pQueHeader->clear_flag = 0x01;
while (pObj->pQueHeader->clear_flag!=0x00) {Sleep(1);}
}
}
ReleaseMutex(pObj->hMutex);
return -1;
}
if (NULL!=mediatype) *mediatype = pNode->mediatype;
if (NULL != channelid) *channelid = pNode->channelid;
memcpy(frameinfo, &pNode->frameinfo, sizeof(MEDIA_FRAME_INFO));
if ( (*pOffset + pNode->frameinfo.length+sizeof(SS_BUF_T)) <= pObj->pQueHeader->bufsize)
{
//从头到尾读
*pOffset += sizeof(SS_BUF_T);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData+*pOffset, frameinfo->length);
*pOffset += frameinfo->length;
*pTotalSize -= (frameinfo->length+sizeof(SS_BUF_T));
if (*pOffset == pObj->pQueHeader->bufsize)
{
*pOffset = 0;
}
if (*pOffset > pObj->pQueHeader->bufsize)
{
SSQ_TRACE("[SSQ_GetDataByPosition]读位置错误11111...: %d / %d\n", *pOffset, pObj->pQueHeader->bufsize);
}
}
else
{
remain = pObj->pQueHeader->bufsize - *pOffset;
if (remain>=sizeof(SS_BUF_T))
{
*pOffset += sizeof(SS_BUF_T);
remain = pObj->pQueHeader->bufsize - *pOffset;
if (remain>0)
{
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData + *pOffset, remain);
if (NULL!=pbuf) memcpy(pbuf+remain, pObj->pQueData, frameinfo->length-remain);
*pOffset = frameinfo->length-remain;
*pTotalSize -= (frameinfo->length+sizeof(SS_BUF_T));
}
else
{
if (remain < 0)
{
SSQ_TRACE("[SSQ_GetDataByPosition]位移错误: 剩余字节数<0:%d\n", remain);
}
else
{
SSQ_TRACE("[SSQ_GetDataByPosition]尾有部分数据... 首有部分数据... remain<=0: %d\n", remain);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData, frameinfo->length);
//memset(pObj->pQueData, 0x00, frameinfo->length-remain); //clear
*pOffset = frameinfo->length;
*pTotalSize -= (frameinfo->length+sizeof(SS_BUF_T));
}
}
}
else
{
//SSQ_TRACE("remain < sizeof(SS_BUF_T): %d\n", remain);
//执行到此处,说明异常
SSQ_TRACE("[SSQ_GetDataByPosition] 异常 #########... remain>0: %d\n", remain);
if (remain>0)
{
SSQ_TRACE("[SSQ_GetDataByPosition]222尾有部分数据... 首有部分数据... remain>0: %d\n", remain);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData + *pOffset, remain);
if (NULL!=pbuf) memcpy(pbuf+remain, pObj->pQueData, frameinfo->length-remain);
*pOffset = frameinfo->length-remain;
*pTotalSize -= (frameinfo->length);
}
else
{
SSQ_TRACE("[SSQ_GetDataByPosition]222尾有部分数据... 首有部分数据... remain<=0: %d\n", remain);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData, frameinfo->length);
//memset(pObj->pQueData, 0x00, frameinfo->length-remain); //clear
*pOffset = frameinfo->length;
}
}
if (*pOffset > pObj->pQueHeader->bufsize)
{
SSQ_TRACE("[SSQ_GetDataByPosition]读位置错误...: %d / %d\n", *pOffset, pObj->pQueHeader->bufsize);
}
}
if (MEDIA_TYPE_VIDEO==pNode->mediatype && clearflag==0x01) pObj->pQueHeader->videoframes --;
}
else
{
unsigned int remain = pObj->pQueHeader->bufsize - *pOffset;
SS_BUF_T bufnode;
char *pp = (char *)&bufnode;
memset(&bufnode, 0x00, sizeof(SS_BUF_T));
//SSQ_TRACE("GET DATA...\n");
SSQ_TRACE("[SSQ_GetDataByPosition]1 REMAIN: %d\n", remain);
if (remain>0)
{
memcpy(pp, pObj->pQueData + *pOffset, remain);
SSQ_TRACE("[SSQ_GetDataByPosition]2 read: %d\n", sizeof(SS_BUF_T)-remain);
memcpy(pp+remain, pObj->pQueData, sizeof(SS_BUF_T)-remain);
memcpy(frameinfo, &bufnode.frameinfo, sizeof(MEDIA_FRAME_INFO));
if (NULL != channelid) *channelid = bufnode.channelid;
if (bufnode.flag != BUF_QUE_FLAG)
{
//Unlock();
SSQ_Clear(pObj);
ReleaseMutex(pObj->hMutex);
SSQ_TRACE("[SSQ_GetDataByPosition]SSQ_标志符错误...\n");
return -1;
}
*pOffset = sizeof(SS_BUF_T)-remain;
if (NULL!=mediatype) *mediatype = bufnode.mediatype;
SSQ_TRACE("[SSQ_GetDataByPosition]3 frame length: %d\n", bufnode.frameinfo.length);
if (NULL!=pbuf) memcpy(pbuf, pObj->pQueData + *pOffset, bufnode.frameinfo.length);
*pOffset += bufnode.frameinfo.length;
*pTotalSize -= (frameinfo->length+sizeof(SS_BUF_T));
SSQ_TRACE("[SSQ_GetDataByPosition]GET DATA OK..\n");
if (MEDIA_TYPE_VIDEO==bufnode.mediatype && clearflag==0x01) pObj->pQueHeader->videoframes --;
}
else
{
SSQ_TRACE("[SSQ_GetDataByPosition]异常... REMAIN <= 0....\n");
}
ret = 1000;
}
//Unlock();
ReleaseMutex(pObj->hMutex);
return ret;
}
int SSQ_TRACE(char* szFormat, ...)
{
#ifdef _DEBUG
char buff[1024] = {0,};
wchar_t wszbuff[1024] = {0,};
va_list args;
va_start(args,szFormat);
_vsnprintf(buff, 1023, szFormat,args);
va_end(args);
MByteToWChar(buff, wszbuff, sizeof(wszbuff)/sizeof(wszbuff[0]));
#ifdef _WIN32
OutputDebugString(wszbuff);
#endif
printf("TRACE: %s", buff);
#endif
return 0;
}
具体代码实现及调用参考:https://github.com/EasyDarwin/EasyPlayer/blob/master/win32/libEasyPlayer/ssqueue.cpp
EasyPlayer开源RTSP流媒体播放器项目:https://github.com/EasyDarwin/EasyPlayer
获取更多信息
Copyright © EasyDarwin.org 2012-2016
EasyPusher直播推送中用到的缓冲区设计和丢帧原理的更多相关文章
- EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器
在不断进行EasyDarwin开源流媒体服务器的功能和性能完善的同时,我们也配套实现了目前在安防和移动互联网行业比较火热的移动端手机直播方案,主要就是我们的 EasyPusher直播推送项目 和 Ea ...
- 基于EasyDarwin EasyPusher实现Android手机直播推送功能
EasyPusher直播推送在之前就已经稳定支持了Windows.Linux.ARM上的RTSP直播推送功能,配合EasyDarwin开源流媒体服务器,延时基本在1s以内,这个技术方案经过一年多时间, ...
- EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体server,EasyPlayer手机播放器
在不断进行EasyDarwin开源流媒体server的功能和性能完好的同一时候,我们也配套实现了眼下在安防和移动互联网行业比較火热的移动端手机直播方案,主要就是我们的 EasyPusher直播推送项目 ...
- EasyRTMP实现RTMP异步直播推送之环形缓冲区设计
本文转自EasyDarwin团队kim的博客:http://blog.csdn.net/jinlong0603 EasyRTMP的推送缓冲区设计 EasyRTMP内部也同样采用的环形缓冲的设计方法,将 ...
- EasyPusher安卓Android手机直播推送之RTSP流媒体协议流程
EasyPusher移动端推送同我们平时用的RTSP直播推送流程一样,都是采用标准RTSP/RTP推送流程:ANNOUNCE->SETUP->PLAY->RTP/RTCP->T ...
- EasyPusher安卓Android手机直播推送之MediaCodec 硬编码H264格式
本文转自Holo的博客:http://blog.csdn.net/u013758734/article/details/50834770 最近在研究EasyDarwin的Push库EasyPusher ...
- EasyPusher进行Android UVC外接摄像头直播推送实现方法
最近EasyPusher针对UVC摄像头做了适配.我们结合了UVCCamera与EasyPusher,支持将UVC摄像头的视频推送到RTSP服务器上.在此特别感谢UVCCamera这个牛逼的项目! 来 ...
- EasyPusher手机直播推送是如何实现后台直播推送的
本文由EasyDarwin开源团队成员John提供:http://blog.csdn.net/jyt0551/article/details/52276062 EasyPusher Android是使 ...
- EasyPusher:基于live555的DarwinInjector实现的RTSP直播推送程序
先简单介绍一下EasyPusher的功能,后面再对具体内部架构做介绍: EasyPusher:https://github.com/EasyDarwin/EasyPusher EasyPusher是什 ...
随机推荐
- 【CF675E】Trains and Statistic(贪心,DP,线段树优化)
题意:a[i]表示从第i个车站可以一张票到第[i+1,a[i]]这些车站;p[i][j]表示从第i个车站到第j个车站的最少的票数,现在要求∑dp[i][j](1<=i<=n,i<j& ...
- 【CF645D】 Robot Rapping Results Report(拓扑排序,二分)
题意:有一张N点M边的有向图,求最小的K使根据前K条边就能够确定图是否有唯一的拓扑序, 若没有唯一拓扑序输出-1 思路:二分答案再拓扑排序,以入度为0的节点作为新的一层,若某一层的节点个数<&g ...
- HOJ - 2543最小费用流
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2543 这个题目挺有意思. 自己扣了一会儿,发现图挺好建,就把(u,v,f,w) 拆成(u,v,f,0) ...
- WinRT 开发:在 MVVM 模式中,关于绑定的几处技巧
以下会提到三个绑定的技巧,分别是 在 ListView 中为 ListViewItem 的 MenuFlyout 绑定 Command: 在 ListView 的 事件中绑定所选择项目,即其 Sele ...
- 2017 CCPC 哈尔滨站 题解
题目链接 2017 CCPC Harbin Problem A Problem B Problem D Problem F Problem L 考虑二分答案. 设当前待验证的答案为x 我们可以把第二 ...
- 洛谷——P2527 [SHOI2001]Panda的烦恼
P2527 [SHOI2001]Panda的烦恼 题目描述 panda是个数学怪人,他非常喜欢研究跟别人相反的事情.最近他正在研究筛法,众所周知,对一个范围内的整数,经过筛法处理以后,剩下的全部都 ...
- DNS重绑定DNS Rebinding攻击
DNS重绑定DNS Rebinding攻击 在网页浏览过程中,用户在地址栏中输入包含域名的网址.浏览器通过DNS服务器将域名解析为IP地址,然后向对应的IP地址请求资源,最后展现给用户.而对于域名所有 ...
- uitableview中文排序问题
1,uitableview中涉及到排序的问题,查找资料后发现使用UILocalizedIndexedCollation可以很好处理中文和英文系统下中文的排序.而且如果第一个汉字首字母一样那么就会按照第 ...
- Android减少布局层次--有关Activity根视图DecorView的思考
1 Android应用图层 一直觉得有关DecorView还是有些问题没有搞清楚,今天在看了一点有关SurfaceFlinger的内容以后,顿时突发奇想,想到之前的问题,之前的思考是: 虽然可以将De ...
- 为了安全,linux下如何使用某个用户启动某个进程?
安全里有个原则,叫最小权限原则 根据这个原则,对于启动某个应用或者进程,应该赋予其最小权限,根据应用权限要求,创建一个相应权限的用户,赋予其应用相应的权限,然后使用这个用户启用这个应用 如何使用某个用 ...