Commit 44c701d1 authored by Mathieu Duponchelle's avatar Mathieu Duponchelle 🐸 Committed by Mathieu Duponchelle

basetsmux: extract m2ts-mode to mpegtsmux

parent 649cc2d5
This diff is collapsed.
......@@ -134,7 +134,6 @@ struct BaseTsMux {
GHashTable *programs;
/* properties */
gboolean m2ts_mode;
GstStructure *prog_map;
guint pat_interval;
guint pmt_interval;
......@@ -155,12 +154,8 @@ struct BaseTsMux {
gboolean is_header;
GstClockTime last_ts;
/* m2ts specific */
gint64 previous_pcr;
gint64 previous_offset;
gint64 pcr_rate_num;
gint64 pcr_rate_den;
GstAdapter *adapter;
gsize packet_size;
gsize automatic_alignment;
/* output buffer aggregation */
GstAdapter *out_adapter;
......@@ -183,8 +178,15 @@ struct BaseTsMuxClass {
TsMux * (*create_ts_mux) (BaseTsMux *mux);
guint (*handle_media_type) (BaseTsMux *mux, const gchar *media_type, BaseTsPadData * ts_data);
void (*allocate_packet) (BaseTsMux *mux, GstBuffer **buffer);
gboolean (*output_packet) (BaseTsMux *mux, GstBuffer *buffer, gint64 new_pcr);
void (*reset) (BaseTsMux *mux);
gboolean (*drain) (BaseTsMux *mux);
};
void gst_base_tsmux_set_packet_size (BaseTsMux *mux, gsize size);
void gst_base_tsmux_set_automatic_alignment (BaseTsMux *mux, gsize alignment);
struct BaseTsPadData {
/* parent */
GstCollectData collect;
......
......@@ -84,6 +84,14 @@
#include "mpegtsmux.h"
#define MPEGTSMUX_DEFAULT_M2TS FALSE
enum
{
PROP_0,
PROP_M2TS_MODE,
};
static GstStaticPadTemplate mpegtsmux_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink_%d",
GST_PAD_SINK,
......@@ -135,14 +143,242 @@ GST_DEBUG_CATEGORY (mpegtsmux_debug);
G_DEFINE_TYPE (MpegTsMux, mpegtsmux, GST_TYPE_BASE_TSMUX);
#define parent_class mpegtsmux_parent_class
static void
gst_mpegtsmux_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
MpegTsMux *mux = GST_MPEG_TSMUX (object);
switch (prop_id) {
case PROP_M2TS_MODE:
/* set incase if the output stream need to be of 192 bytes */
mux->m2ts_mode = g_value_get_boolean (value);
gst_base_tsmux_set_packet_size (GST_BASE_TSMUX (mux),
mux->m2ts_mode ? M2TS_PACKET_LENGTH : NORMAL_TS_PACKET_LENGTH);
gst_base_tsmux_set_automatic_alignment (GST_BASE_TSMUX (mux),
mux->m2ts_mode ? 32 : 0);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_mpegtsmux_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec)
{
MpegTsMux *mux = GST_MPEG_TSMUX (object);
switch (prop_id) {
case PROP_M2TS_MODE:
g_value_set_boolean (value, mux->m2ts_mode);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_mpegtsmux_allocate_packet (BaseTsMux * mux, GstBuffer ** buffer)
{
((BaseTsMuxClass *) parent_class)->allocate_packet (mux, buffer);
gst_buffer_set_size (*buffer, NORMAL_TS_PACKET_LENGTH);
}
static gboolean
new_packet_m2ts (MpegTsMux * mux, GstBuffer * buf, gint64 new_pcr)
{
GstBuffer *out_buf;
int chunk_bytes;
GstMapInfo map;
GST_LOG_OBJECT (mux, "Have buffer %p with new_pcr=%" G_GINT64_FORMAT,
buf, new_pcr);
chunk_bytes = gst_adapter_available (mux->adapter);
if (G_LIKELY (buf)) {
if (new_pcr < 0) {
/* If there is no pcr in current ts packet then just add the packet
to the adapter for later output when we see a PCR */
GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
gst_adapter_push (mux->adapter, buf);
goto exit;
}
/* no first interpolation point yet, then this is the one,
* otherwise it is the second interpolation point */
if (mux->previous_pcr < 0 && chunk_bytes) {
mux->previous_pcr = new_pcr;
mux->previous_offset = chunk_bytes;
GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
gst_adapter_push (mux->adapter, buf);
goto exit;
}
} else {
g_assert (new_pcr == -1);
}
/* interpolate if needed, and 2 points available */
if (chunk_bytes && (new_pcr != mux->previous_pcr)) {
gint64 offset = 0;
GST_LOG_OBJECT (mux, "Processing pending packets; "
"previous pcr %" G_GINT64_FORMAT ", previous offset %d, "
"current pcr %" G_GINT64_FORMAT ", current offset %d",
mux->previous_pcr, (gint) mux->previous_offset,
new_pcr, (gint) chunk_bytes);
g_assert (chunk_bytes > mux->previous_offset);
/* if draining, use previous rate */
if (G_LIKELY (new_pcr > 0)) {
mux->pcr_rate_num = new_pcr - mux->previous_pcr;
mux->pcr_rate_den = chunk_bytes - mux->previous_offset;
}
while (offset < chunk_bytes) {
guint64 cur_pcr, ts;
/* Loop, pulling packets of the adapter, updating their 4 byte
* timestamp header and pushing */
/* interpolate PCR */
if (G_LIKELY (offset >= mux->previous_offset))
cur_pcr = mux->previous_pcr +
gst_util_uint64_scale (offset - mux->previous_offset,
mux->pcr_rate_num, mux->pcr_rate_den);
else
cur_pcr = mux->previous_pcr -
gst_util_uint64_scale (mux->previous_offset - offset,
mux->pcr_rate_num, mux->pcr_rate_den);
/* FIXME: what about DTS here? */
ts = gst_adapter_prev_pts (mux->adapter, NULL);
out_buf = gst_adapter_take_buffer (mux->adapter, M2TS_PACKET_LENGTH);
g_assert (out_buf);
offset += M2TS_PACKET_LENGTH;
GST_BUFFER_PTS (out_buf) = ts;
gst_buffer_map (out_buf, &map, GST_MAP_WRITE);
/* The header is the bottom 30 bits of the PCR, apparently not
* encoded into base + ext as in the packets themselves */
GST_WRITE_UINT32_BE (map.data, cur_pcr & 0x3FFFFFFF);
gst_buffer_unmap (out_buf, &map);
GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, cur_pcr);
((BaseTsMuxClass *) parent_class)->output_packet (GST_BASE_TSMUX (mux),
out_buf, -1);
}
}
if (G_UNLIKELY (!buf))
goto exit;
gst_buffer_map (buf, &map, GST_MAP_WRITE);
/* Finally, output the passed in packet */
/* Only write the bottom 30 bits of the PCR */
GST_WRITE_UINT32_BE (map.data, new_pcr & 0x3FFFFFFF);
gst_buffer_unmap (buf, &map);
GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, new_pcr);
((BaseTsMuxClass *) parent_class)->output_packet (GST_BASE_TSMUX (mux), buf,
-1);
if (new_pcr != mux->previous_pcr) {
mux->previous_pcr = new_pcr;
mux->previous_offset = -M2TS_PACKET_LENGTH;
}
exit:
return TRUE;
}
static gboolean
gst_mpegtsmux_output_packet (BaseTsMux * base_tsmux, GstBuffer * buffer,
gint64 new_pcr)
{
MpegTsMux *mux = (MpegTsMux *) base_tsmux;
GstMapInfo map;
if (!mux->m2ts_mode)
return ((BaseTsMuxClass *) parent_class)->output_packet (base_tsmux, buffer,
new_pcr);
gst_buffer_set_size (buffer, M2TS_PACKET_LENGTH);
gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
/* there should be a better way to do this */
memmove (map.data + 4, map.data, map.size - 4);
gst_buffer_unmap (buffer, &map);
return new_packet_m2ts (mux, buffer, new_pcr);
}
static void
gst_mpegtsmux_reset (BaseTsMux * base_tsmux)
{
MpegTsMux *mux = (MpegTsMux *) base_tsmux;
if (mux->adapter)
gst_adapter_clear (mux->adapter);
mux->previous_pcr = -1;
mux->previous_offset = 0;
mux->pcr_rate_num = mux->pcr_rate_den = 1;
}
static void
gst_mpegtsmux_dispose (GObject * object)
{
MpegTsMux *mux = (MpegTsMux *) object;
if (mux->adapter) {
g_object_unref (mux->adapter);
mux->adapter = NULL;
}
GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
}
static gboolean
gst_mpegtsmux_drain (BaseTsMux * mux)
{
return new_packet_m2ts ((MpegTsMux *) mux, NULL, -1);
}
static void
mpegtsmux_class_init (MpegTsMuxClass * klass)
{
GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
BaseTsMuxClass *base_tsmux_class = (BaseTsMuxClass *) klass;
GST_DEBUG_CATEGORY_INIT (mpegtsmux_debug, "mpegtsmux", 0,
"MPEG Transport Stream muxer");
gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_mpegtsmux_set_property);
gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_mpegtsmux_get_property);
gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_mpegtsmux_dispose);
base_tsmux_class->allocate_packet =
GST_DEBUG_FUNCPTR (gst_mpegtsmux_allocate_packet);
base_tsmux_class->output_packet =
GST_DEBUG_FUNCPTR (gst_mpegtsmux_output_packet);
base_tsmux_class->reset = GST_DEBUG_FUNCPTR (gst_mpegtsmux_reset);
base_tsmux_class->drain = GST_DEBUG_FUNCPTR (gst_mpegtsmux_drain);
gst_element_class_set_static_metadata (gstelement_class,
"MPEG Transport Stream Muxer", "Codec/Muxer",
"Multiplexes media streams into an MPEG Transport Stream",
......@@ -153,9 +389,17 @@ mpegtsmux_class_init (MpegTsMuxClass * klass)
gst_element_class_add_static_pad_template (gstelement_class,
&mpegtsmux_src_factory);
g_object_class_install_property (gobject_class, PROP_M2TS_MODE,
g_param_spec_boolean ("m2ts-mode", "M2TS(192 bytes) Mode",
"Set to TRUE to output Blu-Ray disc format with 192 byte packets. "
"FALSE for standard TS format with 188 byte packets.",
MPEGTSMUX_DEFAULT_M2TS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
}
static void
mpegtsmux_init (MpegTsMux * mux)
{
mux->m2ts_mode = MPEGTSMUX_DEFAULT_M2TS;
mux->adapter = gst_adapter_new ();
}
......@@ -102,6 +102,16 @@ typedef struct MpegTsPadData MpegTsPadData;
struct MpegTsMux {
BaseTsMux parent;
/* Properties */
gboolean m2ts_mode;
/* m2ts specific */
gint64 previous_pcr;
gint64 previous_offset;
gint64 pcr_rate_num;
gint64 pcr_rate_den;
GstAdapter *adapter;
};
struct MpegTsMuxClass {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment