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 */ /* activate new pmt */
if (program->pmt_info) if (program->pmt_info)
gst_structure_free (program->pmt_info); gst_structure_free (program->pmt_info);
program->pmt_info = gst_structure_copy (pmt_info); program->pmt_info = gst_structure_copy (pmt_info);
program->pmt_pid = pmt_pid; program->pmt_pid = pmt_pid;
program->pcr_pid = pcr_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)) if (MPEGTS_BIT_IS_SET (base->known_psi, packet->pid))
retval = TRUE; 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)) if (MPEGTS_BIT_IS_SET (base->is_pes, packet->pid))
return FALSE; return FALSE;
@ -839,7 +840,6 @@ mpegts_base_apply_pmt (MpegTSBase * base,
{ {
MpegTSBaseProgram *program, *old_program; MpegTSBaseProgram *program, *old_program;
guint program_number; guint program_number;
gboolean deactivate_old_program = FALSE;
/* FIXME : not so sure this is valid anymore */ /* FIXME : not so sure this is valid anymore */
if (G_UNLIKELY (base->seen_pat == FALSE)) { 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); program = mpegts_base_new_program (base, program_number, pmt_pid);
g_hash_table_insert (base->programs, g_hash_table_insert (base->programs,
GINT_TO_POINTER (program_number), program); 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 } else
program = old_program; program = old_program;
/* First activate program */ /* First activate program */
mpegts_base_activate_program (base, program, pmt_pid, pmt_info); 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) */ /* if (program->pmt_info) */
/* gst_structure_free (program->pmt_info); */ /* gst_structure_free (program->pmt_info); */
/* program->pmt_info = NULL; */ /* program->pmt_info = NULL; */
@ -1266,8 +1263,7 @@ mpegts_base_chain (GstPad * pad, GstBuffer * buf)
} }
mpegts_packetizer_push (base->packetizer, buf); mpegts_packetizer_push (base->packetizer, buf);
while (((pret = while (((pret = mpegts_packetizer_next_packet (base->packetizer,
mpegts_packetizer_next_packet (base->packetizer,
&packet)) != PACKET_NEED_MORE) && res == GST_FLOW_OK) { &packet)) != PACKET_NEED_MORE) && res == GST_FLOW_OK) {
if (G_UNLIKELY (pret == PACKET_BAD)) if (G_UNLIKELY (pret == PACKET_BAD))
/* bad header, skip the packet */ /* bad header, skip the packet */
@ -1319,8 +1315,9 @@ mpegts_base_scan (MpegTSBase * base)
/* Find initial sync point */ /* Find initial sync point */
for (i = 0; i < 10; i++) { for (i = 0; i < 10; i++) {
GST_DEBUG ("Grabbing %d => %d", GST_DEBUG ("Grabbing %d => %d", i * 50 * MPEGTS_MAX_PACKETSIZE,
i * 50 * MPEGTS_MAX_PACKETSIZE, 50 * MPEGTS_MAX_PACKETSIZE); 50 * MPEGTS_MAX_PACKETSIZE);
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); 50 * MPEGTS_MAX_PACKETSIZE, &buf);
if (G_UNLIKELY (ret != GST_FLOW_OK)) 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 MAX_CONTINUITY 15
#define VERSION_NUMBER_UNSET 255 #define VERSION_NUMBER_UNSET 255
#define TABLE_ID_UNSET 0xFF #define TABLE_ID_UNSET 0xFF
#define PACKET_SYNC_BYTE 0x47
static gint static gint
mpegts_packetizer_stream_subtable_compare (gconstpointer a, gconstpointer b) mpegts_packetizer_stream_subtable_compare (gconstpointer a, gconstpointer b)
@ -396,7 +397,7 @@ mpegts_packetizer_parse_descriptors (MpegTSPacketizer2 * packetizer,
/* include length */ /* include length */
desc = g_string_new_len ((gchar *) data - 2, length + 2); desc = g_string_new_len ((gchar *) data - 2, length + 2);
data += length; 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_init (&value, G_TYPE_GSTRING);
g_value_take_boxed (&value, desc); g_value_take_boxed (&value, desc);
g_value_array_append (descriptors, &value); g_value_array_append (descriptors, &value);
@ -521,6 +522,8 @@ mpegts_packetizer_parse_pmt (MpegTSPacketizer2 * packetizer,
program_number = GST_READ_UINT16_BE (data); program_number = GST_READ_UINT16_BE (data);
data += 2; data += 2;
GST_DEBUG ("Parsing %d Program Map Table", program_number);
tmp = *data++; tmp = *data++;
section->version_number = (tmp >> 1) & 0x1F; section->version_number = (tmp >> 1) & 0x1F;
section->current_next_indicator = tmp & 0x01; section->current_next_indicator = tmp & 0x01;
@ -565,6 +568,7 @@ mpegts_packetizer_parse_pmt (MpegTSPacketizer2 * packetizer,
* bytes) plus the CRC */ * bytes) plus the CRC */
while (data <= end - 4 - 5) { while (data <= end - 4 - 5) {
stream_type = *data++; stream_type = *data++;
GST_DEBUG ("Stream type 0x%02x found", stream_type);
pid = GST_READ_UINT16_BE (data) & 0x1FFF; pid = GST_READ_UINT16_BE (data) & 0x1FFF;
data += 2; data += 2;
@ -2159,13 +2163,14 @@ mpegts_try_discover_packet_size (MpegTSPacketizer2 * packetizer)
/* find first sync byte */ /* find first sync byte */
pos = -1; pos = -1;
for (i = 0; i < MPEGTS_MAX_PACKETSIZE; i++) { for (i = 0; i < MPEGTS_MAX_PACKETSIZE; i++) {
if (dest[i] == 0x47) { if (dest[i] == PACKET_SYNC_BYTE) {
for (j = 0; j < 4; j++) { for (j = 0; j < 4; j++) {
guint packetsize = psizes[j]; guint packetsize = psizes[j];
/* check each of the packet size possibilities in turn */ /* check each of the packet size possibilities in turn */
if (dest[i] == 0x47 && dest[i + packetsize] == 0x47 && if (dest[i] == PACKET_SYNC_BYTE
dest[i + packetsize * 2] == 0x47 && && dest[i + packetsize] == PACKET_SYNC_BYTE
dest[i + packetsize * 3] == 0x47) { && dest[i + packetsize * 2] == PACKET_SYNC_BYTE
&& dest[i + packetsize * 3] == PACKET_SYNC_BYTE) {
packetizer->know_packet_size = TRUE; packetizer->know_packet_size = TRUE;
packetizer->packet_size = packetsize; packetizer->packet_size = packetsize;
packetizer->caps = gst_caps_new_simple ("video/mpegts", 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_DEBUG ("Flushing out %d bytes", pos);
gst_adapter_flush (packetizer->adapter, pos); gst_adapter_flush (packetizer->adapter, pos);
packetizer->offset += 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; return packetizer->know_packet_size;
@ -2235,6 +2240,7 @@ mpegts_packetizer_next_packet (MpegTSPacketizer2 * packetizer,
while ((avail = packetizer->adapter->size) >= packetizer->packet_size) { while ((avail = packetizer->adapter->size) >= packetizer->packet_size) {
packet->buffer = gst_adapter_take_buffer (packetizer->adapter, packet->buffer = gst_adapter_take_buffer (packetizer->adapter,
packetizer->packet_size); packetizer->packet_size);
/* M2TS packets don't start with the sync byte, all other variants do */ /* M2TS packets don't start with the sync byte, all other variants do */
if (packetizer->packet_size == MPEGTS_M2TS_PACKETSIZE) { if (packetizer->packet_size == MPEGTS_M2TS_PACKETSIZE) {
packet->data_start = GST_BUFFER_DATA (packet->buffer) + 4; 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, sub_buf = gst_buffer_create_sub (packet->buffer,
data - GST_BUFFER_DATA (packet->buffer), packet->data_end - data); data - GST_BUFFER_DATA (packet->buffer), packet->data_end - data);
stream = packetizer->streams[packet->pid]; stream = packetizer->streams[packet->pid];
if (stream == NULL) { if (stream == NULL) {
stream = mpegts_packetizer_stream_new (); stream = mpegts_packetizer_stream_new ();

View File

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