Click here to Skip to main content
Click here to Skip to main content

VLCWrapper - A Little C++-wrapper Around libvlc

By , 12 Mar 2012
 
VLCWrapper/VLCWrapper1.jpg

Introduction

This article presents a little C++-wrapper for the libvlc-library, which is the core component of the VLC media player. I was looking for an easy way to integrate video playback in my C++ applications. Because I've been using VLC for media playback for many years now, I started playing around with the VLC API. The result is a little wrapper around the libvlc-library. It provides basic media playback functionality. Since v.2.0 the  libvlc-library is released under the LGPL, so you can use it in commercial applications. All sourcecode in this arcticle is licensed under the CPOL. Please excuse any bugs because this wrapper is more a quick hack than a feature complete wrapper. ;)

For testing purposes, I wrote a simple media player which uses the VLCWrapper. It's included in the example above.

Background

The most valuable source of information about the VLC API for me was the developer section on the VLC website and the documented C-headers of the VLC source. The project website is a good starting point if you plan to code something with VLC or extend the VLCWrapper.

The C++-Interface of VLCWrapper to LIBVLC

The interface declared in "VLCWrapper.h" is small and the member functions are quite self-explanatory. To get a good overlook, I stripped of the comments. Check out the sources for more information.  

class VLCWrapper
{
    std::auto_ptr<VLCWrapperImpl> pImpl_;
 
public:
    VLCWrapper();
    ~VLCWrapper();
    void SetOutputWindow(void* pHwnd);
    void SetEventHandler(VLCEventHandler event, void* pUserData);
    void OpenMedia(const char* pMediaPathName);
    void Play();
    void Pause();
    void Stop();
    int64_t GetLength();
    int64_t GetTime();
    void SetTime(int64_t newTime);
    void Mute(bool mute = true);
    bool GetMute();
    int  GetVolume();
    void SetVolume(int volume);
};  

Using the Code

Using the VLCWrapper is easy. The first step would be to add a new VLCWraper as a member of your window class and a CStatic for video output:

#include "VLCWrapper.h"

class CVlcDialogDlg : public CDialog
{
    VLCWrapper vlcPlayer_; /// Our VLCWrapper.
    .
    .
    //{{AFX_DATA(CVlcDialogDlg)
    CStatic vlcControl_;  /// This CStatic will be used as device context by libvlc.
    . . . 
    //}}AFX_DATA

Now we can initialize the VLCWrapper in OnInitDialog():

BOOL CVlcDialogDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    vlcPlayer_.SetOutputWindow((void*)vlcControl_.GetSafeHwnd()); // the CStatic will be 
							          // used for video output

    vlcPlayer_.SetEventHandler(&HandleVLCEvents, this);           // Set handler for vlc events

The handler function for VLC events:

static void HandleVLCEvents(const VLCEvent* pEvent, void* pUserData)
{
    CVlcDialogDlg* pDlg = reinterpret_cast<CVlcDialogDlg*>(pUserData); 
 
    switch(pEvent->type)
    {
    case libvlc_MediaPlayerTimeChanged:
       TRACE("VLC_EVT_TIME_CHANGED: new_time %d[ms]\n",
              pEvent->u.media_player_time_changed.new_time);
       if(pDlg)
           pDlg->UpdatePosition();
       break;
    } 
}

Now everything is in place, and you can start playing media files. E.g., the "Load" and "Play" members look like this:

void CVlcDialogDlg::OnBnClickedButtonLoad()
{
    CFileDialog dlgFile(TRUE);
    if(dlgFile.DoModal()==IDOK)
    {
        CString file=dlgFile.GetPathName();
        vlcPlayer_.OpenMedia((LPCTSTR)file);
        vlcPlayer_.Play();  // start media after loading....
    }
}

void CVlcDialogDlg::OnBnClickedButtonPlay()
{
    vlcPlayer_.Play();
}

History

  • 03/12/2012 
    • Updated the project to use the VLC API 2.0.0.
    • Changed license to CPOL.  
  • 09/20/2011 
    • Added project files for Visual Studio 2010. 
  • 09/11/2010
    • Some bugfixes and optimizations, thanks to heretic13!
    • Build the demo with VLC 1.1.4.
    • Removed demo & source links to old VLCDialog versions which required VLC < 1.1.
  • 05/29/2010
    • Updated the project to use the VLC API 1.1
    • The wrapper now uses an auto_ptr for its Pimpl. I missed deleting the Pimpl in the first versions
    • Fixed a redraw error
    • Thanks to Haim Engler for the hint on the memory leak and the redraw problem!
  • 08/20/2009
    • Pimped VlcDialog (Resizing, Icon-Buttons)
    • Striped off dependency to "StdAfx.h" in class VLCWrapper and class VLCWrapperImpl
    • Replaced a deprecated function call to libvlc; now SetOutputWindow(void*) takes a void pointer to a window handle
  • 08/14/2009
    • Some fixes in the example listings
  • 08/11/2009
    • Initial release

License

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

About the Author

Alex Skoruppa
NOKIA Location & Commerce
Germany Germany
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanksmemberMaxwellG9 May '13 - 1:38 
This has saved me so much time. Thanks for sharing. Dankeschön.
Questionhelpmemberangel_boom28 Mar '13 - 0:18 
hello!
I am newcomer in this field.
I like your article abort VLC.
However,not work for me.I find that the return value find libvlc_new is NULL.
Do you know why??
GeneralMy vote of 5memberBartlomiej Filipek23 Mar '13 - 5:20 
I had an occasion to use this wrapper in my project. Great that you managed to port it to newer version of VLC.
QuestionProblems with http accessmemberMember 959508022 Nov '12 - 4:44 
Hello,
 
Very nice wrapper !
 
I've juste replaced
(LPCTSTR)file
in the following line :
vlcPlayer_.OpenMedia((LPCTSTR)file);
by the URL or address of the source to test.
 
It's working fine with rtsp protocol and IP camera, with screen://// or with the path of video file in my disk.
 
But if I use an internet source like Youtube, it doesn't work !
vlcPlayer_.OpenMedia("http://www.youtube.com/watch?v=b-3oWajA6ZQ");
 
Do you know why ???
 
regards
AnswerRe: Problems with http accessmemberMember 959508022 Nov '12 - 21:39 
Even if I replace in OpenMedia method
pMedia_ = libvlc_media_new_path(pVLCInstance_, pMediaPathName);
by
pMedia_ = libvlc_media_new_location(pVLCInstance_, pMediaPathName);
which seems to be indicated for valid URL, the internet video isn't displayed.
 
However, libvlc_media_player_play API doesn't return error.
 
I really don't understand why it doesn't work with such URL.
GeneralMy vote of 2membermshahbazm11 Nov '12 - 2:10 
Not working for me
QuestionCDialog and CStaticmemberhasnain26639 Nov '12 - 10:27 
i can not inherit my app's class from public CDialog,what am i missing>
and i also cant declare CStatic,what could be wrong?
 
Please elaborate a little on how and what files i should include.
I am including the headers mentioned in the article.And they do get included without any problem.
SuggestionLoop the playbackmemberSkywalker200825 Sep '12 - 10:47 
Excellent! Thanks.
 
In my application, I need play a movie and auto-repeat the playing until the Stop button is hit. Here is how I did based on the VLCWrapper in this article:
 
1. Define a couple of more members in class VLCWrapperImpl:
libvlc_media_list_t* pMediaList_;
libvlc_media_list_player_t* pMdeiaListPlayer_;
 
2. In the VLCWrapperImpl constructor, add
 
pMediaList_ = libvlc_media_list_new(pVLCInstance_);
pMdeiaListPlayer_ = libvlc_media_list_player_new(pVLCInstance_);
 
libvlc_media_list_player_set_media_list(pMdeiaListPlayer_, pMediaList_);
libvlc_media_list_player_set_media_player(pMdeiaListPlayer_, pMediaPlayer_);
libvlc_media_list_player_set_playback_mode(pMdeiaListPlayer_, libvlc_playback_mode_loop);
 

3. In VLCWrapperImpl::OpenMedia(...), replace
libvlc_media_player_set_media(pMediaPlayer_, pMedia_);
with
libvlc_media_list_add_media(pMediaList_, pMedia_);
 
4. In VLCWrapperImpl::Play(), replace with
libvlc_media_list_player_play(pMdeiaListPlayer_);
Similar changes apply in ::Pause() and ::Stop().
 

Just share it here in case someone wants to know.
 

Shhu
QuestionActiver le désentrelacementmemberjeanmarc0424 Sep '12 - 5:22 
Bel article merci
Comment activer le désentrelacement?
Je seche
Merci
QuestionCreating project from beginmemberaleks301 Sep '12 - 23:36 
Solved
1. Create new project MFC Aplication
 
2. Aplication Type Dialog Based
- Use MFC in shared dll
- Use Unicode libraries
 
3. copy VLCWrapper.cpp, VLCWrapper.h, VLCWrapperImpl.cpp, VLCWrapperImpl.h
in Project folder
find stdint.h in internet and put in Project folder
 
4. from http://www.videolan.org/vlc/ Download latest VLC zip package, Unzip,copy from VLC folder to project folder
sdk\lib -> to project folder name Lib
sdk\include\vlc -> to project folder name vlc
plugins (.dll) -> to project folder name plugins, to Release and Debug folder name plugins
copy libvlc.dll, libvlc.dll.manifest, libvlccore.dll -> to project folder, to Release and Debug folder
create emptu folder lua in project folder
 
5. Modify Project propertis for Debug and Release
Project-> General Aditional include Directories : .\,.\vlc
Linker -> General -> Additional Libray Directories: .\lib
Linker -> Input -> Additional Dependences : libvlc.lib libvlccore.lib
 
6. Modify Project propertis for Release
Linker -> Optimization -> Keep Unreferenced Data: (/OPT:NOREF)
 
7. Modify Solution Explorer -> Source Files -> Properties only for VLCWrapperImpl.cpp, VLCWrapper.cpp
Properties -> C/C++ -> Precompilied Header -> Create/Use Precompilied Header : Not Using Precompilied Header
 
8. in xxDlg.cpp put #include
in xxDlg.h put #include "VLCWrapper.h"
 
9. put LGPL licenz, autors for used source in project
 
10. for unicode bulid change
CString file = FileDlg.GetPathName();
// Kind of like a symbol of UTF-16 convertible is usually not more than 3 char in UTF-8
char pszMediaPathNameUTF8 [1024 * 3];
WideCharToMultiByte (CP_UTF8, 0, file, -1, pszMediaPathNameUTF8, _countof (pszMediaPathNameUTF8), NULL, NULL);
vlcPlayer_.OpenMedia( pszMediaPathNameUTF8);
 
10. build project

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 12 Mar 2012
Article Copyright 2009 by Alex Skoruppa
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid