diff --git a/src/postprocessing/janus-pp-rec.c b/src/postprocessing/janus-pp-rec.c index 70d408c22c..29a1f1b40f 100644 --- a/src/postprocessing/janus-pp-rec.c +++ b/src/postprocessing/janus-pp-rec.c @@ -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); diff --git a/src/postprocessing/pp-opus.c b/src/postprocessing/pp-opus.c index 56ef4e6c40..2e761d1522 100644 --- a/src/postprocessing/pp-opus.c +++ b/src/postprocessing/pp-opus.c @@ -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); @@ -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; inext != 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; } @@ -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)); } } @@ -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 diff --git a/src/postprocessing/pp-rtp.h b/src/postprocessing/pp-rtp.h index 6880592b02..cdd5c37aca 100644 --- a/src/postprocessing/pp-rtp.h +++ b/src/postprocessing/pp-rtp.h @@ -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;