📄 a2dp.c
字号:
/* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2004-2007 Marcel Holtmann <marcel@holtmann.org> * * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; 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 <stdlib.h>#include <dbus/dbus.h>#include <glib.h>#include <bluetooth/bluetooth.h>#include <bluetooth/sdp.h>#include <bluetooth/sdp_lib.h>#include "logging.h"#include "device.h"#include "manager.h"#include "avdtp.h"#include "sink.h"#include "a2dp.h"#define MAX_BITPOOL 64#define MIN_BITPOOL 2/* The duration that streams without users are allowed to stay in * STREAMING state. */#define SUSPEND_TIMEOUT 5000#ifndef MIN# define MIN(x, y) ((x) < (y) ? (x) : (y))#endif#ifndef MAX# define MAX(x, y) ((x) > (y) ? (x) : (y))#endifstruct a2dp_sep { uint8_t type; struct avdtp_local_sep *sep; struct avdtp *session; struct avdtp_stream *stream; guint suspend_timer; gboolean locked; gboolean suspending; gboolean starting;};struct a2dp_stream_cb { a2dp_stream_cb_t cb; void *user_data; int id;};struct a2dp_stream_setup { struct avdtp *session; struct a2dp_sep *sep; struct avdtp_stream *stream; struct avdtp_service_capability *media_codec; gboolean start; gboolean canceled; GSList *cb;};static DBusConnection *connection = NULL;static GSList *sinks = NULL;static GSList *sources = NULL;static uint32_t source_record_id = 0;static uint32_t sink_record_id = 0;static GSList *setups = NULL;static void stream_setup_free(struct a2dp_stream_setup *s){ setups = g_slist_remove(setups, s); if (s->session) avdtp_unref(s->session); g_slist_foreach(s->cb, (GFunc) g_free, NULL); g_slist_free(s->cb); g_free(s);}static struct device *a2dp_get_dev(struct avdtp *session){ bdaddr_t addr; avdtp_get_peers(session, NULL, &addr); return manager_device_connected(&addr, A2DP_SOURCE_UUID);}static void setup_callback(struct a2dp_stream_cb *cb, struct a2dp_stream_setup *s){ cb->cb(s->session, s->sep, s->stream, cb->user_data);}static gboolean finalize_stream_setup(struct a2dp_stream_setup *s){ g_slist_foreach(s->cb, (GFunc) setup_callback, s); stream_setup_free(s); return FALSE;}static struct a2dp_stream_setup *find_setup_by_session(struct avdtp *session){ GSList *l; for (l = setups; l != NULL; l = l->next) { struct a2dp_stream_setup *setup = l->data; if (setup->session == session) return setup; } return NULL;}static struct a2dp_stream_setup *find_setup_by_dev(struct device *dev){ GSList *l; for (l = setups; l != NULL; l = l->next) { struct a2dp_stream_setup *setup = l->data; struct device *setup_dev = a2dp_get_dev(setup->session); if (setup_dev == dev) return setup; } return NULL;}static void stream_state_changed(struct avdtp_stream *stream, avdtp_state_t old_state, avdtp_state_t new_state, struct avdtp_error *err, void *user_data){ struct a2dp_sep *sep = user_data; if (new_state != AVDTP_STATE_IDLE) return; if (sep->suspend_timer) { g_source_remove(sep->suspend_timer); sep->suspend_timer = 0; } if (sep->session) { avdtp_unref(sep->session); sep->session = NULL; } sep->stream = NULL;}static uint8_t default_bitpool(uint8_t freq, uint8_t mode){ switch (freq) { case A2DP_SAMPLING_FREQ_16000: case A2DP_SAMPLING_FREQ_32000: return 53; case A2DP_SAMPLING_FREQ_44100: switch (mode) { case A2DP_CHANNEL_MODE_MONO: case A2DP_CHANNEL_MODE_DUAL_CHANNEL: return 31; case A2DP_CHANNEL_MODE_STEREO: case A2DP_CHANNEL_MODE_JOINT_STEREO: return 53; default: error("Invalid channel mode %u", mode); return 53; } case A2DP_SAMPLING_FREQ_48000: switch (mode) { case A2DP_CHANNEL_MODE_MONO: case A2DP_CHANNEL_MODE_DUAL_CHANNEL: return 29; case A2DP_CHANNEL_MODE_STEREO: case A2DP_CHANNEL_MODE_JOINT_STEREO: return 51; default: error("Invalid channel mode %u", mode); return 51; } default: error("Invalid sampling freq %u", freq); return 53; }}static gboolean select_sbc_params(struct sbc_codec_cap *cap, struct sbc_codec_cap *supported){ uint max_bitpool, min_bitpool; memset(cap, 0, sizeof(struct sbc_codec_cap)); cap->cap.media_type = AVDTP_MEDIA_TYPE_AUDIO; cap->cap.media_codec_type = A2DP_CODEC_SBC; if (supported->frequency & A2DP_SAMPLING_FREQ_44100) cap->frequency = A2DP_SAMPLING_FREQ_44100; else if (supported->frequency & A2DP_SAMPLING_FREQ_48000) cap->frequency = A2DP_SAMPLING_FREQ_48000; else if (supported->frequency & A2DP_SAMPLING_FREQ_32000) cap->frequency = A2DP_SAMPLING_FREQ_32000; else if (supported->frequency & A2DP_SAMPLING_FREQ_16000) cap->frequency = A2DP_SAMPLING_FREQ_16000; else { error("No supported frequencies"); return FALSE; } if (supported->channel_mode & A2DP_CHANNEL_MODE_JOINT_STEREO) cap->channel_mode = A2DP_CHANNEL_MODE_JOINT_STEREO; else if (supported->channel_mode & A2DP_CHANNEL_MODE_STEREO) cap->channel_mode = A2DP_CHANNEL_MODE_STEREO; else if (supported->channel_mode & A2DP_CHANNEL_MODE_DUAL_CHANNEL) cap->channel_mode = A2DP_CHANNEL_MODE_DUAL_CHANNEL; else if (supported->channel_mode & A2DP_CHANNEL_MODE_MONO) cap->channel_mode = A2DP_CHANNEL_MODE_MONO; else { error("No supported channel modes"); return FALSE; } if (supported->block_length & A2DP_BLOCK_LENGTH_16) cap->block_length = A2DP_BLOCK_LENGTH_16; else if (supported->block_length & A2DP_BLOCK_LENGTH_12) cap->block_length = A2DP_BLOCK_LENGTH_12; else if (supported->block_length & A2DP_BLOCK_LENGTH_8) cap->block_length = A2DP_BLOCK_LENGTH_8; else if (supported->block_length & A2DP_BLOCK_LENGTH_4) cap->block_length = A2DP_BLOCK_LENGTH_4; else { error("No supported block lengths"); return FALSE; } if (supported->subbands & A2DP_SUBBANDS_8) cap->subbands = A2DP_SUBBANDS_8; else if (supported->subbands & A2DP_SUBBANDS_4) cap->subbands = A2DP_SUBBANDS_4; else { error("No supported subbands"); return FALSE; } if (supported->allocation_method & A2DP_ALLOCATION_LOUDNESS) cap->allocation_method = A2DP_ALLOCATION_LOUDNESS; else if (supported->allocation_method & A2DP_ALLOCATION_SNR) cap->allocation_method = A2DP_ALLOCATION_SNR; min_bitpool = MAX(MIN_BITPOOL, supported->min_bitpool); max_bitpool = MIN(default_bitpool(cap->frequency, cap->channel_mode), supported->max_bitpool); cap->min_bitpool = min_bitpool; cap->max_bitpool = max_bitpool; return TRUE;}static gboolean a2dp_select_capabilities(struct avdtp *session, struct avdtp_remote_sep *rsep, GSList **caps){ struct avdtp_service_capability *media_transport, *media_codec; struct sbc_codec_cap sbc_cap; struct a2dp_stream_setup *setup; setup = find_setup_by_session(session); if (!setup) return FALSE; if (setup->media_codec) { memcpy(&sbc_cap, setup->media_codec->data, sizeof(sbc_cap)); } else { media_codec = avdtp_get_codec(rsep); if (!media_codec) return FALSE; select_sbc_params(&sbc_cap, (struct sbc_codec_cap *) media_codec->data); } media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, NULL, 0); *caps = g_slist_append(*caps, media_transport); media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap, sizeof(sbc_cap)); *caps = g_slist_append(*caps, media_codec); return TRUE;}static void discovery_complete(struct avdtp *session, GSList *seps, int err, void *user_data){ struct avdtp_local_sep *lsep; struct avdtp_remote_sep *rsep; struct a2dp_stream_setup *setup; GSList *caps = NULL; setup = find_setup_by_session(session); if (!setup) return; if (err < 0 || setup->canceled) { setup->stream = NULL; finalize_stream_setup(setup); return; } debug("Discovery complete"); if (avdtp_get_seps(session, AVDTP_SEP_TYPE_SINK, AVDTP_MEDIA_TYPE_AUDIO, A2DP_CODEC_SBC, &lsep, &rsep) < 0) { error("No matching ACP and INT SEPs found"); finalize_stream_setup(setup); return; } if (!a2dp_select_capabilities(session, rsep, &caps)) { error("Unable to select remote SEP capabilities"); finalize_stream_setup(setup); return; } err = avdtp_set_configuration(session, rsep, lsep, caps, &setup->stream); if (err < 0) { error("avdtp_set_configuration: %s", strerror(-err)); finalize_stream_setup(setup); }}static gboolean setconf_ind(struct avdtp *session, struct avdtp_local_sep *sep, struct avdtp_stream *stream, GSList *caps, uint8_t *err, uint8_t *category, void *user_data){ struct a2dp_sep *a2dp_sep = user_data; struct device *dev; struct avdtp_service_capability *cap; struct avdtp_media_codec_capability *codec_cap; struct sbc_codec_cap *sbc_cap; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("SBC Sink: Set_Configuration_Ind"); else debug("SBC Source: Set_Configuration_Ind"); dev = a2dp_get_dev(session); if (!dev) { *err = AVDTP_UNSUPPORTED_CONFIGURATION; *category = 0x00; return FALSE; } /* Check bipool range */ for (codec_cap = NULL; caps; caps = g_slist_next(caps)) { cap = caps->data; if (cap->category == AVDTP_MEDIA_CODEC) { codec_cap = (void *) cap->data; if (codec_cap->media_codec_type == A2DP_CODEC_SBC) { sbc_cap = (void *) codec_cap; if (sbc_cap->min_bitpool < MIN_BITPOOL || sbc_cap->max_bitpool > MAX_BITPOOL) { *err = AVDTP_UNSUPPORTED_CONFIGURATION; *category = AVDTP_MEDIA_CODEC; return FALSE; } } break; } } avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep); a2dp_sep->stream = stream; if (a2dp_sep->type == AVDTP_SEP_TYPE_SOURCE) sink_new_stream(dev, session, stream); return TRUE;}static gboolean getcap_ind(struct avdtp *session, struct avdtp_local_sep *sep, GSList **caps, uint8_t *err, void *user_data){ struct a2dp_sep *a2dp_sep = user_data; struct avdtp_service_capability *media_transport, *media_codec; struct sbc_codec_cap sbc_cap; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("SBC Sink: Get_Capability_Ind"); else debug("SBC Source: Get_Capability_Ind"); *caps = NULL; media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, NULL, 0); *caps = g_slist_append(*caps, media_transport); memset(&sbc_cap, 0, sizeof(struct sbc_codec_cap)); sbc_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO; sbc_cap.cap.media_codec_type = A2DP_CODEC_SBC; sbc_cap.frequency = ( A2DP_SAMPLING_FREQ_48000 | A2DP_SAMPLING_FREQ_44100 | A2DP_SAMPLING_FREQ_32000 | A2DP_SAMPLING_FREQ_16000 ); sbc_cap.channel_mode = ( A2DP_CHANNEL_MODE_JOINT_STEREO | A2DP_CHANNEL_MODE_STEREO | A2DP_CHANNEL_MODE_DUAL_CHANNEL | A2DP_CHANNEL_MODE_MONO ); sbc_cap.block_length = ( A2DP_BLOCK_LENGTH_16 | A2DP_BLOCK_LENGTH_12 | A2DP_BLOCK_LENGTH_8 | A2DP_BLOCK_LENGTH_4 ); sbc_cap.subbands = ( A2DP_SUBBANDS_8 | A2DP_SUBBANDS_4 ); sbc_cap.allocation_method = ( A2DP_ALLOCATION_LOUDNESS | A2DP_ALLOCATION_SNR ); sbc_cap.min_bitpool = MIN_BITPOOL; sbc_cap.max_bitpool = MAX_BITPOOL; media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap, sizeof(sbc_cap)); *caps = g_slist_append(*caps, media_codec); return TRUE;}static void setconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep, struct avdtp_stream *stream, struct avdtp_error *err, void *user_data){ struct a2dp_sep *a2dp_sep = user_data; struct a2dp_stream_setup *setup; struct device *dev; int ret; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("SBC Sink: Set_Configuration_Cfm"); else debug("SBC Source: Set_Configuration_Cfm"); setup = find_setup_by_session(session); if (err) { if (setup) finalize_stream_setup(setup); return; } avdtp_stream_add_cb(session, stream, stream_state_changed, a2dp_sep); a2dp_sep->stream = stream; if (!setup) return; dev = a2dp_get_dev(session); /* Notify sink.c of the new stream */ sink_new_stream(dev, session, setup->stream); ret = avdtp_open(session, stream); if (ret < 0) { error("Error on avdtp_open %s (%d)", strerror(-ret), -ret); setup->stream = NULL; finalize_stream_setup(setup); }}static gboolean getconf_ind(struct avdtp *session, struct avdtp_local_sep *sep, uint8_t *err, void *user_data){ struct a2dp_sep *a2dp_sep = user_data; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("SBC Sink: Get_Configuration_Ind"); else debug("SBC Source: Get_Configuration_Ind"); return TRUE;}static void getconf_cfm(struct avdtp *session, struct avdtp_local_sep *sep, struct avdtp_stream *stream, struct avdtp_error *err, void *user_data){ struct a2dp_sep *a2dp_sep = user_data; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("SBC Sink: Set_Configuration_Cfm"); else debug("SBC Source: Set_Configuration_Cfm");}static gboolean open_ind(struct avdtp *session, struct avdtp_local_sep *sep, struct avdtp_stream *stream, uint8_t *err, void *user_data){ struct a2dp_sep *a2dp_sep = user_data; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("SBC Sink: Open_Ind"); else debug("SBC Source: Open_Ind"); return TRUE;}static void open_cfm(struct avdtp *session, struct avdtp_local_sep *sep, struct avdtp_stream *stream, struct avdtp_error *err, void *user_data){ struct a2dp_sep *a2dp_sep = user_data; struct a2dp_stream_setup *setup; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("SBC Sink: Open_Cfm"); else debug("SBC Source: Open_Cfm"); setup = find_setup_by_session(session); if (!setup) return; if (setup->canceled) { if (!err) avdtp_close(session, stream); stream_setup_free(setup); return; } if (err) { setup->stream = NULL; goto finalize; } if (setup->start) { if (avdtp_start(session, stream) == 0) return; error("avdtp_start failed"); setup->stream = NULL; }finalize: finalize_stream_setup(setup);}static gboolean suspend_timeout(struct a2dp_sep *sep){ if (avdtp_suspend(sep->session, sep->stream) == 0) sep->suspending = TRUE; sep->suspend_timer = 0; avdtp_unref(sep->session); sep->session = NULL; return FALSE;}static gboolean start_ind(struct avdtp *session, struct avdtp_local_sep *sep, struct avdtp_stream *stream, uint8_t *err, void *user_data){ struct a2dp_sep *a2dp_sep = user_data; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("SBC Sink: Start_Ind"); else debug("SBC Source: Start_Ind"); a2dp_sep->session = avdtp_ref(session); a2dp_sep->suspend_timer = g_timeout_add(SUSPEND_TIMEOUT, (GSourceFunc) suspend_timeout, a2dp_sep); return TRUE;}static void start_cfm(struct avdtp *session, struct avdtp_local_sep *sep, struct avdtp_stream *stream, struct avdtp_error *err, void *user_data){ struct a2dp_sep *a2dp_sep = user_data; struct a2dp_stream_setup *setup; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("SBC Sink: Start_Cfm"); else debug("SBC Source: Start_Cfm");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -