⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 a2dp.c

📁 Linux的蓝牙操作工具。配合bluez-lib使用
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * *  BlueZ - Bluetooth protocol stack for Linux * *  Copyright (C) 2006-2007  Nokia Corporation *  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 <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;	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 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_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) {			setup->err = err;			finalize_config(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_config_errno(setup, ret);	}}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_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);		setup_unref(setup);		return;	}	if (setup->reconfigure)		setup->reconfigure = FALSE;	if (err) {		setup->stream = NULL;		setup->err = err;		finalize_config(setup);	}	else		finalize_config_errno(setup, 0);}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");	if (!a2dp_sep->locked) {		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_setup *setup;	if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)		debug("SBC Sink: Start_Cfm");	else		debug("SBC Source: Start_Cfm");	setup = find_setup_by_session(session);	if (!setup)		return;	if (setup->canceled) {		if (!err)			avdtp_close(session, stream);		setup_unref(setup);		return;	}	if (err) {		setup->stream = NULL;		setup->err = err;		finalize_resume(setup);	}	else		finalize_resume_errno(setup, 0);}static gboolean suspend_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: Suspend_Ind");	else		debug("SBC Source: Suspend_Ind");	if (a2dp_sep->suspend_timer) {		g_source_remove(a2dp_sep->suspend_timer);		a2dp_sep->suspend_timer = 0;		avdtp_unref(a2dp_sep->session);		a2dp_sep->session = NULL;	}	return TRUE;}static void suspend_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;	gboolean start;	if (a2dp_sep->type == AVDTP_SEP_TYPE_SINK)		debug("SBC Sink: Suspend_Cfm");	else		debug("SBC Source: Suspend_Cfm");	a2dp_sep->suspending = FALSE;	setup = find_setup_by_session(session);	if (!setup)		return;	start = setup->start;	setup->start = FALSE;	if (err) {		setup->stream = NULL;		setup->err = err;		finalize_suspend(setup);	}	else		finalize_suspend_errno(setup, 0);	if (!start)		return;	if (err) {		setup->err = err;		finalize_suspend(setup);	} else if (avdtp_start(session, a2dp_sep->stream) < 0) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -