-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathInputContext.cpp
More file actions
161 lines (118 loc) · 2.98 KB
/
InputContext.cpp
File metadata and controls
161 lines (118 loc) · 2.98 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "InputContext.h"
#include <cassert>
/////////////////////////////////////////////////////////////////////////////////
////
int
g_read(void * opaque, unsigned char * buf, int buf_size)
{
InputContext * inst = static_cast<InputContext *>(opaque);
return inst->read(buf, buf_size);
}
InputContext::InputContext()
{
pthread_mutex_init(&m_mutex, NULL);
pthread_cond_init(&m_cond, NULL);
m_ctx = avio_alloc_context((unsigned char *)m_buffer,
AVIO_BUFFER_SIZE,
0,
this,
&g_read,
NULL,
NULL);
}
InputContext::~InputContext()
{
clean();
}
void
InputContext::clean()
{
prepareQuit();
pthread_mutex_lock(&m_mutex);
if (m_ctx)
{
/* note: the internal buffer could have changed internally by ffmpeg. */
::av_freep(&m_ctx->buffer);
m_buffer = NULL;
::av_freep(m_ctx);
m_ctx = NULL;
}
while (!m_buffers.empty())
{
VideoBuf * buf = m_buffers.front();
m_buffers.pop_front();
delete[] (unsigned char *)buf;
}
pthread_mutex_unlock(&m_mutex);
pthread_cond_destroy(&m_cond);
pthread_mutex_destroy(&m_mutex);
}
void
InputContext::prepareQuit()
{
IOContext::prepareQuit();
pthread_cond_signal(&m_cond);
}
bool
InputContext::addbuffer(unsigned char * buf, int buf_size, unsigned long ts)
{
VideoBuf * vbuf = NULL;
if (!m_buffer || !m_ctx || m_exit)
{
return false;
}
if (buf_size > 0)
{
unsigned char * mem = new unsigned char[sizeof(VideoBuf) + buf_size - 1];
assert(mem);
if (!mem)
{
return false;
}
vbuf = (VideoBuf *)mem;
vbuf->pos = 0;
vbuf->ts = ts;
vbuf->len = buf_size;
memcpy(vbuf->data, buf, buf_size);
pthread_mutex_lock(&m_mutex);
m_buffers.push_back(vbuf);
pthread_cond_signal(&m_cond);
pthread_mutex_unlock(&m_mutex);
}
return true;
}
int
InputContext::read(unsigned char * buf, int buf_size)
{
int readlen = 0;
if (m_exit)
{
return -1;
}
pthread_mutex_lock(&m_mutex);
while (readlen < buf_size)
{
if (m_buffers.empty())
{
pthread_cond_wait(&m_cond, &m_mutex);
}
if (m_exit)
{
break;
}
VideoBuf * vbuf = m_buffers.front();
int cpylen = FFMIN(vbuf->len - vbuf->pos, buf_size - readlen);
memcpy(buf + readlen, &vbuf->data[vbuf->pos], cpylen);
readlen += cpylen;
vbuf->pos += cpylen;
if (vbuf->pos == vbuf->len)
{
m_buffers.pop_front();
delete[] (unsigned char *)vbuf;
}
}
pthread_mutex_unlock(&m_mutex);
return readlen;
}
/////////////////////////////////////////////////////////////////////////////////
////