From d344f0c55e8c1b5dcf1fffdf16ac882409c6a481 Mon Sep 17 00:00:00 2001
From: =?utf-8?q?S=C3=A9bastien=20Escudier?= <sebastien-devel@celeos.eu>
Date: Tue, 13 Jan 2009 09:23:58 +0100
Subject: [PATCH] Handle vlm medias event in libvlc

---
 include/vlc/libvlc_events.h |   13 +++++++++++++
 include/vlc_vlm.h           |    3 +++
 src/control/vlm.c           |   41 +++++++++++++++++++++++++++++++++++++++++
 src/input/vlm.c             |   15 +++++++++++++++
 4 files changed, 72 insertions(+), 0 deletions(-)

diff --git a/include/vlc/libvlc_events.h b/include/vlc/libvlc_events.h
index 1004989..3bf84b4 100644
--- a/include/vlc/libvlc_events.h
+++ b/include/vlc/libvlc_events.h
@@ -88,6 +88,9 @@ typedef enum libvlc_event_type_t {
 
     libvlc_MediaPlayerTitleChanged,
     libvlc_MediaPlayerSnapshotTaken,
+
+    libvlc_VlmInstanceStarted,
+    libvlc_VlmInstanceStopped,
     /* New event types HERE */
 } libvlc_event_type_t;
 
@@ -202,6 +205,16 @@ typedef struct libvlc_event_t
              char* psz_filename ;
         } media_player_snapshot_taken ;
 
+        struct
+        {
+             char * psz_media_name ;
+        } vlm_media_instance_started;
+
+        struct
+        {
+             char * psz_media_name ;
+        } vlm_media_instance_stopped;
+
     } u;
 } libvlc_event_t;
 
diff --git a/include/vlc_vlm.h b/include/vlc_vlm.h
index aa62d18..33f239d 100644
--- a/include/vlc_vlm.h
+++ b/include/vlc_vlm.h
@@ -117,6 +117,9 @@ enum vlm_query_e
     VLM_GET_MEDIA_LIBVLC,               /* arg1=int64_t id arg2=libvlc_vlm_media_t**  */
     /* Set media libvlc_vlm_media */
     VLM_SET_MEDIA_LIBVLC,               /* arg1=int64_t id arg2=libvlc_vlm_media_t*  */
+    /* Get media event manager */
+    VLM_GET_MEDIA_EVENT_MANAGER,        /* arg1=int64_t id arg2=vlc_event_manager_t**  */
+
     /* Media instance control XXX VOD control are for internal use only */
     /* Get all media instances */
     VLM_GET_MEDIA_INSTANCES,            /* arg1=int64_t id arg2=vlm_media_instance_t *** arg3=int *pi_instance */
diff --git a/src/control/vlm.c b/src/control/vlm.c
index 151c162..10299d8 100644
--- a/src/control/vlm.c
+++ b/src/control/vlm.c
@@ -156,6 +156,27 @@ static vlm_media_instance_t *libvlc_vlm_get_media_instance( libvlc_instance_t *p
     return p_minstance;
 }
 
+/* Vlc event callback. Transmit to libvlc */
+static void vlm_media_event( const vlc_event_t *p_event, void * user_data )
+{
+    libvlc_event_t libvlc_event;
+    libvlc_event_manager_t *p_event_manager = (libvlc_event_manager_t *)user_data;
+    switch( p_event->type )
+    {
+    case vlc_VlmMediaInstanceStarted:
+        libvlc_event.type = libvlc_VlmInstanceStarted;
+        libvlc_event.u.vlm_media_instance_started.psz_media_name = p_event->u.vlm_media_instance_started.psz_name;
+        break;
+    case vlc_VlmMediaInstanceStopped:
+        libvlc_event.type = libvlc_VlmInstanceStopped;
+        libvlc_event.u.vlm_media_instance_stopped.psz_media_name = p_event->u.vlm_media_instance_stopped.psz_name;
+        break;
+    default:
+        return;
+    }
+    libvlc_event_send(p_event_manager, &libvlc_event);
+}
+
 static int set_libvlc_vlm_media( libvlc_instance_t *p_instance, vlm_t *p_vlm, int64_t id, libvlc_exception_t *p_exception )
 {
     int n;
@@ -163,6 +184,10 @@ static int set_libvlc_vlm_media( libvlc_instance_t *p_instance, vlm_t *p_vlm, in
     if( !libvlc_vlm_media )
         return VLC_ENOMEM;
     libvlc_vlm_media->p_event_manager = libvlc_event_manager_new( libvlc_vlm_media, p_instance, p_exception );
+    libvlc_event_manager_register_event_type( libvlc_vlm_media->p_event_manager,
+            libvlc_VlmInstanceStarted, NULL );
+    libvlc_event_manager_register_event_type( libvlc_vlm_media->p_event_manager,
+            libvlc_VlmInstanceStopped, NULL );
 
     n = vlm_Control( p_vlm, VLM_SET_MEDIA_LIBVLC, id, libvlc_vlm_media );
     if ( n )
@@ -171,6 +196,22 @@ static int set_libvlc_vlm_media( libvlc_instance_t *p_instance, vlm_t *p_vlm, in
         free( libvlc_vlm_media );
         return VLC_EGENERIC;
     }
+
+    vlc_event_manager_t *p_media_event_manager;
+    n = vlm_Control( p_vlm, VLM_GET_MEDIA_EVENT_MANAGER, id, &p_media_event_manager );
+
+    if( n )
+        return VLC_EGENERIC;
+
+    vlc_event_attach( p_media_event_manager,
+                      vlc_VlmMediaInstanceStarted,
+                      vlm_media_event,
+                      libvlc_vlm_media->p_event_manager );
+    vlc_event_attach( p_media_event_manager,
+                      vlc_VlmMediaInstanceStopped,
+                      vlm_media_event,
+                      libvlc_vlm_media->p_event_manager );
+
     return VLC_SUCCESS;
 }
 
diff --git a/src/input/vlm.c b/src/input/vlm.c
index 2efe663..dc401d8 100644
--- a/src/input/vlm.c
+++ b/src/input/vlm.c
@@ -758,6 +758,15 @@ static int vlm_ControlMediaSetLibvlc( vlm_t *p_vlm, int64_t id, libvlc_vlm_media
     return VLC_SUCCESS;
 }
 
+static int vlm_ControlMediaEventManager( vlm_t *p_vlm, int64_t id, vlc_event_manager_t **pp_vlc_media_event_manager )
+{
+    vlm_media_sys_t *p_media = vlm_ControlMediaGetById( p_vlm, id );
+    if( !p_media )
+        return VLC_EGENERIC;
+    *pp_vlc_media_event_manager = &p_media->event_manager;
+    return VLC_SUCCESS;
+}
+
 static vlm_media_instance_sys_t *vlm_ControlMediaInstanceGetByName( vlm_media_sys_t *p_media, const char *psz_id )
 {
     int i;
@@ -1061,6 +1070,7 @@ static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
     vlm_media_t **pp_dsc;
     vlm_media_t ***ppp_dsc;
     vlm_media_instance_t ***ppp_idsc;
+    vlc_event_manager_t **pp_vlc_media_event_manager;
     libvlc_vlm_media_t *p_libvlc_vlm_media;
     libvlc_vlm_media_t **pp_libvlc_vlm_media;
     const char *psz_id;
@@ -1119,6 +1129,11 @@ static int vlm_vaControlInternal( vlm_t *p_vlm, int i_query, va_list args )
         pp_libvlc_vlm_media = (libvlc_vlm_media_t **)va_arg( args, libvlc_vlm_media_t ** );
         return vlm_ControlMediaGetLibvlc( p_vlm, id, pp_libvlc_vlm_media );
 
+    case VLM_GET_MEDIA_EVENT_MANAGER:
+        id = (int64_t)va_arg( args, int64_t );
+        pp_vlc_media_event_manager = (vlc_event_manager_t **)va_arg( args, vlc_event_manager_t ** );
+        return vlm_ControlMediaEventManager( p_vlm, id, pp_vlc_media_event_manager );
+
     /* Media instance control */
     case VLM_GET_MEDIA_INSTANCES:
         id = (int64_t)va_arg( args, int64_t );
-- 
1.5.4.3

