]> git.sesse.net Git - vlc/blob - modules/access/eyetv.m
eyetv: latest version of EyeTV capture plugin, needs lotta testing
[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: eyetv.c 23509 2007-12-09 17:39:28Z courmisch $
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 #include <vlc/vlc.h>
29 #include <vlc_access.h>
30
31 #include <vlc_network.h>
32
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <unistd.h>
37
38 #import <Foundation/Foundation.h>
39
40 /* TODO:
41  * watch for PluginQuit or DeviceRemoved to stop output to VLC's core then */
42
43 /*****************************************************************************
44  * Module descriptior
45  *****************************************************************************/
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 #define CHANNEL_TEXT N_("Channel number")
50 #define CHANNEL_LONGTEXT N_( \
51     "EyeTV program number, or use 0 for last channel, " \
52     "-1 for S-Video input, -2 for Composite input" )
53 vlc_module_begin();
54     set_shortname( "EyeTV" );
55     set_description( _("EyeTV access module") );
56     set_category( CAT_INPUT );
57     set_subcategory( SUBCAT_INPUT_ACCESS );
58
59     add_integer( "eyetv-channel", 0, NULL,
60                  CHANNEL_TEXT, CHANNEL_LONGTEXT, VLC_FALSE );
61
62     set_capability( "access2", 0 );
63     add_shortcut( "eyetv" );
64     set_callbacks( Open, Close );
65 vlc_module_end();
66
67 /*****************************************************************************
68  * Access: local prototypes
69  *****************************************************************************/
70 struct access_sys_t
71 {
72     int eyetvSock;
73 };
74
75 static ssize_t Read( access_t *, uint8_t *, size_t );
76 static int Control( access_t *, int, va_list );
77
78 static void selectChannel( vlc_object_t *p_this, int theChannelNum )
79 {
80     NSAppleScript *script;
81     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
82     switch( theChannelNum )
83     {
84         case -2: // Composite
85             script = [[NSAppleScript alloc] initWithSource:
86                         @"tell application \"EyeTV\"\n"
87                          "  input_change input source composite video input\n"
88                          "  volume_change level 0\n"
89                          "  show player_window\n"
90                          "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
91                          "end tell"];
92             break;
93         case -1: // S-Video
94             script = [[NSAppleScript alloc] initWithSource:
95                         @"tell application \"EyeTV\"\n"
96                          "  input_change input source S video input\n"
97                          "  volume_change level 0\n"
98                          "  show player_window\n"
99                          "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
100                          "end tell"];
101             break;
102         case 0: // Last
103             script = [[NSAppleScript alloc] initWithSource:
104                         @"tell application \"EyeTV\"\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         default:
111             if( theChannelNum > 0 )
112             {
113                 NSString *channel_change = [NSString stringWithFormat:
114                     @"tell application \"EyeTV\"\n"
115                      "  channel_change channel number %d\n"
116                      "  volume_change level 0\n"
117                      "  show player_window\n"
118                      "  tell application \"System Events\" to set visible of process \"EyeTV\" to false\n"
119                      "end tell", theChannelNum];
120                 script = [[NSAppleScript alloc] initWithSource:channel_change];
121             }
122             else
123                 return;
124     }
125     NSDictionary *errorDict;
126     NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&errorDict];
127     if( nil == descriptor ) 
128     {
129         NSString *errorString = [errorDict objectForKey:NSAppleScriptErrorMessage];
130         msg_Err( p_this, "EyeTV source change failed with error status '%s'", [errorString UTF8String] );
131     }
132     [script release];
133     [pool release];
134 }
135
136 /*****************************************************************************
137  * Open: sets up the module and its threads
138  *****************************************************************************/
139 static int Open( vlc_object_t *p_this )
140 {
141     access_t        *p_access = (access_t *)p_this;
142     access_sys_t    *p_sys;
143
144     struct sockaddr_un publicAddr, peerAddr;
145     int publicSock;
146  
147     vlc_value_t val;
148
149     /* Init p_access */
150     access_InitFields( p_access ); \
151     ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL ); \
152     MALLOC_ERR( p_access->p_sys, access_sys_t ); \
153     p_sys = p_access->p_sys; memset( p_sys, 0, sizeof( access_sys_t ) );
154
155     var_Create( p_access, "eyetv-channel", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
156     var_Get( p_access, "eyetv-channel", &val);
157
158     msg_Dbg( p_access, "coming up" );
159
160     selectChannel(p_this, val.i_int);
161
162     /* socket */
163     memset(&publicAddr, 0, sizeof(publicAddr));
164     publicAddr.sun_family = AF_UNIX;
165     strncpy(publicAddr.sun_path, "/tmp/.vlc-eyetv-bridge", sizeof(publicAddr.sun_path)-1);
166     /* remove previous public path if it wasn't cleanly removed */
167     if( (0 != unlink(publicAddr.sun_path)) && (ENOENT != errno) )
168     {
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 = socket(AF_UNIX, SOCK_STREAM, 0);
175     if( publicSock == -1 )
176     {
177         msg_Err( p_access, "create local socket failed (errno=%d)", errno );
178         free( p_sys );
179         return VLC_EGENERIC;
180     }
181
182     if( bind(publicSock, (struct sockaddr *)&publicAddr, sizeof(struct sockaddr_un)) == -1 )
183     {
184         msg_Err( p_access, "bind local socket failed (errno=%d)", errno );
185         close( publicSock );
186         free( p_sys );
187         return VLC_EGENERIC;
188     }
189
190     /* we are not expecting more than one connection */
191     if( listen(publicSock, 1) == -1 )
192     {
193         msg_Err( p_access, "cannot accept connection (errno=%d)", errno );
194         close( publicSock );
195         free( p_sys );
196         return VLC_EGENERIC;
197     }
198     else
199     {
200         socklen_t peerSockLen = sizeof(struct sockaddr_un);
201         int peerSock;
202
203         /* tell the EyeTV plugin to open start sending */
204         CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),
205                                               CFSTR("VLCAccessStartDataSending"),
206                                               CFSTR("VLCEyeTVSupport"),
207                                               /*userInfo*/ NULL,
208                                               TRUE );
209
210         msg_Dbg( p_access, "plugin notified" );
211
212         peerSock = accept(publicSock, (struct sockaddr *)&peerAddr, &peerSockLen);
213         if( peerSock == -1 )
214         {
215             msg_Err( p_access, "cannot wait for connection (errno=%d)", errno );
216             close( publicSock );
217             free( p_sys );
218             return VLC_EGENERIC;
219         }
220
221         msg_Dbg( p_access, "plugin connected" );
222
223         p_sys->eyetvSock = peerSock;
224
225         /* remove public access */
226         close(publicSock);
227         unlink(publicAddr.sun_path);
228     }
229     return VLC_SUCCESS;
230 }
231
232 /*****************************************************************************
233  * Close: closes msg-port, free resources
234  *****************************************************************************/
235 static void Close( vlc_object_t *p_this )
236 {
237     access_t     *p_access = (access_t *)p_this;
238     access_sys_t *p_sys = p_access->p_sys;
239  
240     msg_Dbg( p_access, "closing" );
241  
242     /* tell the EyeTV plugin to close its msg port and stop sending */
243     CFNotificationCenterPostNotification( CFNotificationCenterGetDistributedCenter (),
244                                           CFSTR("VLCAccessStopDataSending"),
245                                           CFSTR("VLCEyeTVSupport"),
246                                           /*userInfo*/ NULL,
247                                           TRUE );
248  
249     msg_Dbg( p_access, "plugin notified" );
250
251         close(p_sys->eyetvSock);
252  
253     msg_Dbg( p_access, "msg port closed and freed" );
254  
255     free( p_sys );
256 }
257
258 /*****************************************************************************
259 * Read: forwarding data from EyeTV plugin which was received above
260 *****************************************************************************/
261 static ssize_t Read( access_t *p_access, uint8_t *p_buffer, size_t i_len )
262 {
263     access_sys_t *p_sys = p_access->p_sys;
264     int i_read;
265
266     if( p_access->info.b_eof )
267         return 0;
268
269     i_read = net_Read( p_access, p_sys->eyetvSock, NULL, p_buffer, i_len,
270                        VLC_FALSE );
271     if( i_read == 0 )
272         p_access->info.b_eof = VLC_TRUE;
273     else if( i_read > 0 )
274         p_access->info.i_pos += i_read;
275
276     return i_read;
277 }
278
279 /*****************************************************************************
280  * Control:
281  *****************************************************************************/
282 static int Control( access_t *p_access, int i_query, va_list args )
283 {/*
284     vlc_bool_t   *pb_bool;
285     int          *pi_int;
286     int64_t      *pi_64;
287  
288     switch( i_query )
289     {
290         * *
291         case ACCESS_SET_PAUSE_STATE:
292             * Nothing to do *
293             break;
294
295         case ACCESS_CAN_SEEK:
296         case ACCESS_CAN_FASTSEEK:
297         case ACCESS_CAN_PAUSE:
298         case ACCESS_CAN_CONTROL_PACE:
299         case ACCESS_GET_MTU:
300         case ACCESS_GET_PTS_DELAY:
301         case ACCESS_GET_TITLE_INFO:
302         case ACCESS_SET_TITLE:
303         case ACCESS_SET_SEEKPOINT:
304         case ACCESS_SET_PRIVATE_ID_STATE:
305         case ACCESS_GET_CONTENT_TYPE:
306             return VLC_EGENERIC;
307  
308         default:
309             msg_Warn( p_access, "unimplemented query in control" );
310             return VLC_EGENERIC;
311  
312     }
313     return VLC_SUCCESS;*/
314     return VLC_EGENERIC;
315 }