+ if(control_socket) {
+ pthread_t tid;
+
+ if((err = pthread_create(&tid, 0, control_thread, 0)))
+ fatal(err, "pthread_create control_thread");
+ }
+ if(dumpfile) {
+ int fd;
+ unsigned char buffer[65536];
+ size_t written;
+
+ if((fd = open(dumpfile, O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0)
+ fatal(errno, "opening %s", dumpfile);
+ /* Fill with 0s to a suitable size */
+ memset(buffer, 0, sizeof buffer);
+ for(written = 0; written < dump_size * sizeof(int16_t);
+ written += sizeof buffer) {
+ if(write(fd, buffer, sizeof buffer) < 0)
+ fatal(errno, "clearing %s", dumpfile);
+ }
+ /* Map the buffer into memory for convenience */
+ dump_buffer = mmap(0, dump_size * sizeof(int16_t), PROT_READ|PROT_WRITE,
+ MAP_SHARED, fd, 0);
+ if(dump_buffer == (void *)-1)
+ fatal(errno, "mapping %s", dumpfile);
+ info("dumping to %s", dumpfile);
+ }
+ /* Set up output. Currently we only support L16 so there's no harm setting
+ * the format before we know what it is! */
+ uaudio_set_format(44100/*Hz*/, 2/*channels*/,
+ 16/*bits/channel*/, 1/*signed*/);
+ backend->start(playrtp_callback, NULL);
+ /* We receive and convert audio data in a background thread */
+ if((err = pthread_create(<id, 0, listen_thread, 0)))
+ fatal(err, "pthread_create listen_thread");
+ /* We have a second thread to add received packets to the queue */
+ if((err = pthread_create(<id, 0, queue_thread, 0)))
+ fatal(err, "pthread_create queue_thread");
+ pthread_mutex_lock(&lock);
+ time_t lastlog = 0;
+ for(;;) {
+ /* Wait for the buffer to fill up a bit */
+ playrtp_fill_buffer();
+ /* Start playing now */
+ info("Playing...");
+ next_timestamp = pheap_first(&packets)->timestamp;
+ active = 1;
+ pthread_mutex_unlock(&lock);
+ backend->activate();
+ pthread_mutex_lock(&lock);
+ /* Wait until the buffer empties out
+ *
+ * If there's a packet that we can play right now then we definitely
+ * continue.
+ *
+ * Also if there's at least minbuffer samples we carry on regardless and
+ * insert silence. The assumption is there's been a pause but more data
+ * is now available.
+ */
+ while(nsamples >= minbuffer
+ || (nsamples > 0
+ && contains(pheap_first(&packets), next_timestamp))) {
+ if(monitor) {
+ time_t now = time(0);
+
+ if(now >= lastlog + 60) {
+ int offset = nsamples - minbuffer;
+ double offtime = (double)offset / (uaudio_rate * uaudio_channels);
+ info("%+d samples off (%d.%02ds, %d bytes)",
+ offset,
+ (int)fabs(offtime) * (offtime < 0 ? -1 : 1),
+ (int)(fabs(offtime) * 100) % 100,
+ offset * uaudio_bits / CHAR_BIT);
+ lastlog = now;
+ }
+ }
+ //fprintf(stderr, "%8u/%u (%u) PLAYING\n", nsamples, maxbuffer, minbuffer);
+ pthread_cond_wait(&cond, &lock);
+ }
+#if 0
+ if(nsamples) {
+ struct packet *p = pheap_first(&packets);
+ fprintf(stderr, "nsamples=%u (%u) next_timestamp=%"PRIx32", first packet is [%"PRIx32",%"PRIx32")\n",
+ nsamples, minbuffer, next_timestamp,p->timestamp,p->timestamp+p->nsamples);
+ }
+#endif
+ /* Stop playing for a bit until the buffer re-fills */
+ pthread_mutex_unlock(&lock);
+ backend->deactivate();
+ pthread_mutex_lock(&lock);
+ active = 0;
+ /* Go back round */
+ }