[vlc-devel] [PATCH 09/10] libvlc: keep the meta strings in the libvlc_media_t

Steve Lhomme robux4 at ycbcr.xyz
Mon May 27 15:38:21 CEST 2019


They will be free'd with the media
---
 include/vlc/libvlc_media.h      |  4 ++--
 lib/media.c                     | 23 +++++++++++++----------
 lib/media_internal.h            |  3 +++
 test/libvlc/media.c             |  3 +--
 test/libvlc/media_list_player.c |  6 ++----
 test/libvlc/meta.c              |  3 +--
 6 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/include/vlc/libvlc_media.h b/include/vlc/libvlc_media.h
index 202efba5c0..26b5e52b41 100644
--- a/include/vlc/libvlc_media.h
+++ b/include/vlc/libvlc_media.h
@@ -575,8 +575,8 @@ LIBVLC_API libvlc_media_t *libvlc_media_duplicate( libvlc_media_t *p_md );
  * \param e_meta the meta to read
  * \return the media's meta
  */
-LIBVLC_API char *libvlc_media_get_meta( libvlc_media_t *p_md,
-                                             libvlc_meta_t e_meta );
+LIBVLC_API const char *libvlc_media_get_meta( libvlc_media_t *p_md,
+                                              libvlc_meta_t e_meta );
 
 /**
  * Set the meta of the media (this function will not save the meta, call
diff --git a/lib/media.c b/lib/media.c
index db7ef622bb..c5fe11aebc 100644
--- a/lib/media.c
+++ b/lib/media.c
@@ -577,6 +577,9 @@ void libvlc_media_release( libvlc_media_t *p_md )
 
     libvlc_event_manager_destroy( &p_md->event_manager );
     libvlc_release( p_md->p_libvlc_instance );
+
+    for (size_t i=0; i<ARRAY_SIZE(p_md->metas); i++)
+        free( p_md->metas[i] );
     free( p_md );
 }
 
@@ -613,24 +616,24 @@ libvlc_media_get_mrl( libvlc_media_t * p_md )
  * Getter for meta information
  **************************************************************************/
 
-char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
+const char *libvlc_media_get_meta( libvlc_media_t *p_md, libvlc_meta_t e_meta )
 {
-    char *psz_meta = NULL;
-
+    static_assert((libvlc_meta_Last+1) >= ARRAY_SIZE(libvlc_to_vlc_meta), "total meta mismatch");
+    free( p_md->metas[e_meta] );
     if( e_meta == libvlc_meta_NowPlaying )
     {
-        psz_meta = input_item_GetNowPlayingFb( p_md->p_input_item );
+        p_md->metas[e_meta] = input_item_GetNowPlayingFb( p_md->p_input_item );
     }
     else
     {
-        psz_meta = input_item_GetMeta( p_md->p_input_item,
-                                             libvlc_to_vlc_meta[e_meta] );
-        /* Should be integrated in core */
-        if( psz_meta == NULL && e_meta == libvlc_meta_Title
+        p_md->metas[e_meta] = input_item_GetMeta( p_md->p_input_item,
+                                                  libvlc_to_vlc_meta[e_meta] );
+        /* FIXME Should be integrated in core */
+        if( p_md->metas[e_meta] == NULL && e_meta == libvlc_meta_Title
          && p_md->p_input_item->psz_name != NULL )
-            psz_meta = strdup( p_md->p_input_item->psz_name );
+            p_md->metas[e_meta] = strdup( p_md->p_input_item->psz_name );
     }
-    return psz_meta;
+    return p_md->metas[e_meta];
 }
 
 /**************************************************************************
diff --git a/lib/media_internal.h b/lib/media_internal.h
index aefc731c68..eed5d5708d 100644
--- a/lib/media_internal.h
+++ b/lib/media_internal.h
@@ -47,6 +47,9 @@ struct libvlc_media_t
     libvlc_media_parsed_status_t parsed_status;
     bool is_parsed;
     bool has_asked_preparse;
+
+    /* strings read by the host and released with the media */
+    char *metas[ libvlc_meta_Last+1 ];
 };
 
 /* Media Descriptor */
diff --git a/test/libvlc/media.c b/test/libvlc/media.c
index d6e2484357..60a4435fcf 100644
--- a/test/libvlc/media.c
+++ b/test/libvlc/media.c
@@ -81,10 +81,9 @@ static void print_media(libvlc_media_t *media)
     for (enum libvlc_meta_t i = libvlc_meta_Title;
          i <= libvlc_meta_DiscTotal; ++i)
     {
-        char *psz_meta = libvlc_media_get_meta(media, i);
+        const char *psz_meta = libvlc_media_get_meta(media, i);
         if (psz_meta != NULL)
             test_log("\tmeta(%d): '%s'\n", i, psz_meta);
-        free(psz_meta);
     }
 }
 
diff --git a/test/libvlc/media_list_player.c b/test/libvlc/media_list_player.c
index 27cf3e1fbc..0e74d01e78 100644
--- a/test/libvlc/media_list_player.c
+++ b/test/libvlc/media_list_player.c
@@ -89,15 +89,13 @@ static void check_items_order_callback(const libvlc_event_t * p_event, void * us
     assert(checks->index < checks->count);
     if (checks->items[checks->index] != md)
     {
-        char *title = libvlc_media_get_meta(md, libvlc_meta_Title);
+        const char *title = libvlc_media_get_meta(md, libvlc_meta_Title);
         test_log ("Got items %s\n", title);
-        free(title);
     }
     assert(checks->items[checks->index] == md);
 
-    char *title = libvlc_media_get_meta(md, libvlc_meta_Title);
+    const char *title = libvlc_media_get_meta(md, libvlc_meta_Title);
     test_log ("Item %d '%s' was correctly queued\n", checks->index, title);
-    free(title);
 
     if (checks->index == (checks->count - 1))
     {
diff --git a/test/libvlc/meta.c b/test/libvlc/meta.c
index 6160fc5a4b..a239162c23 100644
--- a/test/libvlc/meta.c
+++ b/test/libvlc/meta.c
@@ -29,7 +29,7 @@ static void test_meta (const char ** argv, int argc)
 {
     libvlc_instance_t *vlc;
     libvlc_media_t *media;
-    char * artist;
+    const char * artist;
 
     test_log ("Testing meta\n");
 
@@ -51,7 +51,6 @@ static void test_meta (const char ** argv, int argc)
     int string_compare = strcmp (artist, expected_artist);
     assert (!string_compare);
 
-    free (artist);
     libvlc_media_release (media);
     libvlc_release (vlc);
 }
-- 
2.17.1



More information about the vlc-devel mailing list