Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
parsers, tsfix: improve the packet log, merge common code
  • Loading branch information
perexg committed Aug 14, 2016
1 parent 258c913 commit 4a9268e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 49 deletions.
47 changes: 47 additions & 0 deletions src/packet.c
Expand Up @@ -143,6 +143,41 @@ pkt_ref_inc_poly(th_pkt_t *pkt, int n)
atomic_add(&pkt->pkt_refcount, n);
}

/**
*
*/
void
pkt_trace_(const char *file, int line, const char *subsys, th_pkt_t *pkt,
int index, streaming_component_type_t type, const char *fmt, ...)
{
char buf[512], _dts[22], _pts[22], _type[2];
va_list args;

va_start(args, fmt);
if (pkt->pkt_frametype) {
_type[0] = pkt_frametype_to_char(pkt->pkt_frametype);
_type[1] = '\0';
} else {
_type[0] = '\0';
}
snprintf(buf, sizeof(buf),
"%s%spkt stream %d %s%s%s"
" dts %s pts %s"
" dur %d len %zu err %i%s",
fmt ? fmt : "",
fmt ? " (" : "",
index,
streaming_component_type2txt(type),
_type[0] ? " type " : "", _type,
pts_to_string(pkt->pkt_dts, _dts),
pts_to_string(pkt->pkt_pts, _pts),
pkt->pkt_duration,
pktbuf_len(pkt->pkt_payload),
pkt->pkt_err,
fmt ? ")" : "");
tvhlogv(file, line, 0, LOG_TRACE, subsys, buf, &args);
va_end(args);
}

/**
*
Expand Down Expand Up @@ -327,3 +362,15 @@ pktbuf_append(pktbuf_t *pb, const void *data, size_t size)
}
return pb;
}

/*
*
*/

const char *pts_to_string(int64_t pts, char *buf)
{
if (pts == PTS_UNSET)
return strcpy(buf, "<unset>");
snprintf(buf, 22, "%"PRId64, pts);
return buf;
}
16 changes: 16 additions & 0 deletions src/packet.h
Expand Up @@ -119,6 +119,16 @@ th_pkt_t *pkt_copy_nodata(th_pkt_t *pkt);

th_pktref_t *pktref_create(th_pkt_t *pkt);

void pkt_trace_
(const char *file, int line, const char *subsys, th_pkt_t *pkt,
int index, streaming_component_type_t type, const char *fmt, ...);

#define pkt_trace(subsys, pkt, index, type, fmt, ...) \
do { \
if (tvhtrace_enabled()) \
pkt_trace_(__FILE__, __LINE__, subsys, pkt, index, type, fmt, ##__VA_ARGS__); \
} while (0)

/*
*
*/
Expand All @@ -138,6 +148,10 @@ pktbuf_t *pktbuf_append(pktbuf_t *pb, const void *data, size_t size);
static inline size_t pktbuf_len(pktbuf_t *pb) { return pb ? pb->pb_size : 0; }
static inline uint8_t *pktbuf_ptr(pktbuf_t *pb) { return pb->pb_data; }

/*
*
*/

static inline int64_t pts_diff(int64_t a, int64_t b)
{
if (a == PTS_UNSET || b == PTS_UNSET)
Expand All @@ -163,4 +177,6 @@ static inline int pts_is_greater_or_equal(int64_t base, int64_t value)
return 0;
}

const char *pts_to_string(int64_t pts, char *buf);

#endif /* PACKET_H_ */
42 changes: 10 additions & 32 deletions src/parsers/parsers.c
Expand Up @@ -1822,20 +1822,7 @@ parser_deliver(service_t *t, elementary_stream_t *st, th_pkt_t *pkt)
pkt->pkt_pts < t->s_current_pts - 180000))
t->s_current_pts = pkt->pkt_pts;

tvhtrace("parser",
"pkt stream %2d %-12s type %c"
" dts %10"PRId64" (%10"PRId64") pts %10"PRId64" (%10"PRId64")"
" dur %10d len %10zu err %i",
st->es_index,
streaming_component_type2txt(st->es_type),
pkt_frametype_to_char(pkt->pkt_frametype),
ts_rescale(pkt->pkt_dts, 1000000),
pkt->pkt_dts,
ts_rescale(pkt->pkt_pts, 1000000),
pkt->pkt_pts,
pkt->pkt_duration,
pktbuf_len(pkt->pkt_payload),
pkt->pkt_err);
pkt_trace("parser", pkt, st->es_index, st->es_type, "deliver");

pkt->pkt_aspect_num = st->es_aspect_num;
pkt->pkt_aspect_den = st->es_aspect_den;
Expand Down Expand Up @@ -1885,24 +1872,15 @@ parser_backlog(service_t *t, elementary_stream_t *st, th_pkt_t *pkt)
pkt_ref_dec(pkt); /* streaming_msg_create_pkt increses ref counter */

#if ENABLE_TRACE
int64_t dts = pts_no_backlog(pkt->pkt_dts);
int64_t pts = pts_no_backlog(pkt->pkt_pts);
tvhtrace("parser",
"pkt bcklog %2d %-12s type %c"
" dts%s%10"PRId64" (%10"PRId64") pts%s%10"PRId64" (%10"PRId64")"
" dur %10d len %10zu err %i",
st->es_index,
streaming_component_type2txt(st->es_type),
pkt_frametype_to_char(pkt->pkt_frametype),
pts_is_backlog(pkt->pkt_dts) ? "+" : " ",
ts_rescale(dts, 1000000),
dts,
pts_is_backlog(pkt->pkt_pts) ? "+" : " ",
ts_rescale(pts, 1000000),
pts,
pkt->pkt_duration,
pktbuf_len(pkt->pkt_payload),
pkt->pkt_err);
if (tvhtrace_enabled()) {
int64_t dts = pkt->pkt_dts;
int64_t pts = pkt->pkt_pts;
pkt->pkt_dts = pts_no_backlog(dts);
pkt->pkt_pts = pts_no_backlog(pts);
pkt_trace("parser", pkt, st->es_index, st->es_type, "backlog");
pkt->pkt_dts = dts;
pkt->pkt_pts = pts;
}
#endif
}

Expand Down
30 changes: 13 additions & 17 deletions src/plumbing/tsfix.c
Expand Up @@ -191,14 +191,7 @@ tsfix_stop(tsfix_t *tf)
static void
tsfix_packet_drop(tfstream_t *tfs, th_pkt_t *pkt, const char *reason)
{
tvhtrace("tsfix", "DROP: %-12s %c %10"PRId64" %10"PRId64" %10d %zd (%s)",
streaming_component_type2txt(tfs->tfs_type),
pkt_frametype_to_char(pkt->pkt_frametype),
pkt->pkt_dts,
pkt->pkt_pts,
pkt->pkt_duration,
pktbuf_len(pkt->pkt_payload),
reason);
pkt_trace("tsfix", pkt, tfs->tfs_index, tfs->tfs_type, "drop");
pkt_ref_dec(pkt);
}

Expand All @@ -208,7 +201,7 @@ tsfix_packet_drop(tfstream_t *tfs, th_pkt_t *pkt, const char *reason)
static void
normalize_ts(tsfix_t *tf, tfstream_t *tfs, th_pkt_t *pkt, int backlog)
{
int64_t ref, dts, d;
int64_t ref, dts, odts, opts, d;

if(tf->tf_tsref == PTS_UNSET) {
if (backlog) {
Expand All @@ -220,6 +213,10 @@ normalize_ts(tsfix_t *tf, tfstream_t *tfs, th_pkt_t *pkt, int backlog)
return;
}

ref = tfs->tfs_local_ref != PTS_UNSET ? tfs->tfs_local_ref : tf->tf_tsref;
odts = pkt->pkt_dts;
opts = pkt->pkt_pts;

if (pkt->pkt_dts == PTS_UNSET) {
if (pkt->pkt_pts != PTS_UNSET)
pkt->pkt_dts = pkt->pkt_pts;
Expand All @@ -230,7 +227,6 @@ normalize_ts(tsfix_t *tf, tfstream_t *tfs, th_pkt_t *pkt, int backlog)
pkt->pkt_dts &= PTS_MASK;

/* Subtract the transport wide start offset */
ref = tfs->tfs_local_ref != PTS_UNSET ? tfs->tfs_local_ref : tf->tf_tsref;
dts = pkt->pkt_dts - ref;

if (tfs->tfs_last_dts_norm == PTS_UNSET) {
Expand Down Expand Up @@ -289,13 +285,13 @@ normalize_ts(tsfix_t *tf, tfstream_t *tfs, th_pkt_t *pkt, int backlog)
pkt->pkt_dts = dts;

deliver:
tvhtrace("tsfix", "%-12s %c %10"PRId64" %10"PRId64" %10d %zd",
streaming_component_type2txt(tfs->tfs_type),
pkt_frametype_to_char(pkt->pkt_frametype),
pkt->pkt_dts,
pkt->pkt_pts,
pkt->pkt_duration,
pktbuf_len(pkt->pkt_payload));
if (tvhtrace_enabled()) {
char _odts[22], _opts[22];
pkt_trace("tsfix", pkt, tfs->tfs_index, tfs->tfs_type,
"deliver odts %s opts %s ref %"PRId64" epoch %"PRId64,
pts_to_string(odts, _odts), pts_to_string(opts, _opts),
ref, tfs->tfs_dts_epoch);
}

streaming_message_t *sm = streaming_msg_create_pkt(pkt);
streaming_target_deliver2(tf->tf_output, sm);
Expand Down

0 comments on commit 4a9268e

Please sign in to comment.