Click here to Skip to main content
15,893,337 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 464.1K   25.8K   169  
An article on wrapping libvlc (VLC media player) in a C++-class
/************************************************************************
    This file is part of VLCWrapper.
    
    File:    CVlcDialogDlg.cpp
    Desc.:   CVlcDialogDlg Implementation.

    Author:  Alex Skoruppa
    Date:    08/10/2009
	Updated: 05/29/2010
    eM@il:   alex.skoruppa@googlemail.com
 
    VLCWrapper is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
     
    VLCWrapper is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.
     
    You should have received a copy of the GNU General Public License
    along with VLCWrapper.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/
#include "stdafx.h"
#include "VlcDialog.h"
#include "VlcDialogDlg.h"
#include <vlc/vlc.h>
#include "Version.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

static void* g_spCVlcDialogDlg=0;

class CAboutDlg : public CDialog
{
    CEdit     m_Licencse;
    CStatic   m_Version;
public:
	CAboutDlg();

	enum { IDD = IDD_ABOUTBOX };

	protected:
    virtual BOOL OnInitDialog();
	virtual void DoDataExchange(CDataExchange* pDX);

protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

BOOL CAboutDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
    CString csVersion;    
    csVersion.Format("Version %s", FILE_VERSIONSTRING);
    m_Version.SetWindowText(csVersion);
	CString strLicense;
	strLicense.LoadString(IDS_LICENCSE);
	m_Licencse.SetWindowText(strLicense);
	return TRUE;
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_EDIT1, m_Licencse);
    DDX_Control(pDX, IDC_STATIC_VERSION, m_Version);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


static void HandleVLCException(const char* pszVLCExceptionMessage)
{   
    TRACE("VLC_ERROR: %s\n", pszVLCExceptionMessage);
}

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

CVlcDialogDlg::CVlcDialogDlg(CWnd* pParent /*=NULL*/)
:   CDialog(CVlcDialogDlg::IDD, pParent),
    m_bMuteFlag(false),
    m_llLength(0),
    m_bCreated(false)
{
    //{{AFX_DATA_INIT(CVlcDialogDlg)
    //}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

CVlcDialogDlg::~CVlcDialogDlg()
{
}

void CVlcDialogDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
    //{{AFX_DATA_MAP(CVlcDialogDlg)
    DDX_Control(pDX, IDC_STATIC_MEDIA_CONTROL, m_MediaControlGroup);
    DDX_Control(pDX, IDC_BUTTON_PLAY, m_buttonPlay);
    DDX_Control(pDX, IDC_BUTTON_PAUSE, m_buttonPause);
    DDX_Control(pDX, IDC_BUTTON_STOP, m_buttonStop);
    DDX_Control(pDX, IDC_BUTTON_MUTE, m_buttonMute);
    DDX_Control(pDX, IDC_BUTTON_LOAD, m_buttonOpen);
	DDX_Control(pDX, IDC_STATIC_VLC, m_VLC);
    DDX_Control(pDX, IDC_SLIDER_MEDIA, m_MediaSlider);
    DDX_Control(pDX, IDC_STATIC_POSITION, m_MediaPosition);
    DDX_Control(pDX, IDC_STATIC_VOLUME_TEXT, m_VolumeText);
    DDX_Control(pDX, IDC_SLIDER_VOLUME, m_VolumeSlider);
    DDX_Control(pDX, IDC_STATIC_VOLUME, m_VolumeLevel);
    //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVlcDialogDlg, CDialog)
    //{{AFX_MSG_MAP(CVlcDialogDlg)	
    ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()	
    ON_WM_HSCROLL()
	ON_WM_SHOWWINDOW()
    ON_WM_GETMINMAXINFO()
	ON_BN_CLICKED(IDC_BUTTON_PLAY, OnBnClickedButtonPlay)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_PAUSE, OnBnClickedButtonPause)
	ON_BN_CLICKED(IDC_BUTTON_LOAD, OnBnClickedButtonLoad)
    ON_BN_CLICKED(IDC_BUTTON_MUTE, OnBnClickedButtonMute)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

    g_spCVlcDialogDlg=this;

	// IDM_ABOUTBOX muss sich im Bereich der Systembefehle befinden.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);
    m_VLCPlayer.SetOutputWindow((void*)m_VLC.GetSafeHwnd());
    m_VLCPlayer.SetExceptionHandler(&HandleVLCException);
    m_VLCPlayer.SetEventHandler(&HandleVLCEvents);
    m_VolumeSlider.SetRange(0,100);
    m_MediaSlider.SetRange(0,100);
    int iVolume=m_VLCPlayer.GetVolume();
    m_VolumeSlider.SetPos(iVolume);
    char szVolume[5]="";
    sprintf(szVolume, "%d%%", iVolume);
    m_VolumeLevel.SetWindowText(szVolume);

    // Set the icon buttons...
    m_buttonPlay.SetIcon((HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PLAY), IMAGE_ICON, 16, 16, LR_SHARED));
    m_buttonPause.SetIcon((HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_PAUSE), IMAGE_ICON, 16, 16, LR_SHARED));
    m_buttonStop.SetIcon((HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_STOP), IMAGE_ICON, 16, 16, LR_SHARED));
    m_NoMute=(HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_MUTE), IMAGE_ICON, 16, 16, LR_SHARED);
    m_Mute=(HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_MUTE2), IMAGE_ICON, 16, 16, LR_SHARED);
    m_buttonMute.SetIcon(m_NoMute);
    m_buttonOpen.SetIcon((HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_EJECT), IMAGE_ICON, 16, 16, LR_SHARED));

    // Save the all elements of the groupbox in list
    m_listDlgItems.AddTail(&m_MediaControlGroup);
    m_listDlgItems.AddTail(&m_MediaSlider);
    m_listDlgItems.AddTail(&m_MediaPosition);
    m_listDlgItems.AddTail(&m_buttonPlay);
    m_listDlgItems.AddTail(&m_buttonPause);
    m_listDlgItems.AddTail(&m_buttonStop);
    m_listDlgItems.AddTail(&m_buttonMute);
    m_listDlgItems.AddTail(&m_buttonOpen);    
    m_listDlgItems.AddTail(&m_VolumeText);
    m_listDlgItems.AddTail(&m_VolumeSlider);
    m_listDlgItems.AddTail(&m_VolumeLevel);
    
    m_bCreated=true;

    // Save actual dialog size
    CRect myRect;
    GetClientRect(&myRect);
    m_CurrentDlgSize.cx=myRect.Width();
    m_CurrentDlgSize.cy=myRect.Height();

    // Save minimum window size
    GetWindowRect(&myRect);
    m_MinDlgSize.cx=myRect.Width();
    m_MinDlgSize.cy=myRect.Height();

	return TRUE;
}

void CVlcDialogDlg::RecalcLayout(int iCX, int iCY)
{
    if(!m_bCreated)
        return;

    if(iCX==0 && iCY==0)
    {
        CRect rect;
        GetClientRect(&rect);
        
        iCX=rect.Width();
        iCY=rect.Height();
    }
    
    int iControlsHeigth=120;
    int iNewHeight=iCY-iControlsHeigth-7;

    // Resize the VLC static control
    m_VLC.MoveWindow(CRect(7,7,iCX-7, iNewHeight));
        
    // calculate the displacement values
    RECT rect;
    m_MediaControlGroup.GetWindowRect(&rect);
    ScreenToClient(&rect);
    int iOffsetX=rect.left;
    int iGroupWidth=rect.right-rect.left;
    int iMoveX = (iCX - iGroupWidth)/2 - iOffsetX;
    int iMoveY = iCY - m_CurrentDlgSize.cy;

    // Move all controls in list
    POSITION pos = m_listDlgItems.GetHeadPosition();
    for(int i=0; i<m_listDlgItems.GetCount(); i++)
    {
        CWnd* pMyWnd = m_listDlgItems.GetNext(pos);
        if(pMyWnd)
        {            
            CRect myPos;
            pMyWnd->GetWindowRect(&myPos);
            ScreenToClient(myPos);            
            pMyWnd->SetWindowPos(0, myPos.left+iMoveX, myPos.top+iMoveY, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER);            
        }
    }
    m_CurrentDlgSize.cx = iCX;
    m_CurrentDlgSize.cy = iCY;
}

void CVlcDialogDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

void CVlcDialogDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
		m_VLC.RedrawWindow(NULL, NULL, RDW_ERASE | RDW_INVALIDATE | RDW_FRAME | RDW_ALLCHILDREN);
	}
}

HCURSOR CVlcDialogDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CVlcDialogDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    CSliderCtrl* pSlider=reinterpret_cast<CSliderCtrl*>(pScrollBar);

    if(*pSlider==m_VolumeSlider)
    {
        int iPos=m_VolumeSlider.GetPos();
        m_VLCPlayer.SetVolume(iPos);
        char szVolume[5]="";
        sprintf(szVolume, "%d%%", iPos);
        m_VolumeLevel.SetWindowText(szVolume);
        m_bMuteFlag=false;
        m_buttonMute.SetIcon(m_NoMute);
    }

    if(*pSlider==m_MediaSlider)
    {
        int iPos=m_MediaSlider.GetPos();
        m_VLCPlayer.SetTime(m_llLength/100*iPos);
    }

    CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CVlcDialogDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
	CDialog::OnShowWindow(bShow, nStatus);
}

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

void CVlcDialogDlg::OnBnClickedButtonStop()
{
    m_VLCPlayer.Stop();
    m_llLength=0;
    m_MediaSlider.SetPos(0);
    CString csLength;
    m_MediaPosition.GetWindowText(csLength);
    csLength=CString("[00:00:00")+csLength.Right(10);
    m_MediaPosition.SetWindowText(csLength);
}

void CVlcDialogDlg::OnBnClickedButtonPause()
{
    m_VLCPlayer.Pause();
}

void CVlcDialogDlg::OnBnClickedButtonMute()
{   
    m_bMuteFlag=!m_VLCPlayer.GetMute();
    (m_bMuteFlag) ? m_buttonMute.SetIcon(m_Mute) : m_buttonMute.SetIcon(m_NoMute);
    m_VLCPlayer.Mute(m_bMuteFlag);
}

void CVlcDialogDlg::OnBnClickedButtonLoad()
{
	CFileDialog dlgFile(TRUE);
	if(dlgFile.DoModal()==IDOK)
	{
		CString csFile=dlgFile.GetPathName();
        m_VLCPlayer.OpenMedia(csFile.GetBuffer(csFile.GetLength()));        
        m_VLCPlayer.Play();
        
        // Show filename as groupbox title
        if(csFile.GetLength()>80)
            csFile=CString("...")+csFile.Right(80);
        m_MediaControlGroup.SetWindowText(csFile);
	}
}

void CVlcDialogDlg::OnOK()
{
	// TODO: Add your specialized code here and/or call the base class
	//CDialog::OnOK();
}

void CVlcDialogDlg::UpdatePosition()
{
    m_llLength=m_VLCPlayer.GetLength();
    int iHours=static_cast<int>(m_llLength/3600000);
    int iMinutes=static_cast<int>((m_llLength-iHours*3600000)/60000);
    int iSeconds=static_cast<int>((m_llLength-iHours*3600000-iMinutes*60000)/1000);
    
    int64_t llNewPosition=m_VLCPlayer.GetTime();
    int iActHours=static_cast<int>(llNewPosition/3600000);
    int iActMinutes=static_cast<int>((llNewPosition-iActHours*3600000)/60000);
    int iActSeconds=static_cast<int>((llNewPosition-iActHours*3600000-iActMinutes*60000)/1000);
    
    CString csLength;
    csLength.Format("[%02d:%02d:%02d/%02d:%02d:%02d]", iActHours, iActMinutes, iActSeconds, iHours, iMinutes, iSeconds);
    m_MediaPosition.SetWindowText(csLength);
    int iNewSliderPos=(int)((double)llNewPosition/(double)m_llLength *100);
    m_MediaSlider.SetPos(iNewSliderPos);
}

void CVlcDialogDlg::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);
    RecalcLayout(cx,cy);		
	Invalidate();
}

void CVlcDialogDlg::OnGetMinMaxInfo( MINMAXINFO FAR* lpMMI )
{
    if(m_bCreated)
    {
        lpMMI->ptMinTrackSize.x =  m_MinDlgSize.cx;
        lpMMI->ptMinTrackSize.y =  m_MinDlgSize.cy;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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