]> git.sesse.net Git - vlc/blob - modules/access/v4l/v4l.c
0cfe12cd3d1a1f139a10ad933e3e6bdbe9ec90d9
[vlc] / modules / access / v4l / v4l.c
1 /*****************************************************************************
2  * v4l.c : Video4Linux input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: v4l.c,v 1.1 2002/08/08 00:35:10 sam Exp $
6  *
7  * Author: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <vlc/vlc.h>
28 #include <vlc/input.h>
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33
34 /*****************************************************************************
35  * Local prototypes
36  *****************************************************************************/
37 static int  V4lOpen  ( vlc_object_t * );
38 static void V4lClose ( vlc_object_t * ); 
39 static int  V4lRead  ( input_thread_t *, byte_t *, size_t );
40
41 /*****************************************************************************
42  * Module descriptior
43  *****************************************************************************/
44 vlc_module_begin();
45     set_description( _("Video4Linux input module") );
46     set_capability( "access", 80 );
47     set_callbacks( V4lOpen, V4lClose );
48 vlc_module_end();
49
50 static int i_fd;
51
52 /*****************************************************************************
53  * V4lOpen: open device
54  *****************************************************************************/
55 static int V4lOpen( vlc_object_t *p_this )
56 {   
57     input_thread_t * p_input = (input_thread_t *)p_this;
58
59     p_input->pf_read = V4lRead;
60     p_input->pf_seek = NULL;
61     p_input->pf_set_area = NULL;
62     p_input->pf_set_program = NULL;
63
64     vlc_mutex_lock( &p_input->stream.stream_lock );
65     p_input->stream.b_pace_control = 0;
66     p_input->stream.b_seekable = 0;
67     p_input->stream.p_selected_area->i_size = 0;
68     p_input->stream.p_selected_area->i_tell = 0;
69     p_input->stream.i_method = INPUT_METHOD_FILE;
70     vlc_mutex_unlock( &p_input->stream.stream_lock );
71
72     i_fd = open( "/dev/v4l/video0", O_RDWR );
73
74     return 0;
75 }
76
77 /*****************************************************************************
78  * V4lClose: close device
79  *****************************************************************************/
80 static void V4lClose( vlc_object_t *p_this )
81 {
82     input_thread_t *   p_input = (input_thread_t *)p_this;
83     //thread_data_t *p_data = (thread_data_t *)p_input->p_access_data;
84
85     //close( p_data->i_handle );
86     close( i_fd );
87     //free( p_data );
88 }
89
90 /*****************************************************************************
91  * V4lRead: reads from the device into PES packets.
92  *****************************************************************************
93  * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
94  * bytes.
95  *****************************************************************************/
96 #define WIDTH 640
97 #define HEIGHT 480
98
99 static int V4lRead( input_thread_t * p_input, byte_t * p_buffer,
100                     size_t i_len )
101 {   
102     struct video_capability vid_caps;
103     struct video_mbuf vid_buf;
104     struct video_mmap vid_mmap;
105
106     char *map = NULL;
107
108     //thread_data_t *     p_data;
109     int i_read = 0;
110
111     if( ioctl( i_fd, VIDIOCGCAP, &vid_caps ) == -1 )
112     {
113         printf("ioctl (VIDIOCGCAP) failed\n");
114         return 0;
115     }
116
117     if( ioctl( i_fd, VIDIOCGMBUF, &vid_buf ) == -1 )
118     {
119         // to do a normal read()
120         map = malloc (WIDTH * HEIGHT * 3);
121         len = read (fd_webcam, map, WIDTH * HEIGHT * 3);
122         if (len <=  0)
123         {
124             free (map);
125             return (NULL);
126         }
127         *size = 0;
128         return (map);
129     }
130
131     //p_data = (thread_data_t *)p_input->p_access_data;
132     return i_read;
133 }
134