]> git.sesse.net Git - vlc/blob - bin/darwinvlc.c
wasapi: audio capture client module (fixes #7205)
[vlc] / bin / darwinvlc.c
1 /*****************************************************************************
2  * darwinvlc.c: the darwin-specific VLC player
3  *****************************************************************************
4  * Copyright (C) 1998-2013 the VideoLAN team
5  * $Id$
6  *
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
12  *
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.
17  *
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.
22  *
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  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc/vlc.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <locale.h>
38 #include <signal.h>
39 #ifdef HAVE_PTHREAD_H
40 # include <pthread.h>
41 #endif
42 #include <unistd.h>
43 #include <TargetConditionals.h>
44 #import <CoreFoundation/CoreFoundation.h>
45
46 extern void vlc_enable_override (void);
47
48 static bool signal_ignored (int signum)
49 {
50     struct sigaction sa;
51
52     if (sigaction (signum, NULL, &sa))
53         return false;
54     return ((sa.sa_flags & SA_SIGINFO)
55             ? (void *)sa.sa_sigaction : (void *)sa.sa_handler) == SIG_IGN;
56 }
57
58 static void vlc_kill (void *data)
59 {
60     pthread_t *ps = data;
61     pthread_kill (*ps, SIGTERM);
62 }
63
64 static void exit_timeout (int signum)
65 {
66     (void) signum;
67     signal (SIGINT, SIG_DFL);
68 }
69
70 /*****************************************************************************
71  * main: parse command line, start interface and spawn threads.
72  *****************************************************************************/
73 int main( int i_argc, const char *ppsz_argv[] )
74 {
75     /* The so-called POSIX-compliant MacOS X reportedly processes SIGPIPE even
76      * if it is blocked in all thread.
77      * Note: this is NOT an excuse for not protecting against SIGPIPE. If
78      * LibVLC runs outside of VLC, we cannot rely on this code snippet. */
79     signal (SIGPIPE, SIG_IGN);
80     /* Restore SIGCHLD in case our parent process ignores it. */
81     signal (SIGCHLD, SIG_DFL);
82
83 #ifndef NDEBUG
84     /* Activate malloc checking routines to detect heap corruptions. */
85     setenv ("MALLOC_CHECK_", "2", 1);
86 #endif
87
88 #ifdef TOP_BUILDDIR
89     setenv ("VLC_PLUGIN_PATH", TOP_BUILDDIR"/modules", 1);
90     setenv ("VLC_DATA_PATH", TOP_SRCDIR"/share", 1);
91 #endif
92
93 #ifndef ALLOW_RUN_AS_ROOT
94     if (geteuid () == 0)
95     {
96         fprintf (stderr, "VLC is not supposed to be run as root. Sorry.\n"
97         "If you need to use real-time priorities and/or privileged TCP ports\n"
98         "you can use %s-wrapper (make sure it is Set-UID root and\n"
99         "cannot be run by non-trusted users first).\n", ppsz_argv[0]);
100         return 1;
101     }
102 #endif
103
104     setlocale (LC_ALL, "");
105
106     if (isatty (STDERR_FILENO))
107         /* This message clutters error logs. It is printed only on a TTY.
108          * Fortunately, LibVLC prints version info with -vv anyway. */
109         fprintf (stderr, "VLC media player %s (revision %s)\n",
110                  libvlc_get_version(), libvlc_get_changeset());
111
112     sigset_t set;
113
114     sigemptyset (&set);
115     /* VLC uses sigwait() to dequeue interesting signals.
116      * For this to work, those signals must be blocked in all threads,
117      * including the thread calling sigwait() (see the man page for details).
118      *
119      * There are two advantages to sigwait() over traditional signal handlers:
120      *  - delivery is synchronous: no need to worry about async-safety,
121      *  - EINTR is not generated: other threads need not handle that error.
122      * That being said, some LibVLC programs do not use sigwait(). Therefore
123      * EINTR must still be handled cleanly, notably from poll() calls.
124      *
125      * Signals that request a clean shutdown, and force an unclean shutdown
126      * if they are triggered again 2+ seconds later.
127      * We have to handle SIGTERM cleanly because of daemon mode. */
128     sigaddset (&set, SIGINT);
129     sigaddset (&set, SIGHUP);
130     sigaddset (&set, SIGQUIT);
131     sigaddset (&set, SIGTERM);
132
133     /* SIGPIPE can happen and would crash the process. On modern systems,
134      * the MSG_NOSIGNAL flag protects socket write operations against SIGPIPE.
135      * But we still need to block SIGPIPE when:
136      *  - writing to pipes,
137      *  - using write() instead of send() for code not specific to sockets.
138      * LibVLC code assumes that SIGPIPE is blocked. Other LibVLC applications
139      * shall block it (or handle it somehow) too.
140      */
141     sigaddset (&set, SIGPIPE);
142
143     /* SIGCHLD must be dequeued to clean up zombie child processes.
144      * Furthermore the handler must not be set to SIG_IGN (see above).
145      * We cannot pragmatically handle EINTR, short reads and short writes
146      * in every code paths (including underlying libraries). So we just
147      * block SIGCHLD in all threads, and dequeue it below. */
148     sigaddset (&set, SIGCHLD);
149
150     /* Block all these signals */
151     pthread_sigmask (SIG_SETMASK, &set, NULL);
152
153     const char *argv[i_argc + 2];
154     int argc = 0;
155
156     argv[argc++] = "--no-ignore-config";
157     argv[argc++] = "--media-library";
158
159     /* overwrite system language on Mac */
160 #if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR // TARGET_OS_MAC is unspecific
161     char *lang = NULL;
162
163     for (int i = 0; i < i_argc; i++) {
164         if (!strncmp(ppsz_argv[i], "--language", 10)) {
165             lang = strstr(ppsz_argv[i], "=");
166             ppsz_argv++, i_argc--;
167             continue;
168         }
169     }
170     if (lang && strncmp( lang, "auto", 4 )) {
171         char tmp[11];
172         snprintf(tmp, 11, "LANG%s", lang);
173         putenv(tmp);
174     }
175
176     if (!lang) {
177         CFStringRef language;
178         language = (CFStringRef)CFPreferencesCopyAppValue(CFSTR("language"),
179                                                           kCFPreferencesCurrentApplication);
180         if (language) {
181             CFIndex length = CFStringGetLength(language) + 1;
182             if (length > 0) {
183                 CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
184                 lang = (char *)malloc(maxSize);
185                 CFStringGetCString(language, lang, maxSize - 1, kCFStringEncodingUTF8);
186             }
187             if (strncmp( lang, "auto", 4 )) {
188                 char tmp[11];
189                 snprintf(tmp, 11, "LANG=%s", lang);
190                 putenv(tmp);
191             }
192             CFRelease(language);
193         }
194     }
195 #endif
196
197     ppsz_argv++; i_argc--; /* skip executable path */
198
199     /* When VLC.app is run by double clicking in Mac OS X, the 2nd arg
200      * is the PSN - process serial number (a unique PID-ish thingie)
201      * still ok for real Darwin & when run from command line
202      * for example -psn_0_9306113 */
203     if (i_argc >= 1 && !strncmp (*ppsz_argv, "-psn" , 4))
204         ppsz_argv++, i_argc--;
205
206     memcpy (argv + argc, ppsz_argv, i_argc * sizeof (*argv));
207     argc += i_argc;
208     argv[argc] = NULL;
209
210     vlc_enable_override ();
211
212     pthread_t self = pthread_self ();
213
214     /* Initialize libvlc */
215     libvlc_instance_t *vlc = libvlc_new (argc, argv);
216     if (vlc == NULL)
217         return 1;
218
219     int ret = 1;
220     libvlc_set_exit_handler (vlc, vlc_kill, &self);
221     libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION, PACKAGE_NAME);
222     libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
223
224     libvlc_playlist_play (vlc, -1, 0, NULL);
225
226     libvlc_add_intf (vlc, "hotkeys,none");
227
228     if (libvlc_add_intf (vlc, NULL))
229         goto out;
230
231     /* Qt4 insists on catching SIGCHLD via signal handler. To work around that,
232      * unblock it after all our child threads are created. */
233     sigdelset (&set, SIGCHLD);
234     pthread_sigmask (SIG_SETMASK, &set, NULL);
235
236     /* Do not dequeue SIGHUP if it is ignored (nohup) */
237     if (signal_ignored (SIGHUP))
238         sigdelset (&set, SIGHUP);
239     /* Ignore SIGPIPE */
240     sigdelset (&set, SIGPIPE);
241
242     int signum;
243     sigwait (&set, &signum);
244
245     /* Restore default signal behaviour after 3 seconds */
246     sigemptyset (&set);
247     sigaddset (&set, SIGINT);
248     sigaddset (&set, SIGALRM);
249     signal (SIGINT, SIG_IGN);
250     signal (SIGALRM, exit_timeout);
251     pthread_sigmask (SIG_UNBLOCK, &set, NULL);
252     alarm (3);
253
254     ret = 0;
255     /* Cleanup */
256 out:
257     libvlc_release (vlc);
258
259     return ret;
260 }