Module Name:    src
Committed By:   nat
Date:           Thu Jul 20 12:33:28 UTC 2023

Modified Files:
        src/usr.sbin/bta2dpd/bta2dpd: sbc_encode.c

Log Message:
Add thottling when playing from file.

This avoids rapid playback when playing from file with affected devices.

Playback using pad(4) is still preferred ad gives a better result.
Playback from pad(4) is unaffected by this change.

XXX pullup-10.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c
diff -u src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c:1.11 src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c:1.12
--- src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c:1.11	Sun May 28 07:59:17 2023
+++ src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c	Thu Jul 20 12:33:27 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: sbc_encode.c,v 1.11 2023/05/28 07:59:17 mlelstv Exp $ */
+/* $NetBSD: sbc_encode.c,v 1.12 2023/07/20 12:33:27 nat Exp $ */
 
 /*-
  * Copyright (c) 2015 - 2016 Nathanial Sloss <nathanialsl...@yahoo.com.au>
@@ -32,6 +32,7 @@
  */
 
 #include <sys/cdefs.h>
+#include <sys/time.h>
 #include <sys/types.h>
 #include <sys/param.h>
 #include <errno.h>
@@ -841,6 +842,7 @@ stream(int in, int outfd, uint8_t mode, 
     blocks, uint8_t alloc_method, uint8_t bitpool, size_t mtu, int volume)
 {
 	struct rtpHeader myHeader;
+	struct timeval myTime;
 	uint8_t *whole, *frameData;
 	int16_t music[2048];
 	ssize_t len, mySize[16], offset, next_pkt;
@@ -849,6 +851,7 @@ stream(int in, int outfd, uint8_t mode, 
 	size_t frequency;
 	static size_t ts = 0;
 	static uint16_t seqnumber = 0;
+	static time_t prevTime, readTime, sleepTime, timeNow;
 	int numpkts, tries;
 
 	global_mode = mode;
@@ -912,9 +915,11 @@ stream(int in, int outfd, uint8_t mode, 
 	next_pkt = 0;
 	len = 0;
 	pkt_len = 80;
+	readTime = 0;
 	while (totalSize + ((size_t)pkt_len * 2) <= mtu) {
 
 		len = readloop(in, music, readsize);
+		readTime += (time_t)readsize;
 		if (len < (int)readsize)
 			break;
 
@@ -934,6 +939,8 @@ stream(int in, int outfd, uint8_t mode, 
 		return -1;
 	}
 
+	readTime = readTime * 1000000 / 2 / global_chan / (time_t)frequency;
+
 	myHeader.numFrames = (uint8_t)numpkts;
 	whole = malloc(totalSize);
 	if (whole == NULL)
@@ -946,6 +953,20 @@ stream(int in, int outfd, uint8_t mode, 
 	free(frameData);
 
 	tries = 1;
+
+	/* Wait if necessary to avoid rapid playback. */
+	gettimeofday(&myTime, NULL);
+	timeNow = myTime.tv_sec * 1000000 + myTime.tv_usec;
+	if (prevTime == 0)
+		prevTime = timeNow;
+	else
+		sleepTime += readTime - (timeNow - prevTime);
+	if (sleepTime >= 1000) {
+		usleep(500);
+		sleepTime -= 1000;
+	}
+	prevTime = timeNow;
+
 send_again:
 	len = write(outfd, whole, totalSize);
 

Reply via email to