RingBuffer.cpp
资源名称:VC++视频传输.rar [点击查看]
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:2k
源码类别:
VC书籍
开发平台:
Visual C++
- #include "RingBuffer.h"
- MediaRingBuffer::MediaRingBuffer()
- {
- this->ring = (char *) new char[RING_SIZE];
- this->read_pos = 0;
- this->write_pos = 0;
- }
- MediaRingBuffer::~MediaRingBuffer()
- {
- free(this->ring);
- }
- void MediaRingBuffer::Init()
- {
- this->read_pos = 0;
- this->write_pos = 0;
- }
- void MediaRingBuffer::Read(char *outData, unsigned int size)
- {
- if(write_pos <= read_pos) {
- if(read_pos + size < RING_SIZE) {
- memcpy(outData, ring + read_pos, size);
- read_pos += size;
- }
- else {
- if(read_pos + size < RING_SIZE + write_pos) {
- unsigned int before, after;
- before = (RING_SIZE - 1) - read_pos;
- after = size - before;
- memcpy(outData, ring + read_pos, before);
- memcpy(outData + before, ring, after);
- read_pos = after;
- }
- else {
- }
- }
- }
- else {
- if(read_pos + size <= write_pos) {
- memcpy(outData, ring + read_pos, size);
- read_pos += size;
- }
- else {
- }
- }
- }
- void MediaRingBuffer::Write(char *inData, unsigned int size)
- {
- if(write_pos >= read_pos) {
- if(write_pos + size < RING_SIZE) {
- memcpy(ring + write_pos, inData, size);
- write_pos += size;
- }
- else {
- if(write_pos + size < RING_SIZE + read_pos) {
- unsigned int before, after;
- before = (RING_SIZE - 1) - write_pos;
- after = size - before;
- memcpy(ring + write_pos, inData, before);
- memcpy(ring, inData + before, after);
- write_pos = after;
- }
- }
- }
- else {
- if(write_pos + size <= read_pos) {
- memcpy(ring + write_pos, inData, size);
- write_pos += size;
- }
- }
- }
- int MediaRingBuffer::IsFullFor(unsigned int size)
- {
- if(write_pos == read_pos)
- return 0;
- if(write_pos > read_pos) {
- if(write_pos + size < read_pos + RING_SIZE)
- return 0;
- return 1;
- }
- else {
- if(write_pos + size < read_pos)
- return 0;
- return 1;
- }
- }