Module Name:    src
Committed By:   martin
Date:           Mon Jul 24 17:12:49 UTC 2023

Modified Files:
        src/usr.sbin/bta2dpd/bta2dpd [netbsd-10]: sbc_encode.c

Log Message:
Pull up following revision(s) (requested by nat in ticket #240):

        usr.sbin/bta2dpd/bta2dpd/sbc_encode.c: revision 1.12
        usr.sbin/bta2dpd/bta2dpd/sbc_encode.c: revision 1.13

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.

Reorder for readability.
No functional change intended.


To generate a diff of this commit:
cvs rdiff -u -r1.10.8.1 -r1.10.8.2 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.10.8.1 src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c:1.10.8.2
--- src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c:1.10.8.1	Sun May 28 10:34:59 2023
+++ src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c	Mon Jul 24 17:12:49 2023
@@ -1,4 +1,4 @@
-/* $NetBSD: sbc_encode.c,v 1.10.8.1 2023/05/28 10:34:59 martin Exp $ */
+/* $NetBSD: sbc_encode.c,v 1.10.8.2 2023/07/24 17:12:49 martin 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)
@@ -945,6 +952,19 @@ stream(int in, int outfd, uint8_t mode, 
 	memcpy(whole + offset, frameData, (size_t)next_pkt);
 	free(frameData);
 
+	/* 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;
+
 	tries = 1;
 send_again:
 	len = write(outfd, whole, totalSize);

Reply via email to