Click here to Skip to main content
15,888,461 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using libvlc to display the video stream after capturing the ts fragments exposed by m3u8 index files. I have never used libvlc before. According some tutorials, the following lines:
C++
libvlc_instance_t* inst;
libvlc_media_player_t* mp;
libvlc_media_t* m;

inst=libvlc_new(0,NULL);
m=libvlc_media_new_path(inst,"...");
mp=libvlc_media_player_new_from_media(m);

libvlc_media_release(m);

//For windows. binding the displaying window.
libvlc_media_player_set_hwnd(mp,hwnd);

libvlc_media_player_play(mp);
Sleep(3000);

libvlc_media_player_stop(mp);
libvlc_media_player_release(mp);
libvlc_release(inst);

After following those codes, the window just flickered once and exited. I don't know why yet.

What I have tried:

My code :
C++
<pre>int test(HWND hwnd)
{
    //const char* version=libvlc_get_version();
    //const char* compiler=libvlc_get_compiler();
    //const char* changeset=libvlc_get_changeset();
    
    //printf("Libvlc.\n");
    //printf("Version:%s\n",version);
    //printf("Compiler:%s\n",compiler);
    //printf("Changeset:%s\n",changeset);
    
    libvlc_instance_t* inst;
    libvlc_media_player_t* mp;
    libvlc_media_t* m;
    
    inst=libvlc_new(0,NULL);
    m=libvlc_media_new_path(inst,"my video resource file path...");
    mp=libvlc_media_player_new_from_media(m);
    
    libvlc_media_release(m);
    
    //For windows. binding the displaying window.
    libvlc_media_player_set_hwnd(mp,hwnd);
    
    libvlc_media_player_play(mp);
    Sleep(3000);
    
    libvlc_media_player_stop(mp);
    libvlc_media_player_release(mp);
    libvlc_release(inst);
    
    return 0;
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, PSTR szcmdLine, int icmdshow)
{
    HWND hwnd=NULL;
    
    InitCommonControls();
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    
    if(Frame_Register(hinstance)!=0) {
        MessageBox(NULL,"window register failed","Sorry",MB_OK|MB_ICONERROR);
        return 0;
    }
    if(NULL==(hwnd=Frame_Create(hinstance,1200,600))) return 0;
    
    Frame_Test(hwnd);
    
    return Frame_Run(hwnd);
}

int regist_window(char* class_name,HINSTANCE instance,WNDPROC proc)
{
    WNDCLASSEX wndclass;

    wndclass.cbSize=sizeof(wndclass);
    wndclass.style = CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW;
    wndclass.lpfnWndProc = proc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = instance;
    wndclass.hIcon = NULL;
    wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0,0,0));
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = class_name;
    wndclass.hIconSm = NULL;

    if(!RegisterClassEx(&wndclass)) return -1;
    return 0;
}

HWND Frame_Create(HINSTANCE instance,int width,int height)
{
    int cx,cy;
    POINT pt;
    
    cx=GetSystemMetrics(SM_CXSCREEN);
    cy=GetSystemMetrics(SM_CYSCREEN);
    
    pt.x=(cx-width)>>1;
    pt.y=(cy-height)>>1;
    
    HWND hwnd=CreateWindowEx(NULL,
                             WINDOW_CLASS_NAME,
                             "Docker Frame Capture",
                             WS_OVERLAPPEDWINDOW|WS_VISIBLE|WS_HSCROLL|WS_CLIPCHILDREN,
                             pt.x,pt.y,width,height,NULL,
                             (HMENU)NULL,
                             instance,NULL);

    // Some owner-drawing and data refferring to the customized window.
    Frame_InitialSettings(hwnd);
    
    return hwnd;
}

int Frame_Run(HWND frame)
{
    MSG msg;

    while(GetMessage(&msg,NULL,0,0)) {
        if(!IsDialogMessage(frame,&msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    
    GdiplusShutdown(gdiplusToken);
    
    return(msg.wParam);
}

int Frame_Test(HWND hwnd)
{
    //create a child window , as a container for vlc player.
    RECT rc;
    GetClientRect(hwnd,&rc);
    int width=rc.right-rc.left,height=rc.bottom-rc.top;
    
    HWND vlc_hwnd=CreateWindowEx(NULL,
                             WC_STATIC,
                             "",
                             WS_CHILD|WS_VISIBLE,
                             0,0,width,height,hwnd,
                             (HMENU)NULL,//LoadMenu(instance,"IDR_FRMENU"),
                             (HINSTANCE)GetWindowLongPtr(hwnd,GWLP_HINSTANCE),NULL);
    
    test(vlc_hwnd);
    return 0;
}
Posted
Updated 27-Jun-21 20:33pm

1 solution

When you start playing you must wait til the data is played - you leave to soon. Get the playtime and wait for it.

Take also a look at this Github project which uses the play API in an interesting flavor.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900