]> git.sesse.net Git - vlc/blob - modules/access/v4l2/radio.c
decklink: update build rules
[vlc] / modules / access / v4l2 / radio.c
1 /*****************************************************************************
2  * radio.c : V4L2 analog radio receiver
3  *****************************************************************************
4  * Copyright (C) 2012 RĂ©mi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <fcntl.h>
26
27 #include <vlc_common.h>
28 #include <vlc_demux.h>
29 #include <vlc_fs.h>
30
31 #include "v4l2.h"
32
33 struct demux_sys_t
34 {
35     int fd;
36     vlc_v4l2_ctrl_t *controls;
37     mtime_t start;
38 };
39
40 static int RadioControl (demux_t *demux, int query, va_list args)
41 {
42     demux_sys_t *sys = demux->p_sys;
43
44     switch (query)
45     {
46         case DEMUX_CAN_PAUSE:
47         case DEMUX_CAN_SEEK:
48         case DEMUX_CAN_CONTROL_PACE:
49             *va_arg (args, bool *) = false;
50             break;
51
52         case DEMUX_GET_PTS_DELAY:
53             *va_arg (args,int64_t *) = INT64_C(1000)
54                 * var_InheritInteger (demux, "live-caching");
55             break;
56
57         case DEMUX_GET_TIME:
58             *va_arg (args, int64_t *) = mdate () - sys->start;
59             break;
60
61         /* TODO implement others */
62         default:
63             return VLC_EGENERIC;
64     }
65     return VLC_SUCCESS;
66 }
67
68 int RadioOpen (vlc_object_t *obj)
69 {
70     demux_t *demux = (demux_t *)obj;
71
72     /* Parse MRL */
73     size_t pathlen = strcspn (demux->psz_location, ":;");
74     char *path = (pathlen != 0) ? strndup (demux->psz_location, pathlen)
75                               : var_InheritString (obj, CFG_PREFIX"radio-dev");
76     if (unlikely(path == NULL))
77         return VLC_ENOMEM;
78     if (demux->psz_location[pathlen] != '\0')
79         var_LocationParse (obj, demux->psz_location + pathlen + 1, CFG_PREFIX);
80
81     /* Open device */
82     uint32_t caps;
83     int fd = OpenDevice (obj, path, &caps);
84     free (path);
85     if (fd == -1)
86         return VLC_EGENERIC;
87     if (!(caps & V4L2_CAP_TUNER))
88     {
89         msg_Err (obj, "not a radio tuner device");
90         goto error;
91     }
92
93     if (SetupTuner (obj, fd, 0))
94         goto error;
95
96     demux_sys_t *sys = malloc (sizeof (*sys));
97     if (unlikely(sys == NULL))
98         goto error;
99
100     sys->fd = fd;
101     sys->controls = ControlsInit (VLC_OBJECT(demux), fd);
102     sys->start = mdate ();
103
104     demux->p_sys = sys;
105     demux->pf_demux = NULL;
106     demux->pf_control = RadioControl;
107     demux->info.i_update = 0;
108     demux->info.i_title = 0;
109     demux->info.i_seekpoint = 0;
110     return VLC_SUCCESS;
111
112 error:
113     v4l2_close (fd);
114     return VLC_EGENERIC;
115 }
116
117 void RadioClose (vlc_object_t *obj)
118 {
119     demux_t *demux = (demux_t *)obj;
120     demux_sys_t *sys = demux->p_sys;
121
122     ControlsDeinit (obj, sys->controls);
123     v4l2_close (sys->fd);
124     free (sys);
125 }