1、相关的文件: gstmpegdemux.cpp,gstmpegdemux.h,gstpesfilter.cpp,gstpesfilter.h
类定义
/* Information associated with a single FluPS stream. */
struct _GstPsStream
{
GstPad *pad;
gint id;
gint type;
GstClockTime segment_thresh;
GstClockTime last_ts;
gboolean discont;
gboolean notlinked;
gboolean need_segment;
GstTagList *pending_tags;
};
struct _GstPsDemux
{
GstElement parent;
GstPad *sinkpad;
gboolean random_access; /* If we operate in pull mode */
gboolean flushing;
gboolean have_group_id;
guint group_id;
GstAdapter *adapter;
GstAdapter *rev_adapter;
guint64 adapter_offset;
guint32 last_sync_code;
GstPESFilter filter; //回调出PES包数据
gint64 mux_rate;
guint64 first_scr;
guint64 last_scr;
guint64 first_dts;
guint64 base_time;
guint64 current_scr;
guint64 next_scr;
guint64 bytes_since_scr;
gint64 scr_adjust;
guint64 scr_rate_n;
guint64 scr_rate_d;
guint64 first_scr_offset;
guint64 last_scr_offset;
guint64 cur_scr_offset;
guint64 first_pts;
guint64 last_pts;
gint16 psm[GST_PS_DEMUX_MAX_PSM];
GstSegment sink_segment;
GstSegment src_segment;
gboolean adjust_segment;
/* stream output */
GstPsStream *current_stream;
guint64 next_pts;
guint64 next_dts;
GstPsStream **streams;
GstPsStream **streams_found;
gint found_count;
gboolean need_no_more_pads;
GstFlowCombiner *flowcombiner;
/* Indicates an MPEG-2 stream */
gboolean is_mpeg2_pack;
};
struct _GstPsDemuxClass
{
GstElementClass parent_class;
GstPadTemplate *sink_template;
GstPadTemplate *video_template;
GstPadTemplate *audio_template;
GstPadTemplate *subpicture_template;
GstPadTemplate *private_template;
};
2、注册插件
gboolean
gst_flups_demux_plugin_init (GstPlugin * plugin)
{
GST_DEBUG_CATEGORY_INIT (mpegpspesfilter_debug, "rsnpesfilter", 0,
"MPEG program stream PES filter debug");
GST_DEBUG_CATEGORY_INIT (gstflupsdemux_debug, "rsndvddemux", 0,
"MPEG program stream demuxer debug");
return TRUE;
}
3、类型注册
GType
gst_ps_demux_get_type (void)
{
static GType ps_demux_type = 0;
if (!ps_demux_type) {
static const GTypeInfo ps_demux_info = {
sizeof (GstPsDemuxClass),
(GBaseInitFunc) gst_ps_demux_base_init,
NULL,
(GClassInitFunc) gst_ps_demux_class_init,
NULL,
NULL,
sizeof (GstPsDemux),
0,
(GInstanceInitFunc) gst_ps_demux_init,
NULL
};
ps_demux_type =
g_type_register_static (GST_TYPE_ELEMENT, "GstMpegPSDemux",
&ps_demux_info, 0);
GST_DEBUG_CATEGORY_INIT (gstflupsdemux_debug, "mpegpsdemux", 0,
"MPEG program stream demultiplexer element");
}
宏的方式注册类型:
#define gst_nvr_hk_src_ps_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE(GstNvrHkSrcPs, gst_nvr_hk_src_ps, GST_TYPE_BASE_SRC,NULL);
4、能力集解析caps
//ps流sink caps
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("video/mpeg, "
"mpegversion = (int) { 1, 2 }, "
"systemstream = (boolean) TRUE;" "video/x-cdxa")
);
//视频src caps
static GstStaticPadTemplate video_template =
GST_STATIC_PAD_TEMPLATE ("video_%02x",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS ("video/mpeg, "
"mpegversion = (int) { 1, 2, 4 }, " "systemstream = (boolean) FALSE, "
"parsed = (boolean) FALSE; " "video/x-h264, "
"stream-format=(string)byte-stream; " "video/x-h265, "
"stream-format=(string)byte-stream;")
);
//音频src caps
static GstStaticPadTemplate audio_template =
GST_STATIC_PAD_TEMPLATE ("audio_%02x",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS ("audio/mpeg, mpegversion = (int) 1;"
"audio/mpeg, mpegversion = (int) 4, stream-format = (string) { adts, loas };"
"audio/x-private1-lpcm; "
"audio/x-private1-ac3;" "audio/x-private1-dts;" "audio/ac3")
);
//字幕src caps
static GstStaticPadTemplate subpicture_template =
GST_STATIC_PAD_TEMPLATE ("subpicture_%02x",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS ("subpicture/x-dvd")
);
//私有数据 src caps
static GstStaticPadTemplate private_template =
GST_STATIC_PAD_TEMPLATE ("private_%d",
GST_PAD_SRC,
GST_PAD_SOMETIMES,
GST_STATIC_CAPS_ANY);
5、类的初始化
gst_ps_demux_class_init (GstPsDemuxClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
parent_class = g_type_class_ref (GST_TYPE_ELEMENT);
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
gobject_class->finalize = (GObjectFinalizeFunc) gst_ps_demux_finalize;
gstelement_class->change_state = gst_ps_demux_change_state;
}
6、对象的初始化
static void
gst_ps_demux_init (GstPsDemux * demux)
{
GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
demux->sinkpad = gst_pad_new_from_template (klass->sink_template, "sink");
gst_pad_set_event_function (demux->sinkpad,
GST_DEBUG_FUNCPTR (gst_ps_demux_sink_event));
gst_pad_set_chain_function (demux->sinkpad,
GST_DEBUG_FUNCPTR (gst_ps_demux_chain));
gst_pad_set_activate_function (demux->sinkpad,
GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate));
gst_pad_set_activatemode_function (demux->sinkpad,
GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate_mode));
gst_element_add_pad (GST_ELEMENT (demux), demux->sinkpad);
demux->streams =g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
demux->streams_found =g_malloc0 (sizeof (GstPsStream *) * (GST_PS_DEMUX_MAX_STREAMS));
demux->found_count = 0;
demux->adapter = gst_adapter_new ();
demux->rev_adapter = gst_adapter_new ();
demux->flowcombiner = gst_flow_combiner_new ();
gst_ps_demux_reset (demux);
}
7、创建解复用后的视频流、音频流、字幕流,创建相应的GstPad
static GstPsStream *
gst_ps_demux_create_stream (GstPsDemux * demux, gint id, gint stream_type)
{
GstPsStream *stream;
GstPadTemplate *template;
gchar *name;
GstPsDemuxClass *klass = GST_PS_DEMUX_GET_CLASS (demux);
GstCaps *caps;
GstClockTime threshold = SEGMENT_THRESHOLD;
GstEvent *event;
gchar *stream_id;
name = NULL;
template = NULL;
caps = NULL;
GST_DEBUG_OBJECT (demux, "create stream id 0x%02x, type 0x%02x", id, stream_type);
//设置GstPad的模板,名称,能力集
switch (stream_type) {
case ST_VIDEO_MPEG1:
case ST_VIDEO_MPEG2:
case ST_VIDEO_MPEG4:
case ST_GST_VIDEO_MPEG1_OR_2:
{
gint mpeg_version = 1;
if (stream_type == ST_VIDEO_MPEG2 ||
(stream_type == ST_GST_VIDEO_MPEG1_OR_2 && demux->is_mpeg2_pack)) {
mpeg_version = 2;
}
if (stream_type == ST_VIDEO_MPEG4) {
mpeg_version = 4;
}
template = klass->video_template;
name = g_strdup_printf ("video_%02x", id);
caps = gst_caps_new_simple ("video/mpeg",
"mpegversion", G_TYPE_INT, mpeg_version,
"systemstream", G_TYPE_BOOLEAN, FALSE,
"parsed", G_TYPE_BOOLEAN, FALSE, NULL);
threshold = VIDEO_SEGMENT_THRESHOLD;
break;
}
case ST_AUDIO_MPEG1:
case ST_AUDIO_MPEG2:
template = klass->audio_template;
name = g_strdup_printf ("audio_%02x", id);
caps = gst_caps_new_simple ("audio/mpeg",
"mpegversion", G_TYPE_INT, 1, NULL);
break;
case ST_PRIVATE_SECTIONS:
case ST_PRIVATE_DATA:
case ST_MHEG:
case ST_DSMCC:
break;
case ST_AUDIO_AAC_ADTS:
template = klass->audio_template;
name = g_strdup_printf ("audio_%02x", id);
caps = gst_caps_new_simple ("audio/mpeg",
"mpegversion", G_TYPE_INT, 4,
"stream-format", G_TYPE_STRING, "adts", NULL);
break;
case ST_AUDIO_AAC_LOAS: // LATM/LOAS AAC syntax
template = klass->audio_template;
name = g_strdup_printf ("audio_%02x", id);
caps = gst_caps_new_simple ("audio/mpeg",
"mpegversion", G_TYPE_INT, 4,
"stream-format", G_TYPE_STRING, "loas", NULL);
break;
case ST_VIDEO_H264:
template = klass->video_template;
name = g_strdup_printf ("video_%02x", id);
caps = gst_caps_new_simple ("video/x-h264",
"stream-format", G_TYPE_STRING, "byte-stream", NULL);
threshold = VIDEO_SEGMENT_THRESHOLD;
break;
case ST_VIDEO_H265:
template = klass->video_template;
name = g_strdup_printf ("video_%02x", id);
caps = gst_caps_new_simple ("video/x-h265",
"stream-format", G_TYPE_STRING, "byte-stream", NULL);
threshold = VIDEO_SEGMENT_THRESHOLD;
break;
case ST_PS_AUDIO_AC3:
template = klass->audio_template;
name = g_strdup_printf ("audio_%02x", id);
caps = gst_caps_new_empty_simple ("audio/x-private1-ac3");
break;
case ST_PS_AUDIO_DTS:
template = klass->audio_template;
name = g_strdup_printf ("audio_%02x", id);
caps = gst_caps_new_empty_simple ("audio/x-private1-dts");
break;
case ST_PS_AUDIO_LPCM:
template = klass->audio_template;
name = g_strdup_printf ("audio_%02x", id);
caps = gst_caps_new_empty_simple ("audio/x-private1-lpcm");
break;
case ST_PS_DVD_SUBPICTURE:
template = klass->subpicture_template;
name = g_strdup_printf ("subpicture_%02x", id);
caps = gst_caps_new_empty_simple ("subpicture/x-dvd");
break;
case ST_GST_AUDIO_RAWA52:
template = klass->audio_template;
name = g_strdup_printf ("audio_%02x", id);
caps = gst_caps_new_empty_simple ("audio/ac3");
break;
default:
break;
}
if (name == NULL || template == NULL || caps == NULL) {
g_free (name);
if (caps)
gst_caps_unref (caps);
return FALSE;
}
stream = g_new0 (GstPsStream, 1);
stream->id = id;
stream->discont = TRUE;
stream->need_segment = TRUE;
stream->notlinked = FALSE;
stream->type = stream_type;
stream->pending_tags = NULL;
stream->pad = gst_pad_new_from_template (template, name); //创建GstPad
stream->segment_thresh = threshold;
//GstPad设置函数
gst_pad_set_event_function (stream->pad,
GST_DEBUG_FUNCPTR (gst_ps_demux_src_event));
gst_pad_set_query_function (stream->pad,
GST_DEBUG_FUNCPTR (gst_ps_demux_src_query));
gst_pad_use_fixed_caps (stream->pad);
/* 设置set_caps之前需要先激活GstPad needed for set_caps to work */
if (!gst_pad_set_active (stream->pad, TRUE)) {
GST_WARNING_OBJECT (demux, "Failed to activate pad %" GST_PTR_FORMAT,stream->pad);
}
//发送事件
stream_id = gst_pad_create_stream_id_printf (stream->pad, GST_ELEMENT_CAST (demux),"%02x", id);
event = gst_pad_get_sticky_event (demux->sinkpad, GST_EVENT_STREAM_START, 0);
if (event) {
if (gst_event_parse_group_id (event, &demux->group_id))
demux->have_group_id = TRUE;
else
demux->have_group_id = FALSE;
gst_event_unref (event);
} else if (!demux->have_group_id) {
demux->have_group_id = TRUE;
demux->group_id = gst_util_group_id_next ();
}
event = gst_event_new_stream_start (stream_id);
if (demux->have_group_id)
gst_event_set_group_id (event, demux->group_id);
gst_pad_push_event (stream->pad, event);
g_free (stream_id);
//设置caps
gst_pad_set_caps (stream->pad, caps);
if (!stream->pending_tags)
stream->pending_tags = gst_tag_list_new_empty ();
gst_pb_utils_add_codec_description_to_tag_list (stream->pending_tags, NULL,caps);
GST_DEBUG_OBJECT (demux, "create pad %s, caps %" GST_PTR_FORMAT, name, caps);
gst_caps_unref (caps);
g_free (name);
return stream;
}
8、获取GstPsStream
gst_ps_demux_get_stream:通过id获取GstPsStream,没有就通gst_ps_demux_create_stream创建,gst_ps_demux_get_stream_from_pad 通过GstPad获取GstPsStream
static GstPsStream *
gst_ps_demux_get_stream (GstPsDemux * demux, gint id, gint type)
{
GstPsStream *stream = demux->streams[id];
if (stream == NULL) {
if (!(stream = gst_ps_demux_create_stream (demux, id, type)))
goto unknown_stream;
GST_DEBUG_OBJECT (demux, "adding pad for stream id 0x%02x type 0x%02x", id,
type);
if (demux->need_no_more_pads) {
gst_element_add_pad (GST_ELEMENT (demux), stream->pad);
gst_flow_combiner_add_pad (demux->flowcombiner, stream->pad);
} else {
/* only likely to confuse decodebin etc, so discard */
/* FIXME should perform full switch protocol:
* add a whole new set of pads, drop old and no-more-pads again */
GST_DEBUG_OBJECT (demux,
"but already signalled no-more-pads; not adding");
gst_object_ref_sink (stream->pad);
}
demux->streams[id] = stream;
demux->streams_found[demux->found_count++] = stream;
}
return stream;
/* ERROR */
unknown_stream:
{
GST_DEBUG_OBJECT (demux, "unknown stream id 0x%02x type 0x%02x", id, type);
return NULL;
}
}
9、发送数据
static inline void
gst_ps_demux_send_segment (GstPsDemux * demux, GstPsStream * stream, GstClockTime pts)
{
/* discont */
if (G_UNLIKELY (stream->need_segment)) {
GstSegment segment;
GST_DEBUG ("PTS timestamp:%" GST_TIME_FORMAT " base_time %" GST_TIME_FORMAT
" src_segment.start:%" GST_TIME_FORMAT " .stop:%" GST_TIME_FORMAT,
GST_TIME_ARGS (pts), GST_TIME_ARGS (demux->base_time),
GST_TIME_ARGS (demux->src_segment.start),
GST_TIME_ARGS (demux->src_segment.stop));
/* adjust segment start if estimating a seek was off quite a bit,
* make sure to do for all streams though to preserve a/v sync */
/* FIXME such adjustment tends to be frowned upon */
if (pts != GST_CLOCK_TIME_NONE && demux->adjust_segment) {
if (demux->src_segment.rate > 0) {
if (GST_CLOCK_DIFF (demux->src_segment.start, pts) > GST_SECOND)
demux->src_segment.start = pts - demux->base_time;
} else {
if (GST_CLOCK_DIFF (demux->src_segment.stop, pts) > GST_SECOND)
demux->src_segment.stop = pts - demux->base_time;
}
}
demux->adjust_segment = FALSE;
/* we should be in sync with downstream, so start from our segment notion,
* which also includes proper base_time etc, tweak it a bit and send */
gst_segment_copy_into (&demux->src_segment, &segment);
if (GST_CLOCK_TIME_IS_VALID (demux->base_time)) {
if (GST_CLOCK_TIME_IS_VALID (segment.start))
segment.start += demux->base_time;
if (GST_CLOCK_TIME_IS_VALID (segment.stop))
segment.stop += demux->base_time;
segment.time = segment.start - demux->base_time;
}
GST_INFO_OBJECT (demux, "sending segment event %" GST_SEGMENT_FORMAT
" to pad %" GST_PTR_FORMAT, &segment, stream->pad);
gst_pad_push_event (stream->pad, gst_event_new_segment (&segment));
stream->need_segment = FALSE;
}
if (G_UNLIKELY (stream->pending_tags)) {
GST_DEBUG_OBJECT (demux, "Sending pending_tags %p for pad %s:%s : %"
GST_PTR_FORMAT, stream->pending_tags,
GST_DEBUG_PAD_NAME (stream->pad), stream->pending_tags);
gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags));
stream->pending_tags = NULL;
}
}
static GstFlowReturn
gst_ps_demux_send_data (GstPsDemux * demux, GstPsStream * stream, GstBuffer * buf)
{
GstFlowReturn result;
GstClockTime pts = GST_CLOCK_TIME_NONE, dts = GST_CLOCK_TIME_NONE;
if (stream == NULL)
goto no_stream;
/* timestamps */
if (G_UNLIKELY (demux->next_pts != G_MAXUINT64))
pts = MPEGTIME_TO_GSTTIME (demux->next_pts);
if (G_UNLIKELY (demux->next_dts != G_MAXUINT64))
dts = MPEGTIME_TO_GSTTIME (demux->next_dts);
gst_ps_demux_send_segment (demux, stream, pts);
/* OK, sent new segment now prepare the buffer for sending */
GST_BUFFER_PTS (buf) = pts;
GST_BUFFER_DTS (buf) = dts;
/* update position in the segment */
gst_segment_set_position (&demux->src_segment, GST_FORMAT_TIME,
MPEGTIME_TO_GSTTIME (demux->current_scr - demux->first_scr));
GST_LOG_OBJECT (demux, "last stop position is now %" GST_TIME_FORMAT
" current scr is %" GST_TIME_FORMAT,
GST_TIME_ARGS (demux->src_segment.position),
GST_TIME_ARGS (MPEGTIME_TO_GSTTIME (demux->current_scr)));
if (demux->src_segment.position != GST_CLOCK_TIME_NONE &&
demux->base_time != GST_CLOCK_TIME_NONE) {
GstClockTime new_time = demux->base_time + demux->src_segment.position;
if (stream->last_ts == GST_CLOCK_TIME_NONE || stream->last_ts < new_time) {
GST_LOG_OBJECT (demux,
"last_ts update on pad %s to time %" GST_TIME_FORMAT,
GST_PAD_NAME (stream->pad), GST_TIME_ARGS (new_time));
stream->last_ts = new_time;
}
gst_ps_demux_send_gap_updates (demux, new_time);
}
/* Set the buffer discont flag, and clear discont state on the stream */
if (stream->discont) {
GST_DEBUG_OBJECT (demux, "discont buffer to pad %" GST_PTR_FORMAT
" with PTS %" GST_TIME_FORMAT " DTS %" GST_TIME_FORMAT,
stream->pad, GST_TIME_ARGS (pts), GST_TIME_ARGS (dts));
GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
stream->discont = FALSE;
} else {
GST_BUFFER_FLAG_UNSET (buf, GST_BUFFER_FLAG_DISCONT);
}
demux->next_pts = G_MAXUINT64;
demux->next_dts = G_MAXUINT64;
GST_LOG_OBJECT (demux, "pushing stream id 0x%02x type 0x%02x, pts time: %"
GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT,
stream->id, stream->type, GST_TIME_ARGS (pts), gst_buffer_get_size (buf));
result = gst_pad_push (stream->pad, buf);
GST_LOG_OBJECT (demux, "result: %s", gst_flow_get_name (result));
return result;
/* ERROR */
no_stream:
{
GST_DEBUG_OBJECT (demux, "no stream given");
gst_buffer_unref (buf);
return GST_FLOW_OK;
}
}
10、遍历所有,发送消息
static inline void
gst_ps_demux_mark_discont (GstPsDemux * demux, gboolean discont,
gboolean need_segment)
{
gint i, count = demux->found_count;
/* mark discont on all streams */
for (i = 0; i < count; i++) {
GstPsStream *stream = demux->streams_found[i];
if (G_LIKELY (stream)) {
stream->discont |= discont;
stream->need_segment |= need_segment;
demux->adjust_segment |= need_segment;
GST_DEBUG_OBJECT (demux, "marked stream as discont %d, need_segment %d",stream->discont, stream->need_segment);
}
}
}
static gboolean
gst_ps_demux_send_event (GstPsDemux * demux, GstEvent * event)
{
gint i, count = demux->found_count;
gboolean ret = FALSE;
for (i = 0; i < count; i++) {
GstPsStream *stream = demux->streams_found[i];
if (stream) {
if (!gst_pad_push_event (stream->pad, gst_event_ref (event))) {
GST_DEBUG_OBJECT (stream->pad, "%s event was not handled",
GST_EVENT_TYPE_NAME (event));
} else {
/* If at least one push returns TRUE, then we return TRUE. */
GST_DEBUG_OBJECT (stream->pad, "%s event was handled",
GST_EVENT_TYPE_NAME (event));
ret = TRUE;
}
}
}
gst_event_unref (event);
return ret;
}
11、sink事件处理
gst_ps_demux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
gboolean res = TRUE;
GstPsDemux *demux = GST_PS_DEMUX (parent);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_FLUSH_START:
gst_ps_demux_send_event (demux, event);
break;
case GST_EVENT_FLUSH_STOP:
gst_ps_demux_send_event (demux, event);
gst_segment_init (&demux->sink_segment, GST_FORMAT_UNDEFINED);
gst_ps_demux_flush (demux);
break;
case GST_EVENT_SEGMENT:
{
const GstSegment *segment;
gst_event_parse_segment (event, &segment);
gst_segment_copy_into (segment, &demux->sink_segment);
GST_INFO_OBJECT (demux, "received segment %" GST_SEGMENT_FORMAT, segment);
/* we need to emit a new segment */
gst_ps_demux_mark_discont (demux, TRUE, TRUE);
if (segment->format == GST_FORMAT_BYTES && demux->scr_rate_n != G_MAXUINT64 && demux->scr_rate_d != G_MAXUINT64) {
demux->src_segment.rate = segment->rate;
demux->src_segment.applied_rate = segment->applied_rate;
demux->src_segment.format = GST_FORMAT_TIME;
demux->src_segment.start = BYTES_TO_GSTTIME (segment->start);
demux->src_segment.stop = BYTES_TO_GSTTIME (segment->stop);
demux->src_segment.time = BYTES_TO_GSTTIME (segment->time);
} else if (segment->format == GST_FORMAT_TIME) {
/* we expect our timeline (SCR, PTS) to match the one from upstream,
* if not, will adjust with offset later on */
gst_segment_copy_into (segment, &demux->src_segment);
/* accept upstream segment without adjusting */
demux->adjust_segment = FALSE;
}
gst_event_unref (event);
break;
}
case GST_EVENT_EOS:
GST_INFO_OBJECT (demux, "Received EOS");
if (!gst_ps_demux_send_event (demux, event) && !have_open_streams (demux)) {
GST_WARNING_OBJECT (demux, "EOS and no streams open");
GST_ELEMENT_ERROR (demux, STREAM, FAILED,("Internal data stream error."), ("No valid streams detected"));
}
break;
case GST_EVENT_CUSTOM_DOWNSTREAM:
case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:
{
const GstStructure *structure = gst_event_get_structure (event);
if (structure != NULL
&& gst_structure_has_name (structure, "application/x-gst-dvd")) {
res = gst_ps_demux_handle_dvd_event (demux, event);
} else {
gst_ps_demux_send_event (demux, event);
}
break;
}
case GST_EVENT_CAPS:
gst_event_unref (event);
break;
default:
gst_ps_demux_send_event (demux, event);//发送下一个元件处理函数
break;
}
return res;
}
//遍历发送事件Source GstPad
static gboolean
gst_ps_demux_send_event (GstPsDemux * demux, GstEvent * event)
{
gint i, count = demux->found_count;
gboolean ret = FALSE;
for (i = 0; i < count; i++) {
GstPsStream *stream = demux->streams_found[i];
if (stream) {
if (!gst_pad_push_event (stream->pad, gst_event_ref (event))) {
GST_DEBUG_OBJECT (stream->pad, "%s event was not handled",
GST_EVENT_TYPE_NAME (event));
} else {
/* If at least one push returns TRUE, then we return TRUE. */
GST_DEBUG_OBJECT (stream->pad, "%s event was handled",
GST_EVENT_TYPE_NAME (event));
ret = TRUE;
}
}
}
gst_event_unref (event);
return ret;
}
12、srcpad事件处理
static gboolean
gst_ps_demux_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
{
gboolean res = FALSE;
GstPsDemux *demux = GST_PS_DEMUX (parent);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_SEEK:
if (demux->random_access) {
res = gst_ps_demux_handle_seek_pull (demux, event);
} else {
res = gst_ps_demux_handle_seek_push (demux, event);
}
break;
case GST_EVENT_RECONFIGURE:{
GstPsStream *stream;
stream = gst_ps_demux_get_stream_from_pad (demux, pad);
if (stream != NULL)
stream->notlinked = FALSE;
gst_event_unref (event);
res = TRUE;
break;
}
default:
res = gst_pad_push_event (demux->sinkpad, event);
break;
}
return res;
}
13、srcpad查询处理
static gboolean
gst_ps_demux_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
{
gboolean res = FALSE;
GstPsDemux *demux = GST_PS_DEMUX (parent);
GST_LOG_OBJECT (demux, "Have query of type %d on pad %" GST_PTR_FORMAT,GST_QUERY_TYPE (query), pad);
switch (GST_QUERY_TYPE (query)) {
case GST_QUERY_POSITION: //位置查询
{
GstClockTime pos;
GstFormat format;
/* See if upstream can immediately answer */
res = gst_pad_peer_query (demux->sinkpad, query);
if (res)
break;
gst_query_parse_position (query, &format, NULL);
if (format != GST_FORMAT_TIME) {
GST_DEBUG_OBJECT (demux, "position not supported for format: %s",
gst_format_get_name (format));
goto not_supported;
}
pos = demux->src_segment.position - demux->src_segment.start;
GST_LOG_OBJECT (demux, "Position %" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
gst_query_set_position (query, format, pos);
res = TRUE;
break;
}
case GST_QUERY_DURATION: //时长查询
{
GstFormat format;
gint64 duration;
GstQuery *byte_query;
gst_query_parse_duration (query, &format, NULL);
if (G_LIKELY (format == GST_FORMAT_TIME &&
GST_CLOCK_TIME_IS_VALID (demux->src_segment.duration))) {
gst_query_set_duration (query, GST_FORMAT_TIME,
demux->src_segment.duration);
res = TRUE;
break;
}
/* For any format other than bytes, see if upstream knows first */
if (format == GST_FORMAT_BYTES) {
GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
gst_format_get_name (format));
goto not_supported;
}
if (gst_pad_peer_query (demux->sinkpad, query)) {
res = TRUE;
break;
}
/* Upstream didn't know, so we can only answer TIME queries from here on */
if (format != GST_FORMAT_TIME) {
GST_DEBUG_OBJECT (demux, "duration not supported for format: %s",
gst_format_get_name (format));
goto not_supported;
}
if (demux->mux_rate == -1) {
GST_DEBUG_OBJECT (demux, "duration not possible, no mux_rate");
goto not_supported;
}
byte_query = gst_query_new_duration (GST_FORMAT_BYTES);
if (!gst_pad_peer_query (demux->sinkpad, byte_query)) {
GST_LOG_OBJECT (demux, "query on peer pad failed");
gst_query_unref (byte_query);
goto not_supported;
}
gst_query_parse_duration (byte_query, &format, &duration);
gst_query_unref (byte_query);
GST_LOG_OBJECT (demux,"query on peer pad reported bytes %" G_GUINT64_FORMAT, duration);
duration = BYTES_TO_GSTTIME ((guint64) duration);
GST_LOG_OBJECT (demux, "converted to time %" GST_TIME_FORMAT,GST_TIME_ARGS (duration));
gst_query_set_duration (query, GST_FORMAT_TIME, duration);
res = TRUE;
break;
}
case GST_QUERY_SEEKING:{ //seek位置查询
GstFormat fmt;
gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
res = TRUE;
if (demux->random_access) {
/* In pull mode we can seek in TIME format if we have the SCR */
if (fmt != GST_FORMAT_TIME || demux->scr_rate_n == G_MAXUINT64
|| demux->scr_rate_d == G_MAXUINT64)
gst_query_set_seeking (query, fmt, FALSE, -1, -1);
else
gst_query_set_seeking (query, fmt, TRUE, 0, -1);
} else {
if (fmt == GST_FORMAT_BYTES) {
/* Seeking in BYTES format not supported at all */
gst_query_set_seeking (query, fmt, FALSE, -1, -1);
} else {
GstQuery *peerquery;
gboolean seekable;
/* Then ask upstream */
res = gst_pad_peer_query (demux->sinkpad, query);
if (res) {
/* If upstream can handle seeks we're done, if it
* can't we still have our TIME->BYTES conversion seek
*/
gst_query_parse_seeking (query, NULL, &seekable, NULL, NULL);
if (seekable || fmt != GST_FORMAT_TIME)
goto beach;
}
/* We can seek if upstream supports BYTES seeks and we
* have the SCR
*/
peerquery = gst_query_new_seeking (GST_FORMAT_BYTES);
res = gst_pad_peer_query (demux->sinkpad, peerquery);
if (!res || demux->scr_rate_n == G_MAXUINT64
|| demux->scr_rate_d == G_MAXUINT64) {
gst_query_set_seeking (query, fmt, FALSE, -1, -1);
} else {
gst_query_parse_seeking (peerquery, NULL, &seekable, NULL, NULL);
if (seekable)
gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, -1);
else
gst_query_set_seeking (query, fmt, FALSE, -1, -1);
}
gst_query_unref (peerquery);
res = TRUE;
}
}
break;
}
case GST_QUERY_SEGMENT:{ //播放段查询
GstFormat format;
gint64 start, stop;
format = demux->src_segment.format;
start =gst_segment_to_stream_time (&demux->src_segment, format,demux->src_segment.start);
if ((stop = demux->src_segment.stop) == -1)
stop = demux->src_segment.duration;
else
stop = gst_segment_to_stream_time (&demux->src_segment, format, stop);
gst_query_set_segment (query, demux->src_segment.rate, format, start,
stop);
res = TRUE;
break;
}
default:
res = gst_pad_query_default (pad, parent, query);
break;
}
beach:
return res;
not_supported:
return FALSE;
}
14、sink模式激活
//注册sink GstPad中的函数
gst_pad_set_activate_function (demux->sinkpad,
GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate));
gst_pad_set_activatemode_function (demux->sinkpad,
GST_DEBUG_FUNCPTR (gst_ps_demux_sink_activate_mode));
/* If we can pull that's prefered */
static gboolean
gst_ps_demux_sink_activate (GstPad * sinkpad, GstObject * parent)
{
gboolean res = FALSE;
GstQuery *query = gst_query_new_scheduling ();
if (gst_pad_peer_query (sinkpad, query)) {
if (gst_query_has_scheduling_mode_with_flags (query,
GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE)) {
res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
} else {
res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
}
} else {
res = gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
}
gst_query_unref (query);
return res;
}
/* This function gets called when we activate ourselves in push mode. */
static gboolean
gst_ps_demux_sink_activate_push (GstPad * sinkpad, GstObject * parent,
gboolean active)
{
GstPsDemux *demux = GST_PS_DEMUX (parent);
demux->random_access = FALSE;
return TRUE;
}
/* this function gets called when we activate ourselves in pull mode. We can perform random access to the resource and we start a task to start reading */
static gboolean
gst_ps_demux_sink_activate_pull (GstPad * sinkpad, GstObject * parent,
gboolean active)
{
GstPsDemux *demux = GST_PS_DEMUX (parent);
if (active) {
GST_DEBUG ("pull mode activated");
demux->random_access = TRUE;
return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_ps_demux_loop, sinkpad, NULL);
} else {
demux->random_access = FALSE;
return gst_pad_stop_task (sinkpad);
}
}
static gboolean
gst_ps_demux_sink_activate_mode (GstPad * pad, GstObject * parent,
GstPadMode mode, gboolean active)
{
if (mode == GST_PAD_MODE_PUSH) {
return gst_ps_demux_sink_activate_push (pad, parent, active);
} else if (mode == GST_PAD_MODE_PULL) {
return gst_ps_demux_sink_activate_pull (pad, parent, active);
}
return FALSE;
}
15:pull模式
static inline GstFlowReturn
gst_ps_demux_pull_block (GstPad * pad, GstPsDemux * demux,
guint64 offset, guint size)
{
GstFlowReturn ret;
GstBuffer *buffer = NULL;
ret = gst_pad_pull_range (pad, offset, size, &buffer);
if (G_UNLIKELY (ret != GST_FLOW_OK)) {
GST_DEBUG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
" size %u failed", offset, size);
goto beach;
} else
GST_LOG_OBJECT (demux, "pull range at %" G_GUINT64_FORMAT
" size %u done", offset, size);
if (demux->sink_segment.rate < 0) {
GST_LOG_OBJECT (demux, "setting discont flag on backward rate");
GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
}
ret = gst_ps_demux_chain (pad, GST_OBJECT (demux), buffer);
beach:
return ret;
}
static void
gst_ps_demux_loop (GstPad * pad)
{
GstPsDemux *demux;
GstFlowReturn ret = GST_FLOW_OK;
guint64 offset = 0;
demux = GST_PS_DEMUX (gst_pad_get_parent (pad));
if (G_UNLIKELY (demux->flushing)) {
ret = GST_FLOW_FLUSHING;
goto pause;
}
if (G_UNLIKELY (demux->sink_segment.format == GST_FORMAT_UNDEFINED))
gst_ps_sink_get_duration (demux);
offset = demux->sink_segment.position;
if (demux->sink_segment.rate >= 0) {
guint size = BLOCK_SZ;
if (G_LIKELY (demux->sink_segment.stop != (guint64) - 1)) {
size = MIN (size, demux->sink_segment.stop - offset);
}
/* pull in data */
ret = gst_ps_demux_pull_block (pad, demux, offset, size);
/* pause if something went wrong */
if (G_UNLIKELY (ret != GST_FLOW_OK))
goto pause;
/* update our position */
offset += size;
gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
/* check EOS condition */
if ((demux->sink_segment.position >= demux->sink_segment.stop) || (demux->src_segment.stop != (guint64) - 1 && demux->src_segment.position >= demux->src_segment.stop)) {
GST_DEBUG_OBJECT (demux,
"forward mode using segment reached end of " "segment pos %"
GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
GST_TIME_ARGS (demux->src_segment.position),
GST_TIME_ARGS (demux->src_segment.stop),
demux->sink_segment.position, demux->sink_segment.stop);
ret = GST_FLOW_EOS;
goto pause;
}
} else { /* Reverse playback */
guint64 size = MIN (offset, BLOCK_SZ);
/* pull in data */
ret = gst_ps_demux_pull_block (pad, demux, offset - size, size);
/* pause if something went wrong */
if (G_UNLIKELY (ret != GST_FLOW_OK))
goto pause;
/* update our position */
offset -= size;
gst_segment_set_position (&demux->sink_segment, GST_FORMAT_BYTES, offset);
/* check EOS condition */
if (demux->sink_segment.position <= demux->sink_segment.start ||
demux->src_segment.position <= demux->src_segment.start) {
GST_DEBUG_OBJECT (demux,
"reverse mode using segment reached end of " "segment pos %"
GST_TIME_FORMAT " stop %" GST_TIME_FORMAT " pos in bytes %"
G_GUINT64_FORMAT " stop in bytes %" G_GUINT64_FORMAT,
GST_TIME_ARGS (demux->src_segment.position),
GST_TIME_ARGS (demux->src_segment.start),
demux->sink_segment.position, demux->sink_segment.start);
ret = GST_FLOW_EOS;
goto pause;
}
}
gst_object_unref (demux);
return;
pause:
{
const gchar *reason = gst_flow_get_name (ret);
GST_LOG_OBJECT (demux, "pausing task, reason %s", reason);
gst_pad_pause_task (pad);
if (ret == GST_FLOW_EOS) {
/* perform EOS logic */
gst_element_no_more_pads (GST_ELEMENT_CAST (demux));
if (demux->src_segment.flags & GST_SEEK_FLAG_SEGMENT) {
gint64 stop;
/* for segment playback we need to post when (in stream time)
* we stopped, this is either stop (when set) or the duration. */
if ((stop = demux->src_segment.stop) == -1)
stop = demux->src_segment.duration;
if (demux->sink_segment.rate >= 0) {
GST_LOG_OBJECT (demux, "Sending segment done, at end of segment");
gst_element_post_message (GST_ELEMENT_CAST (demux),
gst_message_new_segment_done (GST_OBJECT_CAST (demux),
GST_FORMAT_TIME, stop));
gst_ps_demux_send_event (demux,
gst_event_new_segment_done (GST_FORMAT_TIME, stop));
} else { /* Reverse playback */
GST_LOG_OBJECT (demux,
"Sending segment done, at beginning of " "segment");
gst_element_post_message (GST_ELEMENT_CAST (demux),
gst_message_new_segment_done (GST_OBJECT_CAST (demux),
GST_FORMAT_TIME, demux->src_segment.start));
gst_ps_demux_send_event (demux,
gst_event_new_segment_done (GST_FORMAT_TIME,
demux->src_segment.start));
}
} else {
/* normal playback, send EOS to all linked pads */
gst_element_no_more_pads (GST_ELEMENT (demux));
GST_LOG_OBJECT (demux, "Sending EOS, at end of stream");
if (!gst_ps_demux_send_event (demux, gst_event_new_eos ())
&& !have_open_streams (demux)) {
GST_WARNING_OBJECT (demux, "EOS and no streams open");
GST_ELEMENT_ERROR (demux, STREAM, FAILED,
("Internal data stream error."), ("No valid streams detected"));
}
}
} else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
GST_ELEMENT_FLOW_ERROR (demux, ret);
gst_ps_demux_send_event (demux, gst_event_new_eos ());
}
gst_object_unref (demux);
return;
}
}
16、push模式和pull模式都会调用:chain函数,流的核心处理流程
static GstFlowReturn
gst_ps_demux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
{
GstPsDemux *demux = GST_PS_DEMUX (parent);
GstFlowReturn ret = GST_FLOW_OK;
guint32 avail;
gboolean save, discont;
//标记不满
discont = GST_BUFFER_IS_DISCONT (buffer);
if (discont) {
GST_LOG_OBJECT (demux,"Received buffer with discont flag and" " offset %"G_GUINT64_FORMAT, GST_BUFFER_OFFSET (buffer));
gst_pes_filter_drain (&demux->filter);
gst_ps_demux_mark_discont (demux, TRUE, FALSE);
/* mark discont on all streams */
if (demux->sink_segment.rate >= 0.0) {
demux->current_scr = G_MAXUINT64;
demux->bytes_since_scr = 0;
}
} else {
GST_LOG_OBJECT (demux,"Received buffer with offset %" G_GUINT64_FORMAT,GST_BUFFER_OFFSET (buffer));
}
/* We keep the offset to interpolate SCR */
demux->adapter_offset = GST_BUFFER_OFFSET (buffer);
gst_adapter_push (demux->adapter, buffer);
demux->bytes_since_scr += gst_buffer_get_size (buffer);
avail = gst_adapter_available (demux->rev_adapter);
if (avail > 0) {
GST_LOG_OBJECT (demux, "appending %u saved bytes", avail);
/* if we have a previous reverse chunk, append this now FIXME this code assumes we receive discont buffers all thei time */
gst_adapter_push (demux->adapter,gst_adapter_take_buffer (demux->rev_adapter, avail));
}
avail = gst_adapter_available (demux->adapter);
GST_LOG_OBJECT (demux, "avail now: %d, state %d", avail, demux->filter.state);
switch (demux->filter.state) {
case STATE_DATA_SKIP:
case STATE_DATA_PUSH:
ret = gst_pes_filter_process (&demux->filter);
break;
case STATE_HEADER_PARSE:
break;
default:
break;
}
switch (ret) {
case GST_FLOW_NEED_MORE_DATA:
/* Go and get more data */
ret = GST_FLOW_OK;
goto done;
case GST_FLOW_LOST_SYNC:
/* for FLOW_OK or lost-sync, carry onto resync */
ret = GST_FLOW_OK;
break;
case GST_FLOW_OK:
break;
default:
/* Any other return value should be sent upstream immediately */
goto done;
}
/* align adapter data to sync boundary, we keep the data up to the next sync point. */
save = TRUE;
while (gst_ps_demux_resync (demux, save)) {
gboolean ps_sync = TRUE;
if (G_UNLIKELY (demux->flushing)) {
ret = GST_FLOW_FLUSHING;
goto done;
}
/* now switch on last synced byte */
switch (demux->last_sync_code) {
case ID_PS_PACK_START_CODE:
ret = gst_ps_demux_parse_pack_start (demux);
break;
case ID_PS_SYSTEM_HEADER_START_CODE:
ret = gst_ps_demux_parse_sys_head (demux);
break;
case ID_PS_END_CODE:
/* Skip final 4 bytes */
gst_adapter_flush (demux->adapter, 4);
ADAPTER_OFFSET_FLUSH (4);
ret = GST_FLOW_OK;
goto done;
case ID_PS_PROGRAM_STREAM_MAP:
ret = gst_ps_demux_parse_psm (demux);
break;
default:
if (gst_ps_demux_is_pes_sync (demux->last_sync_code)) {
ret = gst_pes_filter_process (&demux->filter);
} else {
GST_DEBUG_OBJECT (demux, "sync_code=%08x, non PES sync found"
", continuing", demux->last_sync_code);
ps_sync = FALSE;
ret = GST_FLOW_LOST_SYNC;
}
break;
}
/* if we found a ps sync, we stop saving the data, any non-ps sync gets saved up to the next ps sync. */
if (ps_sync)
save = FALSE;
switch (ret) {
case GST_FLOW_NEED_MORE_DATA:
GST_DEBUG_OBJECT (demux, "need more data");
ret = GST_FLOW_OK;
goto done;
case GST_FLOW_LOST_SYNC:
if (!save || demux->sink_segment.rate >= 0.0) {
GST_DEBUG_OBJECT (demux, "flushing 3 bytes");
gst_adapter_flush (demux->adapter, 3);
ADAPTER_OFFSET_FLUSH (3);
} else {
GST_DEBUG_OBJECT (demux, "saving 3 bytes");
gst_adapter_push (demux->rev_adapter,
gst_adapter_take_buffer (demux->adapter, 3));
}
ret = GST_FLOW_OK;
break;
default:
ret = gst_ps_demux_combine_flows (demux, ret);
if (ret != GST_FLOW_OK)
goto done;
break;
}
}
done:
return ret;
}
17、PS流解包核心函数
static void
gst_ps_demux_reset_psm (GstPsDemux * demux)
{
gint i;
#define FILL_TYPE(start, stop, type) \
for (i=start; i <= stop; i++) \
demux->psm[i] = type;
/* Initialize all fields to -1 first */
FILL_TYPE (0x00, GST_PS_DEMUX_MAX_PSM - 1, -1);
FILL_TYPE (0x20, 0x3f, ST_PS_DVD_SUBPICTURE);
FILL_TYPE (0x80, 0x87, ST_PS_AUDIO_AC3);
FILL_TYPE (0x88, 0x9f, ST_PS_AUDIO_DTS);
FILL_TYPE (0xa0, 0xaf, ST_PS_AUDIO_LPCM);
FILL_TYPE (0xc0, 0xdf, ST_AUDIO_MPEG1);
FILL_TYPE (0xe0, 0xef, ST_GST_VIDEO_MPEG1_OR_2);
#undef FILL_TYPE
}
/* ISO/IEC 13818-1:
* pack_header() {
* pack_start_code 32 bslbf -+
* '01' 2 bslbf |
* system_clock_reference_base [32..30] 3 bslbf |
* marker_bit 1 bslbf |
* system_clock_reference_base [29..15] 15 bslbf |
* marker_bit 1 bslbf |
* system_clock_reference_base [14..0] 15 bslbf |
* marker_bit 1 bslbf | 112 bits
* system_clock_reference_extension 9 ubslbf |
* marker_bit 1 bslbf |
* program_mux_rate 22 ubslbf |
* marker_bit 1 bslbf |
* marker_bit 1 bslbf |
* reserved 5 bslbf |
* pack_stuffing_length 3 ubslbf -+
*
* for (i = 0; i < pack_stuffing_length; i++) {
* stuffing_byte '1111 1111' 8 bslbf
* }
*
* 112 bits = 14 bytes, as max value for pack_stuffing_length is 7, then
* in total it's needed 14 + 7 = 21 bytes.
*/
#define PACK_START_SIZE 21
static GstFlowReturn
gst_ps_demux_parse_pack_start (GstPsDemux * demux)
{
const guint8 *data;
guint length;
guint32 scr1, scr2;
guint64 scr, scr_adjusted, new_rate;
guint64 scr_rate_n;
guint64 scr_rate_d;
guint avail = gst_adapter_available (demux->adapter);
GST_LOG ("parsing pack start");
if (G_UNLIKELY (avail < PACK_START_SIZE))
goto need_more_data;
data = gst_adapter_map (demux->adapter, PACK_START_SIZE);
/* skip start code */
data += 4;
scr1 = GST_READ_UINT32_BE (data);
scr2 = GST_READ_UINT32_BE (data + 4);
/* fixed length to begin with, start code and two scr values */
length = 8 + 4;
/* start parsing the stream */
if ((*data & 0xc0) == 0x40) {
guint32 scr_ext;
guint32 next32;
guint8 stuffing_bytes;
GST_LOG ("Found MPEG2 stream");
demux->is_mpeg2_pack = TRUE;
/* mpeg2 has more data */
length += 2;
/* :2=01 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 */
/* check markers */
if (G_UNLIKELY ((scr1 & 0xc4000400) != 0x44000400))
goto lost_sync;
scr = ((guint64) scr1 & 0x38000000) << 3;
scr |= ((guint64) scr1 & 0x03fff800) << 4;
scr |= ((guint64) scr1 & 0x000003ff) << 5;
scr |= ((guint64) scr2 & 0xf8000000) >> 27;
/* marker:1==1 ! scr_ext:9 ! marker:1==1 */
if (G_UNLIKELY ((scr2 & 0x04010000) != 0x04010000))
goto lost_sync;
scr_ext = (scr2 & 0x03fe0000) >> 17;
/* We keep the offset of this scr */
demux->cur_scr_offset = demux->adapter_offset + 12;
GST_LOG_OBJECT (demux, "SCR: 0x%08" G_GINT64_MODIFIER "x SCRE: 0x%08x",scr, scr_ext);
if (scr_ext) {
scr = (scr * 300 + scr_ext % 300) / 300;
}
/* SCR has been converted into units of 90Khz ticks to make it comparable to DTS/PTS, that also implies 1 tick rounding error */
data += 6;
/* PMR:22 ! :2==11 ! reserved:5 ! stuffing_len:3 */
next32 = GST_READ_UINT32_BE (data);
if (G_UNLIKELY ((next32 & 0x00000300) != 0x00000300))
goto lost_sync;
new_rate = (next32 & 0xfffffc00) >> 10;
stuffing_bytes = (next32 & 0x07);
GST_LOG_OBJECT (demux, "stuffing bytes: %d", stuffing_bytes);
data += 4;
length += stuffing_bytes;
while (stuffing_bytes--) {
if (*data++ != 0xff)
goto lost_sync;
}
} else {
GST_DEBUG ("Found MPEG1 stream");
demux->is_mpeg2_pack = FALSE;
/* check markers */
if (G_UNLIKELY ((scr1 & 0xf1000100) != 0x21000100))
goto lost_sync;
if (G_UNLIKELY ((scr2 & 0x01800001) != 0x01800001))
goto lost_sync;
/* :4=0010 ! scr:3 ! marker:1==1 ! scr:15 ! marker:1==1 ! scr:15 ! marker:1==1 */
scr = ((guint64) scr1 & 0x0e000000) << 5;
scr |= ((guint64) scr1 & 0x00fffe00) << 6;
scr |= ((guint64) scr1 & 0x000000ff) << 7;
scr |= ((guint64) scr2 & 0xfe000000) >> 25;
/* We keep the offset of this scr */
demux->cur_scr_offset = demux->adapter_offset + 8;
/* marker:1==1 ! mux_rate:22 ! marker:1==1 */
new_rate = (scr2 & 0x007ffffe) >> 1;
data += 8;
}
new_rate *= MPEG_MUX_RATE_MULT;
/* scr adjusted is the new scr found + the colected adjustment */
scr_adjusted = scr + demux->scr_adjust;
GST_LOG_OBJECT (demux,"SCR: %" G_GINT64_FORMAT " (%" G_GINT64_FORMAT "), mux_rate %"
G_GINT64_FORMAT ", GStreamer Time:%" GST_TIME_FORMAT,
scr, scr_adjusted, new_rate,
GST_TIME_ARGS (MPEGTIME_TO_GSTTIME ((guint64) scr)));
/* keep the first src in order to calculate delta time */
if (G_UNLIKELY (demux->first_scr == G_MAXUINT64)) {
gint64 diff;
demux->first_scr = scr;
demux->first_scr_offset = demux->cur_scr_offset;
demux->base_time = MPEGTIME_TO_GSTTIME (demux->first_scr);
GST_DEBUG_OBJECT (demux, "determined base_time %" GST_TIME_FORMAT, GST_TIME_ARGS (demux->base_time));
/* at begin consider the new_rate as the scr rate, bytes/clock ticks */
scr_rate_n = new_rate;
scr_rate_d = CLOCK_FREQ;
/* our SCR timeline might have offset wrt upstream timeline */
if (demux->sink_segment.format == GST_FORMAT_TIME) {
if (demux->sink_segment.start > demux->base_time)
diff = -(demux->sink_segment.start - demux->base_time);
else
diff = demux->base_time - demux->sink_segment.start;
if (diff > GST_SECOND) {
GST_DEBUG_OBJECT (demux, "diff of %" GST_TIME_FORMAT
" wrt upstream start %" GST_TIME_FORMAT "; adjusting base",
GST_TIME_ARGS (diff), GST_TIME_ARGS (demux->sink_segment.start));
demux->base_time += diff;
}
}
} else if (G_LIKELY (demux->first_scr_offset != demux->cur_scr_offset)) {
/* estimate byte rate related to the SCR */
scr_rate_n = demux->cur_scr_offset - demux->first_scr_offset;
scr_rate_d = scr_adjusted - demux->first_scr;
} else {
scr_rate_n = demux->scr_rate_n;
scr_rate_d = demux->scr_rate_d;
}
GST_LOG_OBJECT (demux, "%s mode scr: %" G_GUINT64_FORMAT " at %" G_GUINT64_FORMAT ", first scr: %" G_GUINT64_FORMAT
" at %" G_GUINT64_FORMAT ", scr rate: %" G_GUINT64_FORMAT "/%" G_GUINT64_FORMAT "(%f)",
((demux->sink_segment.rate >= 0.0) ? "forward" : "backward"),
scr, demux->cur_scr_offset,
demux->first_scr, demux->first_scr_offset,
scr_rate_n, scr_rate_d, (float) scr_rate_n / scr_rate_d);
/* adjustment of the SCR */
if (G_LIKELY (demux->current_scr != G_MAXUINT64)) {
guint64 diff;
guint64 old_scr, old_mux_rate, bss, adjust = 0;
/* keep SCR of the previous packet */
old_scr = demux->current_scr;
old_mux_rate = demux->mux_rate;
/* Bytes since SCR is the amount we placed in the adapter since then
* (demux->bytes_since_scr) minus the amount remaining in the adapter,
* clamped to >= 0 */
bss = MAX (0, (gint) (demux->bytes_since_scr - avail));
/* estimate the new SCR using the previous one according the notes
on point 2.5.2.2 of the ISO/IEC 13818-1 document */
if (old_mux_rate != 0)
adjust = (bss * CLOCK_FREQ) / old_mux_rate;
if (demux->sink_segment.rate >= 0.0)
demux->next_scr = old_scr + adjust;
else
demux->next_scr = old_scr - adjust;
GST_LOG_OBJECT (demux,
"bss: %" G_GUINT64_FORMAT ", next_scr: %" G_GUINT64_FORMAT
", old_scr: %" G_GUINT64_FORMAT ", scr: %" G_GUINT64_FORMAT,
bss, demux->next_scr, old_scr, scr_adjusted);
/* calculate the absolute deference between the last scr and
the new one */
if (G_UNLIKELY (old_scr > scr_adjusted))
diff = old_scr - scr_adjusted;
else
diff = scr_adjusted - old_scr;
/* if the difference is more than 1 second we need to reconfigure
adjustment */
if (G_UNLIKELY (diff > CLOCK_FREQ)) {
demux->scr_adjust = demux->next_scr - scr;
GST_LOG_OBJECT (demux, "discont found, diff: %" G_GINT64_FORMAT
", adjust %" G_GINT64_FORMAT, diff, demux->scr_adjust);
scr_adjusted = demux->next_scr;
/* don't update rate estimation on disconts */
scr_rate_n = demux->scr_rate_n;
scr_rate_d = demux->scr_rate_d;
} else {
demux->next_scr = scr_adjusted;
}
}
/* update the current_scr and rate members */
demux->mux_rate = new_rate;
demux->current_scr = scr_adjusted;
demux->scr_rate_n = scr_rate_n;
demux->scr_rate_d = scr_rate_d;
/* Reset the bytes_since_scr value to count the data remaining in the
* adapter */
demux->bytes_since_scr = avail;
gst_adapter_unmap (demux->adapter);
gst_adapter_flush (demux->adapter, length);
ADAPTER_OFFSET_FLUSH (length);
return GST_FLOW_OK;
lost_sync:
{
GST_DEBUG_OBJECT (demux, "lost sync");
gst_adapter_unmap (demux->adapter);
return GST_FLOW_LOST_SYNC;
}
need_more_data:
{
GST_DEBUG_OBJECT (demux, "need more data");
return GST_FLOW_NEED_MORE_DATA;
}
}
/* ISO/IEC 13818-1:
* system_header () {
* system_header_start_code 32 bslbf -+
* header_length 16 uimsbf |
* marker_bit 1 bslbf |
* rate_bound 22 uimsbf |
* marker_bit 1 bslbf |
* audio_bound 6 uimsbf |
* fixed_flag 1 bslbf |
* CSPS_flag 1 bslbf | 96 bits
* system_audio_lock_flag 1 bslbf |
* system_video_lock_flag 1 bslbf |
* marker_bit 1 bslbf |
* video_bound 5 uimsbf |
* packet_rate_restriction_flag 1 bslbf |
* reserved_bits 7 bslbf -+
* while (nextbits () = = '1') {
* stream_id 8 uimsbf -+
* '11' 2 bslbf | 24 bits
* P-STD_buffer_bound_scale 1 bslbf |
* P-STD_buffer_size_bound 13 uimsbf -+
* }
* }
* 96 bits = 12 bytes, 24 bits = 3 bytes.
*/
static GstFlowReturn
gst_ps_demux_parse_sys_head (GstPsDemux * demux)
{
guint16 length;
const guint8 *data;
#ifndef GST_DISABLE_GST_DEBUG
gboolean csps;
#endif
if (gst_adapter_available (demux->adapter) < 6)
goto need_more_data;
/* start code + length */
data = gst_adapter_map (demux->adapter, 6);
/* skip start code */
data += 4;
length = GST_READ_UINT16_BE (data);
GST_DEBUG_OBJECT (demux, "length %d", length);
length += 6;
gst_adapter_unmap (demux->adapter);
if (gst_adapter_available (demux->adapter) < length)
goto need_more_data;
data = gst_adapter_map (demux->adapter, length);
/* skip start code and length */
data += 6;
/* marker:1==1 ! rate_bound:22 | marker:1==1 */
if ((*data & 0x80) != 0x80)
goto marker_expected;
{
guint32 rate_bound;
if ((data[2] & 0x01) != 0x01)
goto marker_expected;
rate_bound = ((guint32) data[0] & 0x7f) << 15;
rate_bound |= ((guint32) data[1]) << 7;
rate_bound |= ((guint32) data[2] & 0xfe) >> 1;
rate_bound *= MPEG_MUX_RATE_MULT;
GST_DEBUG_OBJECT (demux, "rate bound %u", rate_bound);
data += 3;
}
/* audio_bound:6==1 ! fixed:1 | constrained:1 */
{
#ifndef GST_DISABLE_GST_DEBUG
guint8 audio_bound;
gboolean fixed;
/* max number of simultaneous audio streams active */
audio_bound = (data[0] & 0xfc) >> 2;
/* fixed or variable bitrate */
fixed = (data[0] & 0x02) == 0x02;
/* meeting constraints */
csps = (data[0] & 0x01) == 0x01;
GST_DEBUG_OBJECT (demux, "audio_bound %d, fixed %d, constrained %d",
audio_bound, fixed, csps);
#endif
data += 1;
}
/* audio_lock:1 | video_lock:1 | marker:1==1 | video_bound:5 */
{
#ifndef GST_DISABLE_GST_DEBUG
gboolean audio_lock;
gboolean video_lock;
guint8 video_bound;
audio_lock = (data[0] & 0x80) == 0x80;
video_lock = (data[0] & 0x40) == 0x40;
#endif
if ((data[0] & 0x20) != 0x20)
goto marker_expected;
#ifndef GST_DISABLE_GST_DEBUG
/* max number of simultaneous video streams active */
video_bound = (data[0] & 0x1f);
GST_DEBUG_OBJECT (demux, "audio_lock %d, video_lock %d, video_bound %d",
audio_lock, video_lock, video_bound);
#endif
data += 1;
}
/* packet_rate_restriction:1 | reserved:7==0x7F */
{
#ifndef GST_DISABLE_GST_DEBUG
gboolean packet_rate_restriction;
#endif
if ((data[0] & 0x7f) != 0x7f)
goto marker_expected;
#ifndef GST_DISABLE_GST_DEBUG
/* only valid if csps is set */
if (csps) {
packet_rate_restriction = (data[0] & 0x80) == 0x80;
GST_DEBUG_OBJECT (demux, "packet_rate_restriction %d",
packet_rate_restriction);
}
#endif
}
data += 1;
{
gint stream_count = (length - 12) / 3;
gint i;
GST_DEBUG_OBJECT (demux, "number of streams: %d ", stream_count);
for (i = 0; i < stream_count; i++) {
guint8 stream_id;
#ifndef GST_DISABLE_GST_DEBUG
gboolean STD_buffer_bound_scale;
guint16 STD_buffer_size_bound;
guint32 buf_byte_size_bound;
#endif
stream_id = *data++;
if (!(stream_id & 0x80))
goto sys_len_error;
/* check marker bits */
if ((*data & 0xC0) != 0xC0)
goto no_placeholder_bits;
#ifndef GST_DISABLE_GST_DEBUG
STD_buffer_bound_scale = *data & 0x20;
STD_buffer_size_bound = ((guint16) (*data++ & 0x1F)) << 8;
STD_buffer_size_bound |= *data++;
if (STD_buffer_bound_scale == 0) {
buf_byte_size_bound = STD_buffer_size_bound * 128;
} else {
buf_byte_size_bound = STD_buffer_size_bound * 1024;
}
GST_DEBUG_OBJECT (demux, "STD_buffer_bound_scale %d",STD_buffer_bound_scale);
GST_DEBUG_OBJECT (demux, "STD_buffer_size_bound %d or %d bytes",STD_buffer_size_bound, buf_byte_size_bound);
#endif
}
}
gst_adapter_unmap (demux->adapter);
gst_adapter_flush (demux->adapter, length);
ADAPTER_OFFSET_FLUSH (length);
return GST_FLOW_OK;
/* ERRORS */
marker_expected:
{
GST_DEBUG_OBJECT (demux, "expecting marker");
gst_adapter_unmap (demux->adapter);
return GST_FLOW_LOST_SYNC;
}
no_placeholder_bits:
{
GST_DEBUG_OBJECT (demux, "expecting placeholder bit values"
" '11' after stream id");
gst_adapter_unmap (demux->adapter);
return GST_FLOW_LOST_SYNC;
}
sys_len_error:
{
GST_DEBUG_OBJECT (demux, "error in system header length");
gst_adapter_unmap (demux->adapter);
return GST_FLOW_LOST_SYNC;
}
need_more_data:
{
GST_DEBUG_OBJECT (demux, "need more data");
gst_adapter_unmap (demux->adapter);
return GST_FLOW_NEED_MORE_DATA;
}
}
static GstFlowReturn
gst_ps_demux_parse_psm (GstPsDemux * demux)
{
guint16 psm_length, info_length = 0, es_map_length = 0;
guint8 psm_version = 0;
GstByteReader br;
#ifndef GST_DISABLE_GST_DEBUG
gboolean applicable;
#endif
/* Need at least 6 bytes for start code + length */
if (gst_adapter_available (demux->adapter) < 6)
goto need_more_data;
{
const guint8 *data;
/* start code + length */
data = gst_adapter_map (demux->adapter, 6);
/* skip start code */
data += 4;
psm_length = GST_READ_UINT16_BE (data);
GST_DEBUG_OBJECT (demux, "PSM length %u", psm_length);
if (G_UNLIKELY (psm_length > 0x3FA))
goto psm_len_error;
psm_length += 6; /* Add start code + size to length */
gst_adapter_unmap (demux->adapter);
if (gst_adapter_available (demux->adapter) < psm_length)
goto need_more_data;
data = gst_adapter_map (demux->adapter, psm_length);
gst_byte_reader_init (&br, data, psm_length);
}
/* skip start code and length */
if (!gst_byte_reader_skip (&br, 6))
goto fail_invalid;
/* Read PSM applicable bit together with version */
if (!gst_byte_reader_get_uint8 (&br, &psm_version))
goto fail_invalid;
#ifndef GST_DISABLE_GST_DEBUG
applicable = (psm_version & 0x80) >> 7;
#endif
psm_version &= 0x1F;
GST_DEBUG_OBJECT (demux, "PSM version %u (applicable now %u)", psm_version,
applicable);
/* Jump over the next byte (marker bit) */
if (!gst_byte_reader_skip (&br, 1))
goto fail_invalid;
/* Read PS info length */
if (!gst_byte_reader_get_uint16_be (&br, &info_length))
goto fail_invalid;
GST_DEBUG_OBJECT (demux, "PS info length %u bytes", info_length);
/* Skip the PS info, we don't use it */
if (!gst_byte_reader_skip (&br, info_length))
goto fail_invalid;
/* Read ES map length */
if (!gst_byte_reader_get_uint16_be (&br, &es_map_length))
goto fail_invalid;
GST_DEBUG_OBJECT (demux, "ES map length %u bytes", es_map_length);
/* Now read the ES map */
{
GstByteReader es_map_br;
if (!gst_byte_reader_get_sub_reader (&br, &es_map_br, es_map_length))
goto fail_invalid;
while (gst_byte_reader_get_remaining (&es_map_br) >= 4) {
guint8 stream_type = 0, stream_id = 0;
guint16 stream_info_length = 0;
if (!gst_byte_reader_get_uint8 (&es_map_br, &stream_type) ||
!gst_byte_reader_get_uint8 (&es_map_br, &stream_id) ||
!gst_byte_reader_get_uint16_be (&es_map_br, &stream_info_length))
break;
GST_DEBUG_OBJECT (demux,
"Stream type %02X with id %02X and %u bytes info", stream_type,
stream_id, stream_info_length);
if (G_LIKELY (stream_id != 0xbd))
demux->psm[stream_id] = stream_type;
else {
/* Ignore stream type for private_stream_1 and discover it looking at
* the stream data.
* Fixes demuxing some clips with lpcm that was wrongly declared as
* mpeg audio */
GST_DEBUG_OBJECT (demux, "stream type for private_stream_1 ignored");
}
/* FIXME: We could use the descriptors instead of skipping them */
if (!gst_byte_reader_skip (&es_map_br, stream_info_length))
break;
}
}
/* We ignore the 4-byte CRC at the end */
gst_adapter_unmap (demux->adapter);
gst_adapter_flush (demux->adapter, psm_length);
ADAPTER_OFFSET_FLUSH (psm_length);
return GST_FLOW_OK;
fail_invalid:
GST_DEBUG_OBJECT (demux, "Failed to parse PSM. Skipping");
gst_adapter_unmap (demux->adapter);
gst_adapter_flush (demux->adapter, psm_length);
ADAPTER_OFFSET_FLUSH (psm_length);
return GST_FLOW_LOST_SYNC;
psm_len_error:
{
GST_DEBUG_OBJECT (demux, "error in PSM length");
gst_adapter_unmap (demux->adapter);
return GST_FLOW_LOST_SYNC;
}
need_more_data:
{
GST_DEBUG_OBJECT (demux, "need more data");
return GST_FLOW_NEED_MORE_DATA;
}
}