bluetooth: unify encoder code paths

Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/507>
This commit is contained in:
Igor V. Kovalenko 2021-03-03 08:12:08 +03:00 committed by PulseAudio Marge Bot
parent 8a87af380a
commit 3902cee4a5
9 changed files with 330 additions and 183 deletions

View file

@ -334,6 +334,52 @@ static void sco_release_cb(pa_bluetooth_transport *t) {
/* device will close the SCO socket for us */
}
static ssize_t sco_transport_write(pa_bluetooth_transport *t, int fd, const void* buffer, size_t size, size_t write_mtu) {
ssize_t l = 0;
size_t written = 0;
size_t write_size;
pa_assert(t);
/* if encoder buffer has less data than required to make complete packet */
if (size < write_mtu)
return 0;
/* write out MTU sized chunks only */
while (written < size) {
write_size = PA_MIN(size - written, write_mtu);
if (write_size < write_mtu)
break;
l = pa_write(fd, buffer + written, write_size, &t->stream_write_type);
if (l < 0)
break;
written += l;
}
if (l < 0) {
if (errno == EAGAIN) {
/* Hmm, apparently the socket was not writable, give up for now */
pa_log_debug("Got EAGAIN on write() after POLLOUT, probably there is a temporary connection loss.");
/* Drain write buffer */
written = size;
} else {
pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno));
/* Report error from write call */
return -1;
}
}
/* if too much data left discard it all */
if (size - written >= write_mtu) {
pa_log_warn("Wrote memory block to socket only partially! %lu written, discarding pending write size %lu larger than write_mtu %lu",
written, size, write_mtu);
/* Drain write buffer */
written = size;
}
return written;
}
static void sco_io_callback(pa_mainloop_api *io, pa_io_event *e, int fd, pa_io_event_flags_t events, void *userdata) {
pa_bluetooth_transport *t = userdata;
@ -730,6 +776,7 @@ static DBusMessage *profile_new_connection(DBusConnection *conn, DBusMessage *m,
t->acquire = sco_acquire_cb;
t->release = sco_release_cb;
t->write = sco_transport_write;
t->destroy = transport_destroy;
/* If PA is the HF/HS we are in control of volume attenuation and