]> git.sesse.net Git - vlc/blob - bin/winvlc.c
demux: ts: ensure iod is present when looking for SLDesc
[vlc] / bin / winvlc.c
1 /*****************************************************************************
2  * winvlc.c: the Windows VLC media player
3  *****************************************************************************
4  * Copyright (C) 1998-2011 the VideoLAN team
5  *
6  * Authors: Vincent Seguin <seguin@via.ecp.fr>
7  *          Samuel Hocevar <sam@zoy.org>
8  *          Gildas Bazin <gbazin@videolan.org>
9  *          Derk-Jan Hartman <hartman at videolan dot org>
10  *          Lots of other people, see the libvlc AUTHORS file
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #ifndef UNICODE
32 #define UNICODE
33 #endif
34
35 #include <vlc/vlc.h>
36 #include <windows.h>
37 #include <shellapi.h>
38
39 #ifndef _WIN32_IE
40 #  define  _WIN32_IE 0x501
41 #endif
42 #include <fcntl.h>
43 #include <io.h>
44 #include <shlobj.h>
45 #include <wininet.h>
46 #define PSAPI_VERSION 1
47 #include <psapi.h>
48 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
49 static void check_crashdump(void);
50 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
51 static const wchar_t *crashdump_path;
52
53 static char *FromWide (const wchar_t *wide)
54 {
55     size_t len;
56     len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
57
58     char *out = (char *)malloc (len);
59     if (out)
60         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
61     return out;
62 }
63
64 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
65                     LPSTR lpCmdLine,
66                     int nCmdShow )
67 {
68     int argc;
69
70     /* VLC does not change the thread locale, so gettext/libintil will use the
71      * user default locale as reference. */
72     /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
73      * if the GETTEXT_MUI environment variable is set. If not set or if running
74      * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
75      * putenv() call may become redundant with later versions of gettext. */
76     putenv("GETTEXT_MUI=1");
77 #ifdef TOP_BUILDDIR
78     putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
79     putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
80 #endif
81
82     SetErrorMode(SEM_FAILCRITICALERRORS);
83     HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
84
85     /* SetProcessDEPPolicy */
86     HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
87     if(h_Kernel32)
88     {
89         BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
90         BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
91 # define PROCESS_DEP_ENABLE 1
92
93         mySetProcessDEPPolicy = (BOOL (WINAPI *)(DWORD))
94                             GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
95         if(mySetProcessDEPPolicy)
96             mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
97
98         /* Do NOT load any library from cwd. */
99         mySetDllDirectoryA = (BOOL (WINAPI *)(const char*))
100                             GetProcAddress(h_Kernel32, "SetDllDirectoryA");
101         if(mySetDllDirectoryA)
102             mySetDllDirectoryA("");
103
104         FreeLibrary(h_Kernel32);
105     }
106
107     /* Args */
108     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
109     if (wargv == NULL)
110         return 1;
111
112     char *argv[argc + 3];
113     BOOL crash_handling = TRUE;
114     int j = 0;
115     char *lang = NULL;
116
117     argv[j++] = FromWide( L"--media-library" );
118     argv[j++] = FromWide( L"--no-ignore-config" );
119     for (int i = 1; i < argc; i++)
120     {
121         if(!wcscmp(wargv[i], L"--no-crashdump"))
122         {
123             crash_handling = FALSE;
124             continue; /* don't give argument to libvlc */
125         }
126         if (!wcsncmp(wargv[i], L"--language", 10) )
127         {
128             if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
129                 lang = FromWide (wargv[++i]);
130             continue;
131         }
132
133         argv[j++] = FromWide (wargv[i]);
134     }
135
136     argc = j;
137     argv[argc] = NULL;
138     LocalFree (wargv);
139
140     if(crash_handling)
141     {
142         static wchar_t path[MAX_PATH];
143         if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
144                     NULL, SHGFP_TYPE_CURRENT, path ) )
145             fprintf( stderr, "Can't open the vlc conf PATH\n" );
146         _snwprintf( path+wcslen( path ), MAX_PATH,  L"%s", L"\\vlc\\crashdump" );
147         crashdump_path = &path[0];
148
149         check_crashdump();
150         SetUnhandledExceptionFilter(vlc_exception_filter);
151     }
152
153     _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
154
155     /* */
156     if (!lang)
157     {
158         HKEY h_key;
159         if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
160                 == ERROR_SUCCESS )
161         {
162             TCHAR szData[256];
163             DWORD len = 256;
164             if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
165                 lang = FromWide( szData );
166         }
167     }
168
169     if (lang && strncmp( lang, "auto", 4 ) )
170     {
171         char tmp[11];
172         snprintf(tmp, 11, "LANG=%s", lang);
173         putenv(tmp);
174     }
175     free(lang);
176
177     /* Initialize libvlc */
178     libvlc_instance_t *vlc;
179     vlc = libvlc_new (argc, (const char **)argv);
180     if (vlc != NULL)
181     {
182         libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
183                            PACKAGE_NAME);
184         libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
185         libvlc_add_intf (vlc, "hotkeys,none");
186         libvlc_add_intf (vlc, "globalhotkeys,none");
187         libvlc_add_intf (vlc, NULL);
188         libvlc_playlist_play (vlc, -1, 0, NULL);
189         libvlc_wait (vlc);
190         libvlc_release (vlc);
191     }
192     else
193         MessageBox (NULL, TEXT("VLC media player could not start.\n"
194                     "Either the command line options were invalid or no plugins were found.\n"),
195                     TEXT("VLC media player"),
196                     MB_OK|MB_ICONERROR);
197
198
199     for (int i = 0; i < argc; i++)
200         free (argv[i]);
201
202     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
203     return 0;
204 }
205
206 /* Crashdumps handling */
207 static void check_crashdump(void)
208 {
209     wchar_t mv_crashdump_path[MAX_PATH];
210     wcscpy (mv_crashdump_path, crashdump_path);
211     wcscat (mv_crashdump_path, L".mv");
212
213     if (_wrename (crashdump_path, mv_crashdump_path))
214         return;
215
216     FILE * fd = _wfopen ( mv_crashdump_path, L"r, ccs=UTF-8" );
217     if( !fd )
218         return;
219     fclose( fd );
220
221     int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
222     "Would you like to send a bug report to the developers team?",
223     L"VLC crash reporting", MB_YESNO);
224
225     if(answer == IDYES)
226     {
227         HINTERNET Hint = InternetOpen(L"VLC Crash Reporter",
228                 INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
229         if(Hint)
230         {
231             HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org",
232                         INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
233                         INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
234             if(ftp)
235             {
236                 SYSTEMTIME now;
237                 GetSystemTime(&now);
238                 wchar_t remote_file[MAX_PATH];
239                 _snwprintf(remote_file, MAX_PATH,
240                         L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
241                         now.wYear, now.wMonth, now.wDay, now.wHour,
242                         now.wMinute, now.wSecond );
243
244                 if( FtpPutFile( ftp, mv_crashdump_path, remote_file,
245                             FTP_TRANSFER_TYPE_BINARY, 0) )
246                     MessageBox( NULL, L"Report sent correctly. Thanks a lot " \
247                                 "for the help.", L"Report sent", MB_OK);
248                 else
249                     MessageBox( NULL, L"There was an error while "\
250                                 "transferring the data to the FTP server.\n"\
251                                 "Thanks a lot for the help.",
252                                 L"Report sending failed", MB_OK);
253                 InternetCloseHandle(ftp);
254             }
255             else
256             {
257                 MessageBox( NULL, L"There was an error while connecting to " \
258                                 "the FTP server. "\
259                                 "Thanks a lot for the help.",
260                                 L"Report sending failed", MB_OK);
261                 fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
262                         (unsigned long)GetLastError());
263             }
264             InternetCloseHandle(Hint);
265         }
266         else
267         {
268               MessageBox( NULL, L"There was an error while connecting to the Internet.\n"\
269                                 "Thanks a lot for the help anyway.",
270                                 L"Report sending failed", MB_OK);
271         }
272     }
273
274     _wremove(mv_crashdump_path);
275 }
276
277 /*****************************************************************************
278  * vlc_exception_filter: handles unhandled exceptions, like segfaults
279  *****************************************************************************/
280 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
281 {
282     if(IsDebuggerPresent())
283     {
284         //If a debugger is present, pass the exception to the debugger
285         //with EXCEPTION_CONTINUE_SEARCH
286         return EXCEPTION_CONTINUE_SEARCH;
287     }
288     else
289     {
290         fprintf( stderr, "unhandled vlc exception\n" );
291
292         FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
293
294         if( !fd )
295         {
296             fprintf( stderr, "\nerror while opening file" );
297             exit( 1 );
298         }
299
300         OSVERSIONINFO osvi;
301         ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
302         osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
303         GetVersionEx( &osvi );
304
305         fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE,
306                 osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
307                 osvi.dwPlatformId, osvi.szCSDVersion);
308
309         const CONTEXT *const pContext = (const CONTEXT *)
310             lpExceptionInfo->ContextRecord;
311         const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
312             lpExceptionInfo->ExceptionRecord;
313         /* No nested exceptions for now */
314         fwprintf( fd, L"\n\n[exceptions]\n%08x at %px", 
315                 pException->ExceptionCode, pException->ExceptionAddress );
316
317         for( unsigned int i = 0; i < pException->NumberParameters; i++ )
318             fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
319
320 #ifdef _WIN64
321         fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
322                     "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
323                     "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
324                     "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
325                     "R13:%px\nR14:%px\nR15:%px\n",
326                         pContext->Rdi,pContext->Rsi,pContext->Rbx,
327                         pContext->Rdx,pContext->Rcx,pContext->Rax,
328                         pContext->Rbp,pContext->Rip,pContext->Rsp,
329                         pContext->R8,pContext->R9,pContext->R10,
330                         pContext->R11,pContext->R12,pContext->R13,
331                         pContext->R14,pContext->R15 );
332 #else
333         fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
334                     "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
335                     "EBP:%px\nEIP:%px\nESP:%px\n",
336                         pContext->Edi,pContext->Esi,pContext->Ebx,
337                         pContext->Edx,pContext->Ecx,pContext->Eax,
338                         pContext->Ebp,pContext->Eip,pContext->Esp );
339 #endif
340
341         fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
342
343 #ifdef _WIN64
344         LPCVOID caller = (LPCVOID)pContext->Rip;
345         LPVOID *pBase  = (LPVOID*)pContext->Rbp;
346 #else
347         LPVOID *pBase  = (LPVOID*)pContext->Ebp;
348         LPCVOID caller = (LPCVOID)pContext->Eip;
349 #endif
350         for( unsigned frame = 0; frame <= 100; frame++ )
351         {
352             MEMORY_BASIC_INFORMATION mbi;
353             wchar_t module[ 256 ];
354             VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
355             GetModuleFileName( mbi.AllocationBase, module, 256 );
356             fwprintf( fd, L"%p|%ls\n", caller, module );
357
358             if( IsBadReadPtr( pBase, 2 * sizeof( void* ) ) )
359                 break;
360
361             /*The last BP points to NULL!*/
362             caller = *(pBase + 1);
363             if( !caller )
364                 break;
365             pBase = *pBase;
366             if( !pBase )
367                 break;
368         }
369
370         HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
371                                         FALSE, GetCurrentProcessId());
372         if (hpid) {
373             HMODULE mods[1024];
374             DWORD size;
375             if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
376                 fwprintf( fd, L"\n\n[modules]\n" );
377                 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
378                     wchar_t module[ 256 ];
379                     GetModuleFileName(mods[i], module, 256);
380                     fwprintf( fd, L"%p|%ls\n", mods[i], module);
381                 }
382             }
383             CloseHandle(hpid);
384         }
385
386         fclose( fd );
387         fflush( stderr );
388         exit( 1 );
389     }
390 }