2011-02-16 16:57:42 +00:00
|
|
|
/*
|
2011-09-08 22:13:19 +00:00
|
|
|
* mpegtsbase.c -
|
2011-02-16 16:57:42 +00:00
|
|
|
* Copyright (C) 2007 Alessandro Decina
|
|
|
|
* 2010 Edward Hervey
|
2011-09-08 22:13:19 +00:00
|
|
|
* Copyright (C) 2011, Hewlett-Packard Development Company, L.P.
|
|
|
|
* Author: Youness Alaoui <youness.alaoui@collabora.co.uk>, Collabora Ltd.
|
|
|
|
* Author: Sebastian Dröge <sebastian.droege@collabora.co.uk>, Collabora Ltd.
|
|
|
|
* Author: Edward Hervey <bilboed@bilboed.com>, Collabora Ltd.
|
2011-02-16 16:57:42 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alessandro Decina <alessandro@nnva.org>
|
|
|
|
* Zaheer Abbas Merali <zaheerabbas at merali dot org>
|
|
|
|
* Edward Hervey <edward.hervey@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 20:38:00 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2011-02-16 16:57:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-03-22 15:49:13 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
#include <gst/gst-i18n-plugin.h>
|
|
|
|
#include "mpegtsbase.h"
|
|
|
|
#include "gstmpegdesc.h"
|
|
|
|
|
|
|
|
/* latency in mseconds */
|
|
|
|
#define TS_LATENCY 700
|
|
|
|
|
|
|
|
#define RUNNING_STATUS_RUNNING 4
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (mpegts_base_debug);
|
|
|
|
#define GST_CAT_DEFAULT mpegts_base_debug
|
|
|
|
|
|
|
|
static GQuark QUARK_PROGRAMS;
|
|
|
|
static GQuark QUARK_PROGRAM_NUMBER;
|
|
|
|
static GQuark QUARK_PID;
|
|
|
|
static GQuark QUARK_PCR_PID;
|
|
|
|
static GQuark QUARK_STREAMS;
|
|
|
|
static GQuark QUARK_STREAM_TYPE;
|
|
|
|
|
|
|
|
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
|
|
|
GST_STATIC_CAPS ("video/mpegts, " "systemstream = (boolean) true ")
|
|
|
|
);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2013-07-06 12:50:52 +00:00
|
|
|
PROP_0,
|
|
|
|
PROP_PARSE_PRIVATE_SECTIONS,
|
2011-02-16 16:57:42 +00:00
|
|
|
/* FILL ME */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void mpegts_base_dispose (GObject * object);
|
|
|
|
static void mpegts_base_finalize (GObject * object);
|
2013-07-06 12:50:52 +00:00
|
|
|
static void mpegts_base_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec);
|
|
|
|
static void mpegts_base_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec);
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static void mpegts_base_free_program (MpegTSBaseProgram * program);
|
2012-04-19 12:20:52 +00:00
|
|
|
static gboolean mpegts_base_sink_activate (GstPad * pad, GstObject * parent);
|
|
|
|
static gboolean mpegts_base_sink_activate_mode (GstPad * pad,
|
|
|
|
GstObject * parent, GstPadMode mode, gboolean active);
|
|
|
|
static GstFlowReturn mpegts_base_chain (GstPad * pad, GstObject * parent,
|
|
|
|
GstBuffer * buf);
|
|
|
|
static gboolean mpegts_base_sink_event (GstPad * pad, GstObject * parent,
|
|
|
|
GstEvent * event);
|
2011-02-16 16:57:42 +00:00
|
|
|
static GstStateChangeReturn mpegts_base_change_state (GstElement * element,
|
|
|
|
GstStateChange transition);
|
2013-06-23 06:43:23 +00:00
|
|
|
static gboolean mpegts_base_get_tags_from_eit (MpegTSBase * base,
|
2013-07-03 11:57:38 +00:00
|
|
|
GstMpegTsSection * section);
|
2013-06-23 06:43:23 +00:00
|
|
|
static gboolean remove_each_program (gpointer key, MpegTSBaseProgram * program,
|
2012-03-30 17:19:12 +00:00
|
|
|
MpegTSBase * base);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-04-19 12:20:52 +00:00
|
|
|
static void
|
|
|
|
_extra_init (void)
|
|
|
|
{
|
|
|
|
QUARK_PROGRAMS = g_quark_from_string ("programs");
|
|
|
|
QUARK_PROGRAM_NUMBER = g_quark_from_string ("program-number");
|
|
|
|
QUARK_PID = g_quark_from_string ("pid");
|
|
|
|
QUARK_PCR_PID = g_quark_from_string ("pcr-pid");
|
|
|
|
QUARK_STREAMS = g_quark_from_string ("streams");
|
|
|
|
QUARK_STREAM_TYPE = g_quark_from_string ("stream-type");
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-04-19 12:20:52 +00:00
|
|
|
#define mpegts_base_parent_class parent_class
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (MpegTSBase, mpegts_base, GST_TYPE_ELEMENT,
|
|
|
|
_extra_init ());
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
mpegts_base_class_init (MpegTSBaseClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *element_class;
|
|
|
|
|
|
|
|
element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
element_class->change_state = mpegts_base_change_state;
|
|
|
|
|
2012-04-19 12:20:52 +00:00
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&sink_template));
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
gobject_class = G_OBJECT_CLASS (klass);
|
|
|
|
gobject_class->dispose = mpegts_base_dispose;
|
|
|
|
gobject_class->finalize = mpegts_base_finalize;
|
2013-07-06 12:50:52 +00:00
|
|
|
gobject_class->set_property = mpegts_base_set_property;
|
|
|
|
gobject_class->get_property = mpegts_base_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, PROP_PARSE_PRIVATE_SECTIONS,
|
|
|
|
g_param_spec_boolean ("parse-private-sections", "Parse private sections",
|
|
|
|
"Parse private sections", FALSE,
|
|
|
|
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpegts_base_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
MpegTSBase *base = GST_MPEGTS_BASE (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_PARSE_PRIVATE_SECTIONS:
|
|
|
|
base->parse_private_sections = g_value_get_boolean (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2013-07-06 12:50:52 +00:00
|
|
|
static void
|
|
|
|
mpegts_base_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
MpegTSBase *base = GST_MPEGTS_BASE (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_PARSE_PRIVATE_SECTIONS:
|
|
|
|
g_value_set_boolean (value, base->parse_private_sections);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static void
|
|
|
|
mpegts_base_reset (MpegTSBase * base)
|
|
|
|
{
|
2011-02-22 11:33:56 +00:00
|
|
|
MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
mpegts_packetizer_clear (base->packetizer);
|
2011-07-19 06:45:51 +00:00
|
|
|
memset (base->is_pes, 0, 1024);
|
|
|
|
memset (base->known_psi, 0, 1024);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
/* FIXME : Actually these are not *always* know SI streams
|
|
|
|
* depending on the variant of mpeg-ts being used. */
|
|
|
|
|
2012-06-28 16:04:10 +00:00
|
|
|
/* Known PIDs : PAT, TSDT, IPMP CIT */
|
2011-07-19 06:45:51 +00:00
|
|
|
MPEGTS_BIT_SET (base->known_psi, 0);
|
2012-06-02 06:05:44 +00:00
|
|
|
MPEGTS_BIT_SET (base->known_psi, 2);
|
|
|
|
MPEGTS_BIT_SET (base->known_psi, 3);
|
|
|
|
/* TDT, TOT, ST */
|
|
|
|
MPEGTS_BIT_SET (base->known_psi, 0x14);
|
|
|
|
/* network synchronization */
|
|
|
|
MPEGTS_BIT_SET (base->known_psi, 0x15);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
/* ATSC */
|
|
|
|
MPEGTS_BIT_SET (base->known_psi, 0x1ffb);
|
|
|
|
|
2014-02-21 13:06:24 +00:00
|
|
|
if (base->pat) {
|
|
|
|
g_ptr_array_unref (base->pat);
|
|
|
|
base->pat = NULL;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-15 10:08:40 +00:00
|
|
|
gst_segment_init (&base->segment, GST_FORMAT_UNDEFINED);
|
2013-07-15 09:15:11 +00:00
|
|
|
base->last_seek_seqnum = (guint32) - 1;
|
2011-07-15 10:08:40 +00:00
|
|
|
|
2011-08-01 13:46:12 +00:00
|
|
|
base->mode = BASE_MODE_STREAMING;
|
|
|
|
base->seen_pat = FALSE;
|
2012-03-01 17:05:17 +00:00
|
|
|
base->seek_offset = -1;
|
2011-08-01 13:46:12 +00:00
|
|
|
|
2011-11-08 08:44:18 +00:00
|
|
|
base->upstream_live = FALSE;
|
2012-03-03 15:47:01 +00:00
|
|
|
base->queried_latency = FALSE;
|
2011-11-08 08:44:18 +00:00
|
|
|
|
2012-03-30 17:19:12 +00:00
|
|
|
g_hash_table_foreach_remove (base->programs, (GHRFunc) remove_each_program,
|
|
|
|
base);
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
if (klass->reset)
|
|
|
|
klass->reset (base);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-04-19 12:20:52 +00:00
|
|
|
mpegts_base_init (MpegTSBase * base)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
|
|
|
base->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
|
|
|
|
gst_pad_set_activate_function (base->sinkpad, mpegts_base_sink_activate);
|
2012-04-19 12:20:52 +00:00
|
|
|
gst_pad_set_activatemode_function (base->sinkpad,
|
|
|
|
mpegts_base_sink_activate_mode);
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_pad_set_chain_function (base->sinkpad, mpegts_base_chain);
|
|
|
|
gst_pad_set_event_function (base->sinkpad, mpegts_base_sink_event);
|
|
|
|
gst_element_add_pad (GST_ELEMENT (base), base->sinkpad);
|
|
|
|
|
|
|
|
base->disposed = FALSE;
|
|
|
|
base->packetizer = mpegts_packetizer_new ();
|
|
|
|
base->programs = g_hash_table_new_full (g_direct_hash, g_direct_equal,
|
|
|
|
NULL, (GDestroyNotify) mpegts_base_free_program);
|
|
|
|
|
2013-07-06 12:50:52 +00:00
|
|
|
base->parse_private_sections = FALSE;
|
2011-07-19 06:45:51 +00:00
|
|
|
base->is_pes = g_new0 (guint8, 1024);
|
|
|
|
base->known_psi = g_new0 (guint8, 1024);
|
2011-02-16 16:57:42 +00:00
|
|
|
base->program_size = sizeof (MpegTSBaseProgram);
|
|
|
|
base->stream_size = sizeof (MpegTSBaseStream);
|
|
|
|
|
2013-07-07 09:42:50 +00:00
|
|
|
base->push_data = TRUE;
|
|
|
|
base->push_section = TRUE;
|
|
|
|
|
2011-08-01 13:46:12 +00:00
|
|
|
mpegts_base_reset (base);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpegts_base_dispose (GObject * object)
|
|
|
|
{
|
|
|
|
MpegTSBase *base = GST_MPEGTS_BASE (object);
|
|
|
|
|
|
|
|
if (!base->disposed) {
|
|
|
|
g_object_unref (base->packetizer);
|
|
|
|
base->disposed = TRUE;
|
|
|
|
g_free (base->known_psi);
|
|
|
|
g_free (base->is_pes);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (G_OBJECT_CLASS (parent_class)->dispose)
|
|
|
|
G_OBJECT_CLASS (parent_class)->dispose (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpegts_base_finalize (GObject * object)
|
|
|
|
{
|
|
|
|
MpegTSBase *base = GST_MPEGTS_BASE (object);
|
|
|
|
|
|
|
|
if (base->pat) {
|
2013-08-21 06:58:23 +00:00
|
|
|
g_ptr_array_unref (base->pat);
|
2011-02-16 16:57:42 +00:00
|
|
|
base->pat = NULL;
|
|
|
|
}
|
|
|
|
g_hash_table_destroy (base->programs);
|
|
|
|
|
|
|
|
if (G_OBJECT_CLASS (parent_class)->finalize)
|
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* returns NULL if no matching descriptor found *
|
|
|
|
* otherwise returns a descriptor that needs to *
|
|
|
|
* be freed */
|
2013-10-05 12:45:32 +00:00
|
|
|
const GstMpegTsDescriptor *
|
2011-02-16 16:57:42 +00:00
|
|
|
mpegts_get_descriptor_from_stream (MpegTSBaseStream * stream, guint8 tag)
|
|
|
|
{
|
2013-07-03 11:57:38 +00:00
|
|
|
GstMpegTsPMTStream *pmt = stream->stream;
|
2012-05-26 12:11:24 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
GST_DEBUG ("Searching for tag 0x%02x in stream 0x%04x (stream_type 0x%02x)",
|
|
|
|
tag, stream->pid, stream->stream_type);
|
2012-05-26 12:11:24 +00:00
|
|
|
|
2013-10-05 12:45:32 +00:00
|
|
|
return gst_mpegts_find_descriptor (pmt->descriptors, tag);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
gboolean res;
|
|
|
|
guint16 pid;
|
|
|
|
} PIDLookup;
|
|
|
|
|
|
|
|
static void
|
|
|
|
foreach_pid_in_program (gpointer key, MpegTSBaseProgram * program,
|
|
|
|
PIDLookup * lookup)
|
|
|
|
{
|
|
|
|
if (!program->active)
|
|
|
|
return;
|
|
|
|
if (program->streams[lookup->pid])
|
|
|
|
lookup->res = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
mpegts_pid_in_active_programs (MpegTSBase * base, guint16 pid)
|
|
|
|
{
|
|
|
|
PIDLookup lookup;
|
|
|
|
|
|
|
|
lookup.res = FALSE;
|
|
|
|
lookup.pid = pid;
|
|
|
|
g_hash_table_foreach (base->programs, (GHFunc) foreach_pid_in_program,
|
|
|
|
&lookup);
|
|
|
|
|
|
|
|
return lookup.res;
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
/* returns NULL if no matching descriptor found *
|
|
|
|
* otherwise returns a descriptor that needs to *
|
|
|
|
* be freed */
|
2013-10-05 12:45:32 +00:00
|
|
|
const GstMpegTsDescriptor *
|
2011-02-16 16:57:42 +00:00
|
|
|
mpegts_get_descriptor_from_program (MpegTSBaseProgram * program, guint8 tag)
|
|
|
|
{
|
2013-07-03 11:57:38 +00:00
|
|
|
const GstMpegTsPMT *pmt = program->pmt;
|
2012-05-26 12:11:24 +00:00
|
|
|
|
2013-10-05 12:45:32 +00:00
|
|
|
return gst_mpegts_find_descriptor (pmt->descriptors, tag);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
static MpegTSBaseProgram *
|
|
|
|
mpegts_base_new_program (MpegTSBase * base,
|
2011-02-16 16:57:42 +00:00
|
|
|
gint program_number, guint16 pmt_pid)
|
|
|
|
{
|
|
|
|
MpegTSBaseProgram *program;
|
|
|
|
|
2011-07-19 07:36:53 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "program_number : %d, pmt_pid : %d",
|
|
|
|
program_number, pmt_pid);
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
program = g_malloc0 (base->program_size);
|
|
|
|
program->program_number = program_number;
|
|
|
|
program->pmt_pid = pmt_pid;
|
|
|
|
program->pcr_pid = G_MAXUINT16;
|
|
|
|
program->streams = g_new0 (MpegTSBaseStream *, 0x2000);
|
|
|
|
program->patcount = 0;
|
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
MpegTSBaseProgram *
|
|
|
|
mpegts_base_add_program (MpegTSBase * base,
|
|
|
|
gint program_number, guint16 pmt_pid)
|
|
|
|
{
|
|
|
|
MpegTSBaseProgram *program;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (base, "program_number : %d, pmt_pid : %d",
|
|
|
|
program_number, pmt_pid);
|
|
|
|
|
|
|
|
program = mpegts_base_new_program (base, program_number, pmt_pid);
|
|
|
|
|
|
|
|
/* Mark the PMT PID as being a known PSI PID */
|
2013-06-23 06:43:23 +00:00
|
|
|
if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, pmt_pid))) {
|
|
|
|
GST_FIXME ("Refcounting. Setting twice a PID (0x%04x) as known PSI",
|
|
|
|
pmt_pid);
|
|
|
|
}
|
2011-07-21 11:26:55 +00:00
|
|
|
MPEGTS_BIT_SET (base->known_psi, pmt_pid);
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
g_hash_table_insert (base->programs,
|
|
|
|
GINT_TO_POINTER (program_number), program);
|
|
|
|
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
MpegTSBaseProgram *
|
|
|
|
mpegts_base_get_program (MpegTSBase * base, gint program_number)
|
|
|
|
{
|
|
|
|
MpegTSBaseProgram *program;
|
|
|
|
|
|
|
|
program = (MpegTSBaseProgram *) g_hash_table_lookup (base->programs,
|
|
|
|
GINT_TO_POINTER ((gint) program_number));
|
|
|
|
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
static MpegTSBaseProgram *
|
|
|
|
mpegts_base_steal_program (MpegTSBase * base, gint program_number)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2011-07-21 11:26:55 +00:00
|
|
|
MpegTSBaseProgram *program;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
program = (MpegTSBaseProgram *) g_hash_table_lookup (base->programs,
|
|
|
|
GINT_TO_POINTER ((gint) program_number));
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
if (program)
|
|
|
|
g_hash_table_steal (base->programs,
|
|
|
|
GINT_TO_POINTER ((gint) program_number));
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
return program;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpegts_base_free_program (MpegTSBaseProgram * program)
|
|
|
|
{
|
2011-07-19 07:34:37 +00:00
|
|
|
GList *tmp;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
if (program->pmt) {
|
|
|
|
gst_mpegts_section_unref (program->section);
|
|
|
|
program->pmt = NULL;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-19 07:34:37 +00:00
|
|
|
for (tmp = program->stream_list; tmp; tmp = tmp->next)
|
2013-06-23 06:43:23 +00:00
|
|
|
g_free (tmp->data);
|
2011-07-19 07:34:37 +00:00
|
|
|
if (program->stream_list)
|
|
|
|
g_list_free (program->stream_list);
|
|
|
|
|
2011-04-29 10:08:38 +00:00
|
|
|
g_free (program->streams);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
if (program->tags)
|
2012-08-04 15:31:30 +00:00
|
|
|
gst_tag_list_unref (program->tags);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
g_free (program);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mpegts_base_remove_program (MpegTSBase * base, gint program_number)
|
|
|
|
{
|
2011-07-19 07:36:53 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "program_number : %d", program_number);
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
g_hash_table_remove (base->programs, GINT_TO_POINTER (program_number));
|
|
|
|
}
|
|
|
|
|
2013-07-06 08:36:33 +00:00
|
|
|
static guint32
|
2013-08-21 06:58:23 +00:00
|
|
|
get_registration_from_descriptors (GPtrArray * descriptors)
|
2013-07-06 08:36:33 +00:00
|
|
|
{
|
|
|
|
const GstMpegTsDescriptor *desc;
|
|
|
|
|
|
|
|
if ((desc =
|
|
|
|
gst_mpegts_find_descriptor (descriptors,
|
|
|
|
GST_MTS_DESC_REGISTRATION))) {
|
2013-08-21 06:58:23 +00:00
|
|
|
if (G_UNLIKELY (desc->length < 4)) {
|
2013-07-06 08:36:33 +00:00
|
|
|
GST_WARNING ("Registration descriptor with length < 4. (Corrupted ?)");
|
|
|
|
} else
|
2013-08-21 06:58:23 +00:00
|
|
|
return GST_READ_UINT32_BE (desc->data + 2);
|
2013-07-06 08:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static MpegTSBaseStream *
|
|
|
|
mpegts_base_program_add_stream (MpegTSBase * base,
|
|
|
|
MpegTSBaseProgram * program, guint16 pid, guint8 stream_type,
|
2013-07-03 11:57:38 +00:00
|
|
|
GstMpegTsPMTStream * stream)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
|
|
|
MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
|
2013-06-23 06:43:23 +00:00
|
|
|
MpegTSBaseStream *bstream;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
GST_DEBUG ("pid:0x%04x, stream_type:0x%03x", pid, stream_type);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
if (G_UNLIKELY (program->streams[pid])) {
|
2013-06-24 06:24:58 +00:00
|
|
|
if (stream_type != 0xff)
|
|
|
|
GST_WARNING ("Stream already present !");
|
2011-07-21 11:26:55 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
bstream = g_malloc0 (base->stream_size);
|
|
|
|
bstream->pid = pid;
|
|
|
|
bstream->stream_type = stream_type;
|
|
|
|
bstream->stream = stream;
|
2013-07-06 08:36:33 +00:00
|
|
|
if (stream) {
|
|
|
|
bstream->registration_id =
|
|
|
|
get_registration_from_descriptors (stream->descriptors);
|
|
|
|
GST_DEBUG ("PID 0x%04x, registration_id %" SAFE_FOURCC_FORMAT,
|
|
|
|
bstream->pid, SAFE_FOURCC_ARGS (bstream->registration_id));
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
program->streams[pid] = bstream;
|
|
|
|
program->stream_list = g_list_append (program->stream_list, bstream);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
if (klass->stream_added)
|
2013-06-23 06:43:23 +00:00
|
|
|
klass->stream_added (base, bstream, program);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
return bstream;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
mpegts_base_program_remove_stream (MpegTSBase * base,
|
|
|
|
MpegTSBaseProgram * program, guint16 pid)
|
|
|
|
{
|
2011-07-19 07:36:10 +00:00
|
|
|
MpegTSBaseClass *klass;
|
2011-07-19 07:34:37 +00:00
|
|
|
MpegTSBaseStream *stream = program->streams[pid];
|
|
|
|
|
|
|
|
GST_DEBUG ("pid:0x%04x", pid);
|
|
|
|
|
2011-07-19 07:36:10 +00:00
|
|
|
if (G_UNLIKELY (stream == NULL)) {
|
|
|
|
/* Can happen if the PCR PID is the same as a audio/video PID */
|
|
|
|
GST_DEBUG ("Stream already removed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
klass = GST_MPEGTS_BASE_GET_CLASS (base);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
/* If subclass needs it, inform it of the stream we are about to remove */
|
|
|
|
if (klass->stream_removed)
|
2011-07-19 07:34:37 +00:00
|
|
|
klass->stream_removed (base, stream);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-19 07:34:37 +00:00
|
|
|
program->stream_list = g_list_remove_all (program->stream_list, stream);
|
2013-06-23 06:43:23 +00:00
|
|
|
g_free (stream);
|
2011-02-16 16:57:42 +00:00
|
|
|
program->streams[pid] = NULL;
|
|
|
|
}
|
|
|
|
|
2012-03-02 09:54:48 +00:00
|
|
|
/* Return TRUE if programs are equal */
|
|
|
|
static gboolean
|
|
|
|
mpegts_base_is_same_program (MpegTSBase * base, MpegTSBaseProgram * oldprogram,
|
2013-07-03 11:57:38 +00:00
|
|
|
guint16 new_pmt_pid, const GstMpegTsPMT * new_pmt)
|
2012-03-02 09:54:48 +00:00
|
|
|
{
|
|
|
|
guint i, nbstreams;
|
|
|
|
MpegTSBaseStream *oldstream;
|
|
|
|
gboolean sawpcrpid = FALSE;
|
|
|
|
|
|
|
|
if (oldprogram->pmt_pid != new_pmt_pid) {
|
|
|
|
GST_DEBUG ("Different pmt_pid (new:0x%04x, old:0x%04x)", new_pmt_pid,
|
|
|
|
oldprogram->pmt_pid);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
if (oldprogram->pcr_pid != new_pmt->pcr_pid) {
|
2012-03-02 09:54:48 +00:00
|
|
|
GST_DEBUG ("Different pcr_pid (new:0x%04x, old:0x%04x)",
|
2013-06-23 06:43:23 +00:00
|
|
|
new_pmt->pcr_pid, oldprogram->pcr_pid);
|
2012-03-02 09:54:48 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the streams */
|
2013-06-23 06:43:23 +00:00
|
|
|
nbstreams = new_pmt->streams->len;
|
2012-03-02 09:54:48 +00:00
|
|
|
for (i = 0; i < nbstreams; ++i) {
|
2013-07-03 11:57:38 +00:00
|
|
|
GstMpegTsPMTStream *stream = g_ptr_array_index (new_pmt->streams, i);
|
2012-03-02 09:54:48 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
oldstream = oldprogram->streams[stream->pid];
|
2012-03-02 09:54:48 +00:00
|
|
|
if (!oldstream) {
|
2013-06-23 06:43:23 +00:00
|
|
|
GST_DEBUG ("New stream 0x%04x not present in old program", stream->pid);
|
2012-03-02 09:54:48 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2013-06-23 06:43:23 +00:00
|
|
|
if (oldstream->stream_type != stream->stream_type) {
|
2012-03-02 09:54:48 +00:00
|
|
|
GST_DEBUG
|
|
|
|
("New stream 0x%04x has a different stream type (new:%d, old:%d)",
|
2013-06-23 06:43:23 +00:00
|
|
|
stream->pid, stream->stream_type, oldstream->stream_type);
|
2012-03-02 09:54:48 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2013-06-23 06:43:23 +00:00
|
|
|
if (stream->pid == oldprogram->pcr_pid)
|
2012-03-02 09:54:48 +00:00
|
|
|
sawpcrpid = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the pcr is not shared with an existing stream, we'll have one extra stream */
|
|
|
|
if (!sawpcrpid)
|
|
|
|
nbstreams += 1;
|
|
|
|
|
|
|
|
if (nbstreams != g_list_length (oldprogram->stream_list)) {
|
|
|
|
GST_DEBUG ("Different number of streams (new:%d, old:%d)",
|
|
|
|
nbstreams, g_list_length (oldprogram->stream_list));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_DEBUG ("Programs are equal");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static void
|
2011-07-21 11:26:55 +00:00
|
|
|
mpegts_base_deactivate_program (MpegTSBase * base, MpegTSBaseProgram * program)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2013-06-23 06:43:23 +00:00
|
|
|
gint i;
|
2011-02-16 16:57:42 +00:00
|
|
|
MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
|
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
if (G_UNLIKELY (program->active == FALSE))
|
|
|
|
return;
|
|
|
|
|
2011-07-19 07:36:53 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "Deactivating PMT");
|
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
program->active = FALSE;
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
if (program->pmt) {
|
|
|
|
for (i = 0; i < program->pmt->streams->len; ++i) {
|
2013-07-03 11:57:38 +00:00
|
|
|
GstMpegTsPMTStream *stream = g_ptr_array_index (program->pmt->streams, i);
|
2011-07-21 11:26:55 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
mpegts_base_program_remove_stream (base, program, stream->pid);
|
2011-07-21 11:26:55 +00:00
|
|
|
|
2013-07-06 12:50:52 +00:00
|
|
|
/* Only unset the is_pes/known_psi bit if the PID isn't used in any other active
|
2011-07-21 11:26:55 +00:00
|
|
|
* program */
|
2013-07-06 12:50:52 +00:00
|
|
|
if (!mpegts_pid_in_active_programs (base, stream->pid)) {
|
|
|
|
switch (stream->stream_type) {
|
2013-09-27 14:45:00 +00:00
|
|
|
case GST_MPEG_TS_STREAM_TYPE_SCTE_DSMCC_DCB:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_SCTE_SIGNALING:
|
|
|
|
{
|
|
|
|
guint32 registration_id =
|
|
|
|
get_registration_from_descriptors (stream->descriptors);
|
|
|
|
|
|
|
|
/* Not a private section stream */
|
|
|
|
if (registration_id != DRF_ID_CUEI
|
|
|
|
&& registration_id != DRF_ID_ETV1)
|
|
|
|
break;
|
|
|
|
/* Fall through on purpose - remove this PID from known_psi */
|
|
|
|
}
|
2013-07-06 12:50:52 +00:00
|
|
|
case GST_MPEG_TS_STREAM_TYPE_PRIVATE_SECTIONS:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_MHEG:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSM_CC:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSMCC_A:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSMCC_B:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSMCC_C:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSMCC_D:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_SL_FLEXMUX_SECTIONS:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_METADATA_SECTIONS:
|
|
|
|
/* Set known PSI streams */
|
|
|
|
if (base->parse_private_sections)
|
|
|
|
MPEGTS_BIT_UNSET (base->known_psi, stream->pid);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MPEGTS_BIT_UNSET (base->is_pes, stream->pid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2011-07-21 11:26:55 +00:00
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
/* remove pcr stream */
|
2011-07-21 11:26:55 +00:00
|
|
|
/* FIXME : This might actually be shared with another stream ? */
|
2011-02-16 16:57:42 +00:00
|
|
|
mpegts_base_program_remove_stream (base, program, program->pcr_pid);
|
2011-07-21 11:26:55 +00:00
|
|
|
if (!mpegts_pid_in_active_programs (base, program->pcr_pid))
|
|
|
|
MPEGTS_BIT_UNSET (base->is_pes, program->pcr_pid);
|
|
|
|
|
|
|
|
GST_DEBUG ("program stream_list is now %p", program->stream_list);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2013-04-10 14:19:00 +00:00
|
|
|
|
|
|
|
/* Inform subclasses we're deactivating this program */
|
|
|
|
if (klass->program_stopped)
|
|
|
|
klass->program_stopped (base, program);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
static void
|
|
|
|
mpegts_base_activate_program (MpegTSBase * base, MpegTSBaseProgram * program,
|
2013-07-03 11:57:38 +00:00
|
|
|
guint16 pmt_pid, GstMpegTsSection * section, const GstMpegTsPMT * pmt,
|
2013-06-23 06:43:23 +00:00
|
|
|
gboolean initial_program)
|
2011-07-21 11:26:55 +00:00
|
|
|
{
|
2013-06-23 06:43:23 +00:00
|
|
|
guint i;
|
2011-07-21 11:26:55 +00:00
|
|
|
MpegTSBaseClass *klass;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (program->active))
|
|
|
|
return;
|
|
|
|
|
|
|
|
GST_DEBUG ("Activating program %d", program->program_number);
|
|
|
|
|
|
|
|
/* activate new pmt */
|
2013-06-23 06:43:23 +00:00
|
|
|
if (program->section)
|
|
|
|
gst_mpegts_section_unref (program->section);
|
|
|
|
program->section = gst_mpegts_section_ref (section);
|
2012-02-15 14:06:57 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
program->pmt = pmt;
|
2011-07-21 11:26:55 +00:00
|
|
|
program->pmt_pid = pmt_pid;
|
2013-06-23 06:43:23 +00:00
|
|
|
program->pcr_pid = pmt->pcr_pid;
|
|
|
|
|
2013-07-06 08:36:33 +00:00
|
|
|
/* extract top-level registration_id if present */
|
|
|
|
program->registration_id =
|
|
|
|
get_registration_from_descriptors (pmt->descriptors);
|
|
|
|
GST_DEBUG ("program 0x%04x, registration_id %" SAFE_FOURCC_FORMAT,
|
|
|
|
program->program_number, SAFE_FOURCC_ARGS (program->registration_id));
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
for (i = 0; i < pmt->streams->len; ++i) {
|
2013-07-03 11:57:38 +00:00
|
|
|
GstMpegTsPMTStream *stream = g_ptr_array_index (pmt->streams, i);
|
2013-06-23 06:43:23 +00:00
|
|
|
|
2013-07-06 12:50:52 +00:00
|
|
|
switch (stream->stream_type) {
|
2013-09-27 14:45:00 +00:00
|
|
|
case GST_MPEG_TS_STREAM_TYPE_SCTE_DSMCC_DCB:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_SCTE_SIGNALING:
|
|
|
|
{
|
|
|
|
guint32 registration_id =
|
|
|
|
get_registration_from_descriptors (stream->descriptors);
|
|
|
|
/* Not a private section stream */
|
|
|
|
if (registration_id != DRF_ID_CUEI && registration_id != DRF_ID_ETV1)
|
|
|
|
break;
|
|
|
|
/* Fall through on purpose - remove this PID from known_psi */
|
|
|
|
}
|
2013-07-06 12:50:52 +00:00
|
|
|
case GST_MPEG_TS_STREAM_TYPE_PRIVATE_SECTIONS:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_MHEG:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSM_CC:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSMCC_A:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSMCC_B:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSMCC_C:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_DSMCC_D:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_SL_FLEXMUX_SECTIONS:
|
|
|
|
case GST_MPEG_TS_STREAM_TYPE_METADATA_SECTIONS:
|
|
|
|
/* Set known PSI streams */
|
|
|
|
if (base->parse_private_sections)
|
|
|
|
MPEGTS_BIT_SET (base->known_psi, stream->pid);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->is_pes, stream->pid)))
|
|
|
|
GST_FIXME
|
|
|
|
("Refcounting issue. Setting twice a PID (0x%04x) as known PES",
|
|
|
|
stream->pid);
|
|
|
|
if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, stream->pid))) {
|
|
|
|
GST_FIXME
|
|
|
|
("Refcounting issue. Setting a known PSI PID (0x%04x) as known PES",
|
|
|
|
stream->pid);
|
|
|
|
MPEGTS_BIT_UNSET (base->known_psi, stream->pid);
|
|
|
|
}
|
|
|
|
|
|
|
|
MPEGTS_BIT_SET (base->is_pes, stream->pid);
|
|
|
|
break;
|
2013-06-23 06:43:23 +00:00
|
|
|
}
|
2011-07-21 11:26:55 +00:00
|
|
|
mpegts_base_program_add_stream (base, program,
|
2013-06-23 06:43:23 +00:00
|
|
|
stream->pid, stream->stream_type, stream);
|
2011-07-21 11:26:55 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
/* We add the PCR pid last. If that PID is already used by one of the media
|
|
|
|
* streams above, no new stream will be created */
|
2013-06-23 06:43:23 +00:00
|
|
|
mpegts_base_program_add_stream (base, program, pmt->pcr_pid, -1, NULL);
|
|
|
|
MPEGTS_BIT_SET (base->is_pes, pmt->pcr_pid);
|
2011-07-21 11:26:55 +00:00
|
|
|
|
|
|
|
program->active = TRUE;
|
2012-03-01 17:05:17 +00:00
|
|
|
program->initial_program = initial_program;
|
2011-07-21 11:26:55 +00:00
|
|
|
|
|
|
|
klass = GST_MPEGTS_BASE_GET_CLASS (base);
|
|
|
|
if (klass->program_started != NULL)
|
|
|
|
klass->program_started (base, program);
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "new pmt activated");
|
2011-07-21 11:26:55 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
static gboolean
|
2013-07-03 11:57:38 +00:00
|
|
|
mpegts_base_apply_pat (MpegTSBase * base, GstMpegTsSection * section)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2013-08-21 06:58:23 +00:00
|
|
|
GPtrArray *pat = gst_mpegts_section_get_pat (section);
|
|
|
|
GPtrArray *old_pat;
|
2011-02-16 16:57:42 +00:00
|
|
|
MpegTSBaseProgram *program;
|
2013-06-23 06:43:23 +00:00
|
|
|
gint i;
|
2011-07-21 11:26:55 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
if (G_UNLIKELY (pat == NULL))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (base, "PAT");
|
2011-07-21 11:26:55 +00:00
|
|
|
|
|
|
|
/* Applying a new PAT does two things:
|
|
|
|
* * It adds the new programs to the list of programs this element handles
|
|
|
|
* and increments at the same time the number of times a program is referenced.
|
|
|
|
*
|
|
|
|
* * If there was a previously active PAT, It decrements the reference count
|
|
|
|
* of all program it used. If a program is no longer needed, it is removed.
|
|
|
|
*/
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
old_pat = base->pat;
|
2013-06-23 06:43:23 +00:00
|
|
|
base->pat = pat;
|
2011-07-21 11:26:55 +00:00
|
|
|
|
|
|
|
GST_LOG ("Activating new Program Association Table");
|
2011-02-16 16:57:42 +00:00
|
|
|
/* activate the new table */
|
2013-06-23 06:43:23 +00:00
|
|
|
for (i = 0; i < pat->len; ++i) {
|
2013-08-21 06:58:23 +00:00
|
|
|
GstMpegTsPatProgram *patp = g_ptr_array_index (pat, i);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
program = mpegts_base_get_program (base, patp->program_number);
|
2011-02-16 16:57:42 +00:00
|
|
|
if (program) {
|
2011-07-21 11:26:55 +00:00
|
|
|
/* IF the program already existed, just check if the PMT PID changed */
|
2013-06-23 06:43:23 +00:00
|
|
|
if (program->pmt_pid != patp->network_or_program_map_PID) {
|
2011-02-16 16:57:42 +00:00
|
|
|
if (program->pmt_pid != G_MAXUINT16) {
|
|
|
|
/* pmt pid changed */
|
|
|
|
/* FIXME: when this happens it may still be pmt pid of another
|
|
|
|
* program, so setting to False may make it go through expensive
|
|
|
|
* path in is_psi unnecessarily */
|
2011-07-19 06:45:51 +00:00
|
|
|
MPEGTS_BIT_UNSET (base->known_psi, program->pmt_pid);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
program->pmt_pid = patp->network_or_program_map_PID;
|
|
|
|
if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi, program->pmt_pid)))
|
|
|
|
GST_FIXME
|
|
|
|
("Refcounting issue. Setting twice a PMT PID (0x%04x) as know PSI",
|
|
|
|
program->pmt_pid);
|
|
|
|
MPEGTS_BIT_SET (base->known_psi, patp->network_or_program_map_PID);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-07-21 11:26:55 +00:00
|
|
|
/* Create a new program */
|
2013-06-23 06:43:23 +00:00
|
|
|
program =
|
|
|
|
mpegts_base_add_program (base, patp->program_number,
|
|
|
|
patp->network_or_program_map_PID);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2011-07-21 11:26:55 +00:00
|
|
|
/* We mark this program as being referenced by one PAT */
|
2011-02-16 16:57:42 +00:00
|
|
|
program->patcount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (old_pat) {
|
|
|
|
/* deactivate the old table */
|
2011-07-21 11:26:55 +00:00
|
|
|
GST_LOG ("Deactivating old Program Association Table");
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
for (i = 0; i < old_pat->len; ++i) {
|
2013-08-21 06:58:23 +00:00
|
|
|
GstMpegTsPatProgram *patp = g_ptr_array_index (old_pat, i);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
program = mpegts_base_get_program (base, patp->program_number);
|
2011-07-21 11:26:55 +00:00
|
|
|
if (G_UNLIKELY (program == NULL)) {
|
2011-02-16 16:57:42 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "broken PAT, duplicated entry for program %d",
|
2013-06-23 06:43:23 +00:00
|
|
|
patp->program_number);
|
2011-02-16 16:57:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--program->patcount > 0)
|
|
|
|
/* the program has been referenced by the new pat, keep it */
|
|
|
|
continue;
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
GST_INFO_OBJECT (base, "PAT removing program 0x%04x 0x%04x",
|
|
|
|
patp->program_number, patp->network_or_program_map_PID);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
mpegts_base_deactivate_program (base, program);
|
2013-06-23 06:43:23 +00:00
|
|
|
mpegts_base_remove_program (base, patp->program_number);
|
2011-02-16 16:57:42 +00:00
|
|
|
/* FIXME: when this happens it may still be pmt pid of another
|
|
|
|
* program, so setting to False may make it go through expensive
|
|
|
|
* path in is_psi unnecessarily */
|
2013-06-23 06:43:23 +00:00
|
|
|
if (G_UNLIKELY (MPEGTS_BIT_IS_SET (base->known_psi,
|
|
|
|
patp->network_or_program_map_PID))) {
|
|
|
|
GST_FIXME
|
|
|
|
("Program refcounting : Setting twice a pid (0x%04x) as known PSI",
|
|
|
|
patp->network_or_program_map_PID);
|
|
|
|
}
|
|
|
|
MPEGTS_BIT_SET (base->known_psi, patp->network_or_program_map_PID);
|
|
|
|
mpegts_packetizer_remove_stream (base->packetizer,
|
|
|
|
patp->network_or_program_map_PID);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2013-08-21 06:58:23 +00:00
|
|
|
g_ptr_array_unref (old_pat);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
2013-06-23 06:43:23 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
static gboolean
|
2013-07-03 11:57:38 +00:00
|
|
|
mpegts_base_apply_pmt (MpegTSBase * base, GstMpegTsSection * section)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2013-07-03 11:57:38 +00:00
|
|
|
const GstMpegTsPMT *pmt;
|
2011-07-21 11:26:55 +00:00
|
|
|
MpegTSBaseProgram *program, *old_program;
|
2011-02-16 16:57:42 +00:00
|
|
|
guint program_number;
|
2012-03-01 17:05:17 +00:00
|
|
|
gboolean initial_program = TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
pmt = gst_mpegts_section_get_pmt (section);
|
|
|
|
if (G_UNLIKELY (pmt == NULL)) {
|
|
|
|
GST_ERROR ("Could not get PMT (corrupted ?)");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
/* FIXME : not so sure this is valid anymore */
|
2011-07-14 20:08:56 +00:00
|
|
|
if (G_UNLIKELY (base->seen_pat == FALSE)) {
|
2011-02-16 16:57:42 +00:00
|
|
|
GST_WARNING ("Got pmt without pat first. Returning");
|
2011-03-21 17:54:46 +00:00
|
|
|
/* remove the stream since we won't get another PMT otherwise */
|
2013-06-23 06:43:23 +00:00
|
|
|
mpegts_packetizer_remove_stream (base->packetizer, section->pid);
|
|
|
|
return TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
program_number = section->subtable_extension;
|
2011-07-21 11:26:55 +00:00
|
|
|
GST_DEBUG ("Applying PMT (program_number:%d, pid:0x%04x)",
|
2013-06-23 06:43:23 +00:00
|
|
|
program_number, section->pid);
|
2011-07-19 07:36:53 +00:00
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
/* In order for stream switching to happen properly in decodebin(2),
|
|
|
|
* we need to first add the new pads (i.e. activate the new program)
|
|
|
|
* before removing the old ones (i.e. deactivating the old program)
|
|
|
|
*/
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
old_program = mpegts_base_get_program (base, program_number);
|
|
|
|
if (G_UNLIKELY (old_program == NULL))
|
|
|
|
goto no_program;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
if (G_UNLIKELY (mpegts_base_is_same_program (base, old_program, section->pid,
|
|
|
|
pmt)))
|
2012-03-02 09:54:48 +00:00
|
|
|
goto same_program;
|
|
|
|
|
2011-07-21 11:26:55 +00:00
|
|
|
/* If the current program is active, this means we have a new program */
|
|
|
|
if (old_program->active) {
|
|
|
|
old_program = mpegts_base_steal_program (base, program_number);
|
2013-06-23 06:43:23 +00:00
|
|
|
program = mpegts_base_new_program (base, program_number, section->pid);
|
2011-07-21 11:26:55 +00:00
|
|
|
g_hash_table_insert (base->programs,
|
|
|
|
GINT_TO_POINTER (program_number), program);
|
2012-02-15 14:06:57 +00:00
|
|
|
|
|
|
|
/* Desactivate the old program */
|
|
|
|
mpegts_base_deactivate_program (base, old_program);
|
|
|
|
mpegts_base_free_program (old_program);
|
2012-03-01 17:05:17 +00:00
|
|
|
initial_program = FALSE;
|
2011-07-21 11:26:55 +00:00
|
|
|
} else
|
|
|
|
program = old_program;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-06-10 09:21:41 +00:00
|
|
|
/* activate program */
|
|
|
|
/* Ownership of pmt_info is given to the program */
|
2013-06-23 06:43:23 +00:00
|
|
|
mpegts_base_activate_program (base, program, section->pid, section, pmt,
|
2012-03-01 17:05:17 +00:00
|
|
|
initial_program);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
return TRUE;
|
2011-07-21 11:26:55 +00:00
|
|
|
|
|
|
|
no_program:
|
|
|
|
{
|
|
|
|
GST_ERROR ("Attempted to apply a PMT on a program that wasn't created");
|
2013-06-23 06:43:23 +00:00
|
|
|
return TRUE;
|
2011-07-21 11:26:55 +00:00
|
|
|
}
|
2012-03-02 09:54:48 +00:00
|
|
|
|
|
|
|
same_program:
|
|
|
|
{
|
|
|
|
GST_DEBUG ("Not applying identical program");
|
2013-06-23 06:43:23 +00:00
|
|
|
return TRUE;
|
2012-03-02 09:54:48 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2012-06-03 08:56:28 +00:00
|
|
|
static void
|
2013-07-03 11:57:38 +00:00
|
|
|
mpegts_base_handle_psi (MpegTSBase * base, GstMpegTsSection * section)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2013-06-23 06:43:23 +00:00
|
|
|
gboolean post_message = TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-06-03 08:53:52 +00:00
|
|
|
GST_DEBUG ("Handling PSI (pid: 0x%04x , table_id: 0x%02x)",
|
|
|
|
section->pid, section->table_id);
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
switch (section->section_type) {
|
|
|
|
case GST_MPEGTS_SECTION_PAT:
|
|
|
|
post_message = mpegts_base_apply_pat (base, section);
|
|
|
|
if (base->seen_pat == FALSE) {
|
|
|
|
base->seen_pat = TRUE;
|
|
|
|
GST_DEBUG ("First PAT offset: %" G_GUINT64_FORMAT, section->offset);
|
|
|
|
mpegts_packetizer_set_reference_offset (base->packetizer,
|
|
|
|
section->offset);
|
|
|
|
}
|
2013-07-07 05:21:46 +00:00
|
|
|
break;
|
2013-06-23 06:43:23 +00:00
|
|
|
case GST_MPEGTS_SECTION_PMT:
|
|
|
|
post_message = mpegts_base_apply_pmt (base, section);
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
2013-06-23 06:43:23 +00:00
|
|
|
case GST_MPEGTS_SECTION_EIT:
|
|
|
|
/* some tag xtraction + posting */
|
|
|
|
post_message = mpegts_base_get_tags_from_eit (base, section);
|
2012-06-05 07:54:53 +00:00
|
|
|
break;
|
2011-02-16 16:57:42 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
/* Finally post message (if it wasn't corrupted) */
|
|
|
|
if (post_message)
|
|
|
|
gst_element_post_message (GST_ELEMENT_CAST (base),
|
|
|
|
gst_message_new_mpegts_section (GST_OBJECT (base), section));
|
|
|
|
gst_mpegts_section_unref (section);
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
static gboolean
|
2013-07-03 11:57:38 +00:00
|
|
|
mpegts_base_get_tags_from_eit (MpegTSBase * base, GstMpegTsSection * section)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2013-07-03 11:57:38 +00:00
|
|
|
const GstMpegTsEIT *eit;
|
2011-02-16 16:57:42 +00:00
|
|
|
guint i;
|
|
|
|
MpegTSBaseProgram *program;
|
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
/* Early exit if it's not from the present/following table_id */
|
|
|
|
if (section->table_id != GST_MTS_TABLE_ID_EVENT_INFORMATION_ACTUAL_TS_PRESENT
|
|
|
|
|| section->table_id !=
|
|
|
|
GST_MTS_TABLE_ID_EVENT_INFORMATION_OTHER_TS_PRESENT)
|
|
|
|
return TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
eit = gst_mpegts_section_get_eit (section);
|
|
|
|
if (G_UNLIKELY (eit == NULL))
|
|
|
|
return FALSE;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-06-23 06:43:23 +00:00
|
|
|
program = mpegts_base_get_program (base, section->subtable_extension);
|
|
|
|
|
|
|
|
GST_DEBUG
|
|
|
|
("program_id:0x%04x, table_id:0x%02x, actual_stream:%d, present_following:%d, program:%p",
|
|
|
|
section->subtable_extension, section->table_id, eit->actual_stream,
|
|
|
|
eit->present_following, program);
|
|
|
|
|
|
|
|
if (program && eit->present_following) {
|
|
|
|
for (i = 0; i < eit->events->len; i++) {
|
2013-07-03 11:57:38 +00:00
|
|
|
GstMpegTsEITEvent *event = g_ptr_array_index (eit->events, i);
|
|
|
|
const GstMpegTsDescriptor *desc;
|
2013-06-23 06:43:23 +00:00
|
|
|
|
|
|
|
if (event->running_status == RUNNING_STATUS_RUNNING) {
|
|
|
|
program->event_id = event->event_id;
|
|
|
|
if ((desc =
|
|
|
|
gst_mpegts_find_descriptor (event->descriptors,
|
|
|
|
GST_MTS_DESC_DVB_SHORT_EVENT))) {
|
|
|
|
gchar *name;
|
|
|
|
if (gst_mpegts_descriptor_parse_dvb_short_event (desc, NULL, &name,
|
|
|
|
NULL)) {
|
|
|
|
/* FIXME : Is it correct to post an event duration as a GST_TAG_DURATION ??? */
|
|
|
|
program->tags =
|
|
|
|
gst_tag_list_new (GST_TAG_TITLE, name, GST_TAG_DURATION,
|
|
|
|
event->duration * GST_SECOND, NULL);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 06:43:23 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2012-03-30 17:19:12 +00:00
|
|
|
static gboolean
|
2011-08-01 13:46:12 +00:00
|
|
|
remove_each_program (gpointer key, MpegTSBaseProgram * program,
|
|
|
|
MpegTSBase * base)
|
|
|
|
{
|
|
|
|
/* First deactivate it */
|
|
|
|
mpegts_base_deactivate_program (base, program);
|
2012-03-30 17:19:12 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2011-08-01 13:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_mpegts_base_handle_eos (MpegTSBase * base)
|
|
|
|
{
|
2012-03-30 17:19:12 +00:00
|
|
|
g_hash_table_foreach_remove (base->programs, (GHRFunc) remove_each_program,
|
|
|
|
base);
|
2011-08-01 13:46:12 +00:00
|
|
|
/* finally remove */
|
|
|
|
return TRUE;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2014-03-28 16:32:56 +00:00
|
|
|
static inline GstFlowReturn
|
|
|
|
mpegts_base_drain (MpegTSBase * base)
|
|
|
|
{
|
|
|
|
MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
|
|
|
|
|
|
|
|
/* Call implementation */
|
|
|
|
if (klass->drain)
|
|
|
|
return klass->drain (base);
|
|
|
|
|
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
2011-08-26 22:44:08 +00:00
|
|
|
static inline void
|
2013-05-24 08:59:59 +00:00
|
|
|
mpegts_base_flush (MpegTSBase * base, gboolean hard)
|
2011-08-26 22:44:08 +00:00
|
|
|
{
|
|
|
|
MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
|
|
|
|
|
|
|
|
/* Call implementation */
|
2013-06-07 10:53:08 +00:00
|
|
|
if (klass->flush)
|
2013-05-24 08:59:59 +00:00
|
|
|
klass->flush (base, hard);
|
2011-08-26 22:44:08 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static gboolean
|
2012-04-19 12:20:52 +00:00
|
|
|
mpegts_base_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2011-07-15 10:08:40 +00:00
|
|
|
gboolean res = TRUE;
|
2013-05-24 16:18:35 +00:00
|
|
|
gboolean hard;
|
2012-04-19 12:20:52 +00:00
|
|
|
MpegTSBase *base = GST_MPEGTS_BASE (parent);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-06-20 16:10:37 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "Got event %s",
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_event_type_get_name (GST_EVENT_TYPE (event)));
|
|
|
|
|
|
|
|
switch (GST_EVENT_TYPE (event)) {
|
2012-04-19 12:20:52 +00:00
|
|
|
case GST_EVENT_SEGMENT:
|
|
|
|
gst_event_copy_segment (event, &base->segment);
|
2013-06-17 06:15:54 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "Received segment %" GST_SEGMENT_FORMAT,
|
|
|
|
&base->segment);
|
2012-11-08 16:15:26 +00:00
|
|
|
/* Check if we need to switch PCR/PTS handling */
|
|
|
|
if (base->segment.format == GST_FORMAT_TIME) {
|
|
|
|
base->packetizer->calculate_offset = FALSE;
|
|
|
|
base->packetizer->calculate_skew = TRUE;
|
|
|
|
} else {
|
|
|
|
base->packetizer->calculate_offset = TRUE;
|
|
|
|
base->packetizer->calculate_skew = FALSE;
|
|
|
|
}
|
2013-03-30 12:39:49 +00:00
|
|
|
res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
|
2012-04-19 12:20:52 +00:00
|
|
|
break;
|
|
|
|
case GST_EVENT_STREAM_START:
|
2011-02-16 16:57:42 +00:00
|
|
|
gst_event_unref (event);
|
|
|
|
break;
|
2011-08-01 13:46:12 +00:00
|
|
|
case GST_EVENT_EOS:
|
2012-06-20 16:10:37 +00:00
|
|
|
res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
|
2012-10-12 13:26:20 +00:00
|
|
|
res = gst_mpegts_base_handle_eos (base);
|
2011-08-01 13:46:12 +00:00
|
|
|
break;
|
2012-04-19 12:20:52 +00:00
|
|
|
case GST_EVENT_CAPS:
|
|
|
|
/* FIXME, do something */
|
|
|
|
gst_event_unref (event);
|
|
|
|
break;
|
2012-11-10 19:30:13 +00:00
|
|
|
case GST_EVENT_FLUSH_STOP:
|
|
|
|
res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
|
2013-05-24 16:18:35 +00:00
|
|
|
hard = (base->mode != BASE_MODE_SEEKING);
|
|
|
|
mpegts_packetizer_flush (base->packetizer, hard);
|
|
|
|
mpegts_base_flush (base, hard);
|
2011-08-16 19:51:34 +00:00
|
|
|
gst_segment_init (&base->segment, GST_FORMAT_UNDEFINED);
|
|
|
|
base->seen_pat = FALSE;
|
2012-11-10 19:30:13 +00:00
|
|
|
break;
|
2011-02-16 16:57:42 +00:00
|
|
|
default:
|
|
|
|
res = GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-11-08 08:44:18 +00:00
|
|
|
static void
|
|
|
|
query_upstream_latency (MpegTSBase * base)
|
|
|
|
{
|
|
|
|
GstQuery *query;
|
|
|
|
|
|
|
|
query = gst_query_new_latency ();
|
|
|
|
if (gst_pad_peer_query (base->sinkpad, query)) {
|
|
|
|
gst_query_parse_latency (query, &base->upstream_live, NULL, NULL);
|
|
|
|
GST_DEBUG_OBJECT (base, "Upstream is %s",
|
|
|
|
base->upstream_live ? "LIVE" : "NOT LIVE");
|
|
|
|
} else
|
|
|
|
GST_WARNING_OBJECT (base, "Failed to query upstream latency");
|
|
|
|
gst_query_unref (query);
|
2012-03-03 15:47:01 +00:00
|
|
|
base->queried_latency = TRUE;
|
2011-11-08 08:44:18 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static GstFlowReturn
|
2012-04-19 12:20:52 +00:00
|
|
|
mpegts_base_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
|
|
|
GstFlowReturn res = GST_FLOW_OK;
|
|
|
|
MpegTSBase *base;
|
|
|
|
MpegTSPacketizerPacketReturn pret;
|
|
|
|
MpegTSPacketizer2 *packetizer;
|
|
|
|
MpegTSPacketizerPacket packet;
|
2012-06-26 16:51:21 +00:00
|
|
|
MpegTSBaseClass *klass;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-04-19 12:20:52 +00:00
|
|
|
base = GST_MPEGTS_BASE (parent);
|
2012-06-26 16:51:21 +00:00
|
|
|
klass = GST_MPEGTS_BASE_GET_CLASS (base);
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
packetizer = base->packetizer;
|
|
|
|
|
2012-03-03 15:47:01 +00:00
|
|
|
if (G_UNLIKELY (base->queried_latency == FALSE)) {
|
2011-11-08 08:44:18 +00:00
|
|
|
query_upstream_latency (base);
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:51:21 +00:00
|
|
|
if (klass->input_done)
|
|
|
|
gst_buffer_ref (buf);
|
|
|
|
|
2014-03-28 16:32:56 +00:00
|
|
|
if (GST_BUFFER_IS_DISCONT (buf)) {
|
2014-03-28 16:25:02 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "Got DISCONT buffer, flushing");
|
2014-03-28 16:32:56 +00:00
|
|
|
res = mpegts_base_drain (base);
|
|
|
|
if (G_UNLIKELY (res != GST_FLOW_OK))
|
|
|
|
return res;
|
|
|
|
|
2014-03-28 16:25:02 +00:00
|
|
|
mpegts_base_flush (base, FALSE);
|
2014-04-18 14:18:01 +00:00
|
|
|
mpegts_packetizer_flush (base->packetizer, FALSE);
|
2014-03-28 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
mpegts_packetizer_push (base->packetizer, buf);
|
2012-04-19 12:20:52 +00:00
|
|
|
|
|
|
|
while (res == GST_FLOW_OK) {
|
|
|
|
pret = mpegts_packetizer_next_packet (base->packetizer, &packet);
|
|
|
|
|
|
|
|
/* If we don't have enough data, return */
|
|
|
|
if (G_UNLIKELY (pret == PACKET_NEED_MORE))
|
|
|
|
break;
|
|
|
|
|
2012-04-30 10:36:46 +00:00
|
|
|
if (G_UNLIKELY (pret == PACKET_BAD)) {
|
2011-02-16 16:57:42 +00:00
|
|
|
/* bad header, skip the packet */
|
2012-04-30 10:36:46 +00:00
|
|
|
GST_DEBUG_OBJECT (base, "bad packet, skipping");
|
2011-02-16 16:57:42 +00:00
|
|
|
goto next;
|
2012-04-30 10:36:46 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-07-05 14:46:42 +00:00
|
|
|
/* If it's a known PES, push it */
|
|
|
|
if (MPEGTS_BIT_IS_SET (base->is_pes, packet.pid)) {
|
|
|
|
/* push the packet downstream */
|
2013-07-07 09:42:50 +00:00
|
|
|
if (base->push_data)
|
|
|
|
res = klass->push (base, &packet, NULL);
|
2013-07-05 14:46:42 +00:00
|
|
|
} else if (packet.payload
|
|
|
|
&& MPEGTS_BIT_IS_SET (base->known_psi, packet.pid)) {
|
|
|
|
/* base PSI data */
|
2013-07-07 06:29:37 +00:00
|
|
|
GList *others, *tmp;
|
2013-07-03 11:57:38 +00:00
|
|
|
GstMpegTsSection *section;
|
2013-06-23 06:43:23 +00:00
|
|
|
|
2013-07-07 06:29:37 +00:00
|
|
|
section = mpegts_packetizer_push_section (packetizer, &packet, &others);
|
2013-06-23 06:43:23 +00:00
|
|
|
if (section)
|
|
|
|
mpegts_base_handle_psi (base, section);
|
2013-07-07 06:29:37 +00:00
|
|
|
if (G_UNLIKELY (others)) {
|
|
|
|
for (tmp = others; tmp; tmp = tmp->next)
|
|
|
|
mpegts_base_handle_psi (base, (GstMpegTsSection *) tmp->data);
|
|
|
|
g_list_free (others);
|
|
|
|
}
|
2013-06-23 06:43:23 +00:00
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
/* we need to push section packet downstream */
|
2013-07-07 09:42:50 +00:00
|
|
|
if (base->push_section)
|
|
|
|
res = klass->push (base, &packet, section);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2013-07-05 14:46:42 +00:00
|
|
|
} else if (packet.payload && packet.pid != 0x1fff)
|
|
|
|
GST_LOG ("PID 0x%04x Saw packet on a pid we don't handle", packet.pid);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
next:
|
|
|
|
mpegts_packetizer_clear_packet (base->packetizer, &packet);
|
|
|
|
}
|
|
|
|
|
2012-06-26 16:51:21 +00:00
|
|
|
if (klass->input_done) {
|
|
|
|
if (res == GST_FLOW_OK)
|
|
|
|
res = klass->input_done (base, buf);
|
|
|
|
else
|
|
|
|
gst_buffer_unref (buf);
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
mpegts_base_scan (MpegTSBase * base)
|
|
|
|
{
|
2013-03-03 11:30:32 +00:00
|
|
|
GstFlowReturn ret = GST_FLOW_OK;
|
2012-04-19 11:59:37 +00:00
|
|
|
GstBuffer *buf = NULL;
|
2011-02-16 16:57:42 +00:00
|
|
|
guint i;
|
2012-03-01 17:05:17 +00:00
|
|
|
gboolean done = FALSE;
|
|
|
|
MpegTSPacketizerPacketReturn pret;
|
|
|
|
gint64 tmpval;
|
2014-03-27 18:22:03 +00:00
|
|
|
gint64 upstream_size, seek_pos;
|
2012-03-01 17:05:17 +00:00
|
|
|
GstFormat format;
|
|
|
|
guint initial_pcr_seen;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("Scanning for initial sync point");
|
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
/* Find initial sync point and at least 5 PCR values */
|
|
|
|
for (i = 0; i < 10 && !done; i++) {
|
|
|
|
GST_DEBUG ("Grabbing %d => %d", i * 65536, 65536);
|
2012-02-15 14:06:57 +00:00
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
ret = gst_pad_pull_range (base->sinkpad, i * 65536, 65536, &buf);
|
2014-03-27 18:22:03 +00:00
|
|
|
if (G_UNLIKELY (ret == GST_FLOW_EOS)) {
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
} else if (G_UNLIKELY (ret != GST_FLOW_OK)) {
|
2011-02-16 16:57:42 +00:00
|
|
|
goto beach;
|
2014-03-27 18:22:03 +00:00
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
/* Push to packetizer */
|
|
|
|
mpegts_packetizer_push (base->packetizer, buf);
|
2012-04-19 11:59:37 +00:00
|
|
|
buf = NULL;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
if (mpegts_packetizer_has_packets (base->packetizer)) {
|
2012-03-01 17:05:17 +00:00
|
|
|
if (base->seek_offset == -1) {
|
|
|
|
/* Mark the initial sync point and remember the packetsize */
|
|
|
|
base->seek_offset = base->packetizer->offset;
|
|
|
|
GST_DEBUG ("Sync point is now %" G_GUINT64_FORMAT, base->seek_offset);
|
|
|
|
base->packetsize = base->packetizer->packet_size;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
/* Eat up all packets */
|
|
|
|
pret = mpegts_packetizer_process_next_packet (base->packetizer);
|
|
|
|
if (pret == PACKET_NEED_MORE)
|
|
|
|
break;
|
mpegtsdemux: New PCR<=>Offset estimation code
This allows:
* Better duration estimation
* More accurate PCR location
* Overall more accurate running-time location and calculation
Location and values of PCR are recorded in groups (PCROffsetGroup)
with notable PCR/Offset observations in them (when bitrate changed
for example). PCR and offset are stored as 32bit values to
reduce memory usage (they are differences against that group's
first_{pcr|offset}.
Those groups each contain a global PCR offset (pcr_offset) which
indicates how far in the stream that group is.
Whenever new PCR values are observed, we store them in a sliding
window estimator (PCROffsetGroupCurrent).
When a reset/wrapover/gap is detected, we close the current group with
current values and start a new one (the pcr_offset of that new group
is also calculated).
When a notable change in bitrate is observed (+/- 10%), we record
new values in the current group. This is a compromise between
storing all PCR/offset observations and none, while at the same time
providing better information for running-time<=>offset calculation
in VBR streams.
Whenever a new non-contiguous group is start (due to seeking for example)
we re-evaluate the pcr_offset of each groups. This allows detecting as
quickly as possible PCR wrapover/reset.
When wanting to find the offset of a certain running-time, one can
iterate the groups by looking at the pcr_offset (which in essence *is*
the running-time of that group in the overall stream).
Once a group (or neighbouring groups if the running-time is between two
groups) is found, once can use the recorded values to find the most
accurate offset.
Right now this code is only used in pull-mode , but could also
be activated later on for any seekable stream, like live timeshift
with queue2.
Future improvements:
* some heuristics to "compress" the stored values in groups so as to keep
the memory usage down while still keeping a decent amount of notable
points.
* After a seek compare expected and obtained PCR/Offset and if the
difference is too big, re-calculate position with newly observed
values and seek to that more accurate position.
Note that this code will *not* provide keyframe-accurate seeking, but
will allow a much more accurate PCR/running-time/offset location on
any random stream.
For past (observed) values it will be as accurate as can be.
For future values it will be better than the current situation.
Finally the more you seek, the more accurate your positioning will be.
2013-07-26 05:54:30 +00:00
|
|
|
if (pret != PACKET_BAD && base->packetizer->nb_seen_offsets >= 5) {
|
2012-03-01 17:05:17 +00:00
|
|
|
GST_DEBUG ("Got enough initial PCR");
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
mpegtsdemux: New PCR<=>Offset estimation code
This allows:
* Better duration estimation
* More accurate PCR location
* Overall more accurate running-time location and calculation
Location and values of PCR are recorded in groups (PCROffsetGroup)
with notable PCR/Offset observations in them (when bitrate changed
for example). PCR and offset are stored as 32bit values to
reduce memory usage (they are differences against that group's
first_{pcr|offset}.
Those groups each contain a global PCR offset (pcr_offset) which
indicates how far in the stream that group is.
Whenever new PCR values are observed, we store them in a sliding
window estimator (PCROffsetGroupCurrent).
When a reset/wrapover/gap is detected, we close the current group with
current values and start a new one (the pcr_offset of that new group
is also calculated).
When a notable change in bitrate is observed (+/- 10%), we record
new values in the current group. This is a compromise between
storing all PCR/offset observations and none, while at the same time
providing better information for running-time<=>offset calculation
in VBR streams.
Whenever a new non-contiguous group is start (due to seeking for example)
we re-evaluate the pcr_offset of each groups. This allows detecting as
quickly as possible PCR wrapover/reset.
When wanting to find the offset of a certain running-time, one can
iterate the groups by looking at the pcr_offset (which in essence *is*
the running-time of that group in the overall stream).
Once a group (or neighbouring groups if the running-time is between two
groups) is found, once can use the recorded values to find the most
accurate offset.
Right now this code is only used in pull-mode , but could also
be activated later on for any seekable stream, like live timeshift
with queue2.
Future improvements:
* some heuristics to "compress" the stored values in groups so as to keep
the memory usage down while still keeping a decent amount of notable
points.
* After a seek compare expected and obtained PCR/Offset and if the
difference is too big, re-calculate position with newly observed
values and seek to that more accurate position.
Note that this code will *not* provide keyframe-accurate seeking, but
will allow a much more accurate PCR/running-time/offset location on
any random stream.
For past (observed) values it will be as accurate as can be.
For future values it will be better than the current situation.
Finally the more you seek, the more accurate your positioning will be.
2013-07-26 05:54:30 +00:00
|
|
|
initial_pcr_seen = base->packetizer->nb_seen_offsets;
|
2012-03-01 17:05:17 +00:00
|
|
|
if (G_UNLIKELY (initial_pcr_seen == 0))
|
|
|
|
goto no_initial_pcr;
|
|
|
|
GST_DEBUG ("Seen %d initial PCR", initial_pcr_seen);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
/* Now send data from the end */
|
|
|
|
mpegts_packetizer_clear (base->packetizer);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
/* Get the size of upstream */
|
|
|
|
format = GST_FORMAT_BYTES;
|
2012-04-19 12:20:52 +00:00
|
|
|
if (!gst_pad_peer_query_duration (base->sinkpad, format, &tmpval))
|
2012-03-01 17:05:17 +00:00
|
|
|
goto beach;
|
|
|
|
upstream_size = tmpval;
|
|
|
|
done = FALSE;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
/* Find last PCR value */
|
|
|
|
for (seek_pos = MAX (0, upstream_size - 655360);
|
|
|
|
seek_pos < upstream_size && !done; seek_pos += 65536) {
|
2012-03-05 10:27:51 +00:00
|
|
|
GST_DEBUG ("Grabbing %" G_GUINT64_FORMAT " => %d", seek_pos, 65536);
|
2012-03-01 17:05:17 +00:00
|
|
|
|
|
|
|
ret = gst_pad_pull_range (base->sinkpad, seek_pos, 65536, &buf);
|
2014-03-27 18:22:03 +00:00
|
|
|
if (G_UNLIKELY (ret == GST_FLOW_EOS)) {
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
} else if (G_UNLIKELY (ret != GST_FLOW_OK)) {
|
2011-02-16 16:57:42 +00:00
|
|
|
goto beach;
|
2014-03-27 18:22:03 +00:00
|
|
|
}
|
2012-03-01 17:05:17 +00:00
|
|
|
|
|
|
|
/* Push to packetizer */
|
|
|
|
mpegts_packetizer_push (base->packetizer, buf);
|
2012-04-19 11:59:37 +00:00
|
|
|
buf = NULL;
|
2012-03-01 17:05:17 +00:00
|
|
|
|
|
|
|
if (mpegts_packetizer_has_packets (base->packetizer)) {
|
|
|
|
while (1) {
|
|
|
|
/* Eat up all packets */
|
|
|
|
pret = mpegts_packetizer_process_next_packet (base->packetizer);
|
|
|
|
if (pret == PACKET_NEED_MORE)
|
|
|
|
break;
|
|
|
|
if (pret != PACKET_BAD &&
|
mpegtsdemux: New PCR<=>Offset estimation code
This allows:
* Better duration estimation
* More accurate PCR location
* Overall more accurate running-time location and calculation
Location and values of PCR are recorded in groups (PCROffsetGroup)
with notable PCR/Offset observations in them (when bitrate changed
for example). PCR and offset are stored as 32bit values to
reduce memory usage (they are differences against that group's
first_{pcr|offset}.
Those groups each contain a global PCR offset (pcr_offset) which
indicates how far in the stream that group is.
Whenever new PCR values are observed, we store them in a sliding
window estimator (PCROffsetGroupCurrent).
When a reset/wrapover/gap is detected, we close the current group with
current values and start a new one (the pcr_offset of that new group
is also calculated).
When a notable change in bitrate is observed (+/- 10%), we record
new values in the current group. This is a compromise between
storing all PCR/offset observations and none, while at the same time
providing better information for running-time<=>offset calculation
in VBR streams.
Whenever a new non-contiguous group is start (due to seeking for example)
we re-evaluate the pcr_offset of each groups. This allows detecting as
quickly as possible PCR wrapover/reset.
When wanting to find the offset of a certain running-time, one can
iterate the groups by looking at the pcr_offset (which in essence *is*
the running-time of that group in the overall stream).
Once a group (or neighbouring groups if the running-time is between two
groups) is found, once can use the recorded values to find the most
accurate offset.
Right now this code is only used in pull-mode , but could also
be activated later on for any seekable stream, like live timeshift
with queue2.
Future improvements:
* some heuristics to "compress" the stored values in groups so as to keep
the memory usage down while still keeping a decent amount of notable
points.
* After a seek compare expected and obtained PCR/Offset and if the
difference is too big, re-calculate position with newly observed
values and seek to that more accurate position.
Note that this code will *not* provide keyframe-accurate seeking, but
will allow a much more accurate PCR/running-time/offset location on
any random stream.
For past (observed) values it will be as accurate as can be.
For future values it will be better than the current situation.
Finally the more you seek, the more accurate your positioning will be.
2013-07-26 05:54:30 +00:00
|
|
|
base->packetizer->nb_seen_offsets > initial_pcr_seen) {
|
2012-03-01 17:05:17 +00:00
|
|
|
GST_DEBUG ("Got last PCR");
|
|
|
|
done = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
beach:
|
|
|
|
mpegts_packetizer_clear (base->packetizer);
|
|
|
|
return ret;
|
|
|
|
|
2012-03-01 17:05:17 +00:00
|
|
|
no_initial_pcr:
|
|
|
|
mpegts_packetizer_clear (base->packetizer);
|
|
|
|
GST_WARNING_OBJECT (base, "Couldn't find any PCR within the first %d bytes",
|
|
|
|
10 * 65536);
|
|
|
|
return GST_FLOW_ERROR;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
mpegts_base_loop (MpegTSBase * base)
|
|
|
|
{
|
|
|
|
GstFlowReturn ret = GST_FLOW_ERROR;
|
2013-03-03 11:30:32 +00:00
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
switch (base->mode) {
|
|
|
|
case BASE_MODE_SCANNING:
|
|
|
|
/* Find first sync point */
|
|
|
|
ret = mpegts_base_scan (base);
|
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
goto error;
|
|
|
|
base->mode = BASE_MODE_STREAMING;
|
|
|
|
GST_DEBUG ("Changing to Streaming");
|
|
|
|
break;
|
|
|
|
case BASE_MODE_SEEKING:
|
2012-03-01 17:05:17 +00:00
|
|
|
/* FIXME : unclear if we still need mode_seeking... */
|
2011-02-16 16:57:42 +00:00
|
|
|
base->mode = BASE_MODE_STREAMING;
|
|
|
|
break;
|
|
|
|
case BASE_MODE_STREAMING:
|
|
|
|
{
|
2012-04-19 11:59:37 +00:00
|
|
|
GstBuffer *buf = NULL;
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
GST_DEBUG ("Pulling data from %" G_GUINT64_FORMAT, base->seek_offset);
|
|
|
|
|
|
|
|
ret = gst_pad_pull_range (base->sinkpad, base->seek_offset,
|
|
|
|
100 * base->packetsize, &buf);
|
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
goto error;
|
2012-04-19 12:20:52 +00:00
|
|
|
base->seek_offset += gst_buffer_get_size (buf);
|
|
|
|
ret = mpegts_base_chain (base->sinkpad, GST_OBJECT_CAST (base), buf);
|
2011-02-16 16:57:42 +00:00
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
2011-02-22 11:33:56 +00:00
|
|
|
case BASE_MODE_PUSHING:
|
|
|
|
GST_WARNING ("wrong BASE_MODE_PUSHING mode in pull loop");
|
|
|
|
break;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
error:
|
|
|
|
{
|
|
|
|
const gchar *reason = gst_flow_get_name (ret);
|
|
|
|
GST_DEBUG_OBJECT (base, "Pausing task, reason %s", reason);
|
2012-04-19 12:20:52 +00:00
|
|
|
if (ret == GST_FLOW_EOS) {
|
2013-05-23 12:57:49 +00:00
|
|
|
if (!GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base,
|
|
|
|
gst_event_new_eos ()))
|
|
|
|
GST_ELEMENT_ERROR (base, STREAM, FAILED,
|
|
|
|
(_("Internal data stream error.")),
|
|
|
|
("No program activated before EOS"));
|
2012-04-19 12:20:52 +00:00
|
|
|
} else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
|
2011-02-16 16:57:42 +00:00
|
|
|
GST_ELEMENT_ERROR (base, STREAM, FAILED,
|
|
|
|
(_("Internal data stream error.")),
|
|
|
|
("stream stopped, reason %s", reason));
|
|
|
|
GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, gst_event_new_eos ());
|
|
|
|
}
|
|
|
|
gst_pad_pause_task (base->sinkpad);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
mpegts_base_handle_seek_event (MpegTSBase * base, GstPad * pad,
|
|
|
|
GstEvent * event)
|
|
|
|
{
|
|
|
|
MpegTSBaseClass *klass = GST_MPEGTS_BASE_GET_CLASS (base);
|
|
|
|
GstFlowReturn ret = GST_FLOW_ERROR;
|
|
|
|
gdouble rate;
|
|
|
|
gboolean flush;
|
|
|
|
GstFormat format;
|
|
|
|
GstSeekFlags flags;
|
|
|
|
GstSeekType start_type, stop_type;
|
|
|
|
gint64 start, stop;
|
2013-09-10 21:51:52 +00:00
|
|
|
GstEvent *flush_event = NULL;
|
2011-02-22 11:33:56 +00:00
|
|
|
|
|
|
|
gst_event_parse_seek (event, &rate, &format, &flags, &start_type, &start,
|
|
|
|
&stop_type, &stop);
|
|
|
|
|
|
|
|
if (format != GST_FORMAT_TIME)
|
|
|
|
return FALSE;
|
|
|
|
|
2013-07-15 09:15:11 +00:00
|
|
|
if (GST_EVENT_SEQNUM (event) == base->last_seek_seqnum) {
|
|
|
|
GST_DEBUG_OBJECT (base, "Skipping already handled seek");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-04-30 09:05:20 +00:00
|
|
|
if (base->mode == BASE_MODE_PUSHING) {
|
|
|
|
/* First try if upstream supports seeking in TIME format */
|
|
|
|
if (gst_pad_push_event (base->sinkpad, gst_event_ref (event))) {
|
|
|
|
GST_DEBUG ("upstream handled SEEK event");
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-05-24 16:18:35 +00:00
|
|
|
|
|
|
|
/* If the subclass can seek, do that */
|
|
|
|
if (klass->seek) {
|
|
|
|
ret = klass->seek (base, event);
|
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
GST_WARNING ("seeking failed %s", gst_flow_get_name (ret));
|
|
|
|
else {
|
2013-09-10 21:51:52 +00:00
|
|
|
GstEvent *new_seek;
|
2013-05-24 16:18:35 +00:00
|
|
|
base->mode = BASE_MODE_SEEKING;
|
2013-09-10 21:51:52 +00:00
|
|
|
|
|
|
|
new_seek = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags,
|
|
|
|
GST_SEEK_TYPE_SET, base->seek_offset, GST_SEEK_TYPE_NONE, -1);
|
|
|
|
gst_event_set_seqnum (new_seek, GST_EVENT_SEQNUM (event));
|
|
|
|
if (!gst_pad_push_event (base->sinkpad, new_seek))
|
2013-05-24 16:18:35 +00:00
|
|
|
ret = GST_FLOW_ERROR;
|
2013-07-15 09:15:11 +00:00
|
|
|
else
|
|
|
|
base->last_seek_seqnum = GST_EVENT_SEQNUM (event);
|
2013-05-24 16:18:35 +00:00
|
|
|
base->mode = BASE_MODE_PUSHING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret == GST_FLOW_OK;
|
2011-08-23 07:52:20 +00:00
|
|
|
}
|
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
GST_DEBUG ("seek event, rate: %f start: %" GST_TIME_FORMAT
|
|
|
|
" stop: %" GST_TIME_FORMAT, rate, GST_TIME_ARGS (start),
|
|
|
|
GST_TIME_ARGS (stop));
|
|
|
|
|
|
|
|
flush = flags & GST_SEEK_FLAG_FLUSH;
|
|
|
|
|
|
|
|
/* stop streaming, either by flushing or by pausing the task */
|
|
|
|
base->mode = BASE_MODE_SEEKING;
|
|
|
|
if (flush) {
|
|
|
|
GST_DEBUG_OBJECT (base, "sending flush start");
|
2013-09-10 21:51:52 +00:00
|
|
|
flush_event = gst_event_new_flush_start ();
|
|
|
|
gst_event_set_seqnum (flush_event, GST_EVENT_SEQNUM (event));
|
|
|
|
gst_pad_push_event (base->sinkpad, gst_event_ref (flush_event));
|
|
|
|
GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, flush_event);
|
2011-02-22 11:33:56 +00:00
|
|
|
} else
|
|
|
|
gst_pad_pause_task (base->sinkpad);
|
2012-03-01 17:05:17 +00:00
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
/* wait for streaming to finish */
|
|
|
|
GST_PAD_STREAM_LOCK (base->sinkpad);
|
|
|
|
|
|
|
|
if (flush) {
|
|
|
|
/* send a FLUSH_STOP for the sinkpad, since we need data for seeking */
|
|
|
|
GST_DEBUG_OBJECT (base, "sending flush stop");
|
2013-09-10 21:51:52 +00:00
|
|
|
flush_event = gst_event_new_flush_stop (TRUE);
|
|
|
|
gst_event_set_seqnum (flush_event, GST_EVENT_SEQNUM (event));
|
|
|
|
|
|
|
|
/* ref for it to be reused later */
|
|
|
|
gst_pad_push_event (base->sinkpad, gst_event_ref (flush_event));
|
2013-05-24 08:59:59 +00:00
|
|
|
/* And actually flush our pending data but allow to preserve some info
|
|
|
|
* to perform the seek */
|
|
|
|
mpegts_base_flush (base, FALSE);
|
|
|
|
mpegts_packetizer_flush (base->packetizer, FALSE);
|
2011-02-22 11:33:56 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 19:28:04 +00:00
|
|
|
if (flags & (GST_SEEK_FLAG_SEGMENT)) {
|
2011-02-22 11:33:56 +00:00
|
|
|
GST_WARNING ("seek flags 0x%x are not supported", (int) flags);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-15 09:15:11 +00:00
|
|
|
/* If the subclass can seek, do that */
|
|
|
|
if (klass->seek) {
|
|
|
|
ret = klass->seek (base, event);
|
|
|
|
if (G_UNLIKELY (ret != GST_FLOW_OK))
|
|
|
|
GST_WARNING ("seeking failed %s", gst_flow_get_name (ret));
|
|
|
|
else
|
|
|
|
base->last_seek_seqnum = GST_EVENT_SEQNUM (event);
|
|
|
|
} else {
|
|
|
|
/* FIXME : Check this before so we don't do seeks we can't handle ? */
|
|
|
|
GST_WARNING ("subclass has no seek implementation");
|
2011-02-22 11:33:56 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 21:51:52 +00:00
|
|
|
if (flush_event) {
|
2011-02-22 11:33:56 +00:00
|
|
|
/* if we sent a FLUSH_START, we now send a FLUSH_STOP */
|
|
|
|
GST_DEBUG_OBJECT (base, "sending flush stop");
|
2013-09-10 21:51:52 +00:00
|
|
|
GST_MPEGTS_BASE_GET_CLASS (base)->push_event (base, flush_event);
|
|
|
|
flush_event = NULL;
|
2011-02-22 11:33:56 +00:00
|
|
|
}
|
|
|
|
done:
|
2013-09-10 21:51:52 +00:00
|
|
|
if (flush_event)
|
|
|
|
gst_event_unref (flush_event);
|
2012-06-20 08:34:48 +00:00
|
|
|
gst_pad_start_task (base->sinkpad, (GstTaskFunction) mpegts_base_loop, base,
|
|
|
|
NULL);
|
2013-05-24 16:18:35 +00:00
|
|
|
|
2011-02-22 11:33:56 +00:00
|
|
|
GST_PAD_STREAM_UNLOCK (base->sinkpad);
|
|
|
|
return ret == GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
static gboolean
|
2012-04-19 12:20:52 +00:00
|
|
|
mpegts_base_sink_activate (GstPad * sinkpad, GstObject * parent)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2012-04-19 12:20:52 +00:00
|
|
|
GstQuery *query;
|
|
|
|
gboolean pull_mode;
|
|
|
|
|
|
|
|
query = gst_query_new_scheduling ();
|
|
|
|
|
|
|
|
if (!gst_pad_peer_query (sinkpad, query)) {
|
|
|
|
gst_query_unref (query);
|
|
|
|
goto activate_push;
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
2012-09-11 15:47:16 +00:00
|
|
|
pull_mode = gst_query_has_scheduling_mode_with_flags (query,
|
|
|
|
GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
|
2012-04-19 12:20:52 +00:00
|
|
|
gst_query_unref (query);
|
|
|
|
|
|
|
|
if (!pull_mode)
|
|
|
|
goto activate_push;
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (sinkpad, "activating pull");
|
|
|
|
return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
|
|
|
|
|
|
|
|
activate_push:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT (sinkpad, "activating push");
|
|
|
|
return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2012-04-19 12:20:52 +00:00
|
|
|
mpegts_base_sink_activate_mode (GstPad * pad, GstObject * parent,
|
|
|
|
GstPadMode mode, gboolean active)
|
2011-02-16 16:57:42 +00:00
|
|
|
{
|
2012-04-19 12:20:52 +00:00
|
|
|
gboolean res;
|
|
|
|
MpegTSBase *base = GST_MPEGTS_BASE (parent);
|
2011-02-16 16:57:42 +00:00
|
|
|
|
2012-04-19 12:20:52 +00:00
|
|
|
switch (mode) {
|
|
|
|
case GST_PAD_MODE_PUSH:
|
|
|
|
base->mode = BASE_MODE_PUSHING;
|
|
|
|
res = TRUE;
|
|
|
|
break;
|
|
|
|
case GST_PAD_MODE_PULL:
|
|
|
|
if (active) {
|
|
|
|
base->mode = BASE_MODE_SCANNING;
|
2012-11-08 16:15:26 +00:00
|
|
|
/* When working pull-based, we always use offsets for estimation */
|
2012-04-19 12:20:52 +00:00
|
|
|
base->packetizer->calculate_offset = TRUE;
|
2012-11-08 16:15:26 +00:00
|
|
|
base->packetizer->calculate_skew = FALSE;
|
2013-06-17 06:15:54 +00:00
|
|
|
gst_segment_init (&base->segment, GST_FORMAT_BYTES);
|
2012-04-19 12:20:52 +00:00
|
|
|
res =
|
2012-06-20 08:34:48 +00:00
|
|
|
gst_pad_start_task (pad, (GstTaskFunction) mpegts_base_loop, base,
|
|
|
|
NULL);
|
2012-04-19 12:20:52 +00:00
|
|
|
} else
|
|
|
|
res = gst_pad_stop_task (pad);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
res = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2011-02-16 16:57:42 +00:00
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
mpegts_base_change_state (GstElement * element, GstStateChange transition)
|
|
|
|
{
|
|
|
|
MpegTSBase *base;
|
|
|
|
GstStateChangeReturn ret;
|
|
|
|
|
|
|
|
base = GST_MPEGTS_BASE (element);
|
2012-06-29 14:09:31 +00:00
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
|
|
|
mpegts_base_reset (base);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-02-16 16:57:42 +00:00
|
|
|
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
mpegts_base_reset (base);
|
2011-02-22 11:33:56 +00:00
|
|
|
if (base->mode != BASE_MODE_PUSHING)
|
|
|
|
base->mode = BASE_MODE_SCANNING;
|
2011-02-16 16:57:42 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
gst_mpegtsbase_plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
GST_DEBUG_CATEGORY_INIT (mpegts_base_debug, "mpegtsbase", 0,
|
|
|
|
"MPEG transport stream base class");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|