?? pcm_bluetooth.c
?? 基于LINUX內核驅動的開發
?? C
?? 第 1 頁 / 共 3 頁
字號:
??
/* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2006-2007 Nokia Corporation * Copyright (C) 2004-2008 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 <time.h>#include <sys/time.h>#include <pthread.h>#include <signal.h>#include <limits.h>#include <netinet/in.h>#include <alsa/asoundlib.h>#include <alsa/pcm_external.h>#include "ipc.h"#include "sbc.h"#include "rtp.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#ifndef MIN# define MIN(x, y) ((x) < (y) ? (x) : (y))#endif#ifndef MAX# define MAX(x, y) ((x) > (y) ? (x) : (y))#endif#define MAX_BITPOOL 64#define MIN_BITPOOL 2/* adapted from glibc sys/time.h timersub() macro */#define priv_timespecsub(a, b, result) \ do { \ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \ (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \ if ((result)->tv_nsec < 0) { \ --(result)->tv_sec; \ (result)->tv_nsec += 1000000000; \ } \ } while (0)struct bluetooth_a2dp { sbc_capabilities_t sbc_capabilities; sbc_t sbc; /* Codec data */ int sbc_initialized; /* Keep track if the encoder is initialized */ 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_alsa_config { char device[18]; /* Address of the remote Device */ int has_device; uint8_t transport; /* Requested transport */ int has_transport; uint16_t rate; int has_rate; uint8_t channel_mode; /* A2DP only */ int has_channel_mode; uint8_t allocation_method; /* A2DP only */ int has_allocation_method; uint8_t subbands; /* A2DP only */ int has_subbands; uint8_t block_length; /* A2DP only */ int has_block_length; uint8_t bitpool; /* A2DP only */ int has_bitpool; int autoconnect;};struct bluetooth_data { snd_pcm_ioplug_t io; struct bluetooth_alsa_config alsa_config; /* ALSA resource file parameters */ volatile snd_pcm_sframes_t hw_ptr; int transport; /* chosen transport SCO or AD2P */ int link_mtu; /* MTU for selected transport channel */ volatile 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; sig_atomic_t reset; /* Request XRUN handling */};static int audioservice_send(int sk, const bt_audio_msg_header_t *msg);static int audioservice_expect(int sk, bt_audio_msg_header_t *outmsg, int expected_type);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 timespec start; struct pollfd fds[2]; int poll_timeout; data->server.events = POLLIN; /* note: only errors for data->stream.events */ fds[0] = data->server; fds[1] = data->stream; prev_periods = 0; period_time = 1000000.0 * data->io.period_size / data->io.rate; if (period_time > (int) (MIN_PERIOD_TIME * 1000)) poll_timeout = (int) (period_time / 1000.0f); else poll_timeout = MIN_PERIOD_TIME; clock_gettime(CLOCK_MONOTONIC, &start); while (1) { unsigned int dtime, periods; struct timespec cur, delta; int ret; if (data->stopped) goto iter_sleep; if (data->reset) { DBG("Handle XRUN in hw-thread."); data->reset = 0; clock_gettime(CLOCK_MONOTONIC, &start); prev_periods = 0; } clock_gettime(CLOCK_MONOTONIC, &cur); priv_timespecsub(&cur, &start, &delta); dtime = delta.tv_sec * 1000000 + delta.tv_nsec / 1000; periods = 1.0 * dtime / period_time; if (periods > prev_periods) { char c = 'w'; int frags = periods - prev_periods, n; data->hw_ptr += frags * data->io.period_size; data->hw_ptr %= data->io.buffer_size; for (n = 0; n < frags; n++) { /* 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; clock_gettime(CLOCK_MONOTONIC, &start); } }iter_sleep: /* sleep up to one period interval */ ret = poll(fds, 2, poll_timeout); 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);}static int bluetooth_playback_start(snd_pcm_ioplug_t *io){ struct bluetooth_data *data = io->private_data; int err; DBG("%p", io); 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); 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) bt_audio_service_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 (a2dp->sbc_initialized) 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'; char buf[BT_AUDIO_IPC_PACKET_SIZE]; struct bt_streamstart_req *start_req = (void*) buf; bt_audio_rsp_msg_header_t *rsp_hdr = (void*) buf; struct bt_streamfd_ind *streamfd_ind = (void*) buf; uint32_t period_count = io->buffer_size / io->period_size; int opt_name, err; struct timeval t = { 0, period_count }; DBG("Preparing with io->period_size=%lu io->buffer_size=%lu", io->period_size, io->buffer_size); data->reset = 0; /* As we're gonna receive messages on the server socket, we have to stop the hw thread that is polling on it, if any */ if (data->hw_thread) { pthread_cancel(data->hw_thread); pthread_join(data->hw_thread, 0); data->hw_thread = 0; } 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; /* send start */ memset(start_req, 0, BT_AUDIO_IPC_PACKET_SIZE); start_req->h.msg_type = BT_STREAMSTART_REQ; err = audioservice_send(data->server.fd, &start_req->h); if (err < 0) return err; err = audioservice_expect(data->server.fd, &rsp_hdr->msg_h, BT_STREAMSTART_RSP); if (err < 0) return err; if (rsp_hdr->posix_errno != 0) { SNDERR("BT_START failed : %s(%d)", strerror(rsp_hdr->posix_errno), rsp_hdr->posix_errno); return -rsp_hdr->posix_errno; } err = audioservice_expect(data->server.fd, &streamfd_ind->h, BT_STREAMFD_IND); if (err < 0) return err; if (data->stream.fd >= 0) close(data->stream.fd); data->stream.fd = bt_audio_service_get_data_fd(data->server.fd); if (data->stream.fd < 0) { return -errno; } if (data->transport == BT_CAPABILITIES_TRANSPORT_A2DP) { 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 -errno; } else { 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; /* FIXME : handle error codes */ } /* 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; char buf[BT_AUDIO_IPC_PACKET_SIZE]; bt_audio_rsp_msg_header_t *rsp_hdr = (void*) buf; struct bt_setconfiguration_req *setconf_req = (void*) buf; struct bt_setconfiguration_rsp *setconf_rsp = (void*) buf; int err; DBG("Preparing with io->period_size=%lu io->buffer_size=%lu", io->period_size, io->buffer_size); memset(setconf_req, 0, BT_AUDIO_IPC_PACKET_SIZE); setconf_req->h.msg_type = BT_SETCONFIGURATION_REQ; strncpy(setconf_req->device, data->alsa_config.device, 18); setconf_req->transport = BT_CAPABILITIES_TRANSPORT_SCO; setconf_req->access_mode = (io->stream == SND_PCM_STREAM_PLAYBACK ? BT_CAPABILITIES_ACCESS_MODE_WRITE : BT_CAPABILITIES_ACCESS_MODE_READ); err = audioservice_send(data->server.fd, &setconf_req->h); if (err < 0) return err; err = audioservice_expect(data->server.fd, &rsp_hdr->msg_h, BT_SETCONFIGURATION_RSP); if (err < 0) return err; if (rsp_hdr->posix_errno != 0) { SNDERR("BT_SETCONFIGURATION failed : %s(%d)", strerror(rsp_hdr->posix_errno), rsp_hdr->posix_errno); return -rsp_hdr->posix_errno; } data->transport = setconf_rsp->transport; data->link_mtu = setconf_rsp->link_mtu; return 0;}static uint8_t default_bitpool(uint8_t freq, uint8_t mode){ switch (freq) { case BT_SBC_SAMPLING_FREQ_16000: case BT_SBC_SAMPLING_FREQ_32000: return 53; case BT_SBC_SAMPLING_FREQ_44100: switch (mode) { case BT_A2DP_CHANNEL_MODE_MONO: case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL: return 31; case BT_A2DP_CHANNEL_MODE_STEREO: case BT_A2DP_CHANNEL_MODE_JOINT_STEREO: return 53; default: DBG("Invalid channel mode %u", mode); return 53; } case BT_SBC_SAMPLING_FREQ_48000: switch (mode) { case BT_A2DP_CHANNEL_MODE_MONO: case BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL: return 29; case BT_A2DP_CHANNEL_MODE_STEREO: case BT_A2DP_CHANNEL_MODE_JOINT_STEREO: return 51; default: DBG("Invalid channel mode %u", mode); return 51; } default: DBG("Invalid sampling freq %u", freq); return 53; }}static int bluetooth_a2dp_init(struct bluetooth_data *data, snd_pcm_hw_params_t *params){ struct bluetooth_alsa_config *cfg = &data->alsa_config; sbc_capabilities_t *cap = &data->a2dp.sbc_capabilities; unsigned int max_bitpool, min_bitpool, rate, channels; int dir; snd_pcm_hw_params_get_rate(params, &rate, &dir); snd_pcm_hw_params_get_channels(params, &channels); switch (rate) { case 48000: cap->frequency = BT_SBC_SAMPLING_FREQ_48000; break; case 44100: cap->frequency = BT_SBC_SAMPLING_FREQ_44100; break; case 32000: cap->frequency = BT_SBC_SAMPLING_FREQ_32000; break; case 16000: cap->frequency = BT_SBC_SAMPLING_FREQ_16000; break; default: DBG("Rate %d not supported", rate); return -1; } if (cfg->has_channel_mode) cap->channel_mode = cfg->channel_mode; else if (channels == 2) { if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_JOINT_STEREO) cap->channel_mode = BT_A2DP_CHANNEL_MODE_JOINT_STEREO; else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_STEREO) cap->channel_mode = BT_A2DP_CHANNEL_MODE_STEREO; else if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL) cap->channel_mode = BT_A2DP_CHANNEL_MODE_DUAL_CHANNEL; } else { if (cap->channel_mode & BT_A2DP_CHANNEL_MODE_MONO) cap->channel_mode = BT_A2DP_CHANNEL_MODE_MONO; } if (!cap->channel_mode) {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -