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

应用背景:

        此循环缓冲区用于缓冲实时流媒体数据,以不定长度的数据块为存取单位,符合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;


#define		IMG263_BUFFER_SIZE			1024 * 300	//1M. images buffer size
#define		MAXCOUNT_FRAMES_IN_BUFFER	15

struct image263InBufferInfoUnit {
	UINT	data_head;		//image's beginning position in the buffer  
	UINT	data_size;		//ending position in buffer of one image
	BOOL	isBlockCrossBorder;
	UINT32	timestamp;	
	//Maybe some new items will be added here later, depends on experiment result
};


class CImageBufferClass
{
public:	//methods
	CImageBufferClass();	//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 _DEBUG
	BYTE img263Buffer[IMG263_BUFFER_SIZE];	
#else
	BYTE* img263Buffer;	//buffer for storing received images
#endif

	BOOL isBufferBorderCrossed;
	UINT img263BufferData_head;		//beginning of image data in buffer
	UINT freespace_head;			//end of image data in buffer
	queue<image263InBufferInfoUnit *> imageInfoUnitQueue;
	INT activeInfoUnitCount;
	HANDLE hMutex;		//Mutex for buffer reading and writing syc


private:	//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();

};


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值