[vlc-devel] [RFC PATCHv2 06/18] es_out: add vlc_es_id_GetStrId()

Thomas Guillem thomas at gllm.fr
Tue Feb 18 17:11:19 CET 2020


This string id will be used to identify an ES track across different playback
instances.

For now, only ts/ps/bluray demuxers give stable ids, cf. future commits.
---
 include/vlc_es.h   | 29 ++++++++++++++++++
 src/input/es_out.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++
 src/libvlccore.sym |  1 +
 3 files changed, 103 insertions(+)

diff --git a/include/vlc_es.h b/include/vlc_es.h
index 5fb5ec12984..f1ef54283fb 100644
--- a/include/vlc_es.h
+++ b/include/vlc_es.h
@@ -729,6 +729,35 @@ vlc_es_id_Release(vlc_es_id_t *id);
 VLC_API int
 vlc_es_id_GetInputId(vlc_es_id_t *id);
 
+/**
+ * Return whether the ES track identifier is stable
+ *
+ * An string identifier is stable when it is certified to be the same across
+ * different playback instances for the same ES track.
+ *
+ * @param id pointer to the ES track id
+ * @return true if stable
+ */
+VLC_API bool
+vlc_es_id_IsStrIdStable(vlc_es_id_t *id);
+
+/**
+ * Get the unique string identifier
+ *
+ * This id could be used to identify a track across different playback
+ * instances.  For example, it can be used to store a track selection
+ * preference in a database.
+ *
+ * @warning Check with vlc_es_id_IsStrIdStable() if the ES track is stable
+ * before saving it for a future usage.
+ *
+ * @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 4ae27b63734..4b20541aab3 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,8 @@ struct vlc_es_id_t
 {
     int i_id;
     enum es_format_category_e i_cat;
+    bool stable;
+    char *str_id;
 };
 
 struct es_out_id_t
@@ -596,6 +599,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);
     }
 }
@@ -1903,6 +1907,43 @@ static void EsOutFillEsFmt(es_out_t *out, es_format_t *fmt)
     }
 }
 
+static char *EsOutCreateStrId( es_out_id_t *es, bool stable, 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( p_master )
+    {
+        vlc_memstream_puts( &ms, p_master->id.str_id );
+        vlc_memstream_putc( &ms, '/' );
+    }
+    else if ( id )
+    {
+        vlc_memstream_puts( &ms, id );
+        vlc_memstream_putc( &ms, '/' );
+    }
+
+    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;
+    }
+
+    if( !stable )
+        vlc_memstream_puts( &ms, "auto/" );
+
+    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 )
@@ -1931,17 +1972,35 @@ static es_out_id_t *EsOutAddLocked( es_out_t *out, es_out_ctx_t *ctx,
         return NULL;
     }
 
+    bool stable;
     if( es->fmt.i_id < 0 )
+    {
         es->fmt.i_id = es_out_ctx_GetNewEsID( ctx );
+        stable = false;
+    }
+    else
+        stable = true;
+
     if( !es->fmt.i_original_fourcc )
         es->fmt.i_original_fourcc = es->fmt.i_codec;
 
+    char *str_id =
+        EsOutCreateStrId( es, stable, 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 = EsOutProgramInsert( 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;
     }
@@ -1964,6 +2023,8 @@ 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->id.stable = stable;
 
     es_format_Init( &es->fmt_out, UNKNOWN_ES, 0 );
 
@@ -4171,6 +4232,18 @@ vlc_es_id_GetInputId(vlc_es_id_t *id)
     return id->i_id;
 }
 
+bool
+vlc_es_id_IsStrIdStable(vlc_es_id_t *id)
+{
+    return id->stable;
+}
+
+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 1d9e521662c..7ebb0f98adf 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