+static size_t playrtp_callback(void *buffer,
+ size_t max_samples,
+ void attribute((unused)) *userdata) {
+ size_t samples;
+
+ pthread_mutex_lock(&lock);
+ /* Get the next packet, junking any that are now in the past */
+ const struct packet *p = playrtp_next_packet();
+ if(p && contains(p, next_timestamp)) {
+ /* This packet is ready to play; the desired next timestamp points
+ * somewhere into it. */
+
+ /* Timestamp of end of packet */
+ const uint32_t packet_end = p->timestamp + p->nsamples;
+
+ /* Offset of desired next timestamp into current packet */
+ const uint32_t offset = next_timestamp - p->timestamp;
+
+ /* Pointer to audio data */
+ const uint16_t *ptr = (void *)(p->samples_raw + offset);
+
+ /* Compute number of samples left in packet, limited to output buffer
+ * size */
+ samples = packet_end - next_timestamp;
+ if(samples > max_samples)
+ samples = max_samples;
+
+ /* Copy into buffer, converting to native endianness */
+ size_t i = samples;
+ int16_t *bufptr = buffer;
+ while(i > 0) {
+ *bufptr++ = (int16_t)ntohs(*ptr++);
+ --i;
+ }
+ /* We don't junk the packet here; a subsequent call to
+ * playrtp_next_packet() will dispose of it (if it's actually done with). */
+ } else {
+ /* There is no suitable packet. We introduce 0s up to the next packet, or
+ * to fill the buffer if there's no next packet or that's too many. The
+ * comparison with max_samples deals with the otherwise troubling overflow
+ * case. */
+ samples = p ? p->timestamp - next_timestamp : max_samples;
+ if(samples > max_samples)
+ samples = max_samples;
+ //info("infill by %zu", samples);
+ memset(buffer, 0, samples * uaudio_sample_size);
+ }
+ /* Debug dump */
+ if(dump_buffer) {
+ for(size_t i = 0; i < samples; ++i) {
+ dump_buffer[dump_index++] = ((int16_t *)buffer)[i];
+ dump_index %= dump_size;
+ }
+ }
+ /* Advance timestamp */
+ next_timestamp += samples;
+ pthread_mutex_unlock(&lock);
+ return samples;
+}
+