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); if (!ab_synced) { ab_write = seqno; ab_read = seqno-1; ab_synced = 1; } if (seqno == ab_write+1) { // expected packet 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); abuf = audio_buffer + BUFIDX(seqno); ab_write = seqno; } else if (seq_order(ab_read, seqno)) { // late but not yet played abuf = audio_buffer + BUFIDX(seqno); } else { // too late. fprintf(stderr, "\nlate packet %04X (%04X:%04X)\n", seqno, ab_read, ab_write); } buf_fill = ab_write - ab_read; pthread_mutex_unlock(&ab_mutex); if (abuf) { alac_decode(abuf->data, data, len); abuf->ready = 1; } if (ab_buffering && buf_fill >= buffer_start_fill) { 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; } } }
void player_put_packet(seq_t seqno, uint8_t *data, int len) { abuf_t *abuf = 0; int16_t buf_fill; pthread_mutex_lock(&ab_mutex); if (!ab_synced) { debug(1, "syncing to first seqno %04X\n", seqno); ab_write = seqno-1; ab_read = seqno; ab_synced = 1; } if (seq_diff(ab_write, seqno) == 1) { // expected packet abuf = audio_buffer + BUFIDX(seqno); ab_write = seqno; } else if (seq_order(ab_write, seqno)) { // newer than expected // Be careful with new packets that are in advance // Those more than a buffer size in advance will cause an Overrun // When buffering the valid threshold should be the buffer fill target itself which should re-sync if (ab_buffering && (seq_diff(ab_read, seqno) > config.buffer_start_fill)) { warn("out of range re-sync %04X (%04X:%04X)", seqno, ab_read, ab_write); ab_resync(); ab_synced = 1; ab_read = seqno; ab_write = seqno; abuf = audio_buffer + BUFIDX(seqno); } else { debug(1, "advance packet %04X (%04X:%04X)\n", seqno, ab_read, ab_write); 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 abuf = audio_buffer + BUFIDX(seqno); if (abuf->ready) { // discard this frame if frame previously received abuf =0; debug(1, "duplicate packet %04X (%04X:%04X)\n", seqno, ab_read, ab_write); } } else { // too late. debug(1, "late packet %04X (%04X:%04X)\n", seqno, ab_read, ab_write); } buf_fill = seq_diff(ab_read, ab_write); pthread_mutex_unlock(&ab_mutex); if (abuf) { alac_decode(abuf->data, data, len); abuf->ready = 1; } pthread_mutex_lock(&ab_mutex); if (ab_buffering && buf_fill >= config.buffer_start_fill) { debug(1, "buffering over. starting play (%04X:%04X)\n", ab_read, ab_write); ab_buffering = 0; } pthread_mutex_unlock(&ab_mutex); }
// get the next frame, when available. return 0 if underrun/stream reset. static short *buffer_get_frame(void) { short buf_fill; seq_t read; abuf_t *abuf = 0; unsigned short next; int i; pthread_mutex_lock(&ab_mutex); buf_fill = ab_write - ab_read; if (buf_fill < 1 || !ab_synced || ab_buffering) { // init or underrun. stop and wait if (ab_synced) fprintf(stderr, "\nunderrun.\n"); ab_buffering = 1; pthread_cond_wait(&ab_buffer_ready, &ab_mutex); ab_read++; buf_fill = ab_write - ab_read; bf_est_reset(buf_fill); pthread_mutex_unlock(&ab_mutex); return 0; } if (buf_fill >= BUFFER_FRAMES) { // overrunning! uh-oh. restart at a sane distance fprintf(stderr, "\noverrun.\n"); ab_read = ab_write - buffer_start_fill; } read = ab_read; ab_read++; 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); } } } abuf_t *curframe = audio_buffer + BUFIDX(read); if (!curframe->ready) { fprintf(stderr, "\nmissing frame.\n"); memset(curframe->data, 0, FRAME_BYTES); } curframe->ready = 0; pthread_mutex_unlock(&ab_mutex); return curframe->data; }
// get the next frame, when available. return 0 if underrun/stream reset. static short *buffer_get_frame(void) { int16_t buf_fill; seq_t read, next; abuf_t *abuf = 0; int i; if (ab_buffering) return 0; pthread_mutex_lock(&ab_mutex); buf_fill = seq_diff(ab_read, ab_write); if (buf_fill < 1 || !ab_synced) { if (buf_fill < 1) warn("underrun %i (%04X:%04X)", buf_fill, ab_read, ab_write); ab_resync(); pthread_mutex_unlock(&ab_mutex); return 0; } if (buf_fill >= BUFFER_FRAMES) { // overrunning! uh-oh. restart at a sane distance warn("overrun %i (%04X:%04X)", buf_fill, ab_read, ab_write); read = ab_read; ab_read = ab_write - config.buffer_start_fill; ab_reset((ab_write + 1 - BUFFER_FRAMES), ab_read); // reset any ready frames in those we've skipped (avoiding wrap around) } read = ab_read; ab_read++; buf_fill = seq_diff(ab_read, ab_write); bf_est_update(buf_fill); // check if t+16, t+32, t+64, t+128, ... resend focus boundary // packets have arrived... last-chance resend if (!ab_buffering) { for (i = 16; i <= resend_focus(config.buffer_start_fill); i = (i * 2)) { next = ab_read + i; abuf = audio_buffer + BUFIDX(next); if ((!abuf->ready) && (next < ab_write)){ rtp_request_resend(next, next); debug(1, "last chance resend T+%i, %04X (%04X:%04X)\n", i, next, ab_read, ab_write); } } } abuf_t *curframe = audio_buffer + BUFIDX(read); if (!curframe->ready) { debug(1, "missing frame %04X\n", read); memset(curframe->data, 0, FRAME_BYTES(frame_size)); } curframe->ready = 0; pthread_mutex_unlock(&ab_mutex); return curframe->data; }
// get the next frame, when available. return 0 if underrun/stream reset. static short *buffer_get_frame(void) { int16_t buf_fill; seq_t read, next; abuf_t *abuf = 0; int i; if (ab_buffering) return 0; pthread_mutex_lock(&ab_mutex); buf_fill = seq_diff(ab_read, ab_write); if (buf_fill < 1 || !ab_synced) { if (buf_fill < 1) warn("underrun."); ab_buffering = 1; pthread_mutex_unlock(&ab_mutex); return 0; } if (buf_fill >= BUFFER_FRAMES) { // overrunning! uh-oh. restart at a sane distance warn("overrun."); ab_read = ab_write - config.buffer_start_fill; } read = ab_read; ab_read++; buf_fill = seq_diff(ab_read, ab_write); bf_est_update(buf_fill); // check if t+16, t+32, t+64, t+128, ... (buffer_start_fill / 2) // packets have arrived... last-chance resend if (!ab_buffering) { for (i = 16; i < (config.buffer_start_fill / 2); i = (i * 2)) { next = ab_read + i; abuf = audio_buffer + BUFIDX(next); if (!abuf->ready) { rtp_request_resend(next, next); } } } abuf_t *curframe = audio_buffer + BUFIDX(read); if (!curframe->ready) { debug(1, "missing frame %04X.", read); memset(curframe->data, 0, FRAME_BYTES(frame_size)); } curframe->ready = 0; pthread_mutex_unlock(&ab_mutex); return curframe->data; }
// get the next frame, when available. return 0 if underrun/stream reset. static short *buffer_get_frame(void) { short buf_fill; seq_t read; abuf_t *abuf = 0; pthread_mutex_lock(&ab_mutex); buf_fill = ab_write - ab_read; if (buf_fill < 1 || !ab_synced || ab_buffering) { // init or underrun. stop and wait if (ab_synced) fprintf(stderr, "\nunderrun.\n"); ab_buffering = 1; pthread_cond_wait(&ab_buffer_ready, &ab_mutex); ab_read++; buf_fill = ab_write - ab_read; bf_est_reset(buf_fill); pthread_mutex_unlock(&ab_mutex); return 0; } if (buf_fill >= BUFFER_FRAMES) { // overrunning! uh-oh. restart at a sane distance fprintf(stderr, "\noverrun.\n"); ab_read = ab_write - buffer_start_fill; } read = ab_read; ab_read++; buf_fill = ab_write - ab_read; bf_est_update(buf_fill); if (!ab_buffering) { // check if the t+5th packet has arrived... last-chance resend read = ab_read + 5; abuf = audio_buffer + BUFIDX(read); if (!abuf->ready) { rtp_request_resend(read, read); abuf->ready = -1; } } abuf_t *curframe = audio_buffer + BUFIDX(read); if (!curframe->ready) { fprintf(stderr, "\nmissing frame.\n"); memset(curframe->data, 0, FRAME_BYTES); } curframe->ready = 0; pthread_mutex_unlock(&ab_mutex); return curframe->data; }
void player_put_packet(seq_t seqno, uint8_t *data, int len) { abuf_t *abuf = 0; int16_t buf_fill; pthread_mutex_lock(&ab_mutex); if (!ab_synced) { debug(2, "syncing to first seqno %04X\n", seqno); ab_write = seqno-1; ab_read = seqno; ab_synced = 1; } if (seq_diff(ab_write, seqno) == 1) { // expected packet abuf = audio_buffer + BUFIDX(seqno); ab_write = seqno; } else if (seq_order(ab_write, seqno)) { // newer than expected 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 abuf = audio_buffer + BUFIDX(seqno); } else { // too late. debug(1, "late packet %04X (%04X:%04X)", seqno, ab_read, ab_write); } buf_fill = seq_diff(ab_read, ab_write); pthread_mutex_unlock(&ab_mutex); if (abuf) { alac_decode(abuf->data, data, len); abuf->ready = 1; } pthread_mutex_lock(&ab_mutex); if (ab_buffering && buf_fill >= config.buffer_start_fill) { debug(1, "buffering over. starting play\n"); ab_buffering = 0; bf_est_reset(buf_fill); } pthread_mutex_unlock(&ab_mutex); }
void player_put_packet(seq_t seqno, uint32_t timestamp, uint8_t *data, int len) { pthread_mutex_lock(&ab_mutex); packet_count++; time_of_last_audio_packet = get_absolute_time_in_fp(); if (connection_state_to_output) { // if we are supposed to be processing these packets if ((flush_rtp_timestamp != 0) && ((timestamp == flush_rtp_timestamp) || seq32_order(timestamp, flush_rtp_timestamp))) { debug(2, "Dropping flushed packet in player_put_packet, seqno %u, timestamp %u, flushing to " "timestamp: %u.", seqno, timestamp, flush_rtp_timestamp); } else { if ((flush_rtp_timestamp != 0x0) && (!seq32_order(timestamp, flush_rtp_timestamp))) // if we have gone past the flush boundary time flush_rtp_timestamp = 0x0; abuf_t *abuf = 0; if (!ab_synced) { debug(2, "syncing to seqno %u.", seqno); ab_write = seqno; ab_read = seqno; ab_synced = 1; } if (ab_write == seqno) { // expected packet abuf = audio_buffer + BUFIDX(seqno); ab_write = SUCCESSOR(seqno); } else if (seq_order(ab_write, seqno)) { // newer than expected // if (ORDINATE(seqno)>(BUFFER_FRAMES*7)/8) // debug(1,"An interval of %u frames has opened, with ab_read: %u, ab_write: %u and seqno: // %u.",seq_diff(ab_read,seqno),ab_read,ab_write,seqno); int32_t gap = seq_diff(ab_write, PREDECESSOR(seqno)) + 1; if (gap <= 0) debug(1, "Unexpected gap size: %d.", gap); int i; for (i = 0; i < gap; i++) { abuf = audio_buffer + BUFIDX(seq_sum(ab_write, i)); abuf->ready = 0; // to be sure, to be sure abuf->timestamp = 0; abuf->sequence_number = 0; } // debug(1,"N %d s %u.",seq_diff(ab_write,PREDECESSOR(seqno))+1,ab_write); abuf = audio_buffer + BUFIDX(seqno); rtp_request_resend(ab_write, gap); resend_requests++; ab_write = SUCCESSOR(seqno); } else if (seq_order(ab_read, seqno)) { // late but not yet played late_packets++; abuf = audio_buffer + BUFIDX(seqno); } else { // too late. too_late_packets++; /* if (!late_packet_message_sent) { debug(1, "too-late packet received: %u; ab_read: %u; ab_write: %u.", seqno, ab_read, ab_write); late_packet_message_sent=1; } */ } // pthread_mutex_unlock(&ab_mutex); if (abuf) { alac_decode(abuf->data, data, len); abuf->ready = 1; abuf->timestamp = timestamp; abuf->sequence_number = seqno; } // pthread_mutex_lock(&ab_mutex); } int rc = pthread_cond_signal(&flowcontrol); if (rc) debug(1, "Error signalling flowcontrol."); } pthread_mutex_unlock(&ab_mutex); }
static inline void buffer_put_packet(seq_t seqno, char *data, int len) { abuf_t *abuf = 0; abuf_t *tmpbuf = 0; unsigned short buf_fill; pthread_mutex_lock(&ab_mutex); /* sync is required. */ if (!ab_synced) { ab_write = seqno; if (seqno == 0) { ab_read = 0; } else { ab_read = seqno-1; } ab_synced = 1; } /* Check seq_no is what we want. */ buf_fill=BUF_FILL_SIZE(seqno,ab_read); if (buf_fill > 1000) { pthread_mutex_unlock(&ab_mutex); return; } /* Check buf slot is occupied. */ tmpbuf = audio_buffer + BUFIDX(seqno); // buffering fits here! if (tmpbuf->ready) { pthread_mutex_unlock(&ab_mutex); return; } /* if seqno moves faster than expected. */ if (seq_order(ab_write, seqno)) { // late but not yet played rtp_request_resend(ab_write+1, seqno-1); } /* update ab_write */ ab_write = seqno; abuf = audio_buffer + BUFIDX(seqno); // buffering fits here! /* Make sure the slot of abuf is empty! */ if (!abuf->ready) { memcpy(abuf->data, data, len); abuf->len=len; abuf->ready = 1; } buf_fill=BUF_FILL_SIZE(ab_write,ab_read); #ifdef DEBUG fprintf(stderr,"seqno=%d ab_write=%d ab_read=%d buf_fill=%d ab_buffering=%d \n",seqno,ab_write,ab_read,buf_fill,ab_buffering); #endif if (ab_buffering && (buf_fill >= (buffer_start_fill-1))) { ab_buffering = 0; pthread_cond_signal(&ab_buffer_ready); } if (buf_fill >= (BUFFER_FRAMES-1)) { // overrunning! #ifdef DEBUG fprintf(stderr,"overrunning.\n"); #endif pthread_cond_wait(&ab_buffer_full, &ab_mutex); buf_fill=BUF_FILL_SIZE(ab_write,ab_read); } pthread_mutex_unlock(&ab_mutex); }