[vlc-devel] [RFC PATCH 9/9] RFC: es_out: add vlc_es_id_GetStrId()

Thomas Guillem thomas at gllm.fr
Wed Feb 12 15:43:27 CET 2020


---
 include/vlc_es.h   | 18 ++++++++++++++
 src/input/es_out.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++
 src/libvlccore.sym |  1 +
 3 files changed, 80 insertions(+)

diff --git a/include/vlc_es.h b/include/vlc_es.h
index 5fb5ec12984..aa49d2d20af 100644
--- a/include/vlc_es.h
+++ b/include/vlc_es.h
@@ -729,6 +729,24 @@ vlc_es_id_Release(vlc_es_id_t *id);
 VLC_API int
 vlc_es_id_GetInputId(vlc_es_id_t *id);
 
+/**
+ * Get the unique and (almost) reproducible string identifier
+ *
+ * This id could be used to identify a track across multiple run of the player.
+ * For example, it can be used to store a track selection preference in a
+ * database.
+ *
+ * @warning There are some corner cases where this id could collide with other
+ * track ids. It can happen with demuxers that don't set a fmt.i_id and when
+ * opening a media at different offset.
+ *
+ * @param id pointer to the ES track id
+ * @return the ES track string identifier, can't be NULL, valid until id is
+ * released
+ */
+VLC_API const char *
+vlc_es_id_GetStrId(vlc_es_id_t *id);
+
 /**
  * Get the ES category
  *
diff --git a/src/input/es_out.c b/src/input/es_out.c
index d11ef22be33..c2027980dda 100644
--- a/src/input/es_out.c
+++ b/src/input/es_out.c
@@ -39,6 +39,7 @@
 #include <vlc_meta.h>
 #include <vlc_list.h>
 #include <vlc_decoder.h>
+#include <vlc_memstream.h>
 
 #include "input_internal.h"
 #include "../clock/input_clock.h"
@@ -92,6 +93,7 @@ struct vlc_es_id_t
 {
     int i_id;
     enum es_format_category_e i_cat;
+    char *str_id;
 };
 
 struct es_out_id_t
@@ -599,6 +601,7 @@ static void EsRelease(es_out_id_t *es)
         free(es->psz_language_code);
         es_format_Clean(&es->fmt);
         es_out_ctx_Release(es->ctx);
+        free(es->id.str_id);
         free(es);
     }
 }
@@ -1906,6 +1909,46 @@ static void EsOutFillEsFmt(es_out_t *out, es_format_t *fmt)
     }
 }
 
+static char *EsOutCreateTrackId( es_out_id_t *es, const char *id,
+                                 es_out_id_t *p_master )
+{
+    struct vlc_memstream ms;
+    int ret = vlc_memstream_open( &ms );
+    if( ret != 0 )
+        return NULL;
+
+    if( id != NULL )
+    {
+        vlc_memstream_puts( &ms, id );
+        vlc_memstream_putc( &ms, '/' );
+    }
+
+    if( p_master )
+    {
+        char *master_id = EsOutCreateTrackId( p_master, NULL, NULL );
+        if( master_id )
+        {
+            vlc_memstream_puts( &ms, master_id );
+            vlc_memstream_putc( &ms, '/' );
+            free( master_id );
+        }
+    }
+
+    switch (es->fmt.i_cat)
+    {
+        case VIDEO_ES:  vlc_memstream_puts( &ms, "video" );    break;
+        case AUDIO_ES:  vlc_memstream_puts( &ms, "audio" );    break;
+        case SPU_ES:    vlc_memstream_puts( &ms, "spu" );      break;
+        case DATA_ES:   vlc_memstream_puts( &ms, "data" );     break;
+        default:        vlc_memstream_puts( &ms, "unknown" );  break;
+    }
+
+    vlc_memstream_printf( &ms, "/%d", es->fmt.i_id );
+    ret = vlc_memstream_close( &ms );
+
+    return ret == 0 ? ms.ptr : NULL;
+}
+
 static es_out_id_t *EsOutAddLocked( es_out_t *out, es_out_ctx_t *ctx,
                                     const es_format_t *fmt,
                                     es_out_id_t *p_master )
@@ -1951,12 +1994,23 @@ static es_out_id_t *EsOutAddLocked( es_out_t *out, es_out_ctx_t *ctx,
     if( !es->fmt.i_original_fourcc )
         es->fmt.i_original_fourcc = es->fmt.i_codec;
 
+    char *str_id =
+        EsOutCreateTrackId( es, es_out_ctx_GetSourceId(ctx), p_master );
+    if( !str_id )
+    {
+        es_format_Clean( &es->fmt );
+        es_out_ctx_Release( es->ctx );
+        free( es );
+        return NULL;
+    }
+
     /* Search the program */
     p_pgrm = EsOutProgramFindOrAdd( out, ctx, fmt->i_group );
     if( !p_pgrm )
     {
         es_format_Clean( &es->fmt );
         es_out_ctx_Release( es->ctx );
+        free( str_id );
         free( es );
         return NULL;
     }
@@ -1979,6 +2033,7 @@ static es_out_id_t *EsOutAddLocked( es_out_t *out, es_out_ctx_t *ctx,
 
     es->id.i_id = es->fmt.i_id;
     es->id.i_cat = es->fmt.i_cat;
+    es->id.str_id = str_id;
 
     es_format_Init( &es->fmt_out, UNKNOWN_ES, 0 );
 
@@ -4189,6 +4244,12 @@ vlc_es_id_GetInputId(vlc_es_id_t *id)
     return id->i_id;
 }
 
+const char *
+vlc_es_id_GetStrId(vlc_es_id_t *id)
+{
+    return id->str_id;
+}
+
 enum es_format_category_e
 vlc_es_id_GetCat(vlc_es_id_t *id)
 {
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index cf0f2230a19..46e029e2fb1 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -763,6 +763,7 @@ vlc_es_id_Hold
 vlc_es_id_Release
 vlc_es_id_GetInputId
 vlc_es_id_GetCat
+vlc_es_id_GetStrId
 vlc_thumbnailer_Create
 vlc_thumbnailer_RequestByTime
 vlc_thumbnailer_RequestByPos
-- 
2.20.1



More information about the vlc-devel mailing list