Click here to Skip to main content
15,898,939 members
Articles / Desktop Programming / MFC

VLCWrapper - A Little C++-wrapper Around libvlc

Rate me:
Please Sign up or sign in to vote.
4.91/5 (49 votes)
12 Mar 2012CPOL3 min read 465.1K   25.9K   169   127
An article on wrapping libvlc (VLC media player) in a C++-class
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.  

C++
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:

C++
#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():

C++
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:

C++
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:

C++
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)


Written By
Technical Lead HERE, a Nokia Business
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice app - anyone have this in vb.net? if so please post. thanx Pin
bwarehouse12-Mar-12 23:18
bwarehouse12-Mar-12 23:18 
Question.libs included in 2.0.0 Pin
Member 85397381-Mar-12 3:46
Member 85397381-Mar-12 3:46 
QuestionVLC 2.0 Pin
Davide Zaccanti22-Feb-12 7:57
Davide Zaccanti22-Feb-12 7:57 
NewslibVLC is switching to LGPL Pin
Julien Lebot5-Feb-12 22:39
Julien Lebot5-Feb-12 22:39 
GeneralRe: libVLC is switching to LGPL Pin
Alex Skoruppa5-Feb-12 22:57
Alex Skoruppa5-Feb-12 22:57 
GeneralRe: libVLC is switching to LGPL Pin
2374112-Mar-12 4:55
2374112-Mar-12 4:55 
QuestionHelp with the Streaming API for VLC ... Pin
daved900014-Dec-11 7:39
daved900014-Dec-11 7:39 
AnswerRe: Help with the Streaming API for VLC ... Pin
daved900015-Dec-11 8:21
daved900015-Dec-11 8:21 
Ok, so ... never mind. I figured out 'how to fish' on my own. It turns out its 'pretty' easy to stream from libVLC, but the wiki and documentation are a little cryptic if you aren't already familiar with streaming concepts and methods, etc.

the required code changes are here, i just edited the VLCWrapperImpl::OpenMedia method as follows:

void VLCWrapperImpl::OpenMedia(const char* pszMediaPathName)
{
const char *media_name = "mybroadcast";
libvlc_instance_t *vlc;
char sout[1024] = "#transcode{vcodec=h264,vb=0,scale=0}:http{dst=:8080/go.mpg}";

vlc = libvlc_new(0, NULL);
libvlc_vlm_add_broadcast(vlc, media_name, pszMediaPathName, sout, 0, NULL, true, false);
libvlc_vlm_play_media(vlc, media_name);

Sleep(1000 * 45);

libvlc_vlm_stop_media(vlc, media_name);
libvlc_vlm_release(vlc);
MessageBox(NULL, "Video Streaming Complete", "Done", MB_OK);
}

A couple of notes on what i did above.
1. my aim was to stream a chosen file vs play it locally. (I will work on making it play locally while streaming next, but this isn't essential for my effort)
2. To keep things 'simple', i created a new VLC instance each time, and trashed it when i was done.
This may prove problematic down stream, (all the creating and releasing) so i'll likely revisit
this concept as well, i.e. try to resuse the main instance created w/ the vlc wrapper class
3. i was just looking to HTTP broadcast a video segment (no sound) to several remote PCs in the
same subnet, not over the internet (i have no idea how much more complicated that is?)
4. in this way, i can view the stream locally via: "vlc http://127.0.0.1:8080/go.mpg"
or remotely using "vlc http://xxx.xxx.xxx.xxx:8080/go.mpg", where the xxx's are the real ip
address of the host machine
5. i am fairly sure adding the audio portions just involves some monkeying w/ the transcoding part
"#transcode{vcodec=h264,vb=0,scale=0)" ... see the documentation for this.
6. another next step is to move some of this code to spread these functions out, so that the open
button just loads the video, and the "play' begins the streaming (and possibly the displays it
here also?). the stop will stop the stream.
7. clearly what i have above is 'crude' and just a 'proof of concept' test, but many times thats all
thats needed to make your way to what you want/need.

again, i thank you for your work here, this gave me the 'shoulders to stand on' so i could see what i needed to see. I am on my way to incorporating the streaming function into my application.

Dave Doyle
GeneralRe: Help with the Streaming API for VLC ... Pin
huntkao15-Aug-12 23:45
huntkao15-Aug-12 23:45 
QuestionPassing options to hide title Pin
Davide Zaccanti5-Nov-11 9:34
Davide Zaccanti5-Nov-11 9:34 
AnswerRe: Passing options to hide title Pin
Le Roy7-Nov-11 21:06
Le Roy7-Nov-11 21:06 
QuestionRelease bug in Visual Studio 2003 or 2008. Pin
Member 260660318-Sep-11 21:00
Member 260660318-Sep-11 21:00 
AnswerRe: Release bug in Visual Studio 2003 or 2008. Pin
ironfrocodeproject20-Jul-12 1:28
ironfrocodeproject20-Jul-12 1:28 
Questioncropping Pin
labinator21-May-11 3:42
labinator21-May-11 3:42 
GeneralMy vote of 5 Pin
Global Analyser8-Mar-11 6:30
Global Analyser8-Mar-11 6:30 
GeneralMy vote of 4 Pin
xComaWhitex7-Jan-11 23:43
xComaWhitex7-Jan-11 23:43 
QuestionHow to use VLCWrapper to open video/music file from a URL? Pin
huutribk20015-Jan-11 16:37
huutribk20015-Jan-11 16:37 
AnswerRe: How to use VLCWrapper to open video/music file from a URL? Pin
MarkDoubson8-Oct-11 18:21
MarkDoubson8-Oct-11 18:21 
Questionlibvlc_new returns null Pin
Ivan616423-Oct-10 12:33
Ivan616423-Oct-10 12:33 
AnswerRe: libvlc_new returns null Pin
Member 6264827-Oct-10 7:29
Member 6264827-Oct-10 7:29 
GeneralRe: libvlc_new returns null Pin
raddog14-Nov-10 7:38
raddog14-Nov-10 7:38 
AnswerRe: libvlc_new returns null Pin
raddog14-Nov-10 7:39
raddog14-Nov-10 7:39 
GeneralSome BugFixes Pin
heretic138-Aug-10 23:45
heretic138-Aug-10 23:45 
GeneralPlayback Youtube videos Pin
Hari.net30-Jun-10 23:34
Hari.net30-Jun-10 23:34 
Questioncan it use in window 2003? Pin
sffofn11123-May-10 15:35
sffofn11123-May-10 15:35 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.