[vlc-devel] [PATCH] doc: libvlc: add a simple win32 app using set_hwnd()

Steve Lhomme robux4 at ycbcr.xyz
Wed May 6 10:28:28 CEST 2020


---
 doc/libvlc/win_player.c | 148 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 148 insertions(+)
 create mode 100644 doc/libvlc/win_player.c

diff --git a/doc/libvlc/win_player.c b/doc/libvlc/win_player.c
new file mode 100644
index 000000000000..41061d936c4e
--- /dev/null
+++ b/doc/libvlc/win_player.c
@@ -0,0 +1,148 @@
+/* compile: cc win_player.c -o win_player.exe -L<path/libvlc> -lvlc */
+
+#include <windows.h>
+#include <assert.h>
+
+#include <vlc/vlc.h>
+
+#define SCREEN_WIDTH  1500
+#define SCREEN_HEIGHT  900
+#define BORDER_LEFT    (-0.95f)
+#define BORDER_RIGHT   ( 0.85f)
+#define BORDER_TOP     ( 0.95f)
+#define BORDER_BOTTOM  (-0.90f)
+
+#define check_leak(x)  assert(x)
+
+struct render_context
+{
+    HWND hWnd;
+    CRITICAL_SECTION sizeLock; // the ReportSize callback cannot be called during/after the Cleanup_cb is called
+    unsigned width, height;
+    void (*ReportSize)(void *ReportOpaque, unsigned width, unsigned height);
+    void *ReportOpaque;
+};
+
+static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
+{
+    if( message == WM_CREATE )
+    {
+        /* Store p_mp for future use */
+        CREATESTRUCT *c = (CREATESTRUCT *)lParam;
+        SetWindowLongPtr( hWnd, GWLP_USERDATA, (LONG_PTR)c->lpCreateParams );
+        return 0;
+    }
+
+    LONG_PTR p_user_data = GetWindowLongPtr( hWnd, GWLP_USERDATA );
+    if( p_user_data == 0 )
+        return DefWindowProc(hWnd, message, wParam, lParam);
+    struct render_context *ctx = (struct render_context *)p_user_data;
+
+    switch(message)
+    {
+        case WM_SIZE:
+        {
+            /* tell libvlc that our size has changed */
+            ctx->width  = LOWORD(lParam) * (BORDER_RIGHT - BORDER_LEFT) / 2.0f; /* remove the orange part ! */
+            ctx->height = HIWORD(lParam) * (BORDER_TOP - BORDER_BOTTOM) / 2.0f;
+            EnterCriticalSection(&ctx->sizeLock);
+            if (ctx->ReportSize != NULL)
+                ctx->ReportSize(ctx->ReportOpaque, ctx->width, ctx->height);
+            LeaveCriticalSection(&ctx->sizeLock);
+        }
+        break;
+
+        case WM_DESTROY:
+            PostQuitMessage(0);
+            return 0;
+    }
+
+    return DefWindowProc (hWnd, message, wParam, lParam);
+}
+
+int WINAPI WinMain(HINSTANCE hInstance,
+                   HINSTANCE hPrevInstance,
+                   LPSTR lpCmdLine,
+                   int nCmdShow)
+{
+    WNDCLASSEX wc;
+    struct render_context Context = { };
+    char *file_path;
+    libvlc_instance_t *p_libvlc;
+    libvlc_media_t *p_media;
+    libvlc_media_player_t *p_mp;
+    (void)hPrevInstance;
+
+    /* remove "" around the given path */
+    if (lpCmdLine[0] == '"')
+    {
+        file_path = strdup( lpCmdLine+1 );
+        if (file_path[strlen(file_path)-1] == '"')
+            file_path[strlen(file_path)-1] = '\0';
+    }
+    else
+        file_path = strdup( lpCmdLine );
+
+    p_libvlc = libvlc_new( 0, NULL );
+    p_media = libvlc_media_new_path( p_libvlc, file_path );
+    free( file_path );
+    p_mp = libvlc_media_player_new_from_media( p_media );
+
+    InitializeCriticalSection(&Context.sizeLock);
+
+    ZeroMemory(&wc, sizeof(WNDCLASSEX));
+
+    wc.cbSize = sizeof(WNDCLASSEX);
+    wc.style = CS_HREDRAW | CS_VREDRAW;
+    wc.lpfnWndProc = WindowProc;
+    wc.hInstance = hInstance;
+    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
+    wc.lpszClassName = "WindowClass";
+
+    RegisterClassEx(&wc);
+
+    RECT wr = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
+    AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
+
+    Context.hWnd = CreateWindowEx(0,
+                          "WindowClass",
+                          "libvlc Demo app",
+                          WS_OVERLAPPEDWINDOW,
+                          CW_USEDEFAULT, CW_USEDEFAULT,
+                          wr.right - wr.left,
+                          wr.bottom - wr.top,
+                          NULL,
+                          NULL,
+                          hInstance,
+                          &Context);
+
+    ShowWindow(Context.hWnd, nCmdShow);
+
+    libvlc_media_player_set_hwnd(p_mp, Context.hWnd);
+
+    libvlc_media_player_play( p_mp );
+
+    MSG msg;
+
+    while(TRUE)
+    {
+        if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
+        {
+            TranslateMessage(&msg);
+            DispatchMessage(&msg);
+
+            if(msg.message == WM_QUIT)
+                break;
+        }
+    }
+
+    libvlc_media_player_stop_async( p_mp );
+
+    libvlc_media_player_release( p_mp );
+    libvlc_media_release( p_media );
+    libvlc_release( p_libvlc );
+
+    DeleteCriticalSection(&Context.sizeLock);
+
+    return msg.wParam;
+}
-- 
2.17.1



More information about the vlc-devel mailing list