?? a2dp.c
字號:
/* * * BlueZ - Bluetooth protocol stack for Linux * * Copyright (C) 2006-2007 Nokia Corporation * Copyright (C) 2004-2008 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 <errno.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"/* The duration that streams without users are allowed to stay in * STREAMING state. */#define SUSPEND_TIMEOUT 5000#define RECONFIGURE_TIMEOUT 500#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; uint8_t codec; struct avdtp_local_sep *sep; struct avdtp *session; struct avdtp_stream *stream; guint suspend_timer; gboolean locked; gboolean suspending; gboolean starting;};struct a2dp_setup_cb { a2dp_config_cb_t config_cb; a2dp_stream_cb_t resume_cb; a2dp_stream_cb_t suspend_cb; void *user_data; int id;};struct a2dp_setup { struct avdtp *session; struct a2dp_sep *sep; struct avdtp_stream *stream; struct avdtp_error *err; GSList *client_caps; gboolean reconfigure; gboolean canceled; gboolean start; GSList *cb; int ref;};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 unsigned int cb_id = 0;static struct a2dp_setup *setup_ref(struct a2dp_setup *setup){ setup->ref++; debug("setup_ref(%p): ref=%d", setup, setup->ref); return setup;}static void setup_free(struct a2dp_setup *s){ debug("setup_free(%p)", 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 void setup_unref(struct a2dp_setup *setup){ setup->ref--; debug("setup_unref(%p): ref=%d", setup, setup->ref); if (setup->ref <= 0) setup_free(setup);}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 gboolean finalize_config(struct a2dp_setup *s){ GSList *l; setup_ref(s); for (l = s->cb; l != NULL; l = l->next) { struct a2dp_setup_cb *cb = l->data; if (cb->config_cb) { cb->config_cb(s->session, s->sep, s->stream, s->err, cb->user_data); cb->config_cb = NULL; setup_unref(s); } } setup_unref(s); return FALSE;}static gboolean finalize_config_errno(struct a2dp_setup *s, int err){ struct avdtp_error avdtp_err; avdtp_error_init(&avdtp_err, AVDTP_ERROR_ERRNO, -err); s->err = err ? &avdtp_err : NULL; return finalize_config(s);}static gboolean finalize_resume(struct a2dp_setup *s){ GSList *l; setup_ref(s); for (l = s->cb; l != NULL; l = l->next) { struct a2dp_setup_cb *cb = l->data; if (cb->resume_cb) { cb->resume_cb(s->session, s->err, cb->user_data); cb->resume_cb = NULL; setup_unref(s); } } setup_unref(s); return FALSE;}static gboolean finalize_resume_errno(struct a2dp_setup *s, int err){ struct avdtp_error avdtp_err; avdtp_error_init(&avdtp_err, AVDTP_ERROR_ERRNO, -err); s->err = err ? &avdtp_err : NULL; return finalize_resume(s);}static gboolean finalize_suspend(struct a2dp_setup *s){ GSList *l; setup_ref(s); for (l = s->cb; l != NULL; l = l->next) { struct a2dp_setup_cb *cb = l->data; if (cb->suspend_cb) { cb->suspend_cb(s->session, s->err, cb->user_data); cb->suspend_cb = NULL; setup_unref(s); } } setup_unref(s); return FALSE;}static gboolean finalize_suspend_errno(struct a2dp_setup *s, int err){ struct avdtp_error avdtp_err; avdtp_error_init(&avdtp_err, AVDTP_ERROR_ERRNO, -err); s->err = err ? &avdtp_err : NULL; return finalize_suspend(s);}static struct a2dp_setup *find_setup_by_session(struct avdtp *session){ GSList *l; for (l = setups; l != NULL; l = l->next) { struct a2dp_setup *setup = l->data; if (setup->session == session) return setup; } return NULL;}static struct a2dp_setup *find_setup_by_dev(struct device *dev){ GSList *l; for (l = setups; l != NULL; l = l->next) { struct a2dp_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 gboolean sbc_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("Sink %p: Set_Configuration_Ind", sep); else debug("Source %p: Set_Configuration_Ind", sep); 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 sbc_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("Sink %p: Get_Capability_Ind", sep); else debug("Source %p: Get_Capability_Ind", sep); *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 = ( SBC_SAMPLING_FREQ_48000 | SBC_SAMPLING_FREQ_44100 | SBC_SAMPLING_FREQ_32000 | SBC_SAMPLING_FREQ_16000 ); sbc_cap.channel_mode = ( SBC_CHANNEL_MODE_JOINT_STEREO | SBC_CHANNEL_MODE_STEREO | SBC_CHANNEL_MODE_DUAL_CHANNEL | SBC_CHANNEL_MODE_MONO ); sbc_cap.block_length = ( SBC_BLOCK_LENGTH_16 | SBC_BLOCK_LENGTH_12 | SBC_BLOCK_LENGTH_8 | SBC_BLOCK_LENGTH_4 ); sbc_cap.subbands = ( SBC_SUBBANDS_8 | SBC_SUBBANDS_4 ); sbc_cap.allocation_method = ( SBC_ALLOCATION_LOUDNESS | SBC_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 gboolean mpeg_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; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("Sink %p: Set_Configuration_Ind", sep); else debug("Source %p: Set_Configuration_Ind", sep); dev = a2dp_get_dev(session); if (!dev) { *err = AVDTP_UNSUPPORTED_CONFIGURATION; *category = 0x00; return FALSE; } 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 mpeg_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 mpeg_codec_cap mpeg_cap; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("Sink %p: Get_Capability_Ind", sep); else debug("Source %p: Get_Capability_Ind", sep); *caps = NULL; media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT, NULL, 0); *caps = g_slist_append(*caps, media_transport); memset(&mpeg_cap, 0, sizeof(struct mpeg_codec_cap)); mpeg_cap.cap.media_type = AVDTP_MEDIA_TYPE_AUDIO; mpeg_cap.cap.media_codec_type = A2DP_CODEC_MPEG12; mpeg_cap.frequency = ( MPEG_SAMPLING_FREQ_48000 | MPEG_SAMPLING_FREQ_44100 | MPEG_SAMPLING_FREQ_32000 | MPEG_SAMPLING_FREQ_24000 | MPEG_SAMPLING_FREQ_22050 | MPEG_SAMPLING_FREQ_16000 ); mpeg_cap.channel_mode = ( MPEG_CHANNEL_MODE_JOINT_STEREO | MPEG_CHANNEL_MODE_STEREO | MPEG_CHANNEL_MODE_DUAL_CHANNEL | MPEG_CHANNEL_MODE_MONO ); mpeg_cap.layer = ( MPEG_LAYER_MP3 | MPEG_LAYER_MP2 | MPEG_LAYER_MP1 ); mpeg_cap.bitrate = 0xFFFF; media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &mpeg_cap, sizeof(mpeg_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_setup *setup; struct device *dev; int ret; if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK) debug("Sink %p: Set_Configuration_Cfm", sep); else debug("Source %p: Set_Configuration_Cfm", sep);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -