]> git.sesse.net Git - vlc/blobdiff - bin/winvlc.c
demux: ts: only interpolate PCR on missing PCR
[vlc] / bin / winvlc.c
index a2d66162523fea27ab86f9176bdc5e71bcadc97e..33784e294d41c12914bc7ede89871ecfbd6bc62b 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
- * winvlc.c: the Windows VLC player
+ * winvlc.c: the Windows VLC media player
  *****************************************************************************
- * Copyright (C) 1998-2008 the VideoLAN team
+ * Copyright (C) 1998-2011 the VideoLAN team
  *
  * Authors: Vincent Seguin <seguin@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
 # include "config.h"
 #endif
 
+#ifndef UNICODE
 #define UNICODE
+#endif
+
 #include <vlc/vlc.h>
-#include <string.h>
-#include <stdio.h>
-#include <stdlib.h>
 #include <windows.h>
+#include <shellapi.h>
 
-#if !defined(UNDER_CE)
-#   define  _WIN32_IE 0x500
-#   include <shlobj.h>
-#   include <tlhelp32.h>
-#   include <wininet.h>
-static void check_crashdump();
-LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
+#ifndef _WIN32_IE
+#  define  _WIN32_IE 0x501
 #endif
+#include <fcntl.h>
+#include <io.h>
+#include <shlobj.h>
+#include <wininet.h>
+#define PSAPI_VERSION 1
+#include <psapi.h>
+#define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
+static void check_crashdump(void);
+LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
+static const wchar_t *crashdump_path;
 
-#ifndef UNDER_CE
 static char *FromWide (const wchar_t *wide)
 {
     size_t len;
@@ -55,168 +60,218 @@ static char *FromWide (const wchar_t *wide)
         WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
     return out;
 }
-#else
-static int parse_cmdline (char *line, char ***argvp)
-{
-    char **argv = malloc (sizeof (char *));
-    int argc = 0;
-
-    while (*line != '\0')
-    {
-        char quote = 0;
 
-        /* Skips white spaces */
-        while (strchr ("\t ", *line))
-            line++;
-        if (!*line)
-            break;
-
-        /* Starts a new parameter */
-        argv = realloc (argv, (argc + 2) * sizeof (char *));
-        if (*line == '"')
-        {
-            quote = '"';
-            line++;
-        }
-        argv[argc++] = line;
+int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
+                    LPSTR lpCmdLine,
+                    int nCmdShow )
+{
+    int argc;
+
+    /* VLC does not change the thread locale, so gettext/libintil will use the
+     * user default locale as reference. */
+    /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
+     * if the GETTEXT_MUI environment variable is set. If not set or if running
+     * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
+     * putenv() call may become redundant with later versions of gettext. */
+    putenv("GETTEXT_MUI=1");
+#ifdef TOP_BUILDDIR
+    putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
+    putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
+#endif
 
-    more:
-            while (*line && !strchr ("\t ", *line))
-            line++;
+    SetErrorMode(SEM_FAILCRITICALERRORS);
+    HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
 
-    if (line > argv[argc - 1] && line[-1] == quote)
-        /* End of quoted parameter */
-        line[-1] = 0;
-    else
-        if (*line && quote)
+    /* SetProcessDEPPolicy */
+    HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
+    if(h_Kernel32)
     {
-        /* Space within a quote */
-        line++;
-        goto more;
+        BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
+        BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
+# define PROCESS_DEP_ENABLE 1
+
+        mySetProcessDEPPolicy = (BOOL (WINAPI *)(DWORD))
+                            GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
+        if(mySetProcessDEPPolicy)
+            mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
+
+        /* Do NOT load any library from cwd. */
+        mySetDllDirectoryA = (BOOL (WINAPI *)(const char*))
+                            GetProcAddress(h_Kernel32, "SetDllDirectoryA");
+        if(mySetDllDirectoryA)
+            mySetDllDirectoryA("");
+
+        FreeLibrary(h_Kernel32);
     }
-    else
-        /* End of unquoted parameter */
-        if (*line)
-            *line++ = 0;
-    }
-    argv[argc] = NULL;
-    *argvp = argv;
-    return argc;
-}
-#endif
 
-int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
-#ifndef UNDER_CE
-                    LPSTR lpCmdLine,
-#else
-                    LPWSTR lpCmdLine,
-#endif
-                    int nCmdShow )
-{
-    int argc, ret;
-#ifndef UNDER_CE
+    /* Args */
     wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
     if (wargv == NULL)
         return 1;
 
-    char *argv[argc + 1];
-    for (int i = 0; i < argc; i++)
-        argv[i] = FromWide (wargv[i]);
+    char *argv[argc + 3];
+    BOOL crash_handling = TRUE;
+    int j = 0;
+    char *lang = NULL;
+
+    argv[j++] = FromWide( L"--media-library" );
+    argv[j++] = FromWide( L"--no-ignore-config" );
+    for (int i = 1; i < argc; i++)
+    {
+        if(!wcscmp(wargv[i], L"--no-crashdump"))
+        {
+            crash_handling = FALSE;
+            continue; /* don't give argument to libvlc */
+        }
+        if (!wcsncmp(wargv[i], L"--language", 10) )
+        {
+            if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
+                lang = FromWide (wargv[++i]);
+            continue;
+        }
+
+        argv[j++] = FromWide (wargv[i]);
+    }
+
+    argc = j;
     argv[argc] = NULL;
     LocalFree (wargv);
-#else
-    char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
 
-    WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
-                         psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
+    if(crash_handling)
+    {
+        static wchar_t path[MAX_PATH];
+        if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
+                    NULL, SHGFP_TYPE_CURRENT, path ) )
+            fprintf( stderr, "Can't open the vlc conf PATH\n" );
+        _snwprintf( path+wcslen( path ), MAX_PATH,  L"%s", L"\\vlc\\crashdump" );
+        crashdump_path = &path[0];
+
+        check_crashdump();
+        SetUnhandledExceptionFilter(vlc_exception_filter);
+    }
 
-    argc = parse_cmdline (psz_cmdline, &argv);
-#endif
+    _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
 
-    libvlc_exception_t ex, dummy;
-    libvlc_exception_init (&ex);
-    libvlc_exception_init (&dummy);
+    /* */
+    if (!lang)
+    {
+        HKEY h_key;
+        if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
+                == ERROR_SUCCESS )
+        {
+            TCHAR szData[256];
+            DWORD len = 256;
+            if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
+                lang = FromWide( szData );
+        }
+    }
 
-#if !defined( UNDER_CE )
-    check_crashdump();
-    SetUnhandledExceptionFilter(vlc_exception_filter);
-#endif
+    if (lang && strncmp( lang, "auto", 4 ) )
+    {
+        char tmp[11];
+        snprintf(tmp, 11, "LANG=%s", lang);
+        putenv(tmp);
+    }
+    free(lang);
 
     /* Initialize libvlc */
     libvlc_instance_t *vlc;
-    vlc = libvlc_new (argc - 1, (const char **)argv + 1, &ex);
+    vlc = libvlc_new (argc, (const char **)argv);
     if (vlc != NULL)
     {
-        libvlc_add_intf (vlc, "globalhotkeys,none", &ex);
-        libvlc_add_intf (vlc, NULL, &ex);
-        libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
+        libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
+                           PACKAGE_NAME);
+        libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
+        libvlc_add_intf (vlc, "hotkeys,none");
+        libvlc_add_intf (vlc, "globalhotkeys,none");
+        libvlc_add_intf (vlc, NULL);
+        libvlc_playlist_play (vlc, -1, 0, NULL);
         libvlc_wait (vlc);
         libvlc_release (vlc);
     }
+    else
+        MessageBox (NULL, TEXT("VLC media player could not start.\n"
+                    "Either the command line options were invalid or no plugins were found.\n"),
+                    TEXT("VLC media player"),
+                    MB_OK|MB_ICONERROR);
 
-    ret = libvlc_exception_raised (&ex);
-    libvlc_exception_clear (&ex);
-    libvlc_exception_clear (&dummy);
 
     for (int i = 0; i < argc; i++)
         free (argv[i]);
 
     (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
-    return ret;
+    return 0;
 }
 
-#if !defined( UNDER_CE )
-
-static void get_crashdump_path(wchar_t * wdir)
+/* Crashdumps handling */
+static void check_crashdump(void)
 {
-    if( S_OK != SHGetFolderPathW( NULL,
-                        CSIDL_APPDATA | CSIDL_FLAG_CREATE,
-                        NULL, SHGFP_TYPE_CURRENT, wdir ) )
-        fprintf( stderr, "Can't open the vlc conf PATH\n" );
+    wchar_t mv_crashdump_path[MAX_PATH];
+    wcscpy (mv_crashdump_path, crashdump_path);
+    wcscat (mv_crashdump_path, L".mv");
 
-    swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
-}
+    if (_wrename (crashdump_path, mv_crashdump_path))
+        return;
 
-static void check_crashdump()
-{
-    wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
-    get_crashdump_path(wdir);
+    FILE * fd = _wfopen ( mv_crashdump_path, L"r, ccs=UTF-8" );
+    if( !fd )
+        return;
+    fclose( fd );
 
-    FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
-    if( fd )
-    {
-        fclose( fd );
-        int answer = MessageBox( NULL, L"VLC media player just crashed." \
-        " Do you want to send a bug report to the developers team?",
-        L"VLC crash reporting", MB_YESNO);
+    int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
+    "Would you like to send a bug report to the developers team?",
+    L"VLC crash reporting", MB_YESNO);
 
-        if(answer == IDYES)
+    if(answer == IDYES)
+    {
+        HINTERNET Hint = InternetOpen(L"VLC Crash Reporter",
+                INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
+        if(Hint)
         {
-            HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
-            if(Hint)
+            HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org",
+                        INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
+                        INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
+            if(ftp)
             {
-                HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
-                                                NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
-                if(ftp)
-                {
-                    SYSTEMTIME now;
-                    GetSystemTime(&now);
-                    wchar_t remote_file[MAX_PATH];
-                    swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
-                            now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond  );
-
-                    FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
-                    InternetCloseHandle(ftp);
-                }
+                SYSTEMTIME now;
+                GetSystemTime(&now);
+                wchar_t remote_file[MAX_PATH];
+                _snwprintf(remote_file, MAX_PATH,
+                        L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
+                        now.wYear, now.wMonth, now.wDay, now.wHour,
+                        now.wMinute, now.wSecond );
+
+                if( FtpPutFile( ftp, mv_crashdump_path, remote_file,
+                            FTP_TRANSFER_TYPE_BINARY, 0) )
+                    MessageBox( NULL, L"Report sent correctly. Thanks a lot " \
+                                "for the help.", L"Report sent", MB_OK);
                 else
-                    fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
-                InternetCloseHandle(Hint);
+                    MessageBox( NULL, L"There was an error while "\
+                                "transferring the data to the FTP server.\n"\
+                                "Thanks a lot for the help.",
+                                L"Report sending failed", MB_OK);
+                InternetCloseHandle(ftp);
+            }
+            else
+            {
+                MessageBox( NULL, L"There was an error while connecting to " \
+                                "the FTP server. "\
+                                "Thanks a lot for the help.",
+                                L"Report sending failed", MB_OK);
+                fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
+                        (unsigned long)GetLastError());
             }
+            InternetCloseHandle(Hint);
+        }
+        else
+        {
+              MessageBox( NULL, L"There was an error while connecting to the Internet.\n"\
+                                "Thanks a lot for the help anyway.",
+                                L"Report sending failed", MB_OK);
         }
-
-        _wremove(wdir);
     }
-    free((void *)wdir);
+
+    _wremove(mv_crashdump_path);
 }
 
 /*****************************************************************************
@@ -226,17 +281,15 @@ LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
 {
     if(IsDebuggerPresent())
     {
-        //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
+        //If a debugger is present, pass the exception to the debugger
+        //with EXCEPTION_CONTINUE_SEARCH
         return EXCEPTION_CONTINUE_SEARCH;
     }
     else
     {
         fprintf( stderr, "unhandled vlc exception\n" );
 
-        wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
-        get_crashdump_path(wdir);
-        FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
-        free((void *)wdir);
+        FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
 
         if( !fd )
         {
@@ -249,57 +302,89 @@ LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
         osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
         GetVersionEx( &osvi );
 
-        fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
-                                                               osvi.dwMinorVersion,
-                                                               osvi.dwBuildNumber,
-                                                               osvi.dwPlatformId,
-                                                               osvi.szCSDVersion);
-
-        const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
-        const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
-        /*No nested exceptions for now*/
-        fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
-                                                pException->ExceptionAddress );
-        if( pException->NumberParameters > 0 )
-        {
-            unsigned int i;
-            for( i = 0; i < pException->NumberParameters; i++ )
-                fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
-        }
-
-        fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
-                    "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
-                    "EBP:%08x\nEIP:%08x\nESP:%08x\n",
+        fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE,
+                osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
+                osvi.dwPlatformId, osvi.szCSDVersion);
+
+        const CONTEXT *const pContext = (const CONTEXT *)
+            lpExceptionInfo->ContextRecord;
+        const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
+            lpExceptionInfo->ExceptionRecord;
+        /* No nested exceptions for now */
+        fwprintf( fd, L"\n\n[exceptions]\n%08x at %px", 
+                pException->ExceptionCode, pException->ExceptionAddress );
+
+        for( unsigned int i = 0; i < pException->NumberParameters; i++ )
+            fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
+
+#ifdef _WIN64
+        fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
+                    "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
+                    "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
+                    "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
+                    "R13:%px\nR14:%px\nR15:%px\n",
+                        pContext->Rdi,pContext->Rsi,pContext->Rbx,
+                        pContext->Rdx,pContext->Rcx,pContext->Rax,
+                        pContext->Rbp,pContext->Rip,pContext->Rsp,
+                        pContext->R8,pContext->R9,pContext->R10,
+                        pContext->R11,pContext->R12,pContext->R13,
+                        pContext->R14,pContext->R15 );
+#else
+        fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
+                    "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
+                    "EBP:%px\nEIP:%px\nESP:%px\n",
                         pContext->Edi,pContext->Esi,pContext->Ebx,
                         pContext->Edx,pContext->Ecx,pContext->Eax,
                         pContext->Ebp,pContext->Eip,pContext->Esp );
+#endif
 
         fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
 
-        wchar_t module[ 256 ];
-        MEMORY_BASIC_INFORMATION mbi ;
-        VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
-        HINSTANCE hInstance = mbi.AllocationBase;
-        GetModuleFileName( hInstance, module, 256 ) ;
-        fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
-
-        DWORD pEbp = pContext->Ebp;
-        DWORD caller = *((DWORD*)pEbp + 1);
-
-        do
+#ifdef _WIN64
+        LPCVOID caller = (LPCVOID)pContext->Rip;
+        LPVOID *pBase  = (LPVOID*)pContext->Rbp;
+#else
+        LPVOID *pBase  = (LPVOID*)pContext->Ebp;
+        LPCVOID caller = (LPCVOID)pContext->Eip;
+#endif
+        for( unsigned frame = 0; frame <= 100; frame++ )
         {
-            VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
-            HINSTANCE hInstance = mbi.AllocationBase;
-            GetModuleFileName( hInstance, module, 256 ) ;
-            fwprintf( fd, L"%08x|%s\n", caller, module );
-            pEbp = *(DWORD*)pEbp ;
-            caller = *((DWORD*)pEbp + 1) ;
-            /*The last EBP points to NULL!*/
-        }while(caller);
+            MEMORY_BASIC_INFORMATION mbi;
+            wchar_t module[ 256 ];
+            VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
+            GetModuleFileName( mbi.AllocationBase, module, 256 );
+            fwprintf( fd, L"%p|%ls\n", caller, module );
+
+            if( IsBadReadPtr( pBase, 2 * sizeof( void* ) ) )
+                break;
+
+            /*The last BP points to NULL!*/
+            caller = *(pBase + 1);
+            if( !caller )
+                break;
+            pBase = *pBase;
+            if( !pBase )
+                break;
+        }
+
+        HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
+                                        FALSE, GetCurrentProcessId());
+        if (hpid) {
+            HMODULE mods[1024];
+            DWORD size;
+            if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
+                fwprintf( fd, L"\n\n[modules]\n" );
+                for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
+                    wchar_t module[ 256 ];
+                    GetModuleFileName(mods[i], module, 256);
+                    fwprintf( fd, L"%p|%ls\n", mods[i], module);
+                }
+            }
+            CloseHandle(hpid);
+        }
 
         fclose( fd );
         fflush( stderr );
         exit( 1 );
     }
 }
-#endif