diff options
Diffstat (limited to 'src/modules/bluetooth/module-bluetooth-device.c')
-rw-r--r-- | src/modules/bluetooth/module-bluetooth-device.c | 54 |
1 files changed, 31 insertions, 23 deletions
diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c index 7d601e73..1b61a6fc 100644 --- a/src/modules/bluetooth/module-bluetooth-device.c +++ b/src/modules/bluetooth/module-bluetooth-device.c @@ -57,9 +57,6 @@ #define MAX_BITPOOL 64 #define MIN_BITPOOL 2U -#define SOL_SCO 17 -#define SCO_TXBUFS 0x03 -#define SCO_RXBUFS 0x04 PA_MODULE_AUTHOR("Joao Paulo Rechi Vita"); PA_MODULE_DESCRIPTION("Bluetooth audio sink and source"); @@ -78,6 +75,10 @@ PA_MODULE_USAGE( "sco_sink=<SCO over PCM sink name> " "sco_source=<SCO over PCM source name>"); +/* TODO: not close fd when entering suspend mode in a2dp */ + +/* TODO: BT_PCM_FLAG_NREC */ + static const char* const valid_modargs[] = { "name", "card_name", @@ -160,6 +161,8 @@ struct userdata { int service_write_type, service_read_type; }; +#define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source)) + static int init_bt(struct userdata *u); static int init_profile(struct userdata *u); @@ -653,7 +656,7 @@ static int set_conf(struct userdata *u) { return 0; } -/* from IO thread */ +/* from IO thread, except in SCO over PCM */ static int start_stream_fd(struct userdata *u) { union { bt_audio_msg_header_t rsp; @@ -689,9 +692,6 @@ static int start_stream_fd(struct userdata *u) { return -1; } -/* setsockopt(u->stream_fd, SOL_SCO, SCO_TXBUFS, &period_count, sizeof(period_count)); */ -/* setsockopt(u->stream_fd, SOL_SCO, SCO_SNDBUF, &period_count, sizeof(period_count)); */ - pa_make_fd_nonblock(u->stream_fd); pa_make_socket_low_delay(u->stream_fd); @@ -876,7 +876,7 @@ static int hsp_process_render(struct userdata *u) { pa_assert(l != 0); if (l < 0) { - if (errno == EINTR) + if (errno == EINTR || errno == EAGAIN) continue; else { pa_log_error("Failed to write data to SCO socket: %s", pa_cstrerror(errno)); @@ -921,7 +921,7 @@ static int hsp_process_push(struct userdata *u) { pa_memblock_release(memchunk.memblock); if (l <= 0) { - if (l < 0 && errno == EINTR) + if (l < 0 && (errno == EINTR || errno == EAGAIN)) continue; else { pa_log_error("Failed to read data from SCO socket: %s", l < 0 ? pa_cstrerror(errno) : "EOF"); @@ -951,7 +951,7 @@ static int a2dp_process_render(struct userdata *u) { void *d; const void *p; unsigned frame_count; - int written; + size_t written; uint64_t writing_at; pa_assert(u); @@ -977,14 +977,14 @@ static int a2dp_process_render(struct userdata *u) { writing_at = u->write_index; do { - int encoded; + ssize_t encoded; if (!u->write_memchunk.memblock) pa_sink_render_full(u->sink, u->block_size, &u->write_memchunk); p = (const uint8_t*) pa_memblock_acquire(u->write_memchunk.memblock) + u->write_memchunk.index; encoded = sbc_encode(&a2dp->sbc, - (void*) p, u->write_memchunk.length, + p, u->write_memchunk.length, d, left, &written); @@ -999,10 +999,11 @@ static int a2dp_process_render(struct userdata *u) { return -1; } - pa_assert(written >= 0); - pa_assert((size_t) encoded <= u->write_memchunk.length); + pa_assert((size_t) encoded == sbc_get_codesize(&a2dp->sbc)); + pa_assert((size_t) written <= left); + pa_assert((size_t) written == sbc_get_frame_length(&a2dp->sbc)); /* pa_log_debug("SBC: encoded: %d; written: %d", encoded, written); */ @@ -1021,7 +1022,7 @@ static int a2dp_process_render(struct userdata *u) { frame_count++; - } while ((uint8_t*) d - (uint8_t*) a2dp->buffer + written < (ptrdiff_t) u->link_mtu); + } while (((uint8_t*) d - ((uint8_t*) a2dp->buffer + sbc_get_frame_length(&a2dp->sbc))) < (ptrdiff_t) u->link_mtu); /* write it to the fifo */ memset(a2dp->buffer, 0, sizeof(*header) + sizeof(*payload)); @@ -1044,7 +1045,7 @@ static int a2dp_process_render(struct userdata *u) { pa_assert(l != 0); if (l < 0) { - if (errno == EINTR) + if (errno == EINTR || errno == EAGAIN) continue; else { pa_log_error("Failed to write data to socket: %s", pa_cstrerror(errno)); @@ -1126,6 +1127,8 @@ static void thread_func(void *userdata) { } if (writable && do_write) { + if (u->write_index == 0) + u->started_at = pa_rtclock_usec(); if (u->profile == PROFILE_A2DP) { if (a2dp_process_render(u) < 0) @@ -1162,8 +1165,8 @@ static void thread_func(void *userdata) { /* Hmm, nothing to do. Let's sleep */ if (pollfd) - pollfd->events = (short) (((u->sink && PA_SINK_IS_OPENED(u->sink->thread_info.state) && !writable) ? POLLOUT : 0) | - (u->source && PA_SOURCE_IS_OPENED(u->source->thread_info.state) ? POLLIN : 0)); + pollfd->events = (short) (((u->sink && PA_SINK_IS_LINKED(u->sink->thread_info.state) && !writable) ? POLLOUT : 0) | + (u->source && PA_SOURCE_IS_LINKED(u->source->thread_info.state) ? POLLIN : 0)); if ((ret = pa_rtpoll_run(u->rtpoll, TRUE)) < 0) goto fail; @@ -1357,8 +1360,6 @@ static char *get_name(const char *type, pa_modargs *ma, const char *device_id, p return pa_sprintf_malloc("bluez_%s.%s", type, n); } -#define USE_SCO_OVER_PCM(u) (u->profile == PROFILE_HSP && (u->hsp.sco_sink && u->hsp.sco_source)) - static void sco_over_pcm_state_update(struct userdata *u) { pa_assert(u); pa_assert(USE_SCO_OVER_PCM(u)); @@ -1373,11 +1374,14 @@ static void sco_over_pcm_state_update(struct userdata *u) { if ((init_bt(u) < 0) || (init_profile(u) < 0)) pa_log("Can't resume SCO over PCM"); + start_stream_fd(u); } else { if (u->service_fd < 0) return; + stop_stream_fd(u); + pa_log_debug("Closing SCO over PCM"); pa_close(u->service_fd); u->service_fd = -1; @@ -1627,15 +1631,19 @@ static int start_thread(struct userdata *u) { pa_assert(!u->rtpoll); pa_assert(!u->rtpoll_item); + u->rtpoll = pa_rtpoll_new(); + pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll); + if (USE_SCO_OVER_PCM(u)) { + if (start_stream_fd(u) < 0) + return -1; + pa_sink_ref(u->sink); pa_source_ref(u->source); + /* FIXME: monitor stream_fd error */ return 0; } - u->rtpoll = pa_rtpoll_new(); - pa_thread_mq_init(&u->thread_mq, u->core->mainloop, u->rtpoll); - if (!(u->thread = pa_thread_new(thread_func, u))) { pa_log_error("Failed to create IO thread"); stop_thread(u); |