demux: ts: only seek on pcr for current program
[vlc.git] / bin / winvlc.c
blobcd7ee95fe29d318a0a9d98e7011a26dc825e61c8
1 /*****************************************************************************
2 * winvlc.c: the Windows VLC media player
3 *****************************************************************************
4 * Copyright (C) 1998-2011 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #ifndef UNICODE
32 #define UNICODE
33 #endif
35 #include <vlc/vlc.h>
36 #include <windows.h>
37 #include <shellapi.h>
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;
53 static char *FromWide (const wchar_t *wide)
55 size_t len;
56 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
58 char *out = (char *)malloc (len);
59 if (out)
60 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
61 return out;
64 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
65 static BOOL SetDefaultDllDirectories_(DWORD flags)
67 HMODULE h = GetModuleHandle(TEXT("kernel32.dll"));
68 if (h == NULL)
69 return FALSE;
71 BOOL (WINAPI * SetDefaultDllDirectoriesReal)(DWORD);
73 SetDefaultDllDirectoriesReal = GetProcAddress(h,
74 "SetDefaultDllDirectories");
75 if (SetDefaultDllDirectoriesReal == NULL)
76 return FALSE;
78 return SetDefaultDllDirectoriesReal(flags);
80 # define SetDefaultDllDirectories SetDefaultDllDirectories_
82 #endif
84 static void PrioritizeSystem32(void)
86 #ifndef HAVE_PROCESS_MITIGATION_IMAGE_LOAD_POLICY
87 typedef struct _PROCESS_MITIGATION_IMAGE_LOAD_POLICY {
88 union {
89 DWORD Flags;
90 struct {
91 DWORD NoRemoteImages :1;
92 DWORD NoLowMandatoryLabelImages :1;
93 DWORD PreferSystem32Images :1;
94 DWORD ReservedFlags :29;
97 } PROCESS_MITIGATION_IMAGE_LOAD_POLICY;
98 #endif
99 #if _WIN32_WINNT < _WIN32_WINNT_WIN8
100 BOOL WINAPI (*SetProcessMitigationPolicy)(PROCESS_MITIGATION_POLICY, PVOID, SIZE_T);
101 HINSTANCE h_Kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
102 if ( !h_Kernel32 )
103 return;
104 SetProcessMitigationPolicy = (BOOL (WINAPI *)(PROCESS_MITIGATION_POLICY, PVOID, SIZE_T))
105 GetProcAddress(h_Kernel32, "SetProcessMitigationPolicy");
106 if (SetProcessMitigationPolicy == NULL)
107 return;
108 #endif
109 PROCESS_MITIGATION_IMAGE_LOAD_POLICY m = { .Flags = 0 };
110 m.PreferSystem32Images = 1;
111 SetProcessMitigationPolicy( 10 /* ProcessImageLoadPolicy */, &m, sizeof( m ) );
114 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
115 LPSTR lpCmdLine,
116 int nCmdShow )
118 int argc;
120 /* VLC does not change the thread locale, so gettext/libintil will use the
121 * user default locale as reference. */
122 /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
123 * if the GETTEXT_MUI environment variable is set. If not set or if running
124 * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
125 * putenv() call may become redundant with later versions of gettext. */
126 putenv("GETTEXT_MUI=1");
127 #ifdef TOP_BUILDDIR
128 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
129 putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
130 #endif
132 #if (_WIN32_WINNT < _WIN32_WINNT_WIN7)
133 SetErrorMode(SEM_FAILCRITICALERRORS);
134 #endif
135 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
137 /* SetProcessDEPPolicy, SetDllDirectory, & Co. */
138 HINSTANCE h_Kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
139 if (h_Kernel32 != NULL)
141 /* Enable DEP */
142 # define PROCESS_DEP_ENABLE 1
143 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
144 mySetProcessDEPPolicy = (BOOL (WINAPI *)(DWORD))
145 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
146 if(mySetProcessDEPPolicy)
147 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
149 /* Do NOT load any library from cwd. */
150 BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
151 mySetDllDirectoryA = (BOOL (WINAPI *)(const char*))
152 GetProcAddress(h_Kernel32, "SetDllDirectoryA");
153 if(mySetDllDirectoryA)
154 mySetDllDirectoryA("");
157 /***
158 * The LoadLibrary* calls from the modules and the 3rd party code
159 * will search in SYSTEM32 only
160 * */
161 SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32);
162 /***
163 * Load DLLs from system32 before any other folder (when possible)
165 PrioritizeSystem32();
167 /* Args */
168 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
169 if (wargv == NULL)
170 return 1;
172 char *argv[argc + 3];
173 BOOL crash_handling = TRUE;
174 int j = 0;
175 char *lang = NULL;
177 argv[j++] = FromWide( L"--media-library" );
178 argv[j++] = FromWide( L"--no-ignore-config" );
179 for (int i = 1; i < argc; i++)
181 if(!wcscmp(wargv[i], L"--no-crashdump"))
183 crash_handling = FALSE;
184 continue; /* don't give argument to libvlc */
186 if (!wcsncmp(wargv[i], L"--language", 10) )
188 if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
189 lang = FromWide (wargv[++i]);
190 continue;
193 argv[j++] = FromWide (wargv[i]);
196 argc = j;
197 argv[argc] = NULL;
198 LocalFree (wargv);
200 if(crash_handling)
202 static wchar_t path[MAX_PATH];
203 if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
204 NULL, SHGFP_TYPE_CURRENT, path ) )
205 fprintf( stderr, "Can't open the vlc conf PATH\n" );
206 _snwprintf( path+wcslen( path ), MAX_PATH, L"%s", L"\\vlc\\crashdump" );
207 crashdump_path = &path[0];
209 check_crashdump();
210 SetUnhandledExceptionFilter(vlc_exception_filter);
213 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
215 /* */
216 if (!lang)
218 HKEY h_key;
219 if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
220 == ERROR_SUCCESS )
222 TCHAR szData[256];
223 DWORD len = 256;
224 if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
225 lang = FromWide( szData );
229 if (lang && strncmp( lang, "auto", 4 ) )
231 char tmp[11];
232 snprintf(tmp, 11, "LANG=%s", lang);
233 putenv(tmp);
235 free(lang);
237 /* Initialize libvlc */
238 libvlc_instance_t *vlc;
239 vlc = libvlc_new (argc, (const char **)argv);
240 if (vlc != NULL)
242 libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
243 PACKAGE_NAME);
244 libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
245 libvlc_add_intf (vlc, "hotkeys,none");
246 libvlc_add_intf (vlc, "globalhotkeys,none");
247 libvlc_add_intf (vlc, NULL);
248 libvlc_playlist_play (vlc, -1, 0, NULL);
249 libvlc_wait (vlc);
250 libvlc_release (vlc);
252 else
253 MessageBox (NULL, TEXT("VLC media player could not start.\n"
254 "Either the command line options were invalid or no plugins were found.\n"),
255 TEXT("VLC media player"),
256 MB_OK|MB_ICONERROR);
259 for (int i = 0; i < argc; i++)
260 free (argv[i]);
262 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
263 return 0;
266 /* Crashdumps handling */
267 static void check_crashdump(void)
269 wchar_t mv_crashdump_path[MAX_PATH];
270 wcscpy (mv_crashdump_path, crashdump_path);
271 wcscat (mv_crashdump_path, L".mv");
273 if (_wrename (crashdump_path, mv_crashdump_path))
274 return;
276 FILE * fd = _wfopen ( mv_crashdump_path, L"r, ccs=UTF-8" );
277 if( !fd )
278 return;
279 fclose( fd );
281 int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
282 "Would you like to send a bug report to the developers team?",
283 L"VLC crash reporting", MB_YESNO);
285 if(answer == IDYES)
287 HMODULE hWininet = LoadLibrary(TEXT("wininet.dll"));
288 if (hWininet == NULL)
290 fprintf(stderr, "There was an error loading the network"
291 " 0x%08lx\n", (unsigned long)GetLastError());
292 goto done;
295 HINTERNET (WINAPI *InternetOpenW_)(LPCWSTR ,DWORD dwAccessType,LPCWSTR lpszProxy,LPCWSTR lpszProxyBypass,DWORD dwFlags);
296 HINTERNET (WINAPI *InternetConnectW_)(HINTERNET hInternet,LPCWSTR lpszServerName,INTERNET_PORT nServerPort,LPCWSTR lpszUserName,LPCWSTR lpszPassword,DWORD dwService,DWORD dwFlags,DWORD_PTR dwContext);
297 BOOL (WINAPI *InternetCloseHandle_)(HINTERNET hInternet);
298 BOOL (WINAPI *FtpPutFileW_)(HINTERNET hConnect,LPCWSTR lpszLocalFile,LPCWSTR lpszNewRemoteFile,DWORD dwFlags,DWORD_PTR dwContext);
299 InternetOpenW_ = (void*)GetProcAddress(hWininet, "InternetOpenW");
300 InternetConnectW_ = (void*)GetProcAddress(hWininet, "InternetConnectW");
301 InternetCloseHandle_ = (void*)GetProcAddress(hWininet, "InternetCloseHandle");
302 FtpPutFileW_ = (void*)GetProcAddress(hWininet, "FtpPutFileW");
303 if (!InternetOpenW_ || !InternetConnectW_ || !InternetCloseHandle_ || !FtpPutFileW_)
305 fprintf(stderr, "There was an error loading the network API entries"
306 " 0x%08lx\n", (unsigned long)GetLastError());
307 goto done;
310 HINTERNET Hint = InternetOpenW_(L"VLC Crash Reporter",
311 INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
312 if(Hint)
314 HINTERNET ftp = InternetConnectW_(Hint, L"crash.videolan.org",
315 INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
316 INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
317 if(ftp)
319 SYSTEMTIME now;
320 GetSystemTime(&now);
321 wchar_t remote_file[MAX_PATH];
322 _snwprintf(remote_file, MAX_PATH,
323 L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
324 now.wYear, now.wMonth, now.wDay, now.wHour,
325 now.wMinute, now.wSecond );
327 if( FtpPutFileW_( ftp, mv_crashdump_path, remote_file,
328 FTP_TRANSFER_TYPE_BINARY, 0) )
329 fprintf(stderr, "Report sent correctly to FTP.\n");
330 else
331 fprintf(stderr,"Couldn't send report to FTP server\n");
333 InternetCloseHandle_(ftp);
335 else
337 fprintf(stderr, "Can't connect to FTP server 0x%08lx\n",
338 (unsigned long)GetLastError());
340 InternetCloseHandle_(Hint);
342 else
344 fprintf(stderr, "There was an error while connecting to the "
345 "Internet 0x%08lx\n", (unsigned long)GetLastError());
347 done:
348 if (hWininet != NULL)
349 FreeLibrary(hWininet);
350 MessageBox( NULL, L"Thanks a lot for helping improving VLC!",
351 L"VLC crash report" , MB_OK);
354 _wremove(mv_crashdump_path);
357 /*****************************************************************************
358 * vlc_exception_filter: handles unhandled exceptions, like segfaults
359 *****************************************************************************/
360 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
362 if(IsDebuggerPresent())
364 //If a debugger is present, pass the exception to the debugger
365 //with EXCEPTION_CONTINUE_SEARCH
366 return EXCEPTION_CONTINUE_SEARCH;
368 else
370 fprintf( stderr, "unhandled vlc exception\n" );
372 FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
374 if( !fd )
376 fprintf( stderr, "\nerror while opening file" );
377 exit( 1 );
380 OSVERSIONINFO osvi;
381 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
382 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
383 GetVersionEx( &osvi );
385 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE,
386 osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
387 osvi.dwPlatformId, osvi.szCSDVersion);
389 const CONTEXT *const pContext = (const CONTEXT *)
390 lpExceptionInfo->ContextRecord;
391 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
392 lpExceptionInfo->ExceptionRecord;
393 /* No nested exceptions for now */
394 fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",
395 pException->ExceptionCode, pException->ExceptionAddress );
397 for( unsigned int i = 0; i < pException->NumberParameters; i++ )
398 fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
400 #ifdef _WIN64
401 fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
402 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
403 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
404 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
405 "R13:%px\nR14:%px\nR15:%px\n",
406 pContext->Rdi,pContext->Rsi,pContext->Rbx,
407 pContext->Rdx,pContext->Rcx,pContext->Rax,
408 pContext->Rbp,pContext->Rip,pContext->Rsp,
409 pContext->R8,pContext->R9,pContext->R10,
410 pContext->R11,pContext->R12,pContext->R13,
411 pContext->R14,pContext->R15 );
412 #else
413 fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
414 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
415 "EBP:%px\nEIP:%px\nESP:%px\n",
416 pContext->Edi,pContext->Esi,pContext->Ebx,
417 pContext->Edx,pContext->Ecx,pContext->Eax,
418 pContext->Ebp,pContext->Eip,pContext->Esp );
419 #endif
421 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
423 #ifdef _WIN64
424 LPCVOID caller = (LPCVOID)pContext->Rip;
425 LPVOID *pBase = (LPVOID*)pContext->Rbp;
426 #else
427 LPVOID *pBase = (LPVOID*)pContext->Ebp;
428 LPCVOID caller = (LPCVOID)pContext->Eip;
429 #endif
430 for( unsigned frame = 0; frame <= 100; frame++ )
432 MEMORY_BASIC_INFORMATION mbi;
433 wchar_t module[ 256 ];
434 VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
435 GetModuleFileName( mbi.AllocationBase, module, 256 );
436 fwprintf( fd, L"%p|%ls\n", caller, module );
438 if( IsBadReadPtr( pBase, 2 * sizeof( void* ) ) )
439 break;
441 /*The last BP points to NULL!*/
442 caller = *(pBase + 1);
443 if( !caller )
444 break;
445 pBase = *pBase;
446 if( !pBase )
447 break;
450 HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
451 FALSE, GetCurrentProcessId());
452 if (hpid) {
453 HMODULE mods[1024];
454 DWORD size;
455 if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
456 fwprintf( fd, L"\n\n[modules]\n" );
457 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
458 wchar_t module[ 256 ];
459 GetModuleFileName(mods[i], module, 256);
460 fwprintf( fd, L"%p|%ls\n", mods[i], module);
463 CloseHandle(hpid);
466 fclose( fd );
467 fflush( stderr );
468 exit( 1 );