]> git.sesse.net Git - vlc/blob - modules/access/eyetv.m
Use var_CreateGet when needed (and cleaning)
[vlc] / modules / access / eyetv.m
1 /*****************************************************************************
2  * eyetv.c : Access module to connect to our plugin running within EyeTV
3  *****************************************************************************
4  * Copyright (C) 2006-2007 the VideoLAN team
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
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., 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
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/un.h>
41 #include <unistd.h>
42
43 #import <Foundation/Foundation.h>
44
45 #define MTU 65535
46
47 /* TODO:
48  * watch for PluginQuit or DeviceRemoved to stop output to VLC's core then */
49
50 /*****************************************************************************
51  * Module descriptior
52  *****************************************************************************/
53 static int  Open ( vlc_object_t * );
54 static void Close( vlc_object_t * );
55
56 #define CHANNEL_TEXT N_("Channel number")
57 #define CHANNEL_LONGTEXT N_( \
58     "EyeTV program number, or use 0 for last channel, " \
59     "-1 for S-Video input, -2 for Composite input" )
60
61 #define CACHING_TEXT N_("Caching value in ms")
62 #define CACHING_LONGTEXT N_( \
63     "Caching value for EyeTV captures. This " \
64     "value should be set in milliseconds." )
65
66 vlc_module_begin ()
67     set_shortname( "EyeTV" )
68     set_description( N_("EyeTV access module") )
69     set_category( CAT_INPUT )
70     set_subcategory( SUBCAT_INPUT_ACCESS )
71
72     add_integer( "eyetv-channel", 0, NULL,
73                  CHANNEL_TEXT, CHANNEL_LONGTEXT, false );
74
75     set_capability( "access", 0 )
76     add_shortcut( "eyetv" )
77     set_callbacks( Open, Close )
78     add_integer( "eyetv-caching", DEFAULT_PTS_DELAY / 1000, NULL,
79                  CACHING_TEXT, CACHING_LONGTEXT, true);
80 vlc_module_end ()
81
82 /*****************************************************************************
83  * Access: local prototypes
84  *****************************************************************************/
85 struct access_sys_t
86 {
87     int eyetvSock;
88     int i_pts_delay;
89 };
90
91 static block_t *BlockRead( access_t *);
92 static int Control( access_t *, int, va_list );
93
94 static void selectChannel( vlc_object_t *p_this, int theChannelNum )
95 {
96     NSAppleScript *script;
97     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
98     switch( theChannelNum )
99     {
100         case -2: // Composite
101             script = [[NSAppleScript alloc] initWithSource:
102                         @"tell application \"EyeTV\"\n"
103                          "  input_change input source composite video input\n"
104                          "  volume_change level 0\n"
105                          "  show player_window\n"
106                          "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
107                          "end tell"];
108             break;
109         case -1: // S-Video
110             script = [[NSAppleScript alloc] initWithSource:
111                         @"tell application \"EyeTV\"\n"
112                          "  input_change input source S video input\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         case 0: // Last
119             script = [[NSAppleScript alloc] initWithSource:
120                         @"tell application \"EyeTV\"\n"
121                          "  volume_change level 0\n"
122                          "  show player_window\n"
123                          "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
124                          "end tell"];
125             break;
126         default:
127             if( theChannelNum > 0 )
128             {
129                 NSString *channel_change = [NSString stringWithFormat:
130                     @"tell application \"EyeTV\"\n"
131                      "  channel_change channel number %d\n"
132                      "  volume_change level 0\n"
133                      "  show player_window\n"
134                      "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
135                      "end tell", theChannelNum];
136                 script = [[NSAppleScript alloc] initWithSource:channel_change];
137             }
138             else
139                 return;
140     }
141     NSDictionary *errorDict;
142     NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
143     if( nil == descriptor ) 
144     {
145         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
146         msg_Err( p_this, "EyeTV source change failed with error status '%s'", [errorString UTF8String] );
147     }
148     [script release];
149     [pool release];
150 }
151
152 /*****************************************************************************
153  * Open: sets up the module and its threads
154  *****************************************************************************/
155 static int Open( vlc_object_t *p_this )
156 {
157     access_t        *p_access = (access_t *)p_this;
158     access_sys_t    *p_sys;
159
160     struct sockaddr_un publicAddr, peerAddr;
161     int publicSock;
162  
163     /* Init p_access */
164     access_InitFields( p_access );
165     ACCESS_SET_CALLBACKS( NULL, BlockRead, Control, NULL );
166     p_access->info.b_prebuffered = false;
167     p_sys = p_access->p_sys = calloc( 1, sizeof( access_sys_t ) );
168     if( !p_sys )
169         return VLC_ENOMEM;
170
171     p_sys->i_pts_delay = var_CreateGetInteger( p_access, "eyetv-caching" );
172
173     int val = var_CreateGetInteger( p_access, "eyetv-channel" );
174
175     msg_Dbg( p_access, "coming up" );
176
177     selectChannel( p_this, val );
178
179     /* socket */
180     memset(&publicAddr, 0, sizeof(publicAddr));
181     publicAddr.sun_family = AF_UNIX;
182     strncpy(publicAddr.sun_path, "/tmp/.vlc-eyetv-bridge", sizeof(publicAddr.sun_path)-1);
183     /* remove previous public path if it wasn't cleanly removed */
184     if( (0 != unlink(publicAddr.sun_path)) && (ENOENT != errno) )
185     {
186         msg_Err( p_access, "local socket path is not usable (errno=%d)", errno );
187         free( p_sys );
188         return VLC_EGENERIC;
189     }
190
191     publicSock = socket(AF_UNIX, SOCK_STREAM, 0);
192     if( publicSock == -1 )
193     {
194         msg_Err( p_access, "create local socket failed (errno=%d)", errno );
195         free( p_sys );
196         return VLC_EGENERIC;
197     }
198
199     if( bind(publicSock, (struct sockaddr *)&publicAddr, sizeof(struct sockaddr_un)) == -1 )
200     {
201         msg_Err( p_access, "bind local socket failed (errno=%d)", errno );
202         close( publicSock );
203         free( p_sys );
204         return VLC_EGENERIC;
205     }
206
207     /* we are not expecting more than one connection */
208     if( listen(publicSock, 1) == -1 )
209     {
210         msg_Err( p_access, "cannot accept connection (errno=%d)", errno );
211         close( publicSock );
212         free( p_sys );
213         return VLC_EGENERIC;
214     }
215     else
216     {
217         socklen_t peerSockLen = sizeof(struct sockaddr_un);
218         int peerSock;
219
220         /* tell the EyeTV plugin to open start sending */
221         CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),
222                                               CFSTR("VLCAccessStartDataSending"),
223                                               CFSTR("VLCEyeTVSupport"),
224                                               /*userInfo*/ NULL,
225                                               TRUE );
226
227         msg_Dbg( p_access, "plugin notified" );
228
229         peerSock = accept(publicSock, (struct sockaddr *)&peerAddr, &peerSockLen);
230         if( peerSock == -1 )
231         {
232             msg_Err( p_access, "cannot wait for connection (errno=%d)", errno );
233             close( publicSock );
234             free( p_sys );
235             return VLC_EGENERIC;
236         }
237
238         msg_Dbg( p_access, "plugin connected" );
239
240         p_sys->eyetvSock = peerSock;
241
242         /* remove public access */
243         close(publicSock);
244         unlink(publicAddr.sun_path);
245     }
246     return VLC_SUCCESS;
247 }
248
249 /*****************************************************************************
250  * Close: closes msg-port, free resources
251  *****************************************************************************/
252 static void Close( vlc_object_t *p_this )
253 {
254     access_t     *p_access = (access_t *)p_this;
255     access_sys_t *p_sys = p_access->p_sys;
256  
257     msg_Dbg( p_access, "closing" );
258  
259     /* tell the EyeTV plugin to close its msg port and stop sending */
260     CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),
261                                           CFSTR("VLCAccessStopDataSending"),
262                                           CFSTR("VLCEyeTVSupport"),
263                                           /*userInfo*/ NULL,
264                                           TRUE );
265  
266     msg_Dbg( p_access, "plugin notified" );
267
268         close(p_sys->eyetvSock);
269  
270     msg_Dbg( p_access, "msg port closed and freed" );
271  
272     free( p_sys );
273 }
274
275 /*****************************************************************************
276 * BlockRead: forwarding data from EyeTV plugin which was received above
277 *****************************************************************************/
278 static block_t *BlockRead( access_t *p_access )
279 {
280     access_sys_t *p_sys = p_access->p_sys;
281     block_t      *p_block;
282     ssize_t len;
283
284     if( p_access->info.b_eof )
285         return NULL;
286
287     /* Read data */
288     p_block = block_New( p_access, MTU );
289     len = net_Read( p_access, p_sys->eyetvSock, NULL,
290                     p_block->p_buffer, MTU, false );
291
292     if( len < 0 )
293     {
294         block_Release( p_block );
295         return NULL;
296     }
297
298     return block_Realloc( p_block, 0, p_block->i_buffer = len );
299 }
300
301 /*****************************************************************************
302  * Control:
303  *****************************************************************************/
304 static int Control( access_t *p_access, int i_query, va_list args )
305 {
306     bool   *pb_bool;
307     int          *pi_int;
308     int64_t      *pi_64;
309     access_sys_t  *p_sys = (access_sys_t *) p_access->p_sys;
310  
311     switch( i_query )
312     {
313         case ACCESS_CAN_SEEK:
314         case ACCESS_CAN_FASTSEEK:
315             pb_bool = (bool*)va_arg( args, bool* );
316             *pb_bool = false;
317             break;
318         case ACCESS_CAN_PAUSE:
319             pb_bool = (bool*)va_arg( args, bool* );
320             *pb_bool = false;
321             break;
322         case ACCESS_CAN_CONTROL_PACE:
323             pb_bool = (bool*)va_arg( args, bool* );
324             *pb_bool = false;
325             break;
326
327         /* */
328         case ACCESS_GET_MTU:
329             pi_int = (int*)va_arg( args, int * );
330             *pi_int = MTU;
331             break;
332
333         case ACCESS_GET_PTS_DELAY:
334             pi_64 = (int64_t*)va_arg( args, int64_t * );
335             *pi_64 = (int64_t) p_sys->i_pts_delay * 1000;
336             break;
337         
338         case ACCESS_SET_PAUSE_STATE:
339         case ACCESS_GET_TITLE_INFO:
340         case ACCESS_SET_TITLE:
341         case ACCESS_SET_SEEKPOINT:
342         case ACCESS_SET_PRIVATE_ID_STATE:
343         case ACCESS_GET_CONTENT_TYPE:
344             return VLC_EGENERIC;
345  
346         default:
347             msg_Warn( p_access, "unimplemented query in control" );
348             return VLC_EGENERIC;
349  
350     }
351     return VLC_SUCCESS;
352 }