forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess.c
288 lines (247 loc) · 7.77 KB
/
access.c
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*****************************************************************************
* access.c : V4L2 compressed byte stream input module for vlc
*****************************************************************************
* Copyright (C) 2002-2011 VLC authors and VideoLAN
*
* Authors: Benjamin Pracht <bigben at videolan dot org>
* Richard Hosking <richard at hovis dot net>
* Antoine Cellerier <dionoea at videolan d.t org>
* Dennis Lou <dlou99 at yahoo dot com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
#include <errno.h>
#include <poll.h>
#include <vlc_common.h>
#include <vlc_access.h>
#include "v4l2.h"
struct access_sys_t
{
int fd;
uint32_t block_flags;
union
{
uint32_t bufc;
uint32_t blocksize;
};
struct buffer_t *bufv;
vlc_v4l2_ctrl_t *controls;
};
static block_t *MMapBlock (access_t *);
static block_t *ReadBlock (access_t *);
static int AccessControl( access_t *, int, va_list );
static int InitVideo(access_t *, int, uint32_t);
int AccessOpen( vlc_object_t *obj )
{
access_t *access = (access_t *)obj;
access_InitFields( access );
access_sys_t *sys = calloc (1, sizeof (*sys));
if( unlikely(sys == NULL) )
return VLC_ENOMEM;
access->p_sys = sys;
ParseMRL( obj, access->psz_location );
char *path = var_InheritString (obj, CFG_PREFIX"dev");
if (unlikely(path == NULL))
goto error; /* probably OOM */
uint32_t caps;
int fd = OpenDevice (obj, path, &caps);
free (path);
if (fd == -1)
goto error;
sys->fd = fd;
if (InitVideo (access, fd, caps))
{
v4l2_close (fd);
goto error;
}
sys->controls = ControlsInit (VLC_OBJECT(access), fd);
access->pf_seek = NULL;
access->pf_control = AccessControl;
return VLC_SUCCESS;
error:
free (sys);
return VLC_EGENERIC;
}
int InitVideo (access_t *access, int fd, uint32_t caps)
{
access_sys_t *sys = access->p_sys;
if (!(caps & V4L2_CAP_VIDEO_CAPTURE))
{
msg_Err (access, "not a video capture device");
return -1;
}
v4l2_std_id std;
if (SetupInput (VLC_OBJECT(access), fd, &std))
return -1;
/* NOTE: The V4L access_demux expects a VLC FOURCC as "chroma". It is used to set the
* es_format_t structure correctly. However, the V4L access (*here*) has no use for a
* VLC FOURCC and expects a V4L2 format directly instead. That is confusing :-( */
uint32_t pixfmt = 0;
char *fmtstr = var_InheritString (access, CFG_PREFIX"chroma");
if (fmtstr != NULL && strlen (fmtstr) <= 4)
{
memcpy (&pixfmt, fmtstr, strlen (fmtstr));
free (fmtstr);
}
else
/* Use the default^Wprevious format if none specified */
{
struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
if (v4l2_ioctl (fd, VIDIOC_G_FMT, &fmt) < 0)
{
msg_Err (access, "cannot get default format: %s",
vlc_strerror_c(errno));
return -1;
}
pixfmt = fmt.fmt.pix.pixelformat;
}
msg_Dbg (access, "selected format %4.4s", (const char *)&pixfmt);
struct v4l2_format fmt;
struct v4l2_streamparm parm;
if (SetupFormat (access, fd, pixfmt, &fmt, &parm))
return -1;
msg_Dbg (access, "%"PRIu32" bytes for complete image", fmt.fmt.pix.sizeimage);
/* Check interlacing */
switch (fmt.fmt.pix.field)
{
case V4L2_FIELD_INTERLACED:
msg_Dbg (access, "Interlacing setting: interleaved");
/*if (NTSC)
sys->block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
else*/
sys->block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
break;
case V4L2_FIELD_INTERLACED_TB:
msg_Dbg (access, "Interlacing setting: interleaved top bottom" );
sys->block_flags = BLOCK_FLAG_TOP_FIELD_FIRST;
break;
case V4L2_FIELD_INTERLACED_BT:
msg_Dbg (access, "Interlacing setting: interleaved bottom top" );
sys->block_flags = BLOCK_FLAG_BOTTOM_FIELD_FIRST;
break;
default:
break;
}
/* Init I/O method */
if (caps & V4L2_CAP_STREAMING)
{
sys->bufc = 4;
sys->bufv = StartMmap (VLC_OBJECT(access), fd, &sys->bufc);
if (sys->bufv == NULL)
return -1;
access->pf_block = MMapBlock;
}
else if (caps & V4L2_CAP_READWRITE)
{
sys->blocksize = fmt.fmt.pix.sizeimage;
sys->bufv = NULL;
access->pf_block = ReadBlock;
}
else
{
msg_Err (access, "no supported capture method");
return -1;
}
return 0;
}
void AccessClose( vlc_object_t *obj )
{
access_t *access = (access_t *)obj;
access_sys_t *sys = access->p_sys;
if (sys->bufv != NULL)
StopMmap (sys->fd, sys->bufv, sys->bufc);
ControlsDeinit( obj, sys->controls );
v4l2_close (sys->fd);
free( sys );
}
/* Wait for data */
static int AccessPoll (access_t *access)
{
access_sys_t *sys = access->p_sys;
struct pollfd ufd;
ufd.fd = sys->fd;
ufd.events = POLLIN;
switch (poll (&ufd, 1, 500))
{
case -1:
if (errno == EINTR)
case 0:
/* FIXME: kill this case (arbitrary timeout) */
return -1;
msg_Err (access, "poll error: %s", vlc_strerror_c(errno));
access->info.b_eof = true;
return -1;
}
return 0;
}
static block_t *MMapBlock (access_t *access)
{
access_sys_t *sys = access->p_sys;
if (AccessPoll (access))
return NULL;
block_t *block = GrabVideo (VLC_OBJECT(access), sys->fd, sys->bufv);
if( block != NULL )
{
block->i_pts = block->i_dts = mdate();
block->i_flags |= sys->block_flags;
}
return block;
}
static block_t *ReadBlock (access_t *access)
{
access_sys_t *sys = access->p_sys;
if (AccessPoll (access))
return NULL;
block_t *block = block_Alloc (sys->blocksize);
if (unlikely(block == NULL))
return NULL;
ssize_t val = v4l2_read (sys->fd, block->p_buffer, block->i_buffer);
if (val < 0)
{
block_Release (block);
msg_Err (access, "cannot read buffer: %s", vlc_strerror_c(errno));
access->info.b_eof = true;
return NULL;
}
block->i_buffer = val;
access->info.i_pos += val;
return block;
}
static int AccessControl( access_t *access, int query, va_list args )
{
switch( query )
{
case ACCESS_CAN_SEEK:
case ACCESS_CAN_FASTSEEK:
case ACCESS_CAN_PAUSE:
case ACCESS_CAN_CONTROL_PACE:
*va_arg( args, bool* ) = false;
break;
case ACCESS_GET_PTS_DELAY:
*va_arg(args,int64_t *) = INT64_C(1000)
* var_InheritInteger( access, "live-caching" );
break;
case ACCESS_SET_PAUSE_STATE:
/* Nothing to do */
break;
default:
return VLC_EGENERIC;
}
return VLC_SUCCESS;
}