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