alsa: sets period size before buffer size, patch by Matthias P. Nowak
[vlc.git] / src / vlc.c
blob11a7c632d29826d61d3e58cc65f3d640edefa93b
1 /*****************************************************************************
2 * vlc.c: the vlc player
3 *****************************************************************************
4 * Copyright (C) 1998-2004 the VideoLAN team
5 * $Id$
7 * Authors: Vincent Seguin <seguin@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Gildas Bazin <gbazin@videolan.org>
10 * Derk-Jan Hartman <hartman at videolan dot org>
11 * Lots of other people, see the libvlc AUTHORS file
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #include "config.h"
30 #include <vlc/vlc.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <locale.h>
34 #ifdef __GLIBC__
35 #include <dlfcn.h>
36 #endif
39 /* Explicit HACK */
40 extern void LocaleFree (const char *);
41 extern char *FromLocale (const char *);
44 /*****************************************************************************
45 * Local prototypes.
46 *****************************************************************************/
47 #ifdef WIN32
48 #include <windows.h>
49 extern void __wgetmainargs(int *argc, wchar_t ***wargv, wchar_t ***wenviron,
50 int expand_wildcards, int *startupinfo);
51 static inline void Kill(void) { }
52 #else
54 # include <signal.h>
55 # include <time.h>
56 # include <pthread.h>
58 static void Kill (void);
59 static void *SigHandler (void *set);
60 #endif
62 /*****************************************************************************
63 * main: parse command line, start interface and spawn threads.
64 *****************************************************************************/
65 int main( int i_argc, const char *ppsz_argv[] )
67 int i_ret;
69 # ifdef __GLIBC__
70 if (dlsym (RTLD_NEXT, "inet6_rth_add") && !dlsym (RTLD_NEXT, "qsort_r"))
72 /* Way too many Linux users have glibc 2.5-2.7 that keeps crashing
73 * inside its non-thread-safe dcgettext(). */
74 /* Hopefully glibc 2.8 will eventually work, not sure though */
75 fprintf (stderr,
76 "***************************************************\n"
77 "*** glibc version with broken libintl detected. ***\n"
78 "*** Messages localization will be disabled. ***\n"
79 "***************************************************\n");
80 setenv ("LC_MESSAGES", "C", 1);
82 # endif
83 setlocale (LC_ALL, "");
85 #ifndef __APPLE__
86 /* This clutters OSX GUI error logs */
87 fprintf( stderr, "VLC media player %s\n", VLC_Version() );
88 #endif
90 #ifdef HAVE_PUTENV
91 # ifdef DEBUG
92 /* Activate malloc checking routines to detect heap corruptions. */
93 putenv( (char*)"MALLOC_CHECK_=2" );
95 /* Disable the ugly Gnome crash dialog so that we properly segfault */
96 putenv( (char *)"GNOME_DISABLE_CRASH_DIALOG=1" );
97 # endif
98 #endif
100 #if defined (HAVE_GETEUID) && !defined (SYS_BEOS)
101 /* FIXME: rootwrap (); */
102 #endif
104 /* Create a libvlc structure */
105 i_ret = VLC_Create();
106 if( i_ret < 0 )
108 return -i_ret;
111 #if !defined(WIN32) && !defined(UNDER_CE)
112 /* Synchronously intercepted POSIX signals.
114 * In a threaded program such as VLC, the only sane way to handle signals
115 * is to block them in all thread but one - this is the only way to
116 * predict which thread will receive them. If any piece of code depends
117 * on delivery of one of this signal it is intrinsically not thread-safe
118 * and MUST NOT be used in VLC, whether we like it or not.
119 * There is only one exception: if the signal is raised with
120 * pthread_kill() - we do not use this in LibVLC but some pthread
121 * implementations use them internally. You should really use conditions
122 * for thread synchronization anyway.
124 * Signal that request a clean shutdown, and force an unclean shutdown
125 * if they are triggered again 2+ seconds later.
126 * We have to handle SIGTERM cleanly because of daemon mode.
127 * Note that we set the signals after the vlc_create call. */
128 static const int exitsigs[] = { SIGINT, SIGHUP, SIGQUIT, SIGTERM };
129 /* Signals that cause a no-op:
130 * - SIGALRM should not happen, but lets stay on the safe side.
131 * - SIGPIPE might happen with sockets and would crash VLC. It MUST be
132 * blocked by any LibVLC-dependant application, in addition to VLC.
133 * - SIGCHLD is comes after exec*() (such as httpd CGI support) and must
134 * be dequeued to cleanup zombie processes.
136 static const int dummysigs[] = { SIGALRM, SIGPIPE, SIGCHLD };
138 sigset_t set;
139 pthread_t sigth;
141 sigemptyset (&set);
142 for (unsigned i = 0; i < sizeof (exitsigs) / sizeof (exitsigs[0]); i++)
143 sigaddset (&set, exitsigs[i]);
144 for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
145 sigaddset (&set, dummysigs[i]);
147 /* Block all these signals */
148 pthread_sigmask (SIG_BLOCK, &set, NULL);
150 for (unsigned i = 0; i < sizeof (dummysigs) / sizeof (dummysigs[0]); i++)
151 sigdelset (&set, dummysigs[i]);
153 pthread_create (&sigth, NULL, SigHandler, &set);
154 #endif
156 #ifdef WIN32
157 /* Replace argv[1..n] with unicode for Windows NT and above */
158 if( GetVersion() < 0x80000000 )
160 wchar_t **wargv, **wenvp;
161 int i,i_wargc;
162 int si = { 0 };
163 __wgetmainargs(&i_wargc, &wargv, &wenvp, 0, &si);
165 for( i = 0; i < i_wargc; i++ )
167 int len = WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, NULL, 0, NULL, NULL);
168 if( len > 0 )
170 if( len > 1 ) {
171 char *utf8arg = (char *)malloc(len);
172 if( NULL != utf8arg )
174 WideCharToMultiByte(CP_UTF8, 0, wargv[i], -1, utf8arg, len, NULL, NULL);
175 ppsz_argv[i] = utf8arg;
177 else
179 /* failed!, quit */
180 return 1;
183 else
185 ppsz_argv[i] = strdup ("");
188 else
190 /* failed!, quit */
191 return 1;
195 else
196 #endif
198 for (int i = 0; i < i_argc; i++)
199 if ((ppsz_argv[i] = FromLocale (ppsz_argv[i])) == NULL)
200 return 1; // BOOM!
203 /* Initialize libvlc */
204 i_ret = VLC_Init( 0, i_argc, ppsz_argv );
205 if( i_ret < 0 )
207 VLC_Destroy( 0 );
208 return i_ret == VLC_EEXITSUCCESS ? 0 : -i_ret;
211 i_ret = VLC_AddIntf( 0, NULL, VLC_TRUE, VLC_TRUE );
213 /* Finish the threads */
214 VLC_CleanUp( 0 );
216 Kill ();
218 /* Destroy the libvlc structure */
219 VLC_Destroy( 0 );
221 for (int i = 0; i < i_argc; i++)
222 LocaleFree (ppsz_argv[i]);
224 #if !defined(WIN32) && !defined(UNDER_CE)
225 pthread_cancel (sigth);
226 # ifdef __APPLE__
227 /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
228 * cancellation point, so we throw a dummy quit signal to end
229 * sigwait() in the sigth thread */
230 pthread_kill (sigth, SIGQUIT);
231 # endif
232 pthread_join (sigth, NULL);
233 #endif
235 return -i_ret;
238 #if !defined(WIN32) && !defined(UNDER_CE)
239 /*****************************************************************************
240 * SigHandler: system signal handler
241 *****************************************************************************
242 * This thread receives all handled signals synchronously.
243 * It tries to end the program in a clean way.
244 *****************************************************************************/
245 static void *SigHandler (void *data)
247 const sigset_t *exitset = (sigset_t *)data;
248 sigset_t fullset;
249 time_t abort_time = 0;
251 pthread_sigmask (SIG_BLOCK, exitset, &fullset);
253 for (;;)
255 int i_signal, state;
256 if( sigwait (&fullset, &i_signal) != 0 )
257 continue;
259 #ifdef __APPLE__
260 /* In Mac OS X up to 10.4.8 sigwait (among others) is not a pthread
261 * cancellation point */
262 pthread_testcancel();
263 #endif
265 if (!sigismember (exitset, i_signal))
266 continue; /* Ignore "dummy" signals */
268 /* Once a signal has been trapped, the termination sequence will be
269 * armed and subsequent signals will be ignored to avoid sending
270 * signals to a libvlc structure having been destroyed */
272 pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
273 if (abort_time == 0)
275 time (&abort_time);
276 abort_time += 2;
278 fprintf (stderr, "signal %d received, terminating vlc - do it "
279 "again in case it gets stuck\n", i_signal);
281 /* Acknowledge the signal received */
282 Kill ();
284 else
285 if (time (NULL) <= abort_time)
287 /* If user asks again more than 2 seconds later, die badly */
288 pthread_sigmask (SIG_UNBLOCK, exitset, NULL);
289 fprintf (stderr, "user insisted too much, dying badly\n");
290 abort ();
292 pthread_setcancelstate (state, NULL);
294 /* Never reached */
298 static void KillOnce (void)
300 VLC_Die (0);
303 static void Kill (void)
305 static pthread_once_t once = PTHREAD_ONCE_INIT;
306 pthread_once (&once, KillOnce);
309 #endif
311 #if defined(UNDER_CE)
312 # if defined( _MSC_VER ) && defined( UNDER_CE )
313 # include "vlc_common.h"
314 # endif
315 /*****************************************************************************
316 * WinMain: parse command line, start interface and spawn threads. (WinCE only)
317 *****************************************************************************/
318 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
319 LPTSTR lpCmdLine, int nCmdShow )
321 char **argv, psz_cmdline[MAX_PATH];
322 int argc, i_ret;
324 WideCharToMultiByte( CP_ACP, 0, lpCmdLine, -1,
325 psz_cmdline, MAX_PATH, NULL, NULL );
327 argv = vlc_parse_cmdline( psz_cmdline, &argc );
328 argv = realloc( argv, (argc + 1) * sizeof(char *) );
329 if( !argv ) return -1;
331 if( argc ) memmove( argv + 1, argv, argc * sizeof(char *) );
332 argv[0] = ""; /* Fake program path */
334 i_ret = main( argc + 1, argv );
336 /* No need to free the argv memory */
337 return i_ret;
339 #endif