tsmux: Allow specifying PMT order via the prog-map
authorJan Alexander Steffens (heftig) <jan.steffens@ltnglobal.com>
Mon, 10 Jan 2022 12:34:21 +0000 (13:34 +0100)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Mon, 24 Jan 2022 15:37:46 +0000 (15:37 +0000)
Look for an entry `PMT_<PID>` in the `prog-map`, which specifies the
relative index of the stream in the PMT.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/1510>

subprojects/gst-plugins-bad/gst/mpegtsmux/gstbasetsmux.c
subprojects/gst-plugins-bad/gst/mpegtsmux/tsmux/tsmux.c
subprojects/gst-plugins-bad/gst/mpegtsmux/tsmux/tsmuxstream.c
subprojects/gst-plugins-bad/gst/mpegtsmux/tsmux/tsmuxstream.h

index cecedbd22afb8016ff40a9312d5f4d453240db69..1f88072689bfdb97acb9df5da0fa53d37e07a1c3 100644 (file)
@@ -388,6 +388,7 @@ gst_base_ts_mux_create_or_update_stream (GstBaseTsMux * mux,
   guint8 color_spec = 0;
   const gchar *stream_format = NULL;
   const char *interlace_mode = NULL;
+  gchar *pmt_name;
 
   GST_DEBUG_OBJECT (ts_pad,
       "%s stream with PID 0x%04x for caps %" GST_PTR_FORMAT,
@@ -693,6 +694,12 @@ gst_base_ts_mux_create_or_update_stream (GstBaseTsMux * mux,
       goto error;
   }
 
+  pmt_name = g_strdup_printf ("PMT_%d", ts_pad->pid);
+  if (mux->prog_map && gst_structure_has_field (mux->prog_map, pmt_name)) {
+    gst_structure_get_int (mux->prog_map, pmt_name, &ts_pad->stream->pmt_index);
+  }
+  g_free (pmt_name);
+
   interlace_mode = gst_structure_get_string (s, "interlace-mode");
   gst_structure_get_int (s, "rate", &ts_pad->stream->audio_sampling);
   gst_structure_get_int (s, "channels", &ts_pad->stream->audio_channels);
index 299506bce83abd13a974f4beddf17769fef8ce0a..e2b994fd4e12df6aa26efab8bbc3df2b7ed9b82e 100644 (file)
@@ -611,24 +611,39 @@ tsmux_program_add_stream (TsMuxProgram * program, TsMuxStream * stream)
 {
   GPtrArray *streams;
   guint i;
-  gint array_index = -1 /* append */ ;
+  gint pmt_index, array_index = -1 /* append */ ;
   guint16 pid;
 
   g_return_if_fail (program != NULL);
   g_return_if_fail (stream != NULL);
 
   streams = program->streams;
+  pmt_index = stream->pmt_index;
   pid = tsmux_stream_get_pid (stream);
 
-  /* Insert sorted by PID */
-  for (i = 0; i < streams->len; i++) {
-    TsMuxStream *s = g_ptr_array_index (streams, i);
+  if (pmt_index >= 0) {
+    /* Insert into streams with known indices */
+    for (i = 0; i < streams->len; i++) {
+      TsMuxStream *s = g_ptr_array_index (streams, i);
 
-    if (pid < tsmux_stream_get_pid (s)) {
-      array_index = i;
-      GST_DEBUG ("PID 0x%04x: Using PID-order index %d/%u",
-          pid, array_index, streams->len);
-      break;
+      if (s->pmt_index < 0 || pmt_index < s->pmt_index) {
+        array_index = i;
+        GST_DEBUG ("PID 0x%04x: Using known-order index %d/%u",
+            pid, array_index, streams->len);
+        break;
+      }
+    }
+  } else {
+    /* Insert after streams with known indices, sorted by PID */
+    for (i = 0; i < streams->len; i++) {
+      TsMuxStream *s = g_ptr_array_index (streams, i);
+
+      if (s->pmt_index < 0 && pid < tsmux_stream_get_pid (s)) {
+        array_index = i;
+        GST_DEBUG ("PID 0x%04x: Using PID-order index %d/%u",
+            pid, array_index, streams->len);
+        break;
+      }
     }
   }
 
index 151ad83902030141b79e2ef14bb5a7276d0a76b8..04dafa0c5e2c0e8023fd4617153dec3357603d80 100644 (file)
@@ -117,6 +117,7 @@ tsmux_stream_new (guint16 pid, guint stream_type)
   stream->pes_payload_size = 0;
   stream->cur_pes_payload_size = 0;
   stream->pes_bytes_written = 0;
+  stream->pmt_index = -1;
 
   switch (stream_type) {
     case TSMUX_ST_VIDEO_MPEG1:
index 6bdcde86759613e309a647d8d318a6f37b384d1a..3920e4076a5b0fdec60ef9a2fbb3cf202d59aa0c 100644 (file)
@@ -150,6 +150,8 @@ struct TsMuxStream {
   guint8 id;
   /* extended stream id (13818-1 Amdt 2) */
   guint8 id_extended;
+  /* requested index in the PMT */
+  gint pmt_index;
 
   gboolean is_video_stream;