Commit f6b8db92 authored by Hugo Beauzée-Luyssen's avatar Hugo Beauzée-Luyssen

Add a libvlc_event_manager_t binding

parent 00fe8c52
/*****************************************************************************
* libvlc_EventManager.cpp: Exposes libvlc events
*****************************************************************************
* Copyright © 2014 the VideoLAN team
*
* Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "libvlc_EventManager.hpp"
#include "libvlc_Media.hpp"
#include <vlc/libvlc_events.h>
namespace VLC
{
EventManager::EventManager(const EventManager& em)
: Internal( em )
{
}
bool EventManager::attach(libvlc_event_type_t type, IMediaEventCb* cb)
{
if ( type < libvlc_MediaMetaChanged || type > libvlc_MediaSubItemTreeAdded )
return false;
libvlc_event_attach( *this, type, &handleMediaEvent, cb );
return true;
}
bool EventManager::attach(libvlc_event_type_t type, IMediaPlayerEventCb* cb)
{
if ( type < libvlc_MediaPlayerMediaChanged || type > libvlc_MediaPlayerESSelected )
return false;
libvlc_event_attach( *this, type, &handleMediaPlayerEvent, cb );
return true;
}
bool EventManager::attach(libvlc_event_type_t type, IMediaListEventCb* cb)
{
if ( type < libvlc_MediaListItemAdded || type > libvlc_MediaListWillDeleteItem )
return false;
libvlc_event_attach( *this, type, &handleMediaListEvent, cb );
return true;
}
bool EventManager::attach(libvlc_event_type_t type, IMediaListPlayerEventCb* cb)
{
if ( type < libvlc_MediaListPlayerPlayed || type > libvlc_MediaListPlayerStopped )
return false;
libvlc_event_attach( *this, type, &handleMediaListPlayerEvent, cb );
return true;
}
bool EventManager::attach(libvlc_event_type_t type, IMediaDiscovererEventCb* cb)
{
if ( type < libvlc_MediaDiscovererStarted || type > libvlc_MediaDiscovererEnded )
return false;
libvlc_event_attach( *this, type, &handleMediaDiscovererEvent, cb );
return true;
}
bool EventManager::attach( libvlc_event_type_t type, IVLMEventCb* cb )
{
if ( type < libvlc_VlmMediaAdded || type > libvlc_VlmMediaInstanceStatusError )
return false;
libvlc_event_attach( *this, type, &handleVLMEvent, cb );
return true;
}
void EventManager::detach(libvlc_event_type_t type, IMediaEventCb* cb)
{
libvlc_event_detach( *this, type, &handleMediaEvent, cb );
}
void EventManager::detach(libvlc_event_type_t type, IMediaPlayerEventCb* cb)
{
libvlc_event_detach( *this, type, &handleMediaPlayerEvent, cb );
}
void EventManager::detach(libvlc_event_type_t type, IMediaListEventCb* cb)
{
libvlc_event_detach( *this, type, &handleMediaListEvent, cb );
}
void EventManager::detach(libvlc_event_type_t type, IMediaListPlayerEventCb* cb)
{
libvlc_event_detach( *this, type, &handleMediaListPlayerEvent, cb );
}
void EventManager::detach(libvlc_event_type_t type, IMediaDiscovererEventCb* cb)
{
libvlc_event_detach( *this, type, &handleMediaDiscovererEvent, cb );
}
void EventManager::detach(libvlc_event_type_t type, IVLMEventCb* cb)
{
libvlc_event_detach( *this, type, &handleVLMEvent, cb );
}
void EventManager::handleMediaEvent(const libvlc_event_t* event, void* data)
{
IMediaEventCb* cb = reinterpret_cast<IMediaEventCb*>( data );
switch ( event->type )
{
case libvlc_MediaMetaChanged:
cb->metaChanged( event->u.media_meta_changed.meta_type );
break;
case libvlc_MediaSubItemAdded:
cb->subItemAdded( Media( event->u.media_subitem_added.new_child ) );
break;
case libvlc_MediaDurationChanged:
cb->durationChanged( event->u.media_duration_changed.new_duration );
break;
case libvlc_MediaParsedChanged:
cb->parsedChanged( event->u.media_parsed_changed.new_status );
break;
case libvlc_MediaFreed:
cb->freed( event->u.media_freed.md );
break;
case libvlc_MediaStateChanged:
cb->stateChanged( event->u.media_state_changed.new_state );
break;
case libvlc_MediaSubItemTreeAdded:
cb->subItemTreeAdded( event->u.media_subitemtree_added.item );
break;
default:
assert(false);
}
}
void EventManager::handleMediaPlayerEvent(const libvlc_event_t* event, void* data)
{
IMediaPlayerEventCb* cb = reinterpret_cast<IMediaPlayerEventCb*>( data );
switch ( event->type )
{
case libvlc_MediaPlayerMediaChanged:
cb->mediaChanged( Media( event->u.media_player_media_changed.new_media ) );
break;
case libvlc_MediaPlayerNothingSpecial:
cb->nothingSpecial();
break;
case libvlc_MediaPlayerOpening:
cb->opening();
break;
case libvlc_MediaPlayerBuffering:
cb->buffering( event->u.media_player_buffering.new_cache );
break;
case libvlc_MediaPlayerPlaying:
cb->playing();
break;
case libvlc_MediaPlayerPaused:
cb->paused();
break;
case libvlc_MediaPlayerStopped:
cb->stopped();
break;
case libvlc_MediaPlayerForward:
cb->forward();
break;
case libvlc_MediaPlayerBackward:
cb->backward();
break;
case libvlc_MediaPlayerEndReached:
cb->endReached();
break;
case libvlc_MediaPlayerEncounteredError:
cb->encounteredError();
break;
case libvlc_MediaPlayerTimeChanged:
cb->timeChanged( event->u.media_player_time_changed.new_time );
break;
case libvlc_MediaPlayerPositionChanged:
cb->positionChanged( event->u.media_player_position_changed.new_position );
break;
case libvlc_MediaPlayerSeekableChanged:
cb->seekableChanged( event->u.media_player_seekable_changed.new_seekable );
break;
case libvlc_MediaPlayerPausableChanged:
cb->pausableChanged( event->u.media_player_seekable_changed.new_seekable );
break;
case libvlc_MediaPlayerTitleChanged:
cb->titleChanged( event->u.media_player_title_changed.new_title );
break;
case libvlc_MediaPlayerSnapshotTaken:
cb->snapshotTaken( event->u.media_player_snapshot_taken.psz_filename );
break;
case libvlc_MediaPlayerLengthChanged:
cb->lengthChanged( event->u.media_player_length_changed.new_length );
break;
case libvlc_MediaPlayerVout:
cb->vout( event->u.media_player_vout.new_count );
break;
case libvlc_MediaPlayerScrambledChanged:
cb->scrambledChanged( event->u.media_player_scrambled_changed.new_scrambled );
break;
case libvlc_MediaPlayerESAdded:
cb->eSAdded( event->u.media_player_es_changed.i_type, event->u.media_player_es_changed.i_id );
break;
case libvlc_MediaPlayerESDeleted:
cb->eSDeleted( event->u.media_player_es_changed.i_type, event->u.media_player_es_changed.i_id );
break;
case libvlc_MediaPlayerESSelected:
cb->eSSelected( event->u.media_player_es_changed.i_type, event->u.media_player_es_changed.i_id );
break;
default:
assert(false);
}
}
void EventManager::handleMediaListEvent(const libvlc_event_t* event, void* data)
{
IMediaListEventCb* cb = reinterpret_cast<IMediaListEventCb*>( data );
switch ( event->type )
{
case libvlc_MediaListItemAdded:
cb->itemAdded( Media( event->u.media_list_item_added.item ), event->u.media_list_item_added.index );
break;
case libvlc_MediaListWillAddItem:
cb->willAddItem( Media( event->u.media_list_will_add_item.item ), event->u.media_list_will_add_item.index );
break;
case libvlc_MediaListItemDeleted:
cb->itemDeleted( Media( event->u.media_list_item_deleted.item ), event->u.media_list_item_deleted.index );
break;
case libvlc_MediaListWillDeleteItem:
cb->willDeleteItem( Media( event->u.media_list_will_delete_item.item ), event->u.media_list_will_delete_item.index );
break;
default:
assert(false);
}
}
void EventManager::handleMediaListPlayerEvent(const libvlc_event_t* event, void* data)
{
IMediaListPlayerEventCb* cb = reinterpret_cast<IMediaListPlayerEventCb*>( data );
switch ( event->type )
{
case libvlc_MediaListPlayerPlayed:
cb->played();
break;
case libvlc_MediaListPlayerNextItemSet:
cb->nextItemSet( Media( event->u.media_list_player_next_item_set.item ) );
break;
case libvlc_MediaListPlayerStopped:
cb->stopped();
break;
default:
assert(false);
}
}
void EventManager::handleMediaDiscovererEvent(const libvlc_event_t* event, void* data)
{
IMediaDiscovererEventCb* cb = reinterpret_cast<IMediaDiscovererEventCb*>( data );
switch ( event->type )
{
case libvlc_MediaDiscovererStarted:
cb->started();
break;
case libvlc_MediaDiscovererEnded:
cb->stopped();
break;
default:
assert(false);
}
}
void EventManager::handleVLMEvent(const libvlc_event_t* event, void* data)
{
IVLMEventCb* cb = reinterpret_cast<IVLMEventCb*>( data );
std::string mediaName = event->u.vlm_media_event.psz_media_name ? event->u.vlm_media_event.psz_media_name : "";
std::string instance = event->u.vlm_media_event.psz_instance_name ? event->u.vlm_media_event.psz_instance_name : "";
switch ( event->type )
{
case libvlc_VlmMediaAdded:
cb->mediaAdded( mediaName );
break;
case libvlc_VlmMediaRemoved:
cb->mediaRemoved( mediaName );
break;
case libvlc_VlmMediaChanged:
cb->mediaChanged( mediaName );
break;
case libvlc_VlmMediaInstanceStarted:
cb->mediaInstanceStarted( instance, mediaName );
break;
case libvlc_VlmMediaInstanceStopped:
cb->mediaInstanceStopped( instance, mediaName );
break;
case libvlc_VlmMediaInstanceStatusInit:
cb->mediaInstanceStatusInit( instance, mediaName );
break;
case libvlc_VlmMediaInstanceStatusOpening:
cb->mediaInstanceStatusOpening( instance, mediaName );
break;
case libvlc_VlmMediaInstanceStatusPlaying:
cb->mediaInstanceStatusPlaying( instance, mediaName );
break;
case libvlc_VlmMediaInstanceStatusPause:
cb->mediaInstanceStatusPause( instance, mediaName );
break;
case libvlc_VlmMediaInstanceStatusEnd:
cb->mediaInstanceStatusEnd( instance, mediaName );
break;
case libvlc_VlmMediaInstanceStatusError:
cb->mediaInstanceStatusError( instance, mediaName );
break;
default:
assert(false);
}
}
EventManager::EventManager( InternalPtr obj)
: Internal( obj )
{
}
}
/*****************************************************************************
* libvlc_EventManager.hpp: Exposes libvlc events
*****************************************************************************
* Copyright © 2014 the VideoLAN team
*
* Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifndef LIBVLC_EVENTMANAGER_HPP
#define LIBVLC_EVENTMANAGER_HPP
#include <string>
#include <vlc/vlc.h>
#include "libvlcpp.hpp"
namespace VLC
{
class Media;
class IMediaEventCb
{
public:
virtual ~IMediaEventCb() {}
virtual void metaChanged( libvlc_meta_t ) {}
virtual void subItemAdded( const Media& ) {}
virtual void durationChanged( int64_t ) {}
virtual void parsedChanged( bool ) {}
virtual void freed( const Media& ) {}
virtual void stateChanged( libvlc_state_t ) {}
virtual void subItemTreeAdded( const Media& ) {}
};
class IMediaPlayerEventCb
{
public:
virtual ~IMediaPlayerEventCb(){}
virtual void mediaChanged( const Media& ) {}
virtual void nothingSpecial() {}
virtual void opening() {}
virtual void buffering( float ) {}
virtual void playing() {}
virtual void paused() {}
virtual void stopped() {}
virtual void forward() {}
virtual void backward() {}
virtual void endReached() {}
virtual void encounteredError() {}
virtual void timeChanged( libvlc_time_t ) {}
virtual void positionChanged( float ) {}
virtual void seekableChanged( bool ) {}
virtual void pausableChanged( bool ) {}
virtual void titleChanged( int ) {}
virtual void snapshotTaken( const std::string& ) {}
virtual void lengthChanged( libvlc_time_t ) {}
virtual void vout( int ) {}
virtual void scrambledChanged( int ) {}
virtual void eSAdded( libvlc_track_type_t, int ) {}
virtual void eSDeleted( libvlc_track_type_t, int ) {}
virtual void eSSelected( libvlc_track_type_t, int ) {}
};
class IMediaListEventCb
{
public:
virtual ~IMediaListEventCb() {}
virtual void itemAdded( const Media&, int ) {}
virtual void willAddItem( const Media&, int ) {}
virtual void itemDeleted( const Media&, int ) {}
virtual void willDeleteItem( const Media&, int ) {}
};
// MediaListView events are not being sent by VLC, so we don't implement them here
class IMediaListPlayerEventCb
{
public:
virtual ~IMediaListPlayerEventCb() {}
virtual void played() {}
virtual void nextItemSet( const Media& ) {}
virtual void stopped() {}
};
class IMediaDiscovererEventCb
{
public:
virtual ~IMediaDiscovererEventCb() {}
virtual void started() {}
virtual void stopped() {}
};
class IVLMEventCb
{
public:
virtual ~IVLMEventCb() {}
virtual void mediaAdded( const std::string& ) {}
virtual void mediaRemoved( const std::string& ) {}
virtual void mediaChanged( const std::string& ) {}
virtual void mediaInstanceStarted( const std::string&, const std::string& ) {}
virtual void mediaInstanceStopped( const std::string&, const std::string& ) {}
virtual void mediaInstanceStatusInit( const std::string&, const std::string& ) {}
virtual void mediaInstanceStatusOpening( const std::string&, const std::string& ) {}
virtual void mediaInstanceStatusPlaying( const std::string&, const std::string& ) {}
virtual void mediaInstanceStatusPause( const std::string&, const std::string& ) {}
virtual void mediaInstanceStatusEnd( const std::string&, const std::string& ) {}
virtual void mediaInstanceStatusError( const std::string&, const std::string& ) {}
};
class EventManager : Internal<libvlc_event_manager_t>
{
public:
EventManager( const EventManager& em );
bool attach( libvlc_event_type_t type, IMediaEventCb* cb );
bool attach( libvlc_event_type_t type, IMediaPlayerEventCb* cb );
bool attach( libvlc_event_type_t type, IMediaListEventCb* cb );
bool attach( libvlc_event_type_t type, IMediaListPlayerEventCb* cb );
bool attach( libvlc_event_type_t type, IMediaDiscovererEventCb* cb );
bool attach( libvlc_event_type_t type, IVLMEventCb* cb );
void detach( libvlc_event_type_t type, IMediaEventCb* cb );
void detach( libvlc_event_type_t type, IMediaPlayerEventCb* cb );
void detach( libvlc_event_type_t type, IMediaListEventCb* cb );
void detach( libvlc_event_type_t type, IMediaListPlayerEventCb* cb );
void detach( libvlc_event_type_t type, IMediaDiscovererEventCb* cb );
void detach( libvlc_event_type_t type, IVLMEventCb* cb );
private:
static void handleMediaEvent( const libvlc_event_t* event, void* data );
static void handleMediaPlayerEvent( const libvlc_event_t* event, void* data );
static void handleMediaListEvent( const libvlc_event_t* event, void* data );
static void handleMediaListPlayerEvent( const libvlc_event_t* event, void* data );
static void handleMediaDiscovererEvent( const libvlc_event_t* event, void* data );
static void handleVLMEvent( const libvlc_event_t* event, void* data );
static void handleEvent( const libvlc_event_t* event, void* data );
EventManager(InternalPtr obj );
friend class Media;
friend class MediaDiscoverer;
friend class MediaList;
friend class MediaListPlayer;
friend class MediaPlayer;
};
}
#endif // LIBVLC_EVENTMANAGER_HPP
......@@ -232,9 +232,7 @@ public:
* \param psz_aout audio output name (as returned by
* Instance::audioOutputList() )
*
* \return A NULL-terminated linked list of potential audio output
* devices. It must be freed it with
* libvlc_audio_output_device_list_release()
* \return A vector containing all audio output devices for this module
*
* \version LibVLC 2.1.0 or later.
*/
......
......@@ -23,6 +23,8 @@
#include <vlc.hpp>
#include "libvlc_EventManager.hpp"
namespace VLC
{
......@@ -70,6 +72,8 @@ Media::Media(const Media& another)
: Internal(another)
{
retain();
if ( another.m_eventManager != NULL )
m_eventManager = new EventManager( *another.m_eventManager );
}
const Media& Media::operator=(const Media& another)
......@@ -80,6 +84,9 @@ const Media& Media::operator=(const Media& another)
}
release();
m_obj = another.m_obj;
delete m_eventManager;
if ( another.m_eventManager != NULL )
m_eventManager = new EventManager( *another.m_eventManager );
retain();
return *this;
}
......@@ -91,6 +98,7 @@ bool Media::operator==(const Media& another) const
Media::~Media()
{
delete m_eventManager;
release();
}
......@@ -145,9 +153,14 @@ bool Media::stats(libvlc_media_stats_t * p_stats)
return libvlc_media_get_stats(m_obj, p_stats);
}
libvlc_event_manager_t * Media::eventManager()
EventManager& Media::eventManager()
{
return libvlc_media_event_manager(m_obj);
if ( m_eventManager == NULL )
{
libvlc_event_manager_t* obj = libvlc_media_event_manager(m_obj);
m_eventManager = new EventManager( obj );
}
return *m_eventManager;
}
libvlc_time_t Media::duration()
......@@ -197,6 +210,7 @@ std::vector<MediaTrack> Media::tracks()
Media::Media(Internal::InternalPtr ptr)
: Internal(ptr)
, m_eventManager( NULL )
{
}
......
......@@ -32,6 +32,7 @@ namespace VLC
class MediaList;
class MediaPlayer;
class EventManager;
class Media : public Internal<libvlc_media_t>
{
......@@ -243,7 +244,7 @@ public:
*
* \return event manager object
*/
libvlc_event_manager_t * eventManager();
EventManager& eventManager();
/**
* Get duration (in ms) of media descriptor object item.
......@@ -343,8 +344,12 @@ private:
*/
void release();
private:
EventManager* m_eventManager;
friend class MediaList;
friend class MediaPlayer;
friend class EventManager;
};
} // namespace VLC
......
......@@ -23,11 +23,14 @@
#include <vlc.hpp>
#include "libvlc_EventManager.hpp"
namespace VLC
{
MediaDiscoverer::~MediaDiscoverer()
{
delete m_eventManager;
release();
}
......@@ -41,15 +44,20 @@ MediaDiscoverer* MediaDiscoverer::create( Instance &inst, const std::string& nam
std::string MediaDiscoverer::localizedName()
{
char * c_result = libvlc_media_discoverer_localized_name(m_obj);
char* c_result = libvlc_media_discoverer_localized_name(m_obj);
std::string result = c_result;
libvlc_free(c_result);
return result;
}
libvlc_event_manager_t * MediaDiscoverer::eventManager()
EventManager& MediaDiscoverer::eventManager()
{
return libvlc_media_discoverer_event_manager(m_obj);
if ( m_eventManager == NULL )
{
libvlc_event_manager_t* obj = libvlc_media_discoverer_event_manager( m_obj );
m_eventManager = new EventManager( obj );
}
return *m_eventManager;
}
bool MediaDiscoverer::isRunning()
......@@ -59,6 +67,7 @@ bool MediaDiscoverer::isRunning()
MediaDiscoverer::MediaDiscoverer(Internal::InternalPtr ptr)
: Internal( ptr )
, m_eventManager( NULL )
{
}
......
......@@ -30,6 +30,7 @@
namespace VLC
{
class EventManager;
class MediaDiscoverer : public Internal<libvlc_media_discoverer_t>
{
......@@ -58,7 +59,7 @@ public:
*
* \return event manager object.
*/
libvlc_event_manager_t * eventManager();
EventManager& eventManager();
/**
* Query if media service discover object is running.
......@@ -77,6 +78,9 @@ private:
MediaDiscoverer(const MediaDiscoverer& another);
const MediaDiscoverer& operator=(const MediaDiscoverer& another);
private:
EventManager* m_eventManager;
};
} // namespace VLC
......
......@@ -23,12 +23,16 @@
#include <vlc.hpp>
#include "libvlc_EventManager.hpp"
namespace VLC
{
MediaList::MediaList(const MediaList& another)
: Internal( another )
{
if ( another.m_eventManager )
m_eventManager = new EventManager( *another.m_eventManager );
retain();
}
......@@ -40,6 +44,9 @@ const MediaList& MediaList::operator=(const MediaList& another)
}
release();
m_obj = another.m_obj;
delete m_eventManager;
if ( another.m_eventManager != NULL )
m_eventManager = new EventManager( *another.m_eventManager );
retain();
return *this;
}
......@@ -51,6 +58,7 @@ bool MediaList::operator==(const MediaList& another) const
MediaList::~MediaList()
{
delete m_eventManager;
release();
}
......@@ -136,13 +144,19 @@ void MediaList::unlock()
libvlc_media_list_unlock(m_obj);
}
libvlc_event_manager_t * MediaList::eventManager()
EventManager& MediaList::eventManager()
{
return libvlc_media_list_event_manager(m_obj);
if ( m_eventManager == NULL )
{
libvlc_event_manager_t* obj = libvlc_media_list_event_manager( *this );
m_eventManager = new EventManager( obj );
}
return *m_eventManager;
}
MediaList::MediaList( Internal::InternalPtr ptr )
: Internal( ptr )
, m_eventManager( NULL )
{
}
......
......@@ -30,6 +30,8 @@
namespace VLC
{
class EventManager;
class MediaList : public Internal<libvlc_media_list_t>
{
public:
......@@ -187,7 +189,7 @@ public:
*
* \return libvlc_event_manager
*/
libvlc_event_manager_t * eventManager();
VLC::EventManager& eventManager();
private:
MediaList(InternalPtr ptr);
......@@ -200,6 +202,9 @@ private:
* Retain reference to a media list
*/
void retain();