Module Name:    src
Committed By:   nat
Date:           Sun Feb 12 08:25:32 UTC 2017

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

Log Message:
RTP timestamp is now set correctly.  The streaming socket is set to non
block blocking and connection state machinre improvement.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/usr.sbin/bta2dpd/bta2dpd/bta2dpd.c \
    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/bta2dpd.c
diff -u src/usr.sbin/bta2dpd/bta2dpd/bta2dpd.c:1.1 src/usr.sbin/bta2dpd/bta2dpd/bta2dpd.c:1.2
--- src/usr.sbin/bta2dpd/bta2dpd/bta2dpd.c:1.1	Sat Jan 28 16:55:54 2017
+++ src/usr.sbin/bta2dpd/bta2dpd/bta2dpd.c	Sun Feb 12 08:25:31 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: bta2dpd.c,v 1.1 2017/01/28 16:55:54 nat Exp $ */
+/* $NetBSD: bta2dpd.c,v 1.2 2017/02/12 08:25:31 nat Exp $ */
 
 /*-
  * Copyright (c) 2015 - 2016 Nathanial Sloss <nathanialsl...@yahoo.com.au>
@@ -615,7 +615,7 @@ do_ctlreq(int fd, short ev, void *arg)
 	static uint8_t trans;
 	uint8_t buff[1024];
 	static size_t offset = 0;
-	int result;
+	int result, flags;
 
 	if(avdtpCheckResponse(fd, &isCommand, &trans, &signal, NULL, buff,
 	    &bufflen, &sep) == ENOMEM) {
@@ -674,6 +674,7 @@ do_ctlreq(int fd, short ev, void *arg)
 		case AVDTP_SUSPEND:
 		case AVDTP_START:
 			avdtpSendAccept(fd, fd, trans, signal);
+			state = 6;
 			break;
 		default:
 			avdtpSendReject(fd, fd, trans, signal);
@@ -703,17 +704,20 @@ do_ctlreq(int fd, short ev, void *arg)
 
 			if (!result && verbose)
 				fprintf(stderr, "Bitpool value = %d\n",bitpool);
+			state = 3;
 			break;
 		case AVDTP_SET_CONFIGURATION:
-			if (state < 5 && !asSpeaker)
+			if (state == 3 && !asSpeaker)
 				avdtpOpen(fd, fd, mySepInfo.sep);
+			state = 4;
 			break;
 		case AVDTP_OPEN:
-			if (state < 5)
+			if (state == 4)
 				state = 5;
 			break;
 		case AVDTP_SUSPEND:
 		case AVDTP_START:
+			state = 6;
 			break;
 		default:
 			avdtpSendReject(fd, fd, trans, signal);
@@ -723,7 +727,7 @@ do_ctlreq(int fd, short ev, void *arg)
 	}
 
 
-	if (state != 5)
+	if (state < 5 || state > 7)
 		return;
 
 	if (asSpeaker) {
@@ -731,8 +735,9 @@ do_ctlreq(int fd, short ev, void *arg)
 		if ((sc = accept(orighc,(struct sockaddr*)&addr, &len)) < 0)
 			err(EXIT_FAILURE, "stream accept");
 
-		goto opened_connection;
 	}
+	if (state == 6)
+		goto opened_connection;
 
 	memset(&addr, 0, sizeof(addr));
 
@@ -758,31 +763,34 @@ do_ctlreq(int fd, short ev, void *arg)
 	if (connect(sc,(struct sockaddr*)&addr, sizeof(addr)) < 0)
 		return;
 
+	if (!asSpeaker)
+		avdtpStart(fd, fd, mySepInfo.sep); 
+
+	return;
+
 opened_connection:
 	if (asSpeaker) {
 		event_set(&recv_ev, sc, EV_READ | EV_PERSIST, do_recv, NULL);
 		if (event_add(&recv_ev, NULL) < 0)
 			err(EXIT_FAILURE, "recv_ev");
+		state = 7;
 	} else {
+		flags = fcntl(sc, F_GETFL, 0);
+		fcntl(sc, F_SETFL, flags | O_NONBLOCK);
 		event_set(&interrupt_ev, audfile, EV_READ | EV_PERSIST,
 		    do_interrupt, NULL);
 		if (event_add(&interrupt_ev, NULL) < 0)
 			err(EXIT_FAILURE, "interrupt_ev");
+		state = 7;
 	}
 
-	mtusize = sizeof(uint16_t);
 	getsockopt(sc, BTPROTO_L2CAP, SO_L2CAP_OMTU, &mtu, &mtusize);
-
 	if (userset_mtu != 0 && userset_mtu > 100 && userset_mtu < mtu)
 		mtu = userset_mtu;
 	else if (userset_mtu == 0 && mtu >= 500)
 		mtu /= 2;
 
-	if (!asSpeaker)
-		avdtpStart(fd, fd, mySepInfo.sep); 
-
-	state = 6;
-
+	mtusize = sizeof(uint16_t);
 }
 
 static void
Index: src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c
diff -u src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c:1.1 src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c:1.2
--- src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c:1.1	Sat Jan 28 16:55:54 2017
+++ src/usr.sbin/bta2dpd/bta2dpd/sbc_encode.c	Sun Feb 12 08:25:31 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: sbc_encode.c,v 1.1 2017/01/28 16:55:54 nat Exp $ */
+/* $NetBSD: sbc_encode.c,v 1.2 2017/02/12 08:25:31 nat Exp $ */
 
 /*-
  * Copyright (c) 2015 - 2016 Nathanial Sloss <nathanialsl...@yahoo.com.au>
@@ -822,9 +822,10 @@ stream(int in, int outfd, uint8_t mode, 
 	ssize_t len, mySize[16], offset, next_pkt;
 	ssize_t pkt_len;
 	size_t readsize, totalSize;
+	size_t frequency;
 	static size_t ts = 0;
 	static uint16_t seqnumber = 0;
-	int numpkts;
+	int numpkts, tries;
 
 	global_mode = mode;
 	global_bitpool = bitpool;
@@ -855,6 +856,15 @@ stream(int in, int outfd, uint8_t mode, 
 	if (global_mode == MODE_MONO)
 		global_chan = 1;
 
+	if (global_freq == FREQ_16K)
+		frequency = 16000;
+	else if (global_freq == FREQ_32K)
+		frequency = 32000;
+	else if (global_freq == FREQ_48K)
+		frequency = 48000;
+	else
+		frequency = 44100;
+
 	memset(&myHeader, 0, sizeof(myHeader));
 	myHeader.id = 0x80;	/* RTP v2 */
 	myHeader.id2 = 0x60; 	/* payload type 96. */
@@ -911,16 +921,31 @@ stream(int in, int outfd, uint8_t mode, 
 	memcpy(whole + offset, frameData, (size_t)next_pkt);
 	free(frameData);
 
+	tries = 1;
 send_again:
 	len = write(outfd, whole, totalSize);
 
-	if (len == -1 && errno == EAGAIN)
-		goto send_again;
-	free(whole);
+	if (len == -1 && errno == EAGAIN) {
+		tries --;
+		if (tries >= 0) {
+			usleep(1);
+			goto send_again;
+		} else
+			len = (ssize_t)totalSize;
+	} else if (len == -1 && (errno == EINPROGRESS ||
+	    errno == EWOULDBLOCK)) {
+		usleep(1);
+			len = (ssize_t)totalSize;
+	}
 
 	seqnumber++;
-	ts += (readsize / ((size_t)global_chan * sizeof(int16_t)))  *
-	    (size_t)numpkts;
+	ts += (1000000 * (size_t)(global_blocks * global_bands)
+	    / frequency) * (size_t)numpkts;
+
+	free(whole);
+
+	if (seqnumber % 96 == 95)
+		usleep(1);
 
 	return len;
 }

Reply via email to