]> git.sesse.net Git - vlc/blob - modules/access/v4l2/access.c
v4l2: remove useless check
[vlc] / modules / access / v4l2 / access.c
1 /*****************************************************************************
2  * access.c : V4L2 compressed byte stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2011 the VideoLAN team
5  *
6  * Authors: Benjamin Pracht <bigben at videolan dot org>
7  *          Richard Hosking <richard at hovis dot net>
8  *          Antoine Cellerier <dionoea at videolan d.t org>
9  *          Dennis Lou <dlou99 at yahoo dot com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "v4l2.h"
31 #include <vlc_access.h>
32
33 #include <errno.h>
34 #include <poll.h>
35
36 static block_t *AccessRead( access_t * );
37 static ssize_t AccessReadStream( access_t *, uint8_t *, size_t );
38 static int AccessControl( access_t *, int, va_list );
39
40 int AccessOpen( vlc_object_t *obj )
41 {
42     access_t *access = (access_t *)obj;
43
44     access_InitFields( access );
45
46     demux_sys_t *sys = calloc( 1, sizeof( demux_sys_t ));
47     if( unlikely(sys == NULL) )
48         return VLC_ENOMEM;
49     access->p_sys = (access_sys_t *)sys;
50
51     ParseMRL( obj, access->psz_location );
52     sys->i_fd = OpenVideo( obj, sys, false );
53     if( sys->i_fd == -1 )
54     {
55         free( sys );
56         return VLC_EGENERIC;
57     }
58
59     if( sys->io == IO_METHOD_READ )
60         access->pf_read = AccessReadStream;
61     else
62         access->pf_block = AccessRead;
63     access->pf_seek = NULL;
64     access->pf_control = AccessControl;
65     return VLC_SUCCESS;
66 }
67
68 void AccessClose( vlc_object_t *obj )
69 {
70     access_t *access = (access_t *)obj;
71     demux_sys_t *sys = (demux_sys_t *)access->p_sys;
72
73     ControlsDeinit( obj, sys->controls );
74     v4l2_close( sys->i_fd );
75     free( sys );
76 }
77
78 static block_t *AccessRead( access_t *access )
79 {
80     demux_sys_t *sys = (demux_sys_t *)access->p_sys;
81
82     struct pollfd fd;
83     fd.fd = sys->i_fd;
84     fd.events = POLLIN|POLLPRI;
85     fd.revents = 0;
86
87     /* Wait for data */
88     /* FIXME: kill timeout */
89     if( poll( &fd, 1, 500 ) <= 0 )
90         return NULL;
91
92     block_t *block = GrabVideo( VLC_OBJECT(access), sys );
93     if( block != NULL )
94     {
95         block->i_pts = block->i_dts = mdate();
96         block->i_flags |= sys->i_block_flags;
97     }
98     return block;
99 }
100
101 static ssize_t AccessReadStream( access_t *access, uint8_t *buf, size_t len )
102 {
103     demux_sys_t *sys = (demux_sys_t *)access->p_sys;
104     struct pollfd ufd;
105     int i_ret;
106
107     ufd.fd = sys->i_fd;
108     ufd.events = POLLIN;
109
110     if( access->info.b_eof )
111         return 0;
112
113     /* FIXME: kill timeout and vlc_object_alive() */
114     do
115     {
116         if( !vlc_object_alive(access) )
117             return 0;
118
119         ufd.revents = 0;
120     }
121     while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
122
123     if( i_ret < 0 )
124     {
125         if( errno != EINTR )
126             msg_Err( access, "poll error: %m" );
127         return -1;
128     }
129
130     i_ret = v4l2_read( sys->i_fd, buf, len );
131     if( i_ret == 0 )
132         access->info.b_eof = true;
133     else if( i_ret > 0 )
134         access->info.i_pos += i_ret;
135
136     return i_ret;
137 }
138
139 static int AccessControl( access_t *access, int query, va_list args )
140 {
141     switch( query )
142     {
143         /* */
144         case ACCESS_CAN_SEEK:
145         case ACCESS_CAN_FASTSEEK:
146         case ACCESS_CAN_PAUSE:
147         case ACCESS_CAN_CONTROL_PACE:
148             *va_arg( args, bool* ) = false;
149             break;
150
151         /* */
152         case ACCESS_GET_PTS_DELAY:
153             *va_arg(args,int64_t *) = INT64_C(1000)
154                 * var_InheritInteger( access, "live-caching" );
155             break;
156
157         /* */
158         case ACCESS_SET_PAUSE_STATE:
159             /* Nothing to do */
160             break;
161
162         case ACCESS_GET_TITLE_INFO:
163         case ACCESS_SET_TITLE:
164         case ACCESS_SET_SEEKPOINT:
165         case ACCESS_SET_PRIVATE_ID_STATE:
166         case ACCESS_GET_CONTENT_TYPE:
167         case ACCESS_GET_META:
168             return VLC_EGENERIC;
169
170         default:
171             msg_Warn( access, "Unimplemented query %d in control", query );
172             return VLC_EGENERIC;
173
174     }
175     return VLC_SUCCESS;
176 }