📄 pcm_bluetooth.c
字号:
/* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org> * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * */#ifdef HAVE_CONFIG_H#include <config.h>#endif#include <stdint.h>#include <sys/socket.h>#include <sys/un.h>#include <sys/time.h>#include <pthread.h>#include <netinet/in.h>#include <alsa/asoundlib.h>#include <alsa/pcm_external.h>#include "ipc.h"#include "sbc.h"//#define ENABLE_DEBUG#define UINT_SECS_MAX (UINT_MAX / 1000000 - 1)#define MIN_PERIOD_TIME 1#define BUFFER_SIZE 2048#ifdef ENABLE_DEBUG#define DBG(fmt, arg...) printf("DEBUG: %s: " fmt "\n" , __FUNCTION__ , ## arg)#else#define DBG(fmt, arg...)#endif#ifndef SOL_SCO#define SOL_SCO 17#endif#ifndef SCO_TXBUFS#define SCO_TXBUFS 0x03#endif#ifndef SCO_RXBUFS#define SCO_RXBUFS 0x04#endif#if __BYTE_ORDER == __LITTLE_ENDIANstruct rtp_header { uint8_t cc:4; uint8_t x:1; uint8_t p:1; uint8_t v:2; uint8_t pt:7; uint8_t m:1; uint16_t sequence_number; uint32_t timestamp; uint32_t ssrc; uint32_t csrc[0];} __attribute__ ((packed));struct rtp_payload { uint8_t frame_count:4; uint8_t rfa0:1; uint8_t is_last_fragment:1; uint8_t is_first_fragment:1; uint8_t is_fragmented:1;} __attribute__ ((packed));#elif __BYTE_ORDER == __BIG_ENDIANstruct rtp_header { uint8_t v:2; uint8_t p:1; uint8_t x:1; uint8_t cc:4; uint8_t m:1; uint8_t pt:7; uint16_t sequence_number; uint32_t timestamp; uint32_t ssrc; uint32_t csrc[0];} __attribute__ ((packed));struct rtp_payload { uint8_t is_fragmented:1; uint8_t is_first_fragment:1; uint8_t is_last_fragment:1; uint8_t rfa0:1; uint8_t frame_count:4;} __attribute__ ((packed));#else#error "Unknown byte order"#endifstruct bluetooth_a2dp { sbc_t sbc; /* Codec data */ int codesize; /* SBC codesize */ int samples; /* Number of encoded samples */ uint8_t buffer[BUFFER_SIZE]; /* Codec transfer buffer */ int count; /* Codec transfer buffer counter */ int nsamples; /* Cumulative number of codec samples */ uint16_t seq_num; /* Cumulative packet sequence */ int frame_count; /* Current frames in buffer*/};struct bluetooth_data { snd_pcm_ioplug_t io; volatile snd_pcm_sframes_t hw_ptr; struct ipc_data_cfg cfg; /* Bluetooth device config */ struct pollfd stream; /* Audio stream filedescriptor */ struct pollfd server; /* Audio daemon filedescriptor */ uint8_t buffer[BUFFER_SIZE]; /* Encoded transfer buffer */ int count; /* Transfer buffer counter */ struct bluetooth_a2dp a2dp; /* A2DP data */ pthread_t hw_thread; /* Makes virtual hw pointer move */ int pipefd[2]; /* Inter thread communication */ int stopped;};static int bluetooth_start(snd_pcm_ioplug_t *io){ DBG("bluetooth_start %p", io); return 0;}static int bluetooth_stop(snd_pcm_ioplug_t *io){ DBG("bluetooth_stop %p", io); return 0;}static void *playback_hw_thread(void *param){ struct bluetooth_data *data = param; unsigned int prev_periods; double period_time; struct timeval start; struct pollfd fds[2]; fds[0] = data->server; fds[1] = data->stream; prev_periods = 0; period_time = 1000000.0 * data->io.period_size / data->io.rate; gettimeofday(&start, 0); while (1) { unsigned int dtime, periods; struct timeval cur, delta; int ret; if (data->stopped) goto iter_sleep; gettimeofday(&cur, 0); timersub(&cur, &start, &delta); dtime = delta.tv_sec * 1000000 + delta.tv_usec; periods = 1.0 * dtime / period_time; if (periods > prev_periods) { char c = 'w'; data->hw_ptr += (periods - prev_periods) * data->io.period_size; data->hw_ptr %= data->io.buffer_size; /* Notify user that hardware pointer has moved */ if (write(data->pipefd[1], &c, 1) < 0) pthread_testcancel(); /* Reset point of reference to avoid too big values * that wont fit an unsigned int */ if (delta.tv_sec < UINT_SECS_MAX) prev_periods = periods; else { prev_periods = 0; gettimeofday(&start, 0); } }iter_sleep: ret = poll(fds, 2, MIN_PERIOD_TIME); if (ret < 0) { SNDERR("poll error: %s (%d)", strerror(errno), errno); if (errno != EINTR) break; } else if (ret > 0) { ret = (fds[0].revents) ? 0 : 1; SNDERR("poll fd %d revents %d", ret, fds[ret].revents); if (fds[ret].revents & (POLLERR | POLLHUP | POLLNVAL)) break; } /* Offer opportunity to be canceled by main thread */ pthread_testcancel(); } data->hw_thread = 0; pthread_exit(NULL);}#if 0static int bluetooth_state_init(struct ipc_packet *pkt, int newstate){ struct ipc_data_state *state = (void *) pkt->data; pkt->length = sizeof(*state); pkt->type = PKT_TYPE_STATE_REQ; pkt->error = PKT_ERROR_NONE; state->state = newstate; return 0;}static int bluetooth_state(struct bluetooth_data *data, int newstate){ char buf[IPC_MTU]; struct ipc_packet *pkt = (void *) buf; struct ipc_data_state *state = (void *) pkt->data; int ret; memset(buf, 0, sizeof(buf)); ret = bluetooth_state_init(pkt, newstate); if (ret < 0) return -ret; ret = send(data->server.fd, pkt, sizeof(*pkt) + pkt->length, 0); if (ret < 0) return -errno; else if (ret == 0) return -EIO; DBG("OK - %d bytes sent. Waiting for response...", ret); memset(buf, 0, sizeof(buf)); ret = recv(data->server.fd, buf, sizeof(*pkt) + sizeof(*state), 0); if (ret < 0) return -errno; else if (ret == 0) return -EIO; if (pkt->type != PKT_TYPE_STATE_RSP) { SNDERR("Unexpected packet type %d received", pkt->type); return -EINVAL; } if (pkt->error != PKT_ERROR_NONE) { SNDERR("Error %d while configuring device", pkt->error); return -pkt->error; } return 0;}#endifstatic int bluetooth_playback_start(snd_pcm_ioplug_t *io){ struct bluetooth_data *data = io->private_data; int err; DBG("%p", io);#if 0 bluetooth_state(data, STATE_STREAMING);#endif data->stopped = 0; if (data->hw_thread) return 0; err = pthread_create(&data->hw_thread, 0, playback_hw_thread, data); return -err;}static int bluetooth_playback_stop(snd_pcm_ioplug_t *io){ struct bluetooth_data *data = io->private_data; DBG("%p", io);#if 0 bluetooth_state(data, STATE_CONNECTED);#endif data->stopped = 1; return 0;}static snd_pcm_sframes_t bluetooth_pointer(snd_pcm_ioplug_t *io){ struct bluetooth_data *data = io->private_data; return data->hw_ptr;}static void bluetooth_exit(struct bluetooth_data *data){ struct bluetooth_a2dp *a2dp = &data->a2dp; if (data->server.fd >= 0) close(data->server.fd); if (data->stream.fd >= 0) close(data->stream.fd); if (data->hw_thread) { pthread_cancel(data->hw_thread); pthread_join(data->hw_thread, 0); } if (data->cfg.codec == CFG_CODEC_SBC) sbc_finish(&a2dp->sbc); if (data->pipefd[0] > 0) close(data->pipefd[0]); if (data->pipefd[1] > 0) close(data->pipefd[1]); free(data);}static int bluetooth_close(snd_pcm_ioplug_t *io){ struct bluetooth_data *data = io->private_data; DBG("%p", io); bluetooth_exit(data); return 0;}static int bluetooth_prepare(snd_pcm_ioplug_t *io){ struct bluetooth_data *data = io->private_data; char c = 'w'; DBG("Preparing with io->period_size=%lu io->buffer_size=%lu", io->period_size, io->buffer_size); if (io->stream == SND_PCM_STREAM_PLAYBACK) /* If not null for playback, xmms doesn't display time * correctly */ data->hw_ptr = 0; else /* ALSA library is really picky on the fact hw_ptr is not null. * If it is, capture won't start */ data->hw_ptr = io->period_size; /* wake up any client polling at us */ return write(data->pipefd[1], &c, 1);}static int bluetooth_hsp_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params){ struct bluetooth_data *data = io->private_data; uint32_t period_count = io->buffer_size / io->period_size; int opt_name, err; DBG("fd=%d period_count=%d", data->stream.fd, period_count); opt_name = (io->stream == SND_PCM_STREAM_PLAYBACK) ? SCO_TXBUFS : SCO_RXBUFS; if (setsockopt(data->stream.fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0) return 0; opt_name = (io->stream == SND_PCM_STREAM_PLAYBACK) ? SO_SNDBUF : SO_RCVBUF; if (setsockopt(data->stream.fd, SOL_SCO, opt_name, &period_count, sizeof(period_count)) == 0) return 0; err = errno; SNDERR("%s (%d)", strerror(err), err); /* FIXME: We should not ignores errors in the future. */ return 0;}static int bluetooth_a2dp_hw_params(snd_pcm_ioplug_t *io, snd_pcm_hw_params_t *params){ struct bluetooth_data *data = io->private_data; uint32_t period_count = io->buffer_size / io->period_size; int opt_name, err; struct timeval t = { 0, period_count }; DBG("fd=%d period_count=%d", data->stream.fd, period_count); opt_name = (io->stream == SND_PCM_STREAM_PLAYBACK) ? SO_SNDTIMEO : SO_RCVTIMEO; if (setsockopt(data->stream.fd, SOL_SOCKET, opt_name, &t, sizeof(t)) == 0) return 0; err = errno; SNDERR("%s (%d)", strerror(err), err); return -err;}static int bluetooth_poll_descriptors(snd_pcm_ioplug_t *io,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -