一种循环缓冲区的VC++实现

来源:互联网 发布:淘宝网男背心 编辑:程序博客网 时间:2024/05/16 08:50

应用背景:

        此循环缓冲区用于缓冲实时流媒体数据,以不定长度的数据块为存取单位,符合FIFO规则。

 特征:

1、封装成了一个类,便于代码重用;

2、采用Mutex作为读取同步机制;

3、可设置缓冲区内的最多块的数量;

 

 源代码在这里下载:

http://download.csdn.net/detail/qwertyuj/3865070

 下面是类的定义:

/**********************************************************// Name: imgBufferOprations.h// Description: define a CImageBufferClass class// Wrote by: Jianxin, Isoftstone corp.// Version: v0.2//---------------------------------------------------------// This class **********************************************************/#include <Windows.h>#include <queue>#include <assert.h>#include <iostream>using namespace std;#defineIMG263_BUFFER_SIZE1024 * 300//1M. images buffer size#defineMAXCOUNT_FRAMES_IN_BUFFER15struct image263InBufferInfoUnit {UINTdata_head;//image's beginning position in the buffer  UINTdata_size;//ending position in buffer of one imageBOOLisBlockCrossBorder;UINT32timestamp;//Maybe some new items will be added here later, depends on experiment result};class CImageBufferClass{public://methodsCImageBufferClass();//constructor~CImageBufferClass();//destructor//-----------------------------------------------// Return next image in the buffer//-----------------------------------------------BOOL getNextImageInBuffer(BYTE* dataRead, UINT* dataSizeRead, UINT32* timestamp);//-----------------------------------------------// put an image into the buffer//-----------------------------------------------BOOL putOneImageIntoBuffer(BYTE* dataTobeWrote, UINT dataSizeTobeWrote, UINT32 timestamp);//-----------------------------------------------// Return the size of free space in the buffer//-----------------------------------------------UINT getFreeSpaceSizeInBuffer();//-----------------------------------------------// //-----------------------------------------------UINT getFrameCountInBuffer();//-----------------------------------------------// //-----------------------------------------------INT getActiveInfoUnitCount();//-----------------------------------------------// //-----------------------------------------------BOOL isImgQueueEmpty();private://variables#if _DEBUGBYTE img263Buffer[IMG263_BUFFER_SIZE];#elseBYTE* img263Buffer;//buffer for storing received images#endifBOOL isBufferBorderCrossed;UINT img263BufferData_head;//beginning of image data in bufferUINT freespace_head;//end of image data in bufferqueue<image263InBufferInfoUnit *> imageInfoUnitQueue;INT activeInfoUnitCount;HANDLE hMutex;//Mutex for buffer reading and writing sycprivate://methods//--------------------------------------------------------// Write image data to buffer//--------------------------------------------------------BOOL writeImageIntoImgBuffer(image263InBufferInfoUnit *imageToWrite, BYTE* dataTobeWrote);//---------------------------------------------------------// Read image data from buffer. //---------------------------------------------------------BOOL readImageFromImgBuffer(image263InBufferInfoUnit *imageBeRead,BYTE* dataRead, UINT* dataSizeRead);//-----------------------------------------------// Initialization//-----------------------------------------------void Initialization();//-----------------------------------------------// Check if the buffer in a good condition//-----------------------------------------------BOOL checkBufferIfNormal();//-----------------------------------------------// Delete oldest image in the buffer//-----------------------------------------------// If the buffer is full before writing a new image,// we should delete the oldest images in the buffer.// run this routine once, results in the oldest image// be deleted.//-----------------------------------------------void deleteOldestImageInBuffer(image263InBufferInfoUnit * imageToDeleteInfo);//-----------------------------------------------// See if next image to be write into buffer // need store it cross the border//-----------------------------------------------BOOL doesNeedCrossBorder(UINT dataSizeTobeWrote);//-----------------------------------------------// //-----------------------------------------------BOOL isImgBufferEmpty();};