Skip to content

Commit

Permalink
janus-pp-rec: improve DTX detection (#3488)
Browse files Browse the repository at this point in the history
  • Loading branch information
atoppi authored Jan 17, 2025
1 parent bbe648d commit 3cdd6ff
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/postprocessing/janus-pp-rec.c
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,7 @@ int main(int argc, char *argv[]) {

/* Update current packet ts with new ts */
tmp->ts = new_ts;
tmp->restamped = 1;

JANUS_LOG(LOG_WARN, "Timestamp gap detected. Restamping packets from here. Seq: %d\n", tmp->seq);
JANUS_LOG(LOG_INFO, "latency=%.2f mavg=%.2f original_ts=%.ld new_ts=%.ld offset=%.ld\n", current_latency, moving_avg_latency, original_ts, tmp->ts, restamping_offset);
Expand Down
37 changes: 27 additions & 10 deletions src/postprocessing/pp-opus.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int janus_pp_opus_process(FILE *file, janus_pp_frame_packet *list, gboolean rest
janus_pp_frame_packet *tmp = list;
long int offset = 0;
int bytes = 0, len = 0, last_seq = 0;
uint64_t pos = 0, nextPos = 0;
uint64_t pos = 0;
double ts = 0.0;
uint8_t *buffer = g_malloc0(1500);

Expand Down Expand Up @@ -251,21 +251,37 @@ int janus_pp_opus_process(FILE *file, janus_pp_frame_packet *list, gboolean rest
AVRational timebase = {1, 48000};

while(*working && tmp != NULL) {
/* if restamping is being used, do not evaluate the sequence number jump */
if(tmp->prev != NULL && ((tmp->ts - tmp->prev->ts)/48/20 > 1) && (restamping || (tmp->seq != tmp->prev->seq+1))) {
JANUS_LOG(LOG_WARN, "Lost a packet here? (got seq %"SCNu16" after %"SCNu16", time ~%"SCNu64"s)\n",
tmp->seq, tmp->prev->seq, (tmp->ts-list->ts)/48000);
if(tmp->prev != NULL && ((tmp->ts - tmp->prev->ts)/48 > 20)) {
int silence_count = 0;
if(tmp->seq != tmp->prev->seq+1) {
/* Packet Lost */
JANUS_LOG(LOG_WARN, "Lost a packet here? (got seq %"SCNu16" after %"SCNu16", time ~%"SCNu64"s)\n",
tmp->seq, tmp->prev->seq, (tmp->ts-list->ts)/48000);
/* insert 20ms silence packets before the current packet */
silence_count = (tmp->ts - tmp->prev->ts)/48/20 - 1;
} else if(restamping && tmp->restamped == 1) {
/* Packet restamped due to RTP clock issues */
JANUS_LOG(LOG_WARN, "Restamped packet detected (got seq %"SCNu16" after %"SCNu16", time ~%"SCNu64"s)\n",
tmp->seq, tmp->prev->seq, (tmp->ts-list->ts)/48000);
/* insert 20ms silence packets before the current packet */
silence_count = (tmp->ts - tmp->prev->ts)/48/20 - 1;
} else {
/* plen > 20 ms, DTX ?*/
JANUS_LOG(LOG_WARN, "DTX packet detected (got seq %"SCNu16" after %"SCNu16", time ~%"SCNu64"s)\n",
tmp->seq, tmp->prev->seq, (tmp->ts-list->ts)/48000);
/* insert 20ms silence packets for the whole DTX duration */
silence_count = (tmp->ts - tmp->prev->ts)/48/20;
/* drop this packet since it's DTX silence */
tmp->drop = 1;
}
/* use ts differ to insert silence packet */
int silence_count = (tmp->ts - tmp->prev->ts)/48/20 - 1;
pos = (tmp->prev->ts - list->ts) / 48 / 20 + 1;
JANUS_LOG(LOG_WARN, "[FILL] pos: %06"SCNu64", writing silences (count=%d)\n", pos, silence_count);
int i=0;
pos = tmp->prev->ts - list->ts;
for(i=0; i<silence_count; i++) {
pos += OPUS_PACKET_DURATION;
if(tmp->next != NULL)
nextPos = tmp->next->ts - list->ts;
if(pos >= nextPos) {
if(tmp->next != NULL && pos >= (tmp->next->ts - list->ts)) {
JANUS_LOG(LOG_WARN, "[SKIP] pos: %06" SCNu64 ", skipping remaining silence\n", pos / 48 / 20 + 1);
break;
}
Expand All @@ -280,7 +296,7 @@ int janus_pp_opus_process(FILE *file, janus_pp_frame_packet *list, gboolean rest

int res = av_write_frame(fctx, pkt);
if(res < 0) {
JANUS_LOG(LOG_ERR, "Error writing video frame to file... (error %d, %s)\n",
JANUS_LOG(LOG_ERR, "Error writing audio frame to file... (error %d, %s)\n",
res, av_err2str(res));
}
}
Expand Down Expand Up @@ -321,6 +337,7 @@ int janus_pp_opus_process(FILE *file, janus_pp_frame_packet *list, gboolean rest
if(tmp->seq < last_seq) {
last_seq = tmp->seq;
}
pos = tmp->prev != NULL ? ((tmp->prev->ts - list->ts) / 48 / 20 + 1) : 0;
JANUS_LOG(LOG_VERB, "pos: %06"SCNu64", writing %d bytes out of %d (seq=%"SCNu16", step=%"SCNu16", ts=%"SCNu64", time=%"SCNu64"s)\n",
pos, bytes, tmp->len, tmp->seq, diff, tmp->ts, (tmp->ts-list->ts)/48000);
#ifdef FF_API_INIT_PACKET
Expand Down
23 changes: 12 additions & 11 deletions src/postprocessing/pp-rtp.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ typedef struct janus_pp_rtp_header_extension {

typedef struct janus_pp_frame_packet {
janus_pp_rtp_header *header; /* Pointer to RTP header */
int version; /* Version of the .mjr file (2=has timestamps) */
uint32_t p_ts; /* Packet timestamp as saved by Janus (if available) */
uint16_t seq; /* RTP Sequence number */
uint64_t ts; /* RTP Timestamp */
uint16_t len; /* Length of the data */
int pt; /* Payload type of the data */
long offset; /* Offset of the data in the file */
int skip; /* Bytes to skip, besides the RTP header */
uint8_t drop; /* Whether this packet can be dropped (e.g., padding)*/
int audiolevel; /* Value of audio level in RTP extension, if parsed */
int rotation; /* Value of rotation in RTP extension, if parsed */
int version; /* Version of the .mjr file (2=has timestamps) */
uint32_t p_ts; /* Packet timestamp as saved by Janus (if available) */
uint16_t seq; /* RTP Sequence number */
uint64_t ts; /* RTP Timestamp */
uint16_t len; /* Length of the data */
int pt; /* Payload type of the data */
long offset; /* Offset of the data in the file */
int skip; /* Bytes to skip, besides the RTP header */
uint8_t drop; /* Whether this packet can be dropped (e.g., padding)*/
uint8_t restamped; /* Whether this packet has been restamped */
int audiolevel; /* Value of audio level in RTP extension, if parsed */
int rotation; /* Value of rotation in RTP extension, if parsed */
struct janus_pp_frame_packet *next;
struct janus_pp_frame_packet *prev;
} janus_pp_frame_packet;
Expand Down

0 comments on commit 3cdd6ff

Please sign in to comment.