#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "ringQueue.h"
ringQueue *rQueue_init(uint32_t max_qLen, uint32_t max_dLen)
{
size_t n = sizeof(ringQueue) + sizeof(rQueue_data) * max_qLen;
ringQueue *q = (ringQueue *)malloc(n);
if (q == NULL)
{
printf("%d: rQueue_init err, malloc fail\n", __LINE__);
return NULL;
}
memset(q, 0, n);
for (uint32_t i = 0; i < max_qLen; i++)
{
q->data[i].buffer = malloc(max_dLen);
if (q->data[i].buffer == NULL)
{
printf("%d: rQueue_init err, malloc fail\n", __LINE__);
// 释放前面已经申请的内存
for (uint32_t j = 0; j < i; j++)
{
free(q->data[j].buffer);
}
free(q);
return NULL;
}
memset(q->data[i].buffer, 0, max_dLen);
q->data[i].len = 0;
}
q->max_queueLen = max_qLen;
q->max_dataLen = max_dLen;
pthread_mutex_init(&q->mutex, NULL);
return q;
}
int rQueue_en(ringQueue *q, const rQueue_data *e)
{
int ret = -1;
pthread_mutex_lock(&q->mutex);
if (q == NULL || e == NULL || e->buffer == NULL)
{
goto end;
}
if ((q->rear + 1) % q->max_queueLen == q->front) // 队列满的判断
{
printf("queue is full\n");
goto end;
}
if (e->len > q->max_dataLen)
{
printf("rQueue_en err, e->len is too large, e->len: %d byte, max: %d byte\n",
e->len, q->max_dataLen);
goto end;
}
memcpy(q->data[q->rear].buffer, e->buffer, e->len);
q->data[q->rear].len = e->len;
q->rear = (q->rear + 1) % q->max_queueLen; // rear 指针向后移一位置,若到最后则转到数组头部
ret = 0;
end:
pthread_mutex_unlock(&q->mutex);
return ret;
}
// 出队
int rQueue_de(ringQueue *q, rQueue_data *e)
{
int ret = -1;
pthread_mutex_lock(&q->mutex);
if (q == NULL || e == NULL || e->buffer == NULL)
{
goto end;
}
if (q->front == q->rear) // 队列空的判断
{
printf("queue is empty\n");
goto end;
}
if (e->len < q->data[q->front].len)
{
printf("rQueue_de err, e->len is too small, e->len: %d byte, min: %d byte\n",
e->len, q->data[q->front].len);
goto end;
}
memcpy(e->buffer, q->data[q->front].buffer, q->data[q->front].len);
e->len = q->data[q->front].len;
memset(q->data[q->front].buffer, 0, q->max_dataLen);
q->data[q->front].len = 0;
q->front = (q->front + 1) % q->max_queueLen; // front 指针向后移一位置,若到最后则转到数组头部
ret = 0;
end:
if (ret == -1)
{
e->len = 0;
}
pthread_mutex_unlock(&q->mutex);
return ret;
}
int rQueue_length(ringQueue *q)
{
pthread_mutex_lock(&q->mutex);
if (q == NULL)
{
pthread_mutex_unlock(&q->mutex);
return -1;
}
int len = (q->rear - q->front + q->max_queueLen) % q->max_queueLen;
pthread_mutex_unlock(&q->mutex);
return len;
}
bool rQueue_isEmpty(ringQueue *q)
{
bool ret = false;
pthread_mutex_lock(&q->mutex);
if (q == NULL)
{
printf("rQueue_isEmpty err, q is NULL\n");
pthread_mutex_unlock(&q->mutex);
return true;
}
if (q->front == q->rear)
{
ret = true;
}
pthread_mutex_unlock(&q->mutex);
return ret;
}
int rQueue_clear(ringQueue *q)
{
pthread_mutex_lock(&q->mutex);
if (q == NULL)
{
pthread_mutex_unlock(&q->mutex);
return -1;
}
for (uint32_t i = 0; i < q->max_queueLen; i++)
{
memset(q->data[i].buffer, 0, q->max_dataLen);
q->data[i].len = 0;
}
q->front = 0;
q->rear = 0;
pthread_mutex_unlock(&q->mutex);
return 0;
}
void rQueue_destroy(ringQueue **q)
{
if (q == NULL)
{
return;
}
for (uint32_t i = 0; i < (*q)->max_queueLen; i++)
{
free((*q)->data[i].buffer);
}
pthread_mutex_destroy(&((*q)->mutex));
free(*q);
*q = NULL;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
rtmp_aac_h264.7z (33个子文件)
rtmp_aac_h264
rtmp_aac_h264.PR 3KB
lib
librtmp.a 153KB
librtmp.so.0 123KB
librtmp.so 123KB
h264aac_parse.o 211KB
rtmp_pushStream.h 2KB
rtmp_aac_h264.IMB 8KB
rtmp_aac_h264.IMD 408B
rtmp_aac_h264.IAB 28KB
Makefile 1KB
rtmp_aac_h264.PS 68KB
rtmp_aac_h264.PRI 16KB
test3.h264 1.55MB
main.cpp 5KB
rtmp_aac_h264.IAD 608B
ringQueue.h 2KB
h264aac_parse.cpp 8KB
rtmp.h 9KB
test.aac 160KB
rtmp_pushStream.o 233KB
test.h264 2.16MB
rtmp_aac_h264.PFI 32B
lib_ubutun
librtmp.a 153KB
librtmp.so.0 123KB
librtmp.so 123KB
main.o 300KB
rtmp_aac_h264.PO 776B
rtmp_pushStream.cpp 15KB
test 350KB
ringQueue.o 24KB
h264aac_parse.h 2KB
ringQueue.c 4KB
librtmp.so 115KB
共 33 条
- 1
资源评论
u010301391
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功