]> git.sesse.net Git - vlc/blobdiff - src/control/media_descriptor.c
control/media_descriptor.c: Implement libvlc_media_descriptor_new_as_node.
[vlc] / src / control / media_descriptor.c
index 942697ec67c60600cd5a206f880cbd9ba21d5ce5..0c16f78027ff22ba84887f8ebb69f3e99ca77a9c 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * media_descriptor.c: Libvlc API media descriport management
+ * media_descriptor.c: Libvlc API media descripor management
  *****************************************************************************
  * Copyright (C) 2007 the VideoLAN team
  * $Id$
@@ -25,6 +25,9 @@
 #include <vlc_input.h>
 #include <vlc_meta.h>
 
+/* For the preparser */
+#include <vlc_playlist.h>
+
 #include "libvlc_internal.h"
 
 static const vlc_meta_type_t libvlc_to_vlc_meta[] =
@@ -69,6 +72,40 @@ static const libvlc_meta_t vlc_to_libvlc_meta[] =
     [vlc_meta_TrackID]      = libvlc_meta_TrackID
 };
 
+/**************************************************************************
+ * input_item_subitem_added (Private) (vlc event Callback)
+ **************************************************************************/
+static void input_item_subitem_added( const vlc_event_t *p_event,
+                                       void * user_data )
+{
+    libvlc_media_descriptor_t * p_md = user_data;
+    libvlc_media_descriptor_t * p_md_child;
+    libvlc_event_t event;
+
+    p_md_child = libvlc_media_descriptor_new_from_input_item(
+                p_md->p_libvlc_instance,
+                p_event->u.input_item_subitem_added.p_new_child, NULL );
+
+    /* Add this to our media list */
+    if( !p_md->p_subitems )
+    {
+        p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
+        libvlc_media_list_set_media_descriptor( p_md->p_subitems, p_md, NULL );
+    }
+    if( p_md->p_subitems )
+    {
+        libvlc_media_list_add_media_descriptor( p_md->p_subitems, p_md_child, NULL );
+    }
+
+    /* Construct the event */
+    event.type = libvlc_MediaDescriptorSubItemAdded;
+    event.u.media_descriptor_subitem_added.new_child = p_md_child;
+
+    /* Send the event */
+    libvlc_event_send( p_md->p_event_manager, &event );
+    libvlc_media_descriptor_release( p_md_child );
+}
+
 /**************************************************************************
  * input_item_meta_changed (Private) (vlc event Callback)
  **************************************************************************/
@@ -87,16 +124,63 @@ static void input_item_meta_changed( const vlc_event_t *p_event,
     libvlc_event_send( p_md->p_event_manager, &event );
 }
 
+/**************************************************************************
+ * input_item_duration_changed (Private) (vlc event Callback)
+ **************************************************************************/
+static void input_item_duration_changed( const vlc_event_t *p_event,
+                                         void * user_data )
+{
+    libvlc_media_descriptor_t * p_md = user_data;
+    libvlc_event_t event;
+
+    /* Construct the event */
+    event.type = libvlc_MediaDescriptorDurationChanged;
+    event.u.media_descriptor_duration_changed.new_duration = 
+        p_event->u.input_item_duration_changed.new_duration;
+
+    /* Send the event */
+    libvlc_event_send( p_md->p_event_manager, &event );
+}
+
+/**************************************************************************
+ * input_item_preparsed_changed (Private) (vlc event Callback)
+ **************************************************************************/
+static void input_item_preparsed_changed( const vlc_event_t *p_event,
+                                          void * user_data )
+{
+    libvlc_media_descriptor_t * p_md = user_data;
+    libvlc_event_t event;
+
+    /* Construct the event */
+    event.type = libvlc_MediaDescriptorPreparsedChanged;
+    event.u.media_descriptor_preparsed_changed.new_status = 
+        p_event->u.input_item_preparsed_changed.new_status;
+
+    /* Send the event */
+    libvlc_event_send( p_md->p_event_manager, &event );
+}
 
 /**************************************************************************
  * Install event handler (Private)
  **************************************************************************/
 static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
 {
+    vlc_event_attach( &p_md->p_input_item->event_manager,
+                      vlc_InputItemSubItemAdded,
+                      input_item_subitem_added,
+                      p_md );
     vlc_event_attach( &p_md->p_input_item->event_manager,
                       vlc_InputItemMetaChanged,
                       input_item_meta_changed,
                       p_md );
+    vlc_event_attach( &p_md->p_input_item->event_manager,
+                      vlc_InputItemDurationChanged,
+                      input_item_duration_changed,
+                      p_md );
+    vlc_event_attach( &p_md->p_input_item->event_manager,
+                      vlc_InputItemPreparsedChanged,
+                      input_item_preparsed_changed,
+                      p_md );
 }
 
 /**************************************************************************
@@ -104,10 +188,22 @@ static void install_input_item_observer( libvlc_media_descriptor_t *p_md )
  **************************************************************************/
 static void uninstall_input_item_observer( libvlc_media_descriptor_t *p_md )
 {
+    vlc_event_detach( &p_md->p_input_item->event_manager,
+                      vlc_InputItemSubItemAdded,
+                      input_item_subitem_added,
+                      p_md );
     vlc_event_detach( &p_md->p_input_item->event_manager,
                       vlc_InputItemMetaChanged,
                       input_item_meta_changed,
                       p_md );
+    vlc_event_detach( &p_md->p_input_item->event_manager,
+                      vlc_InputItemDurationChanged,
+                      input_item_duration_changed,
+                      p_md );
+    vlc_event_detach( &p_md->p_input_item->event_manager,
+                      vlc_InputItemPreparsedChanged,
+                      input_item_preparsed_changed,
+                      p_md );
 }
 
 /**************************************************************************
@@ -118,8 +214,13 @@ static void preparse_if_needed( libvlc_media_descriptor_t *p_md )
     /* XXX: need some locking here */
     if (!p_md->b_preparsed)
     {
-        input_Preparse( p_md->p_libvlc_instance->p_libvlc_int,
-                        p_md->p_input_item );
+        playlist_PreparseEnqueue(
+                p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
+                p_md->p_input_item );
+        playlist_AskForArtEnqueue(
+                p_md->p_libvlc_instance->p_libvlc_int->p_playlist,
+                p_md->p_input_item );
+
         p_md->b_preparsed = VLC_TRUE;
     }
 }
@@ -145,11 +246,26 @@ libvlc_media_descriptor_t * libvlc_media_descriptor_new_from_input_item(
     p_md = malloc( sizeof(libvlc_media_descriptor_t) );
     p_md->p_libvlc_instance = p_instance;
     p_md->p_input_item      = p_input_item;
-    p_md->b_preparsed       = VLC_TRUE;
+    p_md->b_preparsed       = VLC_FALSE;
     p_md->i_refcount        = 1;
+    p_md->p_user_data       = NULL; // VLC.framework hook
+
+    /* A media descriptor can be a playlist. When you open a playlist
+     * It can give a bunch of item to read. */
+    p_md->p_subitems        = NULL;
+
+    vlc_dictionary_init( &p_md->tags, 1 );
+
     p_md->p_event_manager = libvlc_event_manager_new( p_md, p_instance, p_e );
-    libvlc_event_manager_register_event_type( p_md->p_event_manager, 
+    libvlc_event_manager_register_event_type( p_md->p_event_manager,
         libvlc_MediaDescriptorMetaChanged, p_e );
+    libvlc_event_manager_register_event_type( p_md->p_event_manager,
+        libvlc_MediaDescriptorSubItemAdded, p_e );
+    libvlc_event_manager_register_event_type( p_md->p_event_manager,
+        libvlc_MediaDescriptorFreed, p_e );
+    libvlc_event_manager_register_event_type( p_md->p_event_manager,
+        libvlc_MediaDescriptorDurationChanged, p_e );
+
     vlc_gc_incref( p_md->p_input_item );
 
     install_input_item_observer( p_md );
@@ -168,7 +284,32 @@ libvlc_media_descriptor_t * libvlc_media_descriptor_new(
     input_item_t * p_input_item;
     libvlc_media_descriptor_t * p_md;
 
-    p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, psz_mrl );
+    p_input_item = input_ItemNew( p_instance->p_libvlc_int, psz_mrl, NULL );
+
+    if (!p_input_item)
+    {
+        libvlc_exception_raise( p_e, "Can't create md's input_item" );
+        return NULL;
+    }
+
+    p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
+                p_input_item, p_e );
+
+    return p_md;
+}
+
+/**************************************************************************
+ * Create a new media descriptor object
+ **************************************************************************/
+libvlc_media_descriptor_t * libvlc_media_descriptor_new_as_node(
+                                   libvlc_instance_t *p_instance,
+                                   const char * psz_name,
+                                   libvlc_exception_t *p_e )
+{
+    input_item_t * p_input_item;
+    libvlc_media_descriptor_t * p_md;
+
+    p_input_item = input_ItemNew( p_instance->p_libvlc_int, "vlc:nop", psz_name );
 
     if (!p_input_item)
     {
@@ -179,14 +320,34 @@ libvlc_media_descriptor_t * libvlc_media_descriptor_new(
     p_md = libvlc_media_descriptor_new_from_input_item( p_instance,
                 p_input_item, p_e );
 
+    p_md->p_subitems = libvlc_media_list_new( p_md->p_libvlc_instance, NULL );
+
     return p_md;
 }
 
+/**************************************************************************
+ * Add an option to the media descriptor,
+ * that will be used to determine how the media_instance will read the
+ * media_descriptor. This allow to use VLC advanced reading/streaming
+ * options in a per-media basis
+ *
+ * The options are detailled in vlc --long-help, for instance "--sout-all"
+ **************************************************************************/
+void libvlc_media_descriptor_add_option(
+                                   libvlc_media_descriptor_t * p_md,
+                                   const char * ppsz_option,
+                                   libvlc_exception_t *p_e )
+{
+    (void)p_e;
+    input_ItemAddOptionNoDup( p_md->p_input_item, ppsz_option );
+}
+
 /**************************************************************************
  * Delete a media descriptor object
  **************************************************************************/
 void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
 {
+    int i;
     if (!p_md)
         return;
 
@@ -195,9 +356,35 @@ void libvlc_media_descriptor_release( libvlc_media_descriptor_t *p_md )
     if( p_md->i_refcount > 0 )
         return;
 
+    if( p_md->p_subitems )
+        libvlc_media_list_release( p_md->p_subitems );
+
     uninstall_input_item_observer( p_md );
     vlc_gc_decref( p_md->p_input_item );
 
+    /* Construct the event */
+    libvlc_event_t event;
+    event.type = libvlc_MediaDescriptorFreed;
+    event.u.media_descriptor_freed.md = p_md;
+
+    /* Send the event */
+    libvlc_event_send( p_md->p_event_manager, &event );
+
+    libvlc_event_manager_release( p_md->p_event_manager );
+
+    char ** all_keys = vlc_dictionary_all_keys( &p_md->tags );
+    for( i = 0; all_keys[i]; i++ )
+    {
+        int j;
+        struct libvlc_tags_storage_t * p_ts = vlc_dictionary_value_for_key( &p_md->tags, all_keys[i] );
+        for( j = 0; j < p_ts->i_count; j++ )
+        {
+            free( p_ts->ppsz_tags[j] );
+            free( p_ts->ppsz_tags );
+        }
+        free( p_ts );
+    }
+    vlc_dictionary_clear( &p_md->tags );
     free( p_md );
 }
 
@@ -230,7 +417,7 @@ libvlc_media_descriptor_get_mrl( libvlc_media_descriptor_t * p_md,
                                  libvlc_exception_t * p_e )
 {
     (void)p_e;
-    return strdup( p_md->p_input_item->psz_uri );
+    return input_item_GetURI( p_md->p_input_item );
 }
 
 /**************************************************************************
@@ -241,7 +428,7 @@ char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
                                          libvlc_meta_t e_meta,
                                          libvlc_exception_t *p_e )
 {
-    const char * psz_meta;
+    char * psz_meta;
 
     /* XXX: locking */
 
@@ -252,11 +439,206 @@ char * libvlc_media_descriptor_get_meta( libvlc_media_descriptor_t *p_md,
 
     /* Should be integrated in core */
     if( !psz_meta && e_meta == libvlc_meta_Title && p_md->p_input_item->psz_name )
+    {
+        free( psz_meta );
         return strdup( p_md->p_input_item->psz_name );
+    }
+
+    return psz_meta;
+}
+
+/**************************************************************************
+ * Add a tag
+ **************************************************************************/
+void libvlc_media_descriptor_add_tag( libvlc_media_descriptor_t *p_md,
+                                      const char * key,
+                                      const libvlc_tag_t tag,
+                                      libvlc_exception_t *p_e )
+{
+    struct libvlc_tags_storage_t * p_ts;
+
+    if( !tag || !key )
+        return;
+    p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
+
+    if( !p_ts )
+    {
+        p_ts = malloc(sizeof(struct libvlc_tags_storage_t));
+        memset( p_ts, 0, sizeof(struct libvlc_tags_storage_t) );
+    }
+    p_ts->i_count++;
+
+    if( !p_ts->ppsz_tags )
+        p_ts->ppsz_tags = malloc(sizeof(char*)*(p_ts->i_count));
+    else
+        p_ts->ppsz_tags = realloc(p_ts->ppsz_tags, sizeof(char*)*(p_ts->i_count));
+    p_ts->ppsz_tags[p_ts->i_count-1] = strdup( tag );
+}
+
+
+/**************************************************************************
+ * Remove a tag
+ **************************************************************************/
+void libvlc_media_descriptor_remove_tag( libvlc_media_descriptor_t *p_md,
+                                         const char * key,
+                                         const libvlc_tag_t tag,
+                                         libvlc_exception_t *p_e )
+{
+    struct libvlc_tags_storage_t * p_ts;
+    int i;
+
+    if( !tag || !key )
+        return;
+    p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
+
+    if( !p_ts )
+        return;
+
+    for( i = 0; i < p_ts->i_count; i++ )
+    {
+        if( !strcmp( p_ts->ppsz_tags[i], tag ) )
+        {
+            free( p_ts->ppsz_tags[i] );
+            memcpy( p_ts->ppsz_tags + i + 1, p_ts->ppsz_tags + i, (p_ts->i_count - i - 2)*sizeof(char*) );
+            /* Don't dealloc, the memory will be regain if we add a new tag */
+            p_ts->i_count--;
+            return;
+        }
+    }
+}
+
+/**************************************************************************
+ * Get tags count
+ **************************************************************************/
+int libvlc_media_descriptor_tags_count_for_key( libvlc_media_descriptor_t *p_md,
+                                                 const char * key,
+                                                 libvlc_exception_t *p_e )
+{
+    struct libvlc_tags_storage_t * p_ts;
+
+    if( !key )
+        return 0;
+    p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
+
+    if( !p_ts )
+        return 0;
+    return p_ts->i_count;
+}
+
+/**************************************************************************
+ * Get a tag
+ **************************************************************************/
+libvlc_tag_t
+libvlc_media_descriptor_tag_at_index_for_key( libvlc_media_descriptor_t *p_md,
+                                              int i,
+                                              const char * key,
+                                              libvlc_exception_t *p_e )
+{
+    struct libvlc_tags_storage_t * p_ts;
 
-    if( !psz_meta )
+    if( !key )
         return NULL;
+    p_ts = vlc_dictionary_value_for_key( &p_md->tags, key );
 
-    return strdup( psz_meta );
+    if( !p_ts )
+        return NULL;
+    return strdup( p_ts->ppsz_tags[i] );
+}
+
+/**************************************************************************
+ * subitems
+ **************************************************************************/
+libvlc_media_list_t *
+libvlc_media_descriptor_subitems( libvlc_media_descriptor_t * p_md,
+                                  libvlc_exception_t * p_e )
+{
+    if( p_md->p_subitems )
+        libvlc_media_list_retain( p_md->p_subitems );
+    return p_md->p_subitems;
+}
+
+/**************************************************************************
+ * event_manager
+ **************************************************************************/
+libvlc_event_manager_t *
+libvlc_media_descriptor_event_manager( libvlc_media_descriptor_t * p_md,
+                                       libvlc_exception_t * p_e )
+{
+    return p_md->p_event_manager;
+}
+
+/**************************************************************************
+ * Get duration of media_descriptor object.
+ **************************************************************************/
+vlc_int64_t
+libvlc_media_descriptor_get_duration( libvlc_media_descriptor_t * p_md,
+                                      libvlc_exception_t * p_e )
+{
+    if( p_md && p_md->p_input_item)
+    {
+        return input_item_GetDuration( p_md->p_input_item );
+    }
+    else
+    {
+        return -1;
+    }
+}
+
+/**************************************************************************
+ * Get preparsed status for media_descriptor object.
+ **************************************************************************/
+vlc_bool_t
+libvlc_media_descriptor_is_preparsed( libvlc_media_descriptor_t * p_md,
+                                       libvlc_exception_t * p_e )
+{
+    if( p_md && p_md->p_input_item)
+    {
+        return input_item_IsPreparsed( p_md->p_input_item );
+    }
+    else
+    {
+        return VLC_FALSE;
+    }
+}
+
+/**************************************************************************
+ * Sets media descriptor's user_data. user_data is specialized data 
+ * accessed by the host application, VLC.framework uses it as a pointer to 
+ * an native object that references a libvlc_media_descriptor_t pointer
+ **************************************************************************/
+void 
+libvlc_media_descriptor_set_user_data( libvlc_media_descriptor_t * p_md,
+                                       void * p_new_user_data,
+                                       libvlc_exception_t * p_e )
+{
+    if( p_md )
+    {
+        p_md->p_user_data = p_new_user_data;
+    }
+}
+
+/**************************************************************************
+ * Get media descriptor's user_data. user_data is specialized data 
+ * accessed by the host application, VLC.framework uses it as a pointer to 
+ * an native object that references a libvlc_media_descriptor_t pointer
+ **************************************************************************/
+void *
+libvlc_media_descriptor_get_user_data( libvlc_media_descriptor_t * p_md,
+                                       libvlc_exception_t * p_e )
+{
+    if( p_md )
+    {
+        return p_md->p_user_data;
+    }
+    else
+    {
+        return NULL;
+    }
 }