[v12,04/13] bluetooth: Implement reading SO_TIMESTAMP for A2DP source

Submitted by Pali Rohár on July 5, 2019, 1:02 p.m.

Details

Message ID 20190705130226.5496-5-pali.rohar@gmail.com
State Superseded
Headers show
Series "New API for Bluetooth A2DP codecs" ( rev: 98 97 96 95 94 93 92 91 90 89 88 87 ) in PulseAudio

Not browsing as part of any series.

Commit Message

Pali Rohár July 5, 2019, 1:02 p.m.
---
 src/modules/bluetooth/module-bluez5-device.c | 33 ++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/modules/bluetooth/module-bluez5-device.c b/src/modules/bluetooth/module-bluez5-device.c
index 0b63cd44a..efb092089 100644
--- a/src/modules/bluetooth/module-bluez5-device.c
+++ b/src/modules/bluetooth/module-bluez5-device.c
@@ -544,13 +544,29 @@  static int a2dp_process_push(struct userdata *u) {
     a2dp_prepare_decoder_buffer(u);
 
     for (;;) {
+        uint8_t aux[1024];
+        struct iovec iov;
+        struct cmsghdr *cm;
+        struct msghdr m;
         bool found_tstamp = false;
         pa_usec_t tstamp;
         uint8_t *ptr;
         ssize_t l;
         size_t processed;
 
-        l = pa_read(u->stream_fd, u->decoder_buffer, u->decoder_buffer_size, &u->stream_write_type);
+        pa_zero(m);
+        pa_zero(aux);
+        pa_zero(iov);
+
+        m.msg_iov = &iov;
+        m.msg_iovlen = 1;
+        m.msg_control = aux;
+        m.msg_controllen = sizeof(aux);
+
+        iov.iov_base = u->decoder_buffer;
+        iov.iov_len = u->decoder_buffer_size;
+
+        l = recvmsg(u->stream_fd, &m, 0);
 
         if (l <= 0) {
 
@@ -570,8 +586,21 @@  static int a2dp_process_push(struct userdata *u) {
         pa_assert((size_t) l <= u->decoder_buffer_size);
 
         /* TODO: get timestamp from rtp */
+
+        for (cm = CMSG_FIRSTHDR(&m); cm; cm = CMSG_NXTHDR(&m, cm)) {
+            if (cm->cmsg_level == SOL_SOCKET && cm->cmsg_type == SO_TIMESTAMP) {
+                struct timeval *tv = (struct timeval*) CMSG_DATA(cm);
+                pa_rtclock_from_wallclock(tv);
+                tstamp = pa_timeval_load(tv);
+                found_tstamp = true;
+                break;
+            }
+        }
+
         if (!found_tstamp) {
-            /* pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!"); */
+            PA_ONCE_BEGIN {
+                pa_log_warn("Couldn't find SO_TIMESTAMP data in auxiliary recvmsg() data!");
+            } PA_ONCE_END;
             tstamp = pa_rtclock_now();
         }