tsdemux: Minor refactoring/code cleaning

... add some debugging symbols in the mean time.
This commit is contained in:
Thibault Saunier 2012-02-15 11:06:57 -03:00
parent e373df2a97
commit 6094ec27b1
3 changed files with 58 additions and 58 deletions

View file

@ -639,6 +639,7 @@ mpegts_base_activate_program (MpegTSBase * base, MpegTSBaseProgram * program,
/* activate new pmt */
if (program->pmt_info)
gst_structure_free (program->pmt_info);
program->pmt_info = gst_structure_copy (pmt_info);
program->pmt_pid = pmt_pid;
program->pcr_pid = pcr_pid;
@ -689,7 +690,7 @@ mpegts_base_is_psi (MpegTSBase * base, MpegTSPacketizerPacket * packet)
if (MPEGTS_BIT_IS_SET (base->known_psi, packet->pid))
retval = TRUE;
/* check is it is a pes pid */
/* check if it is a pes pid */
if (MPEGTS_BIT_IS_SET (base->is_pes, packet->pid))
return FALSE;
@ -839,7 +840,6 @@ mpegts_base_apply_pmt (MpegTSBase * base,
{
MpegTSBaseProgram *program, *old_program;
guint program_number;
gboolean deactivate_old_program = FALSE;
/* FIXME : not so sure this is valid anymore */
if (G_UNLIKELY (base->seen_pat == FALSE)) {
@ -870,19 +870,16 @@ mpegts_base_apply_pmt (MpegTSBase * base,
program = mpegts_base_new_program (base, program_number, pmt_pid);
g_hash_table_insert (base->programs,
GINT_TO_POINTER (program_number), program);
deactivate_old_program = TRUE;
/* Desactivate the old program */
mpegts_base_deactivate_program (base, old_program);
mpegts_base_free_program (old_program);
} else
program = old_program;
/* First activate program */
mpegts_base_activate_program (base, program, pmt_pid, pmt_info);
if (deactivate_old_program) {
/* deactivate old pmt */ ;
mpegts_base_deactivate_program (base, old_program);
mpegts_base_free_program (old_program);
}
/* if (program->pmt_info) */
/* gst_structure_free (program->pmt_info); */
/* program->pmt_info = NULL; */
@ -1266,8 +1263,7 @@ mpegts_base_chain (GstPad * pad, GstBuffer * buf)
}
mpegts_packetizer_push (base->packetizer, buf);
while (((pret =
mpegts_packetizer_next_packet (base->packetizer,
while (((pret = mpegts_packetizer_next_packet (base->packetizer,
&packet)) != PACKET_NEED_MORE) && res == GST_FLOW_OK) {
if (G_UNLIKELY (pret == PACKET_BAD))
/* bad header, skip the packet */
@ -1319,8 +1315,9 @@ mpegts_base_scan (MpegTSBase * base)
/* Find initial sync point */
for (i = 0; i < 10; i++) {
GST_DEBUG ("Grabbing %d => %d",
i * 50 * MPEGTS_MAX_PACKETSIZE, 50 * MPEGTS_MAX_PACKETSIZE);
GST_DEBUG ("Grabbing %d => %d", i * 50 * MPEGTS_MAX_PACKETSIZE,
50 * MPEGTS_MAX_PACKETSIZE);
ret = gst_pad_pull_range (base->sinkpad, i * 50 * MPEGTS_MAX_PACKETSIZE,
50 * MPEGTS_MAX_PACKETSIZE, &buf);
if (G_UNLIKELY (ret != GST_FLOW_OK))

View file

@ -78,6 +78,7 @@ static gchar *get_encoding_and_convert (const gchar * text, guint length);
#define MAX_CONTINUITY 15
#define VERSION_NUMBER_UNSET 255
#define TABLE_ID_UNSET 0xFF
#define PACKET_SYNC_BYTE 0x47
static gint
mpegts_packetizer_stream_subtable_compare (gconstpointer a, gconstpointer b)
@ -396,7 +397,7 @@ mpegts_packetizer_parse_descriptors (MpegTSPacketizer2 * packetizer,
/* include length */
desc = g_string_new_len ((gchar *) data - 2, length + 2);
data += length;
/* G_TYPE_GSTING is a GBoxed type and is used so properly marshalled from python */
/* G_TYPE_GSTRING is a GBoxed type and is used so properly marshalled from python */
g_value_init (&value, G_TYPE_GSTRING);
g_value_take_boxed (&value, desc);
g_value_array_append (descriptors, &value);
@ -521,6 +522,8 @@ mpegts_packetizer_parse_pmt (MpegTSPacketizer2 * packetizer,
program_number = GST_READ_UINT16_BE (data);
data += 2;
GST_DEBUG ("Parsing %d Program Map Table", program_number);
tmp = *data++;
section->version_number = (tmp >> 1) & 0x1F;
section->current_next_indicator = tmp & 0x01;
@ -565,6 +568,7 @@ mpegts_packetizer_parse_pmt (MpegTSPacketizer2 * packetizer,
* bytes) plus the CRC */
while (data <= end - 4 - 5) {
stream_type = *data++;
GST_DEBUG ("Stream type 0x%02x found", stream_type);
pid = GST_READ_UINT16_BE (data) & 0x1FFF;
data += 2;
@ -2159,13 +2163,14 @@ mpegts_try_discover_packet_size (MpegTSPacketizer2 * packetizer)
/* find first sync byte */
pos = -1;
for (i = 0; i < MPEGTS_MAX_PACKETSIZE; i++) {
if (dest[i] == 0x47) {
if (dest[i] == PACKET_SYNC_BYTE) {
for (j = 0; j < 4; j++) {
guint packetsize = psizes[j];
/* check each of the packet size possibilities in turn */
if (dest[i] == 0x47 && dest[i + packetsize] == 0x47 &&
dest[i + packetsize * 2] == 0x47 &&
dest[i + packetsize * 3] == 0x47) {
if (dest[i] == PACKET_SYNC_BYTE
&& dest[i + packetsize] == PACKET_SYNC_BYTE
&& dest[i + packetsize * 2] == PACKET_SYNC_BYTE
&& dest[i + packetsize * 3] == PACKET_SYNC_BYTE) {
packetizer->know_packet_size = TRUE;
packetizer->packet_size = packetsize;
packetizer->caps = gst_caps_new_simple ("video/mpegts",
@ -2200,10 +2205,10 @@ mpegts_try_discover_packet_size (MpegTSPacketizer2 * packetizer)
GST_DEBUG ("Flushing out %d bytes", pos);
gst_adapter_flush (packetizer->adapter, pos);
packetizer->offset += pos;
} else if (!packetizer->know_packet_size) {
/* drop invalid data and move to the next possible packets */
gst_adapter_flush (packetizer->adapter, MPEGTS_MAX_PACKETSIZE);
}
} else {
/* drop invalid data and move to the next possible packets */
GST_DEBUG ("Could not determine packet size");
}
return packetizer->know_packet_size;
@ -2235,6 +2240,7 @@ mpegts_packetizer_next_packet (MpegTSPacketizer2 * packetizer,
while ((avail = packetizer->adapter->size) >= packetizer->packet_size) {
packet->buffer = gst_adapter_take_buffer (packetizer->adapter,
packetizer->packet_size);
/* M2TS packets don't start with the sync byte, all other variants do */
if (packetizer->packet_size == MPEGTS_M2TS_PACKETSIZE) {
packet->data_start = GST_BUFFER_DATA (packet->buffer) + 4;
@ -2347,7 +2353,6 @@ mpegts_packetizer_push_section (MpegTSPacketizer2 * packetizer,
sub_buf = gst_buffer_create_sub (packet->buffer,
data - GST_BUFFER_DATA (packet->buffer), packet->data_end - data);
stream = packetizer->streams[packet->pid];
if (stream == NULL) {
stream = mpegts_packetizer_stream_new ();

View file

@ -56,7 +56,7 @@
#define TABLE_ID_UNSET 0xFF
/* Size of the pendingbuffers array. */
#define TS_MAX_PENDING_BUFFERS 256
#define TS_MAX_PENDING_BUFFERS 256
#define PCR_WRAP_SIZE_128KBPS (((gint64)1490)*(1024*1024))
/* small PCR for wrap detection */
@ -1363,10 +1363,10 @@ process_section (MpegTSBase * base)
MpegTSPacketizerPacket packet;
MpegTSPacketizerPacketReturn pret;
while ((!done)
&& ((pret =
mpegts_packetizer_next_packet (base->packetizer,
&packet)) != PACKET_NEED_MORE)) {
while ((!done) &&
((pret = mpegts_packetizer_next_packet (base->packetizer, &packet))
!= PACKET_NEED_MORE)) {
if (G_UNLIKELY (pret == PACKET_BAD))
/* bad header, skip the packet */
goto next;
@ -1375,8 +1375,9 @@ process_section (MpegTSBase * base)
if (packet.payload != NULL && mpegts_base_is_psi (base, &packet)) {
MpegTSPacketizerSection section;
based =
mpegts_packetizer_push_section (base->packetizer, &packet, &section);
based = mpegts_packetizer_push_section (base->packetizer, &packet,
&section);
if (G_UNLIKELY (!based))
/* bad section data */
goto next;
@ -1412,10 +1413,9 @@ process_pes (MpegTSBase * base, TSPcrOffset * pcroffset)
GstTSDemux *demux = GST_TS_DEMUX (base);
guint16 pcr_pid = 0;
while ((!done)
&& ((pret =
mpegts_packetizer_next_packet (base->packetizer,
&packet)) != PACKET_NEED_MORE)) {
while ((!done) &&
((pret = mpegts_packetizer_next_packet (base->packetizer, &packet))
!= PACKET_NEED_MORE)) {
if (G_UNLIKELY (pret == PACKET_BAD))
/* bad header, skip the packet */
goto next;
@ -1428,8 +1428,9 @@ process_pes (MpegTSBase * base, TSPcrOffset * pcroffset)
if (packet.payload != NULL && mpegts_base_is_psi (base, &packet)) {
MpegTSPacketizerSection section;
based =
mpegts_packetizer_push_section (base->packetizer, &packet, &section);
based = mpegts_packetizer_push_section (base->packetizer, &packet,
&section);
if (G_UNLIKELY (!based))
/* bad section data */
goto next;
@ -1587,16 +1588,15 @@ find_timestamps (MpegTSBase * base, guint64 initoff, guint64 * offset)
GST_DEBUG ("Scanning for timestamps");
/* Flush what remained from before */
mpegts_packetizer_clear (base->packetizer);
/* Start scanning from now PAT offset */
/* Start scanning from know PAT offset */
while (!done) {
ret =
gst_pad_pull_range (base->sinkpad, i * 50 * MPEGTS_MAX_PACKETSIZE,
ret = gst_pad_pull_range (base->sinkpad, i * 50 * MPEGTS_MAX_PACKETSIZE,
50 * MPEGTS_MAX_PACKETSIZE, &buf);
if (ret != GST_FLOW_OK)
goto beach;
mpegts_packetizer_push (base->packetizer, buf);
done = process_section (base);
i++;
@ -1605,7 +1605,6 @@ find_timestamps (MpegTSBase * base, guint64 initoff, guint64 * offset)
done = FALSE;
i = 1;
*offset = base->seek_offset;
/* Search for the first PCRs */
@ -1617,7 +1616,7 @@ find_timestamps (MpegTSBase * base, guint64 initoff, guint64 * offset)
}
mpegts_packetizer_clear (base->packetizer);
/* Remove current program so we ensure looking for a PAT when scanning the
/* Remove current program so we ensure looking for a PAT when scanning
* for the final PCR */
gst_structure_free (base->pat);
base->pat = NULL;
@ -1628,9 +1627,9 @@ find_timestamps (MpegTSBase * base, guint64 initoff, guint64 * offset)
if (G_UNLIKELY (!gst_pad_query_peer_duration (base->sinkpad, &format,
&total_bytes) || format != GST_FORMAT_BYTES)) {
GST_WARNING_OBJECT (base, "Couldn't get upstream size in bytes");
ret = GST_FLOW_ERROR;
mpegts_packetizer_clear (base->packetizer);
return ret;
return GST_FLOW_ERROR;
}
GST_DEBUG ("Upstream is %" G_GINT64_FORMAT " bytes", total_bytes);
@ -1640,10 +1639,11 @@ find_timestamps (MpegTSBase * base, guint64 initoff, guint64 * offset)
GST_DEBUG ("Scanning for last sync point between:%" G_GINT64_FORMAT
" and the end:%" G_GINT64_FORMAT, scan_offset, total_bytes);
while ((!done) && (scan_offset < total_bytes)) {
ret =
gst_pad_pull_range (base->sinkpad,
scan_offset, 50 * MPEGTS_MAX_PACKETSIZE, &buf);
ret = gst_pad_pull_range (base->sinkpad, scan_offset,
50 * MPEGTS_MAX_PACKETSIZE, &buf);
if (ret != GST_FLOW_OK)
goto beach;
@ -1654,10 +1654,8 @@ find_timestamps (MpegTSBase * base, guint64 initoff, guint64 * offset)
mpegts_packetizer_clear (base->packetizer);
GST_DEBUG ("Searching PCR");
ret =
process_pcr (base, scan_offset - 50 * MPEGTS_MAX_PACKETSIZE, &final, 10,
FALSE);
ret = process_pcr (base, scan_offset - 50 * MPEGTS_MAX_PACKETSIZE, &final,
10, FALSE);
if (ret != GST_FLOW_OK) {
GST_DEBUG ("Problem getting last PCRs");
@ -1669,6 +1667,7 @@ find_timestamps (MpegTSBase * base, guint64 initoff, guint64 * offset)
gst_segment_set_duration (&demux->segment, GST_FORMAT_TIME,
demux->last_pcr.gsttime - demux->first_pcr.gsttime);
demux->duration = demux->last_pcr.gsttime - demux->first_pcr.gsttime;
GST_DEBUG ("Done, duration:%" GST_TIME_FORMAT,
GST_TIME_ARGS (demux->duration));
@ -1726,8 +1725,7 @@ process_pcr (MpegTSBase * base, guint64 initoff, TSPcrOffset * pcroffset,
for (i = 0; (i < 20) && (nbpcr < numpcr); i++) {
guint offset, size;
ret =
gst_pad_pull_range (base->sinkpad,
ret = gst_pad_pull_range (base->sinkpad,
initoff + i * 500 * base->packetsize, 500 * base->packetsize, &buf);
if (G_UNLIKELY (ret != GST_FLOW_OK))