+/** @brief Flush the OSS output buffer
+ * @return 0 on success, non-0 on error
+ */
+static int playrtp_oss_flush(void) {
+ int nbyteswritten;
+
+ if(!playrtp_oss_bufused)
+ return 0; /* nothing to do */
+ /* 0 out the unused portion of the buffer */
+ memset(playrtp_oss_buffer + playrtp_oss_bufused, 0,
+ playrtp_oss_bufsize - playrtp_oss_bufused);
+#if EMPEG_HOST
+ /* empeg audio driver insists on native-endian samples */
+ {
+ uint16_t *ptr,
+ *const limit = (uint16_t *)(playrtp_oss_buffer + playrtp_oss_bufused);
+
+ for(ptr = (uint16_t *)playrtp_oss_buffer; ptr < limit; ++ptr)
+ *ptr = ntohs(*ptr);
+ }
+#endif
+ for(;;) {
+ nbyteswritten = write(playrtp_oss_fd,
+ playrtp_oss_buffer, playrtp_oss_bufsize);
+ if(nbyteswritten < 0) {
+ switch(errno) {
+ case EINTR:
+ break; /* try again */
+ case EAGAIN:
+ return 0; /* try later */
+ default:
+ error(errno, "error writing to %s", device);
+ return -1;
+ }
+ } else {
+ if(nbyteswritten < playrtp_oss_bufsize)
+ error(0, "%s: short write (%d/%d)",
+ device, nbyteswritten, playrtp_oss_bufsize);
+ if(dump_buffer) {
+ int count;
+ const int16_t *sp = (const int16_t *)playrtp_oss_buffer;
+
+ for(count = 0; count < playrtp_oss_bufsize; count += sizeof(int16_t)) {
+ dump_buffer[dump_index++] = (int16_t)ntohs(*sp++);
+ dump_index %= dump_size;
+ }
+ }
+ playrtp_oss_bufused = 0;
+ return 0;
+ }
+ }
+}
+