forked from EasyDarwin/EasyAACEncoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEasyDSSBuffers.cpp
More file actions
113 lines (103 loc) · 2.92 KB
/
Copy pathEasyDSSBuffers.cpp
File metadata and controls
113 lines (103 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
Copyright (c) 2013-2015 EasyDarwin.ORG. All rights reserved.
Github: https://github.com/EasyDarwin
WEChat: EasyDarwin
Website: http://www.easydarwin.org
*/
#include "EasyDSSBuffers.h"
int init_buffers(buffers_t * bufs, int bufsize, int bufnum)
{
int i;
bufs->rear = 0;
bufs->front = 0;
if(bufnum > MAX_BUF_NUM)
{
bufs->bufnum = MAX_BUF_NUM;
}
else
{
bufs->bufnum = bufnum;
}
for(i = 0; i < bufs->bufnum; i++)
{
bufs->buf[i].length = 0;
bufs->buf[i].start = (char *) calloc(1, bufsize);
bufs->buf[i].frame_type = -1;
bufs->buf[i].channel = -1;
bufs->buf[i].frame_index = 0;
if(bufs->buf[i].start == NULL)
return -1;
}
#ifdef WIN32
InitializeCriticalSection(&(bufs->cs));
#else
InitializeCriticalSection(&(bufs->cs), NULL);
#endif
return 0;
}
int free_buffers(buffers_t* bufs)
{
for(int i = 0; i < bufs->bufnum; i++)
{
if(bufs->buf[i].start != NULL)
free(bufs->buf[i].start);
}
bufs->pOnVideoData = NULL;
DeleteCriticalSection(&(bufs->cs));
return 0;
}
int buffers_get_data(void *data, unsigned int *length, buffers_t * bufs, int *type,
int *channel, int *frame_index)
{
int res = -1;
EnterCriticalSection(&(bufs->cs));
if(bufs->front != bufs->rear)
{
res = *length = bufs->buf[bufs->front].length;
memcpy(data, bufs->buf[bufs->front].start, *length);
*type = bufs->buf[bufs->front].frame_type;
*channel = bufs->buf[bufs->front].channel;
*frame_index = bufs->buf[bufs->front].frame_index;
bufs->front = (bufs->front + 1) % bufs->bufnum;
//res = 1;
}
LeaveCriticalSection(&(bufs->cs));
return res;
}
int buffers_put_data(void *data, unsigned int length, buffers_t * bufs, int type,
int channel, int frame_index)
{
int res = 0;
EnterCriticalSection(&(bufs->cs));
if(((bufs->rear + 1) % bufs->bufnum) == bufs->front)
{
res = -1;
}
else
{
bufs->buf[bufs->rear].length = length;
bufs->buf[bufs->rear].frame_type = type;
bufs->buf[bufs->rear].channel = channel;
bufs->buf[bufs->rear].frame_index = frame_index;
if(length < iBufLen)
{
memcpy(bufs->buf[bufs->rear].start, data, length);
}
else
{
//WriteSystemLog("DataRecv.log", "Frame is too large");
printf("Frame is too large, length=%d\r\n", length);
}
bufs->rear = (bufs->rear + 1) % bufs->bufnum;
res = 1;
}
LeaveCriticalSection(&(bufs->cs));
return res;
}
void buffers_clear_data(buffers_t * bufs)
{
EnterCriticalSection(&(bufs->cs));
bufs->rear = 0;
bufs->front = 0;
LeaveCriticalSection(&(bufs->cs));
}