access: dtv: simplify/regroup setup and drop delsys
[vlc.git] / modules / access / eyetv.m
1 /*****************************************************************************
2  * eyetv.m : Access module to connect to our plugin running within EyeTV
3  *****************************************************************************
4  * Copyright (C) 2006-2007 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Author: Felix K├╝hne <fkuehne at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35
36 #include <vlc_network.h>
37 #include <vlc_interrupt.h>
38
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42 #include <unistd.h>
43
44 #import <Foundation/Foundation.h>
45
46 #define MTU 65535
47
48 /* TODO:
49  * watch for PluginQuit or DeviceRemoved to stop output to VLC's core then */
50
51 /*****************************************************************************
52  * Module descriptior
53  *****************************************************************************/
54 static int  Open (vlc_object_t *);
55 static void Close(vlc_object_t *);
56
57 #define CHANNEL_TEXT N_("Channel number")
58 #define CHANNEL_LONGTEXT N_(\
59     "EyeTV program number, or use 0 for last channel, " \
60     "-1 for S-Video input, -2 for Composite input")
61
62 vlc_module_begin ()
63     set_shortname("EyeTV")
64     set_description(N_("EyeTV input"))
65     set_category(CAT_INPUT)
66     set_subcategory(SUBCAT_INPUT_ACCESS)
67
68     add_integer("eyetv-channel", 0,
69                  CHANNEL_TEXT, CHANNEL_LONGTEXT, false)
70
71     set_capability("access", 0)
72     add_shortcut("eyetv")
73     set_callbacks(Open, Close)
74 vlc_module_end ()
75
76 /*****************************************************************************
77  * Access: local prototypes
78  *****************************************************************************/
79 struct access_sys_t
80 {
81     int eyetvSock;
82 };
83
84 static block_t *BlockRead(access_t *);
85 static int Control(access_t *, int, va_list);
86
87 static void selectChannel(vlc_object_t *p_this, int theChannelNum)
88 {
89     @autoreleasepool {
90         NSAppleScript *script;
91         switch(theChannelNum) {
92             case -2: // Composite
93                 script = [[NSAppleScript alloc] initWithSource:
94                           @"tell application \"EyeTV\"\n"
95                           "  input_change input source composite video input\n"
96                           "  volume_change level 0\n"
97                           "  show player_window\n"
98                           "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
99                           "end tell"];
100                 break;
101             case -1: // S-Video
102                 script = [[NSAppleScript alloc] initWithSource:
103                           @"tell application \"EyeTV\"\n"
104                           "  input_change input source S video input\n"
105                           "  volume_change level 0\n"
106                           "  show player_window\n"
107                           "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
108                           "end tell"];
109                 break;
110             case 0: // Last
111                 script = [[NSAppleScript alloc] initWithSource:
112                           @"tell application \"EyeTV\"\n"
113                           "  volume_change level 0\n"
114                           "  show player_window\n"
115                           "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
116                           "end tell"];
117                 break;
118             default:
119                 if (theChannelNum > 0) {
120                     NSString *channel_change = [NSString stringWithFormat:
121                                                 @"tell application \"EyeTV\"\n"
122                                                 "  channel_change channel number %d\n"
123                                                 "  volume_change level 0\n"
124                                                 "  show player_window\n"
125                                                 "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
126                                                 "end tell", theChannelNum];
127                     script = [[NSAppleScript alloc] initWithSource:channel_change];
128                 }
129                 else
130                     return;
131         }
132         NSDictionary *errorDict;
133         NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
134         if (nil == descriptor) {
135             NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
136             msg_Err(p_this, "EyeTV source change failed with error status '%s'", [errorString UTF8String]);
137         }
138         [script release];
139     }
140 }
141
142 /*****************************************************************************
143  * Open: sets up the module and its threads
144  *****************************************************************************/
145 static int Open(vlc_object_t *p_this)
146 {
147     access_t        *p_access = (access_t *)p_this;
148     access_sys_t    *p_sys;
149
150     struct sockaddr_un publicAddr, peerAddr;
151     int publicSock;
152
153     /* Init p_access */
154     access_InitFields(p_access);
155     ACCESS_SET_CALLBACKS(NULL, BlockRead, Control, NULL);
156     p_sys = p_access->p_sys = calloc(1, sizeof(access_sys_t));
157     if (!p_sys)
158         return VLC_ENOMEM;
159
160     msg_Dbg(p_access, "coming up");
161     selectChannel(p_this, var_InheritInteger(p_access, "eyetv-channel"));
162
163     /* socket */
164     memset(&publicAddr, 0, sizeof(publicAddr));
165     publicAddr.sun_family = AF_UNIX;
166     strncpy(publicAddr.sun_path, "/tmp/.vlc-eyetv-bridge", sizeof(publicAddr.sun_path)-1);
167     /* remove previous public path if it wasn't cleanly removed */
168     if ((0 != unlink(publicAddr.sun_path)) && (ENOENT != errno)) {
169         msg_Err(p_access, "local socket path is not usable (errno=%d)", errno);
170         free(p_sys);
171         return VLC_EGENERIC;
172     }
173
174     publicSock = vlc_socket(PF_UNIX, SOCK_STREAM, 0, false);
175     if (publicSock == -1) {
176         msg_Err(p_access, "create local socket failed (errno=%d)", errno);
177         free(p_sys);
178         return VLC_EGENERIC;
179     }
180
181     if (bind(publicSock, (struct sockaddr *)&publicAddr, sizeof(struct sockaddr_un)) == -1) {
182         msg_Err(p_access, "bind local socket failed (errno=%d)", errno);
183         vlc_close(publicSock);
184         free(p_sys);
185         return VLC_EGENERIC;
186     }
187
188     /* we are not expecting more than one connection */
189     if (listen(publicSock, 1) == -1) {
190         msg_Err(p_access, "cannot accept connection (errno=%d)", errno);
191         vlc_close(publicSock);
192         free(p_sys);
193         return VLC_EGENERIC;
194     } else {
195         socklen_t peerSockLen = sizeof(struct sockaddr_un);
196         int peerSock;
197
198         /* tell the EyeTV plugin to open start sending */
199         CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter (),
200                                               CFSTR("VLCAccessStartDataSending"),
201                                               CFSTR("VLCEyeTVSupport"),
202                                               /*userInfo*/ NULL,
203                                               TRUE);
204
205         msg_Dbg(p_access, "plugin notified");
206
207         peerSock = accept(publicSock, (struct sockaddr *)&peerAddr, &peerSockLen);
208         if (peerSock == -1) {
209             msg_Err(p_access, "cannot wait for connection (errno=%d)", errno);
210             vlc_close(publicSock);
211             free(p_sys);
212             return VLC_EGENERIC;
213         }
214
215         msg_Dbg(p_access, "plugin connected");
216
217         p_sys->eyetvSock = peerSock;
218
219         /* remove public access */
220         vlc_close(publicSock);
221         unlink(publicAddr.sun_path);
222     }
223     return VLC_SUCCESS;
224 }
225
226 /*****************************************************************************
227  * Close: closes msg-port, free resources
228  *****************************************************************************/
229 static void Close(vlc_object_t *p_this)
230 {
231     access_t     *p_access = (access_t *)p_this;
232     access_sys_t *p_sys = p_access->p_sys;
233
234     msg_Dbg(p_access, "closing");
235
236     /* tell the EyeTV plugin to close its msg port and stop sending */
237     CFNotificationCenterPostNotification(CFNotificationCenterGetDistributedCenter (),
238                                           CFSTR("VLCAccessStopDataSending"),
239                                           CFSTR("VLCEyeTVSupport"),
240                                           /*userInfo*/ NULL,
241                                           TRUE);
242     msg_Dbg(p_access, "plugin notified");
243
244     vlc_close(p_sys->eyetvSock);
245     msg_Dbg(p_access, "msg port closed and freed");
246
247     free(p_sys);
248 }
249
250 /*****************************************************************************
251 * BlockRead: forwarding data from EyeTV plugin which was received above
252 *****************************************************************************/
253 static block_t *BlockRead(access_t *p_access)
254 {
255     access_sys_t *p_sys = p_access->p_sys;
256     block_t      *p_block;
257     ssize_t len;
258
259     if (p_access->info.b_eof)
260         return NULL;
261
262     /* Read data */
263     p_block = block_Alloc(MTU);
264     len = vlc_read_i11e(p_sys->eyetvSock, p_block->p_buffer, MTU);
265
266     if (len < 0) {
267         block_Release(p_block);
268         return NULL;
269     }
270
271     return block_Realloc(p_block, 0, p_block->i_buffer = len);
272 }
273
274 /*****************************************************************************
275  * Control:
276  *****************************************************************************/
277 static int Control(access_t *p_access, int i_query, va_list args)
278 {
279     bool   *pb_bool;
280     int          *pi_int;
281     int64_t      *pi_64;
282     access_sys_t  *p_sys = (access_sys_t *) p_access->p_sys;
283
284     switch(i_query) {
285         case ACCESS_CAN_SEEK:
286         case ACCESS_CAN_FASTSEEK:
287             pb_bool = (bool*)va_arg(args, bool*);
288             *pb_bool = false;
289             break;
290         case ACCESS_CAN_PAUSE:
291             pb_bool = (bool*)va_arg(args, bool*);
292             *pb_bool = false;
293             break;
294         case ACCESS_CAN_CONTROL_PACE:
295             pb_bool = (bool*)va_arg(args, bool*);
296             *pb_bool = false;
297             break;
298         case ACCESS_GET_PTS_DELAY:
299             pi_64 = (int64_t*)va_arg(args, int64_t *);
300             *pi_64 =
301                 INT64_C(1000) * var_InheritInteger(p_access, "live-caching");
302             break;
303         default:
304             return VLC_EGENERIC;
305     }
306     return VLC_SUCCESS;
307 }