2010-07-11 12:44:10 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-03 23:05:09 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2010-07-11 12:44:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "gststreamsynchronizer.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (stream_synchronizer_debug);
|
|
|
|
#define GST_CAT_DEFAULT stream_synchronizer_debug
|
|
|
|
|
2012-11-25 18:07:04 +00:00
|
|
|
#define GST_STREAM_SYNCHRONIZER_LOCK(obj) G_STMT_START { \
|
|
|
|
GST_TRACE_OBJECT (obj, \
|
2010-07-11 12:44:10 +00:00
|
|
|
"locking from thread %p", \
|
|
|
|
g_thread_self ()); \
|
2012-11-25 18:07:04 +00:00
|
|
|
g_mutex_lock (&GST_STREAM_SYNCHRONIZER_CAST(obj)->lock); \
|
|
|
|
GST_TRACE_OBJECT (obj, \
|
2010-07-11 12:44:10 +00:00
|
|
|
"locked from thread %p", \
|
|
|
|
g_thread_self ()); \
|
|
|
|
} G_STMT_END
|
|
|
|
|
2012-11-25 18:07:04 +00:00
|
|
|
#define GST_STREAM_SYNCHRONIZER_UNLOCK(obj) G_STMT_START { \
|
|
|
|
GST_TRACE_OBJECT (obj, \
|
2010-07-11 12:44:10 +00:00
|
|
|
"unlocking from thread %p", \
|
|
|
|
g_thread_self ()); \
|
2012-08-14 16:53:52 +00:00
|
|
|
g_mutex_unlock (&GST_STREAM_SYNCHRONIZER_CAST(obj)->lock); \
|
2010-07-11 12:44:10 +00:00
|
|
|
} G_STMT_END
|
|
|
|
|
2011-11-04 09:48:50 +00:00
|
|
|
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src_%u",
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_PAD_SRC,
|
|
|
|
GST_PAD_SOMETIMES,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
2011-11-04 09:48:50 +00:00
|
|
|
static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink_%u",
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_REQUEST,
|
|
|
|
GST_STATIC_CAPS_ANY);
|
|
|
|
|
2011-04-19 09:35:53 +00:00
|
|
|
#define gst_stream_synchronizer_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE (GstStreamSynchronizer, gst_stream_synchronizer,
|
|
|
|
GST_TYPE_ELEMENT);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GstStreamSynchronizer *transform;
|
|
|
|
guint stream_number;
|
|
|
|
GstPad *srcpad;
|
|
|
|
GstPad *sinkpad;
|
|
|
|
GstSegment segment;
|
|
|
|
|
2012-08-14 16:53:52 +00:00
|
|
|
gboolean wait; /* TRUE if waiting/blocking */
|
|
|
|
gboolean is_eos; /* TRUE if EOS was received */
|
2015-01-12 08:08:33 +00:00
|
|
|
gboolean eos_sent; /* when EOS was sent downstream */
|
|
|
|
gboolean flushing; /* set after flush-start and before flush-stop */
|
2010-08-12 08:02:56 +00:00
|
|
|
gboolean seen_data;
|
2017-03-23 05:56:19 +00:00
|
|
|
gboolean send_gap_event;
|
2015-01-12 09:40:25 +00:00
|
|
|
GstClockTime gap_duration;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2015-06-22 18:54:18 +00:00
|
|
|
GstStreamFlags flags;
|
|
|
|
|
2012-08-14 16:53:52 +00:00
|
|
|
GCond stream_finish_cond;
|
2012-07-27 10:58:40 +00:00
|
|
|
|
2012-08-14 16:53:52 +00:00
|
|
|
/* seqnum of the previously received STREAM_START
|
|
|
|
* default: G_MAXUINT32 */
|
|
|
|
guint32 stream_start_seqnum;
|
|
|
|
guint32 segment_seqnum;
|
2013-07-22 11:15:09 +00:00
|
|
|
guint group_id;
|
2015-07-21 07:58:56 +00:00
|
|
|
} GstSyncStream;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
/* Must be called with lock! */
|
2012-08-14 16:53:52 +00:00
|
|
|
static inline GstPad *
|
2015-07-21 07:58:56 +00:00
|
|
|
gst_stream_get_other_pad (GstSyncStream * stream, GstPad * pad)
|
2010-07-11 12:44:10 +00:00
|
|
|
{
|
|
|
|
if (stream->sinkpad == pad)
|
|
|
|
return gst_object_ref (stream->srcpad);
|
2012-08-14 16:53:52 +00:00
|
|
|
if (stream->srcpad == pad)
|
2010-07-11 12:44:10 +00:00
|
|
|
return gst_object_ref (stream->sinkpad);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstPad *
|
2012-08-14 16:53:52 +00:00
|
|
|
gst_stream_get_other_pad_from_pad (GstStreamSynchronizer * self, GstPad * pad)
|
2010-07-11 12:44:10 +00:00
|
|
|
{
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2010-07-11 12:44:10 +00:00
|
|
|
GstPad *opad = NULL;
|
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
|
|
|
if (!stream)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
opad = gst_stream_get_other_pad (stream, pad);
|
|
|
|
|
|
|
|
out:
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
|
|
|
|
if (!opad)
|
|
|
|
GST_WARNING_OBJECT (pad, "Trying to get other pad after releasing");
|
|
|
|
|
|
|
|
return opad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Generic pad functions */
|
|
|
|
static GstIterator *
|
2011-11-16 16:50:03 +00:00
|
|
|
gst_stream_synchronizer_iterate_internal_links (GstPad * pad,
|
|
|
|
GstObject * parent)
|
2010-07-11 12:44:10 +00:00
|
|
|
{
|
|
|
|
GstIterator *it = NULL;
|
|
|
|
GstPad *opad;
|
|
|
|
|
2012-08-14 16:53:52 +00:00
|
|
|
opad =
|
|
|
|
gst_stream_get_other_pad_from_pad (GST_STREAM_SYNCHRONIZER (parent), pad);
|
2010-07-11 12:44:10 +00:00
|
|
|
if (opad) {
|
2011-05-05 14:03:52 +00:00
|
|
|
GValue value = { 0, };
|
|
|
|
|
|
|
|
g_value_init (&value, GST_TYPE_PAD);
|
|
|
|
g_value_set_object (&value, opad);
|
|
|
|
it = gst_iterator_new_single (GST_TYPE_PAD, &value);
|
|
|
|
g_value_unset (&value);
|
2010-07-11 12:44:10 +00:00
|
|
|
gst_object_unref (opad);
|
|
|
|
}
|
|
|
|
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* srcpad functions */
|
|
|
|
static gboolean
|
2011-11-17 11:48:25 +00:00
|
|
|
gst_stream_synchronizer_src_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event)
|
2010-07-11 12:44:10 +00:00
|
|
|
{
|
2011-11-17 11:48:25 +00:00
|
|
|
GstStreamSynchronizer *self = GST_STREAM_SYNCHRONIZER (parent);
|
2010-07-11 12:44:10 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pad, "Handling event %s: %" GST_PTR_FORMAT,
|
2011-05-10 09:54:30 +00:00
|
|
|
GST_EVENT_TYPE_NAME (event), event);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
|
|
|
case GST_EVENT_QOS:{
|
|
|
|
gdouble proportion;
|
|
|
|
GstClockTimeDiff diff;
|
|
|
|
GstClockTime timestamp;
|
2012-08-14 16:53:52 +00:00
|
|
|
gint64 running_time_diff = -1;
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2011-05-09 16:53:03 +00:00
|
|
|
gst_event_parse_qos (event, NULL, &proportion, &diff, ×tamp);
|
2010-07-11 12:44:10 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
|
|
|
if (stream)
|
2012-08-14 16:53:52 +00:00
|
|
|
running_time_diff = stream->segment.base;
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
|
|
|
|
if (running_time_diff == -1) {
|
|
|
|
GST_WARNING_OBJECT (pad, "QOS event before group start");
|
|
|
|
goto out;
|
2012-08-14 16:53:52 +00:00
|
|
|
}
|
|
|
|
if (timestamp < running_time_diff) {
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "QOS event from previous group");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pad,
|
|
|
|
"Adjusting QOS event: %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT " = %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (timestamp),
|
|
|
|
GST_TIME_ARGS (running_time_diff),
|
|
|
|
GST_TIME_ARGS (timestamp - running_time_diff));
|
|
|
|
|
|
|
|
timestamp -= running_time_diff;
|
|
|
|
|
|
|
|
/* That case is invalid for QoS events */
|
|
|
|
if (diff < 0 && -diff > timestamp) {
|
|
|
|
GST_DEBUG_OBJECT (pad, "QOS event from previous group");
|
|
|
|
ret = TRUE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-05-09 16:53:03 +00:00
|
|
|
event =
|
|
|
|
gst_event_new_qos (GST_QOS_TYPE_UNDERFLOW, proportion, diff,
|
|
|
|
timestamp);
|
2010-07-11 12:44:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-08 10:11:07 +00:00
|
|
|
ret = gst_pad_event_default (pad, parent, event);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-12 08:08:33 +00:00
|
|
|
/* must be called with the STREAM_SYNCHRONIZER_LOCK */
|
|
|
|
static gboolean
|
|
|
|
gst_stream_synchronizer_wait (GstStreamSynchronizer * self, GstPad * pad)
|
|
|
|
{
|
|
|
|
gboolean ret = FALSE;
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2015-01-12 08:08:33 +00:00
|
|
|
|
|
|
|
while (!self->eos && !self->flushing) {
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
|
|
|
if (!stream) {
|
2015-02-15 05:51:36 +00:00
|
|
|
GST_WARNING_OBJECT (pad, "unknown stream");
|
2015-01-12 08:08:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (stream->flushing) {
|
|
|
|
GST_DEBUG_OBJECT (pad, "Flushing");
|
|
|
|
break;
|
|
|
|
}
|
2015-02-15 05:51:36 +00:00
|
|
|
if (!stream->wait) {
|
|
|
|
GST_DEBUG_OBJECT (pad, "Stream not waiting anymore");
|
|
|
|
break;
|
|
|
|
}
|
2015-01-12 08:08:33 +00:00
|
|
|
|
2017-03-23 05:56:19 +00:00
|
|
|
if (stream->send_gap_event) {
|
2015-01-12 08:08:33 +00:00
|
|
|
GstEvent *event;
|
|
|
|
|
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (stream->segment.position)) {
|
|
|
|
GST_WARNING_OBJECT (pad, "Have no position and can't send GAP event");
|
2017-03-23 05:56:19 +00:00
|
|
|
stream->send_gap_event = FALSE;
|
2015-01-12 08:08:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-12 09:40:25 +00:00
|
|
|
event =
|
|
|
|
gst_event_new_gap (stream->segment.position, stream->gap_duration);
|
|
|
|
GST_DEBUG_OBJECT (pad,
|
|
|
|
"Send GAP event, position: %" GST_TIME_FORMAT " duration: %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (stream->segment.position),
|
|
|
|
GST_TIME_ARGS (stream->gap_duration));
|
|
|
|
|
2015-01-12 08:08:33 +00:00
|
|
|
/* drop lock when sending GAP event, which may block in e.g. preroll */
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
ret = gst_pad_push_event (pad, event);
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
if (!ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-03-23 05:56:19 +00:00
|
|
|
stream->send_gap_event = FALSE;
|
2015-03-14 18:08:15 +00:00
|
|
|
|
|
|
|
/* force a check on the loop conditions as we unlocked a
|
|
|
|
* few lines above and those variables could have changed */
|
|
|
|
continue;
|
2015-01-12 08:08:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_cond_wait (&stream->stream_finish_cond, &self->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-07-11 12:44:10 +00:00
|
|
|
/* sinkpad functions */
|
|
|
|
static gboolean
|
2011-11-17 11:48:25 +00:00
|
|
|
gst_stream_synchronizer_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event)
|
2010-07-11 12:44:10 +00:00
|
|
|
{
|
2011-11-17 11:48:25 +00:00
|
|
|
GstStreamSynchronizer *self = GST_STREAM_SYNCHRONIZER (parent);
|
2010-07-11 12:44:10 +00:00
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pad, "Handling event %s: %" GST_PTR_FORMAT,
|
2011-05-10 09:54:30 +00:00
|
|
|
GST_EVENT_TYPE_NAME (event), event);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2012-07-10 16:34:41 +00:00
|
|
|
case GST_EVENT_STREAM_START:
|
|
|
|
{
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream, *ostream;
|
2012-08-14 16:53:52 +00:00
|
|
|
guint32 seqnum = gst_event_get_seqnum (event);
|
2013-07-22 11:15:09 +00:00
|
|
|
guint group_id;
|
|
|
|
gboolean have_group_id;
|
2012-08-14 16:53:52 +00:00
|
|
|
GList *l;
|
|
|
|
gboolean all_wait = TRUE;
|
2013-04-23 11:18:45 +00:00
|
|
|
gboolean new_stream = TRUE;
|
2012-07-10 16:34:41 +00:00
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
have_group_id = gst_event_parse_group_id (event, &group_id);
|
|
|
|
|
2012-07-10 16:34:41 +00:00
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
2013-07-22 11:15:09 +00:00
|
|
|
self->have_group_id &= have_group_id;
|
|
|
|
have_group_id = self->have_group_id;
|
|
|
|
|
2012-07-10 16:34:41 +00:00
|
|
|
stream = gst_pad_get_element_private (pad);
|
2013-07-22 11:15:09 +00:00
|
|
|
|
|
|
|
if (!stream) {
|
|
|
|
GST_DEBUG_OBJECT (self, "No stream or STREAM_START from same source");
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-06-22 18:54:18 +00:00
|
|
|
gst_event_parse_stream_flags (event, &stream->flags);
|
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
if ((have_group_id && stream->group_id != group_id) || (!have_group_id
|
|
|
|
&& stream->stream_start_seqnum != seqnum)) {
|
2012-07-10 16:34:41 +00:00
|
|
|
stream->is_eos = FALSE;
|
2015-01-12 08:08:33 +00:00
|
|
|
stream->eos_sent = FALSE;
|
|
|
|
stream->flushing = FALSE;
|
2013-04-23 11:18:45 +00:00
|
|
|
stream->stream_start_seqnum = seqnum;
|
2013-07-22 11:15:09 +00:00
|
|
|
stream->group_id = group_id;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
if (!have_group_id) {
|
|
|
|
/* Check if this belongs to a stream that is already there,
|
|
|
|
* e.g. we got the visualizations for an audio stream */
|
|
|
|
for (l = self->streams; l; l = l->next) {
|
|
|
|
ostream = l->data;
|
|
|
|
|
|
|
|
if (ostream != stream && ostream->stream_start_seqnum == seqnum
|
|
|
|
&& !ostream->wait) {
|
|
|
|
new_stream = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
if (!new_stream) {
|
|
|
|
GST_DEBUG_OBJECT (pad,
|
|
|
|
"Stream %d belongs to running stream %d, no waiting",
|
|
|
|
stream->stream_number, ostream->stream_number);
|
|
|
|
stream->wait = FALSE;
|
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
2012-07-10 16:34:41 +00:00
|
|
|
break;
|
2013-04-23 11:18:45 +00:00
|
|
|
}
|
2013-07-22 11:15:09 +00:00
|
|
|
} else if (group_id == self->group_id) {
|
|
|
|
GST_DEBUG_OBJECT (pad, "Stream %d belongs to running group %d, "
|
|
|
|
"no waiting", stream->stream_number, group_id);
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
break;
|
2012-07-10 16:34:41 +00:00
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "Stream %d changed", stream->stream_number);
|
2013-04-23 11:18:45 +00:00
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
stream->wait = TRUE;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *ostream = l->data;
|
2013-04-23 11:18:45 +00:00
|
|
|
|
2015-06-22 18:54:18 +00:00
|
|
|
all_wait = all_wait && ((ostream->flags & GST_STREAM_FLAG_SPARSE)
|
|
|
|
|| (ostream->wait && (!have_group_id
|
|
|
|
|| ostream->group_id == group_id)));
|
2013-07-22 11:15:09 +00:00
|
|
|
if (!all_wait)
|
|
|
|
break;
|
|
|
|
}
|
2013-04-23 11:18:45 +00:00
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
if (all_wait) {
|
|
|
|
gint64 position = 0;
|
|
|
|
|
|
|
|
if (have_group_id)
|
|
|
|
GST_DEBUG_OBJECT (self,
|
|
|
|
"All streams have changed to group id %u -- unblocking",
|
|
|
|
group_id);
|
|
|
|
else
|
2013-04-23 11:18:45 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "All streams have changed -- unblocking");
|
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
self->group_id = group_id;
|
|
|
|
|
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *ostream = l->data;
|
2013-07-22 11:15:09 +00:00
|
|
|
gint64 stop_running_time;
|
|
|
|
gint64 position_running_time;
|
|
|
|
|
|
|
|
ostream->wait = FALSE;
|
|
|
|
|
|
|
|
if (ostream->segment.format == GST_FORMAT_TIME) {
|
2016-09-20 19:12:22 +00:00
|
|
|
if (ostream->segment.rate > 0)
|
|
|
|
stop_running_time =
|
|
|
|
gst_segment_to_running_time (&ostream->segment,
|
|
|
|
GST_FORMAT_TIME, ostream->segment.stop);
|
|
|
|
else
|
|
|
|
stop_running_time =
|
|
|
|
gst_segment_to_running_time (&ostream->segment,
|
|
|
|
GST_FORMAT_TIME, ostream->segment.start);
|
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
position_running_time =
|
|
|
|
gst_segment_to_running_time (&ostream->segment,
|
|
|
|
GST_FORMAT_TIME, ostream->segment.position);
|
2015-05-05 13:35:46 +00:00
|
|
|
|
|
|
|
position_running_time =
|
|
|
|
MAX (position_running_time, stop_running_time);
|
2016-09-20 19:12:22 +00:00
|
|
|
|
|
|
|
if (ostream->segment.rate > 0)
|
|
|
|
position_running_time -=
|
|
|
|
gst_segment_to_running_time (&ostream->segment,
|
|
|
|
GST_FORMAT_TIME, ostream->segment.start);
|
|
|
|
else
|
|
|
|
position_running_time -=
|
|
|
|
gst_segment_to_running_time (&ostream->segment,
|
|
|
|
GST_FORMAT_TIME, ostream->segment.stop);
|
|
|
|
|
2015-05-05 13:35:46 +00:00
|
|
|
position_running_time = MAX (0, position_running_time);
|
|
|
|
|
|
|
|
position = MAX (position, position_running_time);
|
2013-04-23 11:18:45 +00:00
|
|
|
}
|
2013-07-22 11:15:09 +00:00
|
|
|
}
|
2015-05-05 13:35:46 +00:00
|
|
|
|
|
|
|
self->group_start_time += position;
|
2012-07-10 16:34:41 +00:00
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "New group start time: %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (self->group_start_time));
|
2012-07-10 16:34:41 +00:00
|
|
|
|
2013-07-22 11:15:09 +00:00
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *ostream = l->data;
|
2015-02-15 05:51:36 +00:00
|
|
|
ostream->wait = FALSE;
|
2013-07-22 11:15:09 +00:00
|
|
|
g_cond_broadcast (&ostream->stream_finish_cond);
|
2012-07-27 10:58:40 +00:00
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
}
|
2013-04-23 11:18:45 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 16:34:41 +00:00
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
break;
|
2013-04-23 11:18:45 +00:00
|
|
|
}
|
2011-05-16 11:48:11 +00:00
|
|
|
case GST_EVENT_SEGMENT:{
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2011-05-16 11:48:11 +00:00
|
|
|
GstSegment segment;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2011-05-18 15:23:18 +00:00
|
|
|
gst_event_copy_segment (event, &segment);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
2015-02-15 05:51:36 +00:00
|
|
|
|
|
|
|
gst_stream_synchronizer_wait (self, pad);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2010-07-22 07:13:00 +00:00
|
|
|
if (self->shutdown) {
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
gst_event_unref (event);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2015-02-15 05:51:36 +00:00
|
|
|
stream = gst_pad_get_element_private (pad);
|
2011-05-16 11:48:11 +00:00
|
|
|
if (stream && segment.format == GST_FORMAT_TIME) {
|
2015-05-05 13:35:46 +00:00
|
|
|
GST_DEBUG_OBJECT (pad,
|
|
|
|
"New stream, updating base from %" GST_TIME_FORMAT " to %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (segment.base),
|
|
|
|
GST_TIME_ARGS (segment.base + self->group_start_time));
|
|
|
|
segment.base += self->group_start_time;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pad, "Segment was: %" GST_SEGMENT_FORMAT,
|
|
|
|
&stream->segment);
|
2011-05-16 11:48:11 +00:00
|
|
|
gst_segment_copy_into (&segment, &stream->segment);
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "Segment now is: %" GST_SEGMENT_FORMAT,
|
|
|
|
&stream->segment);
|
2012-08-14 16:53:52 +00:00
|
|
|
stream->segment_seqnum = gst_event_get_seqnum (event);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (pad, "Stream start running time: %" GST_TIME_FORMAT,
|
2011-05-16 11:48:11 +00:00
|
|
|
GST_TIME_ARGS (stream->segment.base));
|
2012-07-19 14:12:22 +00:00
|
|
|
{
|
|
|
|
GstEvent *tmpev;
|
|
|
|
|
|
|
|
tmpev = gst_event_new_segment (&stream->segment);
|
2012-08-14 16:53:52 +00:00
|
|
|
gst_event_set_seqnum (tmpev, stream->segment_seqnum);
|
2012-07-19 14:12:22 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
event = tmpev;
|
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
} else if (stream) {
|
2010-07-16 16:51:35 +00:00
|
|
|
GST_WARNING_OBJECT (pad, "Non-TIME segment: %s",
|
2011-05-16 11:48:11 +00:00
|
|
|
gst_format_get_name (segment.format));
|
2010-07-11 12:44:10 +00:00
|
|
|
gst_segment_init (&stream->segment, GST_FORMAT_UNDEFINED);
|
|
|
|
}
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
}
|
2012-07-27 10:58:40 +00:00
|
|
|
case GST_EVENT_FLUSH_START:{
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2012-07-27 10:58:40 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
2015-01-12 08:08:33 +00:00
|
|
|
self->eos = FALSE;
|
2012-07-27 10:58:40 +00:00
|
|
|
if (stream) {
|
|
|
|
GST_DEBUG_OBJECT (pad, "Flushing streams");
|
2015-01-12 08:08:33 +00:00
|
|
|
stream->flushing = TRUE;
|
2012-08-14 16:53:52 +00:00
|
|
|
g_cond_broadcast (&stream->stream_finish_cond);
|
2012-07-27 10:58:40 +00:00
|
|
|
}
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
case GST_EVENT_FLUSH_STOP:{
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2015-06-22 18:17:56 +00:00
|
|
|
GList *l;
|
|
|
|
GstClockTime new_group_start_time = 0;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
|
|
|
if (stream) {
|
|
|
|
GST_DEBUG_OBJECT (pad, "Resetting segment for stream %d",
|
|
|
|
stream->stream_number);
|
|
|
|
gst_segment_init (&stream->segment, GST_FORMAT_UNDEFINED);
|
2010-07-16 16:25:38 +00:00
|
|
|
|
2010-08-10 09:19:59 +00:00
|
|
|
stream->is_eos = FALSE;
|
2015-01-12 08:08:33 +00:00
|
|
|
stream->eos_sent = FALSE;
|
|
|
|
stream->flushing = FALSE;
|
2010-07-16 16:25:38 +00:00
|
|
|
stream->wait = FALSE;
|
2012-09-04 09:09:50 +00:00
|
|
|
g_cond_broadcast (&stream->stream_finish_cond);
|
2010-07-11 12:44:10 +00:00
|
|
|
}
|
2015-06-22 18:17:56 +00:00
|
|
|
|
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *ostream = l->data;
|
2015-06-22 18:17:56 +00:00
|
|
|
GstClockTime start_running_time;
|
|
|
|
|
2016-01-09 03:35:23 +00:00
|
|
|
if (ostream == stream || ostream->flushing)
|
2015-06-22 18:17:56 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ostream->segment.format == GST_FORMAT_TIME) {
|
2016-09-20 19:12:22 +00:00
|
|
|
if (ostream->segment.rate > 0)
|
|
|
|
start_running_time =
|
|
|
|
gst_segment_to_running_time (&ostream->segment,
|
|
|
|
GST_FORMAT_TIME, ostream->segment.start);
|
|
|
|
else
|
|
|
|
start_running_time =
|
|
|
|
gst_segment_to_running_time (&ostream->segment,
|
|
|
|
GST_FORMAT_TIME, ostream->segment.stop);
|
2015-06-22 18:17:56 +00:00
|
|
|
|
|
|
|
new_group_start_time = MAX (new_group_start_time, start_running_time);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-22 18:29:52 +00:00
|
|
|
GST_DEBUG_OBJECT (pad,
|
|
|
|
"Updating group start time from %" GST_TIME_FORMAT " to %"
|
|
|
|
GST_TIME_FORMAT, GST_TIME_ARGS (self->group_start_time),
|
|
|
|
GST_TIME_ARGS (new_group_start_time));
|
2015-06-22 18:17:56 +00:00
|
|
|
self->group_start_time = new_group_start_time;
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
break;
|
2015-06-17 10:03:09 +00:00
|
|
|
}
|
|
|
|
/* unblocking EOS wait when track switch. */
|
|
|
|
case GST_EVENT_CUSTOM_DOWNSTREAM_OOB:{
|
|
|
|
if (gst_event_has_name (event, "playsink-custom-video-flush")
|
|
|
|
|| gst_event_has_name (event, "playsink-custom-audio-flush")
|
|
|
|
|| gst_event_has_name (event, "playsink-custom-subtitle-flush")) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2015-06-17 10:03:09 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
|
|
|
if (stream) {
|
|
|
|
stream->is_eos = FALSE;
|
|
|
|
stream->eos_sent = FALSE;
|
|
|
|
stream->wait = FALSE;
|
|
|
|
g_cond_broadcast (&stream->stream_finish_cond);
|
|
|
|
}
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
}
|
|
|
|
break;
|
2010-07-11 12:44:10 +00:00
|
|
|
}
|
2010-07-24 16:17:43 +00:00
|
|
|
case GST_EVENT_EOS:{
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2010-07-24 16:17:43 +00:00
|
|
|
GList *l;
|
|
|
|
gboolean all_eos = TRUE;
|
2010-08-12 08:02:56 +00:00
|
|
|
gboolean seen_data;
|
2010-08-10 09:28:43 +00:00
|
|
|
GSList *pads = NULL;
|
2010-08-12 08:02:56 +00:00
|
|
|
GstPad *srcpad;
|
2012-10-24 07:57:23 +00:00
|
|
|
GstClockTime timestamp;
|
2010-07-24 16:17:43 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
2010-10-31 18:08:32 +00:00
|
|
|
if (!stream) {
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
GST_WARNING_OBJECT (pad, "EOS for unknown stream");
|
|
|
|
break;
|
2010-07-24 16:17:43 +00:00
|
|
|
}
|
|
|
|
|
2010-10-31 18:08:32 +00:00
|
|
|
GST_DEBUG_OBJECT (pad, "Have EOS for stream %d", stream->stream_number);
|
|
|
|
stream->is_eos = TRUE;
|
|
|
|
|
2010-08-12 08:02:56 +00:00
|
|
|
seen_data = stream->seen_data;
|
|
|
|
srcpad = gst_object_ref (stream->srcpad);
|
|
|
|
|
2012-10-24 11:34:15 +00:00
|
|
|
if (seen_data && stream->segment.position != -1)
|
|
|
|
timestamp = stream->segment.position;
|
|
|
|
else if (stream->segment.rate < 0.0 || stream->segment.stop == -1)
|
2012-10-24 07:57:23 +00:00
|
|
|
timestamp = stream->segment.start;
|
|
|
|
else
|
|
|
|
timestamp = stream->segment.stop;
|
|
|
|
|
2015-01-12 08:08:33 +00:00
|
|
|
stream->segment.position = timestamp;
|
|
|
|
|
2010-07-24 16:17:43 +00:00
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *ostream = l->data;
|
2010-07-24 16:17:43 +00:00
|
|
|
|
|
|
|
all_eos = all_eos && ostream->is_eos;
|
|
|
|
if (!all_eos)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (all_eos) {
|
|
|
|
GST_DEBUG_OBJECT (self, "All streams are EOS -- forwarding");
|
2015-01-12 08:08:33 +00:00
|
|
|
self->eos = TRUE;
|
2010-07-24 16:17:43 +00:00
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *ostream = l->data;
|
2010-08-10 09:28:43 +00:00
|
|
|
/* local snapshot of current pads */
|
|
|
|
gst_object_ref (ostream->srcpad);
|
|
|
|
pads = g_slist_prepend (pads, ostream->srcpad);
|
2010-07-24 16:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-10 09:28:43 +00:00
|
|
|
if (pads) {
|
|
|
|
GstPad *pad;
|
|
|
|
GSList *epad;
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *ostream;
|
2010-07-24 16:17:43 +00:00
|
|
|
|
2010-08-10 09:28:43 +00:00
|
|
|
ret = TRUE;
|
|
|
|
epad = pads;
|
|
|
|
while (epad) {
|
|
|
|
pad = epad->data;
|
2015-01-12 08:08:33 +00:00
|
|
|
ostream = gst_pad_get_element_private (pad);
|
|
|
|
if (ostream) {
|
|
|
|
g_cond_broadcast (&ostream->stream_finish_cond);
|
|
|
|
}
|
|
|
|
|
2010-08-10 09:28:43 +00:00
|
|
|
gst_object_unref (pad);
|
|
|
|
epad = g_slist_next (epad);
|
|
|
|
}
|
|
|
|
g_slist_free (pads);
|
2010-08-12 08:02:56 +00:00
|
|
|
} else {
|
2015-01-12 08:08:33 +00:00
|
|
|
if (seen_data) {
|
2017-03-23 05:56:19 +00:00
|
|
|
stream->send_gap_event = TRUE;
|
2015-01-12 09:40:25 +00:00
|
|
|
stream->gap_duration = GST_CLOCK_TIME_NONE;
|
2015-02-15 05:51:36 +00:00
|
|
|
stream->wait = TRUE;
|
2015-01-12 08:08:33 +00:00
|
|
|
ret = gst_stream_synchronizer_wait (self, srcpad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 18:08:15 +00:00
|
|
|
/* send eos if haven't seen data. seen_data will be true if data buffer
|
2015-01-12 08:08:33 +00:00
|
|
|
* of the track have received in anytime. sink is ready if seen_data is
|
|
|
|
* true, so can send GAP event. Will send EOS if sink isn't ready. The
|
|
|
|
* scenario for the case is one track haven't any media data and then
|
|
|
|
* send EOS. Or no any valid media data in one track, so decoder can't
|
|
|
|
* get valid CAPS for the track. sink can't ready without received CAPS.*/
|
|
|
|
if (!seen_data || self->eos) {
|
|
|
|
GST_DEBUG_OBJECT (pad, "send EOS event");
|
|
|
|
/* drop lock when sending eos, which may block in e.g. preroll */
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
ret = gst_pad_push_event (srcpad, gst_event_new_eos ());
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
|
|
|
if (stream) {
|
|
|
|
stream->eos_sent = TRUE;
|
2010-08-12 08:02:56 +00:00
|
|
|
}
|
2010-08-10 09:28:43 +00:00
|
|
|
}
|
2015-01-12 08:08:33 +00:00
|
|
|
|
2010-08-12 08:02:56 +00:00
|
|
|
gst_object_unref (srcpad);
|
2012-09-06 13:03:49 +00:00
|
|
|
gst_event_unref (event);
|
2015-01-12 08:08:33 +00:00
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
2010-07-24 16:17:43 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-08-08 10:11:07 +00:00
|
|
|
ret = gst_pad_event_default (pad, parent, event);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2010-07-22 07:13:00 +00:00
|
|
|
done:
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
2011-11-17 11:48:25 +00:00
|
|
|
gst_stream_synchronizer_sink_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buffer)
|
2010-07-11 12:44:10 +00:00
|
|
|
{
|
2011-11-17 11:48:25 +00:00
|
|
|
GstStreamSynchronizer *self = GST_STREAM_SYNCHRONIZER (parent);
|
2010-07-11 12:44:10 +00:00
|
|
|
GstPad *opad;
|
|
|
|
GstFlowReturn ret = GST_FLOW_ERROR;
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2013-03-31 10:55:33 +00:00
|
|
|
GstClockTime duration = GST_CLOCK_TIME_NONE;
|
2010-07-11 12:44:10 +00:00
|
|
|
GstClockTime timestamp = GST_CLOCK_TIME_NONE;
|
|
|
|
GstClockTime timestamp_end = GST_CLOCK_TIME_NONE;
|
|
|
|
|
2011-08-09 14:39:31 +00:00
|
|
|
GST_LOG_OBJECT (pad, "Handling buffer %p: size=%" G_GSIZE_FORMAT
|
|
|
|
", timestamp=%" GST_TIME_FORMAT " duration=%" GST_TIME_FORMAT
|
2010-07-11 12:44:10 +00:00
|
|
|
" offset=%" G_GUINT64_FORMAT " offset_end=%" G_GUINT64_FORMAT,
|
2011-03-27 14:35:28 +00:00
|
|
|
buffer, gst_buffer_get_size (buffer),
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
|
|
|
|
GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)),
|
|
|
|
GST_BUFFER_OFFSET (buffer), GST_BUFFER_OFFSET_END (buffer));
|
|
|
|
|
|
|
|
timestamp = GST_BUFFER_TIMESTAMP (buffer);
|
2013-03-31 10:55:33 +00:00
|
|
|
duration = GST_BUFFER_DURATION (buffer);
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (timestamp)
|
|
|
|
&& GST_CLOCK_TIME_IS_VALID (duration))
|
|
|
|
timestamp_end = timestamp + duration;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
|
|
|
|
2012-08-14 16:53:52 +00:00
|
|
|
if (stream) {
|
2012-01-19 15:40:22 +00:00
|
|
|
stream->seen_data = TRUE;
|
2012-08-14 16:53:52 +00:00
|
|
|
if (stream->segment.format == GST_FORMAT_TIME
|
|
|
|
&& GST_CLOCK_TIME_IS_VALID (timestamp)) {
|
|
|
|
GST_LOG_OBJECT (pad,
|
|
|
|
"Updating position from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (stream->segment.position), GST_TIME_ARGS (timestamp));
|
2013-03-31 10:55:33 +00:00
|
|
|
if (stream->segment.rate > 0.0)
|
|
|
|
stream->segment.position = timestamp;
|
|
|
|
else
|
|
|
|
stream->segment.position = timestamp_end;
|
2012-08-14 16:53:52 +00:00
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
}
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
|
2012-08-14 16:53:52 +00:00
|
|
|
opad = gst_stream_get_other_pad_from_pad (self, pad);
|
2010-07-11 12:44:10 +00:00
|
|
|
if (opad) {
|
|
|
|
ret = gst_pad_push (opad, buffer);
|
|
|
|
gst_object_unref (opad);
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG_OBJECT (pad, "Push returned: %s", gst_flow_get_name (ret));
|
|
|
|
if (ret == GST_FLOW_OK) {
|
2010-07-24 16:17:43 +00:00
|
|
|
GList *l;
|
|
|
|
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
2013-03-31 10:55:33 +00:00
|
|
|
if (stream && stream->segment.format == GST_FORMAT_TIME) {
|
|
|
|
GstClockTime position;
|
|
|
|
|
|
|
|
if (stream->segment.rate > 0.0)
|
|
|
|
position = timestamp_end;
|
|
|
|
else
|
|
|
|
position = timestamp;
|
|
|
|
|
|
|
|
if (GST_CLOCK_TIME_IS_VALID (position)) {
|
|
|
|
GST_LOG_OBJECT (pad,
|
|
|
|
"Updating position from %" GST_TIME_FORMAT " to %" GST_TIME_FORMAT,
|
|
|
|
GST_TIME_ARGS (stream->segment.position), GST_TIME_ARGS (position));
|
|
|
|
stream->segment.position = position;
|
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
}
|
2010-07-24 16:17:43 +00:00
|
|
|
|
|
|
|
/* Advance EOS streams if necessary. For non-EOS
|
|
|
|
* streams the demuxers should already do this! */
|
2012-11-26 18:41:07 +00:00
|
|
|
if (!GST_CLOCK_TIME_IS_VALID (timestamp_end) &&
|
|
|
|
GST_CLOCK_TIME_IS_VALID (timestamp)) {
|
|
|
|
timestamp_end = timestamp + GST_SECOND;
|
|
|
|
}
|
|
|
|
|
2010-07-24 16:17:43 +00:00
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *ostream = l->data;
|
2011-05-16 11:48:11 +00:00
|
|
|
gint64 position;
|
2010-07-24 16:17:43 +00:00
|
|
|
|
2015-01-12 08:08:33 +00:00
|
|
|
if (!ostream->is_eos || ostream->eos_sent ||
|
|
|
|
ostream->segment.format != GST_FORMAT_TIME)
|
2010-07-24 16:17:43 +00:00
|
|
|
continue;
|
|
|
|
|
2011-05-16 11:48:11 +00:00
|
|
|
if (ostream->segment.position != -1)
|
|
|
|
position = ostream->segment.position;
|
2010-07-24 16:17:43 +00:00
|
|
|
else
|
2011-05-16 11:48:11 +00:00
|
|
|
position = ostream->segment.start;
|
2010-07-24 16:17:43 +00:00
|
|
|
|
|
|
|
/* Is there a 1 second lag? */
|
2012-11-26 18:41:07 +00:00
|
|
|
if (position != -1 && GST_CLOCK_TIME_IS_VALID (timestamp_end) &&
|
|
|
|
position + GST_SECOND < timestamp_end) {
|
2012-10-24 11:25:19 +00:00
|
|
|
gint64 new_start;
|
2010-07-24 16:17:43 +00:00
|
|
|
|
|
|
|
new_start = timestamp_end - GST_SECOND;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (ostream->sinkpad,
|
2010-07-26 18:25:55 +00:00
|
|
|
"Advancing stream %u from %" GST_TIME_FORMAT " to %"
|
2011-05-16 11:48:11 +00:00
|
|
|
GST_TIME_FORMAT, ostream->stream_number, GST_TIME_ARGS (position),
|
2010-07-26 18:25:55 +00:00
|
|
|
GST_TIME_ARGS (new_start));
|
2010-07-24 16:17:43 +00:00
|
|
|
|
2011-05-16 11:48:11 +00:00
|
|
|
ostream->segment.position = new_start;
|
|
|
|
|
2017-03-23 05:56:19 +00:00
|
|
|
ostream->send_gap_event = TRUE;
|
2015-01-12 09:40:25 +00:00
|
|
|
ostream->gap_duration = new_start - position;
|
|
|
|
g_cond_broadcast (&ostream->stream_finish_cond);
|
2010-07-24 16:17:43 +00:00
|
|
|
}
|
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GstElement vfuncs */
|
|
|
|
static GstPad *
|
|
|
|
gst_stream_synchronizer_request_new_pad (GstElement * element,
|
2011-05-10 14:44:37 +00:00
|
|
|
GstPadTemplate * temp, const gchar * name, const GstCaps * caps)
|
2010-07-11 12:44:10 +00:00
|
|
|
{
|
|
|
|
GstStreamSynchronizer *self = GST_STREAM_SYNCHRONIZER (element);
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2010-07-11 12:44:10 +00:00
|
|
|
gchar *tmp;
|
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
GST_DEBUG_OBJECT (self, "Requesting new pad for stream %d",
|
|
|
|
self->current_stream_number);
|
|
|
|
|
2015-07-21 07:58:56 +00:00
|
|
|
stream = g_slice_new0 (GstSyncStream);
|
2010-07-11 12:44:10 +00:00
|
|
|
stream->transform = self;
|
|
|
|
stream->stream_number = self->current_stream_number;
|
2012-08-14 16:53:52 +00:00
|
|
|
g_cond_init (&stream->stream_finish_cond);
|
|
|
|
stream->stream_start_seqnum = G_MAXUINT32;
|
|
|
|
stream->segment_seqnum = G_MAXUINT32;
|
2013-07-22 11:15:09 +00:00
|
|
|
stream->group_id = G_MAXUINT;
|
2015-01-12 08:08:33 +00:00
|
|
|
stream->seen_data = FALSE;
|
2017-03-23 05:56:19 +00:00
|
|
|
stream->send_gap_event = FALSE;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2011-11-04 09:48:50 +00:00
|
|
|
tmp = g_strdup_printf ("sink_%u", self->current_stream_number);
|
2010-07-11 12:44:10 +00:00
|
|
|
stream->sinkpad = gst_pad_new_from_static_template (&sinktemplate, tmp);
|
|
|
|
g_free (tmp);
|
|
|
|
gst_pad_set_element_private (stream->sinkpad, stream);
|
|
|
|
gst_pad_set_iterate_internal_links_function (stream->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_synchronizer_iterate_internal_links));
|
|
|
|
gst_pad_set_event_function (stream->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_synchronizer_sink_event));
|
|
|
|
gst_pad_set_chain_function (stream->sinkpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_synchronizer_sink_chain));
|
2013-08-08 10:11:07 +00:00
|
|
|
GST_PAD_SET_PROXY_CAPS (stream->sinkpad);
|
|
|
|
GST_PAD_SET_PROXY_ALLOCATION (stream->sinkpad);
|
|
|
|
GST_PAD_SET_PROXY_SCHEDULING (stream->sinkpad);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2011-11-04 09:48:50 +00:00
|
|
|
tmp = g_strdup_printf ("src_%u", self->current_stream_number);
|
2010-07-11 12:44:10 +00:00
|
|
|
stream->srcpad = gst_pad_new_from_static_template (&srctemplate, tmp);
|
|
|
|
g_free (tmp);
|
|
|
|
gst_pad_set_element_private (stream->srcpad, stream);
|
|
|
|
gst_pad_set_iterate_internal_links_function (stream->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_synchronizer_iterate_internal_links));
|
|
|
|
gst_pad_set_event_function (stream->srcpad,
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_synchronizer_src_event));
|
2013-08-08 10:11:07 +00:00
|
|
|
GST_PAD_SET_PROXY_CAPS (stream->srcpad);
|
|
|
|
GST_PAD_SET_PROXY_ALLOCATION (stream->srcpad);
|
|
|
|
GST_PAD_SET_PROXY_SCHEDULING (stream->srcpad);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
gst_segment_init (&stream->segment, GST_FORMAT_UNDEFINED);
|
|
|
|
|
|
|
|
self->streams = g_list_prepend (self->streams, stream);
|
|
|
|
self->current_stream_number++;
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
|
|
|
|
/* Add pads and activate unless we're going to NULL */
|
2012-01-19 08:48:38 +00:00
|
|
|
g_rec_mutex_lock (GST_STATE_GET_LOCK (self));
|
2010-07-11 12:44:10 +00:00
|
|
|
if (GST_STATE_TARGET (self) != GST_STATE_NULL) {
|
|
|
|
gst_pad_set_active (stream->srcpad, TRUE);
|
|
|
|
gst_pad_set_active (stream->sinkpad, TRUE);
|
|
|
|
}
|
|
|
|
gst_element_add_pad (GST_ELEMENT_CAST (self), stream->srcpad);
|
|
|
|
gst_element_add_pad (GST_ELEMENT_CAST (self), stream->sinkpad);
|
2012-01-19 08:48:38 +00:00
|
|
|
g_rec_mutex_unlock (GST_STATE_GET_LOCK (self));
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
return stream->sinkpad;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Must be called with lock! */
|
|
|
|
static void
|
|
|
|
gst_stream_synchronizer_release_stream (GstStreamSynchronizer * self,
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream * stream)
|
2010-07-11 12:44:10 +00:00
|
|
|
{
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "Releasing stream %d", stream->stream_number);
|
|
|
|
|
|
|
|
for (l = self->streams; l; l = l->next) {
|
|
|
|
if (l->data == stream) {
|
|
|
|
self->streams = g_list_delete_link (self->streams, l);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_assert (l != NULL);
|
2013-07-22 11:15:09 +00:00
|
|
|
if (self->streams == NULL) {
|
|
|
|
self->have_group_id = TRUE;
|
|
|
|
self->group_id = G_MAXUINT;
|
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
|
2010-08-11 10:49:40 +00:00
|
|
|
/* we can drop the lock, since stream exists now only local.
|
|
|
|
* Moreover, we should drop, to prevent deadlock with STREAM_LOCK
|
|
|
|
* (due to reverse lock order) when deactivating pads */
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
|
2010-07-11 12:44:10 +00:00
|
|
|
gst_pad_set_element_private (stream->srcpad, NULL);
|
|
|
|
gst_pad_set_element_private (stream->sinkpad, NULL);
|
|
|
|
gst_pad_set_active (stream->srcpad, FALSE);
|
|
|
|
gst_element_remove_pad (GST_ELEMENT_CAST (self), stream->srcpad);
|
2010-07-16 16:51:35 +00:00
|
|
|
gst_pad_set_active (stream->sinkpad, FALSE);
|
2010-07-11 12:44:10 +00:00
|
|
|
gst_element_remove_pad (GST_ELEMENT_CAST (self), stream->sinkpad);
|
|
|
|
|
2012-08-14 16:53:52 +00:00
|
|
|
g_cond_clear (&stream->stream_finish_cond);
|
2015-07-21 07:58:56 +00:00
|
|
|
g_slice_free (GstSyncStream, stream);
|
2010-07-24 16:17:43 +00:00
|
|
|
|
|
|
|
/* NOTE: In theory we have to check here if all streams
|
|
|
|
* are EOS but the one that was removed wasn't and then
|
|
|
|
* send EOS downstream. But due to the way how playsink
|
|
|
|
* works this is not necessary and will only cause problems
|
|
|
|
* for gapless playback. playsink will only add/remove pads
|
|
|
|
* when it's reconfigured, which happens when the streams
|
|
|
|
* change
|
|
|
|
*/
|
2010-08-11 10:49:40 +00:00
|
|
|
|
|
|
|
/* lock for good measure, since the caller had it */
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
2010-07-11 12:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_stream_synchronizer_release_pad (GstElement * element, GstPad * pad)
|
|
|
|
{
|
|
|
|
GstStreamSynchronizer *self = GST_STREAM_SYNCHRONIZER (element);
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
stream = gst_pad_get_element_private (pad);
|
|
|
|
if (stream) {
|
|
|
|
g_assert (stream->sinkpad == pad);
|
|
|
|
|
|
|
|
gst_stream_synchronizer_release_stream (self, stream);
|
|
|
|
}
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_stream_synchronizer_change_state (GstElement * element,
|
|
|
|
GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstStreamSynchronizer *self = GST_STREAM_SYNCHRONIZER (element);
|
2012-08-14 16:53:52 +00:00
|
|
|
GstStateChangeReturn ret;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
|
|
|
GST_DEBUG_OBJECT (self, "State change NULL->READY");
|
2010-07-22 07:13:00 +00:00
|
|
|
self->shutdown = FALSE;
|
2010-07-11 12:44:10 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
GST_DEBUG_OBJECT (self, "State change READY->PAUSED");
|
2010-07-16 16:51:35 +00:00
|
|
|
self->group_start_time = 0;
|
2013-07-22 11:15:09 +00:00
|
|
|
self->have_group_id = TRUE;
|
|
|
|
self->group_id = G_MAXUINT;
|
2010-07-22 07:13:00 +00:00
|
|
|
self->shutdown = FALSE;
|
2015-01-12 08:08:33 +00:00
|
|
|
self->flushing = FALSE;
|
|
|
|
self->eos = FALSE;
|
2010-07-11 12:44:10 +00:00
|
|
|
break;
|
2012-07-27 10:58:40 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:{
|
|
|
|
GList *l;
|
|
|
|
|
2015-01-12 08:08:33 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "State change PAUSED->READY");
|
2010-07-22 07:13:00 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
2015-01-12 08:08:33 +00:00
|
|
|
self->flushing = TRUE;
|
|
|
|
self->shutdown = TRUE;
|
2012-07-27 10:58:40 +00:00
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *ostream = l->data;
|
2012-08-14 16:53:52 +00:00
|
|
|
g_cond_broadcast (&ostream->stream_finish_cond);
|
2012-07-27 10:58:40 +00:00
|
|
|
}
|
2010-07-22 07:13:00 +00:00
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
2012-07-27 10:58:40 +00:00
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-08-14 16:53:52 +00:00
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
GST_DEBUG_OBJECT (self, "Base class state changed returned: %d", ret);
|
|
|
|
if (G_UNLIKELY (ret != GST_STATE_CHANGE_SUCCESS))
|
|
|
|
return ret;
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
switch (transition) {
|
2015-01-12 08:08:33 +00:00
|
|
|
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:{
|
|
|
|
GList *l;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (self, "State change PLAYING->PAUSED");
|
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream = l->data;
|
2015-01-12 08:08:33 +00:00
|
|
|
/* send GAP event to sink to finished pre-roll. The reason is function
|
|
|
|
* chain () will be blocked on pad_push (), so can't trigger the track
|
|
|
|
* which reach EOS to send GAP event. */
|
|
|
|
if (stream->is_eos && !stream->eos_sent) {
|
2017-03-23 05:56:19 +00:00
|
|
|
stream->send_gap_event = TRUE;
|
2015-01-12 09:40:25 +00:00
|
|
|
stream->gap_duration = GST_CLOCK_TIME_NONE;
|
2015-01-12 08:08:33 +00:00
|
|
|
g_cond_broadcast (&stream->stream_finish_cond);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
|
|
|
break;
|
|
|
|
}
|
2010-07-16 16:25:38 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:{
|
|
|
|
GList *l;
|
|
|
|
|
2010-07-11 12:44:10 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "State change PAUSED->READY");
|
|
|
|
self->group_start_time = 0;
|
2010-07-16 16:25:38 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
for (l = self->streams; l; l = l->next) {
|
2015-07-21 07:58:56 +00:00
|
|
|
GstSyncStream *stream = l->data;
|
2010-07-16 16:25:38 +00:00
|
|
|
|
|
|
|
gst_segment_init (&stream->segment, GST_FORMAT_UNDEFINED);
|
2015-01-12 09:40:25 +00:00
|
|
|
stream->gap_duration = GST_CLOCK_TIME_NONE;
|
2010-07-16 16:25:38 +00:00
|
|
|
stream->wait = FALSE;
|
2010-07-24 16:17:43 +00:00
|
|
|
stream->is_eos = FALSE;
|
2015-01-12 08:08:33 +00:00
|
|
|
stream->eos_sent = FALSE;
|
|
|
|
stream->flushing = FALSE;
|
2017-03-23 05:56:19 +00:00
|
|
|
stream->send_gap_event = FALSE;
|
2010-07-16 16:25:38 +00:00
|
|
|
}
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
2010-07-11 12:44:10 +00:00
|
|
|
break;
|
2010-07-16 16:25:38 +00:00
|
|
|
}
|
2010-07-11 12:44:10 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:{
|
|
|
|
GST_DEBUG_OBJECT (self, "State change READY->NULL");
|
2010-07-16 16:51:35 +00:00
|
|
|
|
|
|
|
GST_STREAM_SYNCHRONIZER_LOCK (self);
|
|
|
|
self->current_stream_number = 0;
|
|
|
|
GST_STREAM_SYNCHRONIZER_UNLOCK (self);
|
2010-07-11 12:44:10 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GObject vfuncs */
|
|
|
|
static void
|
|
|
|
gst_stream_synchronizer_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
GstStreamSynchronizer *self = GST_STREAM_SYNCHRONIZER (object);
|
|
|
|
|
2012-08-14 16:53:52 +00:00
|
|
|
g_mutex_clear (&self->lock);
|
2010-07-11 12:44:10 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* GObject type initialization */
|
|
|
|
static void
|
2011-04-19 09:35:53 +00:00
|
|
|
gst_stream_synchronizer_init (GstStreamSynchronizer * self)
|
2010-07-11 12:44:10 +00:00
|
|
|
{
|
2012-08-14 16:53:52 +00:00
|
|
|
g_mutex_init (&self->lock);
|
2010-07-11 12:44:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_stream_synchronizer_class_init (GstStreamSynchronizerClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class = (GObjectClass *) klass;
|
|
|
|
GstElementClass *element_class = (GstElementClass *) klass;
|
|
|
|
|
|
|
|
gobject_class->finalize = gst_stream_synchronizer_finalize;
|
|
|
|
|
2016-03-03 07:46:24 +00:00
|
|
|
gst_element_class_add_static_pad_template (element_class, &srctemplate);
|
|
|
|
gst_element_class_add_static_pad_template (element_class, &sinktemplate);
|
2011-04-19 09:35:53 +00:00
|
|
|
|
2012-04-09 23:45:16 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class,
|
2011-04-19 09:35:53 +00:00
|
|
|
"Stream Synchronizer", "Generic",
|
|
|
|
"Synchronizes a group of streams to have equal durations and starting points",
|
|
|
|
"Sebastian Dröge <sebastian.droege@collabora.co.uk>");
|
|
|
|
|
2010-07-11 12:44:10 +00:00
|
|
|
element_class->change_state =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_synchronizer_change_state);
|
|
|
|
element_class->request_new_pad =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_synchronizer_request_new_pad);
|
|
|
|
element_class->release_pad =
|
|
|
|
GST_DEBUG_FUNCPTR (gst_stream_synchronizer_release_pad);
|
|
|
|
}
|
2012-11-21 09:28:31 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_stream_synchronizer_plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
GST_DEBUG_CATEGORY_INIT (stream_synchronizer_debug,
|
|
|
|
"streamsynchronizer", 0, "Stream Synchronizer");
|
|
|
|
|
|
|
|
return gst_element_register (plugin, "streamsynchronizer", GST_RANK_NONE,
|
|
|
|
GST_TYPE_STREAM_SYNCHRONIZER);
|
|
|
|
}
|