LibVLC media player within Juce

I want to use it in a free project.

español: quiero usarlo en un proyecto libre.

Component improved and added compatibility for MacOSX.

VideoVLC.h

#ifndef __JUCER_HEADER_VIDEOVLC__
#define __JUCER_HEADER_VIDEOVLC__
//[Headers]
#include <vlc/vlc.h>
#include "juce.h"
//[/Headers]
class VideoVLC  : public Component,
                  private Timer
                  {
public:
    //==============================================================================
    VideoVLC ();
    ~VideoVLC();

    //==============================================================================
    enum{
    STATE_IDLE_CLOSE,
    STATE_OPENING,
    STATE_BUFFERING,
    STATE_PLAYING,
    STATE_PAUSED,
    STATE_STOPPING,
    STATE_ENDED,
    STATE_ERROR,
    };
    //[UserMethods]     -- You can add your own custom methods in this section.
    void play();
    void stop();
    void pause();
    void setPosition(int p_posi);
    int getPosition();
    void setRate(float p_rate);
    float getRate();
    void setVolume(int p_vol);
    int getVolume();
    void loadMedia(String media);
    bool isLoadMedia;
    int getState();
    float getDuration();
    //[/UserMethods]

    void paint (Graphics& g);
    void resized();


    //==============================================================================
    juce_UseDebuggingNewOperator

private:
    //[UserVariables]   -- You can add your own custom variables in this section.
    void timerCallback();
    void raise(libvlc_exception_t * ex);
    Component *vlc_visor;
    libvlc_exception_t vlc_except;
    libvlc_instance_t *vlc_instan;
    libvlc_media_player_t *vlc_mplayer;
    libvlc_media_t *vlc_media;
    //[/UserVariables]
    //==============================================================================

    //==============================================================================
    // (prevent copy constructor and operator= being generated..)
    VideoVLC (const VideoVLC&);
    const VideoVLC& operator= (const VideoVLC&);
};


#endif   // __JUCER_HEADER_VIDEOVLC__

VideoVLC.cpp

#include "VideoVLC.h"

VideoVLC::VideoVLC ()
        : Component (T("VideoVLC"))
{
    setSize (400, 200);
    vlc_visor=new Component("VisorVLC");
    vlc_visor->setOpaque(true);
    vlc_visor->addToDesktop(ComponentPeer::windowIsTemporary);
    //toBehind (vlc_visor);
    libvlc_exception_init (&vlc_except);

    isLoadMedia=false;
}
VideoVLC::~VideoVLC()
{
     /* Stop playing */
    libvlc_media_player_stop (vlc_mplayer, &vlc_except);
    /* Free the media_player */
    libvlc_media_player_release (vlc_mplayer);
    libvlc_release (vlc_instan);
    raise (&vlc_except);
    delete vlc_visor;
}
void VideoVLC::paint (Graphics& g)
{
    g.fillAll (Colours::black);
}
void VideoVLC::resized()
{

}
void VideoVLC::timerCallback()
{
    vlc_visor->setBounds(getScreenX(),getScreenY(),getWidth(),getHeight());
    if (getState()==STATE_ENDED)
    {
        stopTimer();
        vlc_visor->setVisible(false);
    }
    vlc_visor->toFront(false);
}
void VideoVLC::play()
{
    if (isLoadMedia==false)
    {
        return;
    }
    libvlc_media_player_play (vlc_mplayer, &vlc_except);
    raise (&vlc_except);
    startTimer(1);
    vlc_visor->setVisible(true);
}
void VideoVLC::stop()
{
    if (isLoadMedia==false)
    {
        return;
    }
    libvlc_media_player_stop(vlc_mplayer, &vlc_except);
    raise (&vlc_except);
    vlc_visor->setVisible(false);
    stopTimer();
}
void VideoVLC::pause()
{
    if (isLoadMedia==false)
    {
        return;
    }
    libvlc_media_player_pause(vlc_mplayer, &vlc_except);
    raise (&vlc_except);
}
void VideoVLC::setRate(float p_rate)
{
    if (isLoadMedia==false)
    {
        return;
    }
    libvlc_media_player_set_rate(vlc_mplayer,p_rate ,&vlc_except);
    raise (&vlc_except);
}
float VideoVLC::getRate()
{
    if (isLoadMedia==false)
    {
        return -1;
    }
    float vr=libvlc_media_player_get_rate(vlc_mplayer,&vlc_except);
    raise (&vlc_except);
    return vr;
}
void VideoVLC::setPosition(int p_posi)
{
    if (isLoadMedia==false)
    {
        return;
    }
    libvlc_exception_clear(&vlc_except);
    libvlc_media_t *curMedia = libvlc_media_player_get_media (vlc_mplayer, &vlc_except);
    libvlc_exception_clear(&vlc_except);
    if (curMedia == NULL)
        return;

    float pos=(float)(p_posi)/(float)10000;
    libvlc_media_player_set_position (vlc_mplayer,pos, &vlc_except);
    raise(&vlc_except);
}
int VideoVLC::getPosition()
{
    if (isLoadMedia==false)
    {
        return -1;
    }
    libvlc_media_t *curMedia = libvlc_media_player_get_media (vlc_mplayer,&vlc_except);
    libvlc_exception_clear(&vlc_except);
    if (curMedia == NULL)
        return -1;

    float pos=libvlc_media_player_get_position (vlc_mplayer,&vlc_except);
    int vr=(int)(pos*(float)(10000));
    return vr;
}
void VideoVLC::loadMedia(String pmedia)
{

    const char * const vlc_args[] =
    {
        "-I", "dummy",
        "--ignore-config",
        "--plugin-path=C:\\plugins\\"
    };

    vlc_instan = libvlc_new (sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args, &vlc_except);
    raise (&vlc_except);

    vlc_media = libvlc_media_new (vlc_instan,pmedia,&vlc_except);
    raise (&vlc_except);

    if (vlc_mplayer !=0)
    {
        stop();
    }
    vlc_mplayer = libvlc_media_player_new_from_media (vlc_media, &vlc_except);
    raise (&vlc_except);
    libvlc_media_release (vlc_media);

    SystemStats system;
    if (system.getOperatingSystemName()==T("Linux"))
    {
        libvlc_media_player_set_xwindow(vlc_mplayer,(uint32_t)vlc_visor->getWindowHandle(),&vlc_except);
    }
    else if(system.getOperatingSystemName()==T("MacOSX")){
        libvlc_media_player_set_agl(vlc_mplayer,(uint32_t)vlc_visor->getWindowHandle(),&vlc_except);
    }else{
        libvlc_media_player_set_hwnd(vlc_mplayer,vlc_visor->getWindowHandle(),&vlc_except);
    }
    raise (&vlc_except);
    isLoadMedia=true;
}
void VideoVLC::setVolume(int p_vol)
{
    if (isLoadMedia==false)
    {
        return;
    }
    if (p_vol>100)
    {
        p_vol=100;
    }
    if (p_vol<0)
    {
        p_vol=0;
    }
    libvlc_exception_clear(&vlc_except);
    libvlc_audio_set_volume(vlc_instan,p_vol,&vlc_except);
    raise (&vlc_except);
}
int VideoVLC::getVolume()
{
    if (isLoadMedia==false)
    {
        return -1;
    }
    int vr=libvlc_audio_get_volume(vlc_instan,&vlc_except);
    raise (&vlc_except);
    return vr;
}
int VideoVLC::getState()
{
    if (isLoadMedia==false)
    {
        return 0;
    }
    return libvlc_media_get_state(vlc_media,&vlc_except);
}
float VideoVLC::getDuration()
{
    if (isLoadMedia==false)
    {
        return 0;
    }
    return libvlc_media_player_get_length(vlc_mplayer,&vlc_except);
}
void VideoVLC::raise(libvlc_exception_t * ex)
{
    if (libvlc_exception_raised (ex))
    {
        AlertWindow::showMessageBox (AlertWindow::InfoIcon,
                                     T("VCLPlayer"),
                                     libvlc_exception_get_message(ex));
        fprintf (stderr, "error: %s\n", libvlc_exception_get_message(ex));
        //exit (-1);
    }
}

Looks awesome. I will test it out as soon as I get a chance. Thanks.

Corrected
Always put on top only of the containing window of the component when there is more of a window.

español: corregido:
siempre encima solo de la ventana contenedora del componente cuando hay mas de una ventana.

replace:

void VideoVLC::timerCallback()
{
    vlc_visor->setBounds(getScreenX(),getScreenY(),getWidth(),getHeight());
    if (getState()==STATE_ENDED)
    {
        stopTimer();
        vlc_visor->setVisible(false);
    }
   
      vlc_visor->toFront(false);
}

by that

void VideoVLC::timerCallback()
{
    vlc_visor->setBounds(getScreenX(),getScreenY(),getWidth(),getHeight());
    if (getState()==STATE_ENDED)
    {
        stopTimer();
        vlc_visor->setVisible(false);
    }
    if(getPeer()->isMinimised()==false){
      getPeer()->toBehind(vlc_visor->getPeer());
    }
    if(getPeer()->isFocused()==true){
      vlc_visor->getPeer()->toFront(false);
    }

}

update for juce 1.51

Its my first comment on this site, so please answer me.

I am using libvlc library 2.1.5win64 for window 7 with Qt 4.8.5, C++ and Cmake 2.8 to build a project that capture a streaming video from VLC server in rtsp protocol. I am facing two problems very badly

 


1: My program is bulding .exe file in Release mode but it crash when we open(double click) this .exe file, while it is working correctly in debug mode.

2: My program is not working in debug mode also, when I open .exe file it shown a widget but video is not streaming, it encounters a error

[0000000001c96b20] main input error: open of 'rtsp"//192.168.1.101:8554/' failed 
[0000000001bcce90] main input error: your input can't be opened
[0000000001bcce90] main input error: VLC is unable to open the MRL 'rtsp://192.168.1.101:8554/'.

Please somebody anserw my question..............

I am also providing my source code with cmakelist............

If possible please edit my code because i am new on Qt,Cmake,LibVlc

 

   CmakeList.txt
    
    cmake_minimum_required(VERSION 2.8)
    PROJECT(VlcInterfaceWindow_FULL)
    FIND_PACKAGE( Qt4 4.8.5 COMPONENTS QtMain QtCore QtGui REQUIRED )
    #SET(VlcInterfaceWindow_SOURCES  main.cpp  vlc_on_qt.cpp)
    SET(VlcInterfaceWindow_HEAERS vlc_on_qt.h)
    INCLUDE(${QT_USE_FILE})
    include_directories( "C:/vlc-2.1.5/sdk/include")
    QT4_WRAP_CPP(VlcInterfaceWindow_HEAERS_MOC ${VlcInterfaceWindow_HEAERS})

    add_library(lib_vlc STATIC IMPORTED)
    set_property(TARGET lib_vlc PROPERTY IMPORTED_LOCATION_RELEASE C:/vlc-2.1.5/sdk/lib/libvlc.lib)
    set_property(TARGET lib_vlc PROPERTY IMPORTED_LOCATION_DEBUG C:/vlc-2.1.5/sdk/lib/libvlc.lib)
    add_library(lib_vlc_core STATIC IMPORTED)
    set_property(TARGET lib_vlc_core PROPERTY IMPORTED_LOCATION_RELEASE C:/vlc-    
    2.1.5/sdk/lib/libvlccore.lib)
    set_property(TARGET lib_vlc_core PROPERTY IMPORTED_DEBUG C:/vlc-2.1.5/sdk/lib/libvlccore.lib)
    set(VlcTest_SRCS  main.cpp vlc_on_qt.cpp )
    ADD_EXECUTABLE(VlcInterfaceWindow_FULL ${VlcTest_SRCS} ${VlcInterfaceWindow_HEAERS_MOC})
    TARGET_LINK_LIBRARIES(VlcInterfaceWindow_FULL ${QT_LIBRARIES} lib_vlc lib_vlc_core)
    
    main.cpp
    #include "vlc_on_qt.h"
    #include <QtGui/QApplication>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Player p;
    p.resize(640,480);
    //p.playFile("rtsp://38.117.88.90/TenTV/video");
    p.playFile("rtsp://192.168.1.101:8554/");
    p.show();
    return a.exec();
    }
 
    
    
   

     vlc_on_qt.h
   

    #ifndef VLC_ON_QT_H
    #define VLC_ON_QT_H
    #include <vlc/vlc.h>
    #include <vlc/libvlc.h>
    #include <QWidget>
    class 
    QVBoxLayout;
    class
    QTimer;
    class 
    QFrame;
    class
    QSlider;
    #define POSITION_RESOLUTION 10000
    class Player : public QWidget
    {
    Q_OBJECT
    QSlider *_positionSlider;
       
    QFrame *_videoWidget;
    QTimer *poller;
    bool _isPlaying;
    libvlc_instance_t *_vlcinstance;
    libvlc_media_player_t *_mp;
    libvlc_media_t *_m;
    public:
    Player();
   ~Player();
        
    public slots:
    void playFile(QString file);
    void updateInterface();
    void changePosition(int newPosition);
    };
    #endif
    
   
    vlc_on_qt.cpp
   

 

    #include "vlc_on_qt.h"
    #include <QVBoxLayout>
    #include <QPushButton>
    #include <QSlider>
    #include <QTimer>
    #include <QFrame>
    #include <iostream>
    using namespace std;
    Player::Player()
    : QWidget()
    {
    
     _videoWidget=new QFrame(this);
    _positionSlider=new QSlider(Qt::Horizontal,this); 
    _positionSlider->setMaximum(POSITION_RESOLUTION);
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(_videoWidget);
    layout->addWidget(_positionSlider);
    setLayout(layout);
    _isPlaying=false;
    poller=new QTimer(this);
    connect(poller, SIGNAL(timeout()),this, SLOT(updateInterface()));
    connect(_positionSlider, SIGNAL(sliderMoved(int)), this, SLOT(changePosition(int)));
    
    poller->start(100); 
    }
    Player::~Player() 
    {
    libvlc_media_player_stop (_mp);
       
    libvlc_media_player_release (_mp);
    libvlc_release (_vlcinstance);
    
    }
    void Player::playFile(QString file)
    {
     _vlcinstance=libvlc_new(0, NULL);          
    
     //Create a new LibVLC media descriptor
    _m = libvlc_media_new_location(_vlcinstance, file.toAscii());
    
    _mp=libvlc_media_player_new_from_media (_m);
    
    // Get our media instance to use our window 
     libvlc_media_player_set_hwnd(_mp, _videoWidget->winId());
    
    // Play 
    libvlc_media_player_play (_mp);
      
    _isPlaying=true;   
    
    }

    void Player::changePosition(int newPosition)
    {
    libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp);
    if (curMedia == NULL)
        return;
    float pos=(float)(newPosition)/(float)POSITION_RESOLUTION;
    libvlc_media_player_set_position (_mp, pos);
    
    }
    void Player::updateInterface()
    {
    if(!_isPlaying)
        return;
    libvlc_media_t *curMedia = libvlc_media_player_get_media (_mp);
    if (curMedia == NULL)
        return;
    float pos=libvlc_media_player_get_position (_mp);
    int siderPos=(int)(pos*(float)(POSITION_RESOLUTION));
    _positionSlider->setValue(siderPos);
    
    }