Skip to content

Commit

Permalink
Improve resending of frames (and avoid missed frames)
Browse files Browse the repository at this point in the history
- when requesting a packet, do not request an already received packet
- move the last-chance resend to buffer_get_frame
- check more often for missing frames
  own checks show that some frames need to be requested up to 4 times
  on bad connections
  • Loading branch information
Stef Simoens authored and toofishes committed Feb 24, 2012
1 parent 7fcfd3d commit d996438
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions hairtunes.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ void alac_decode(short *dest, char *buf, int len) {

void buffer_put_packet(seq_t seqno, char *data, int len) {
volatile abuf_t *abuf = 0;
short read;
short buf_fill;

pthread_mutex_lock(&ab_mutex);
Expand All @@ -367,7 +366,7 @@ void buffer_put_packet(seq_t seqno, char *data, int len) {
abuf = audio_buffer + BUFIDX(seqno);
ab_write = seqno;
} else if (seq_order(ab_write, seqno)) { // newer than expected
rtp_request_resend(ab_write, seqno-1);
rtp_request_resend(ab_write+1, seqno-1);
abuf = audio_buffer + BUFIDX(seqno);
ab_write = seqno;
} else if (seq_order(ab_read, seqno)) { // late but not yet played
Expand All @@ -387,15 +386,6 @@ void buffer_put_packet(seq_t seqno, char *data, int len) {
ab_buffering = 0;
pthread_cond_signal(&ab_buffer_ready);
}
if (!ab_buffering) {
// check if the t+10th packet has arrived... last-chance resend
read = ab_read + 10;
abuf = audio_buffer + BUFIDX(read);
if (abuf->ready != 1) {
rtp_request_resend(read, read);
abuf->ready = -1;
}
}
}

static int rtp_sockets[2]; // data, control
Expand Down Expand Up @@ -642,6 +632,9 @@ void bf_est_update(short fill) {
short *buffer_get_frame(void) {
short buf_fill;
seq_t read;
volatile abuf_t *abuf = 0;
unsigned short next;
int i;

pthread_mutex_lock(&ab_mutex);

Expand Down Expand Up @@ -670,8 +663,19 @@ short *buffer_get_frame(void) {
buf_fill = ab_write - ab_read;
bf_est_update(buf_fill);

// check if t+16, t+32, t+64, t+128, ... (START_FILL / 2) packets have arrived... last-chance resend
if (!ab_buffering) {
for (i = 16; i < (START_FILL / 2); i = (i * 2)) {
next = ab_read + i;
abuf = audio_buffer + BUFIDX(next);
if (!abuf->ready) {
rtp_request_resend(next, next);
}
}
}

volatile abuf_t *curframe = audio_buffer + BUFIDX(read);
if (curframe->ready != 1) {
if (!curframe->ready) {
fprintf(stderr, "\nmissing frame.\n");
memset(curframe->data, 0, FRAME_BYTES);
}
Expand Down

0 comments on commit d996438

Please sign in to comment.