2007-07-23 23:43:32 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
*
|
2010-01-07 17:02:51 +08:00
|
|
|
* Copyright (C) 2006-2010 Nokia Corporation
|
2010-01-02 09:08:17 +08:00
|
|
|
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
|
2007-07-23 23:43:32 +08:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2007-08-13 16:14:22 +08:00
|
|
|
#include <stdint.h>
|
2007-08-11 19:05:24 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
2008-05-09 04:23:45 +08:00
|
|
|
#include <bluetooth/bluetooth.h>
|
2009-04-23 02:41:53 +08:00
|
|
|
#include <bluetooth/sdp.h>
|
2008-05-09 04:23:45 +08:00
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
#include <glib.h>
|
|
|
|
#include <dbus/dbus.h>
|
2008-05-09 06:19:14 +08:00
|
|
|
#include <gdbus.h>
|
2007-08-11 19:05:24 +08:00
|
|
|
|
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
#include "device.h"
|
2009-03-25 03:39:20 +08:00
|
|
|
#include "avdtp.h"
|
2007-08-11 19:05:24 +08:00
|
|
|
#include "a2dp.h"
|
|
|
|
#include "error.h"
|
2007-08-13 07:58:15 +08:00
|
|
|
#include "sink.h"
|
2008-10-03 03:38:43 +08:00
|
|
|
#include "dbus-common.h"
|
2009-04-23 02:41:53 +08:00
|
|
|
#include "../src/adapter.h"
|
|
|
|
#include "../src/device.h"
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2008-11-15 04:27:16 +08:00
|
|
|
#define STREAM_SETUP_RETRY_TIMER 2
|
2007-10-09 21:36:33 +08:00
|
|
|
|
2007-08-14 05:43:30 +08:00
|
|
|
struct pending_request {
|
2007-08-16 23:42:10 +08:00
|
|
|
DBusConnection *conn;
|
2007-08-11 19:05:24 +08:00
|
|
|
DBusMessage *msg;
|
2007-08-17 05:48:33 +08:00
|
|
|
unsigned int id;
|
2007-08-11 19:05:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sink {
|
2009-04-15 18:32:13 +08:00
|
|
|
struct audio_device *dev;
|
2007-08-11 19:05:24 +08:00
|
|
|
struct avdtp *session;
|
|
|
|
struct avdtp_stream *stream;
|
2007-08-29 20:29:42 +08:00
|
|
|
unsigned int cb_id;
|
2009-04-23 02:41:53 +08:00
|
|
|
guint dc_id;
|
2009-04-27 16:10:24 +08:00
|
|
|
guint retry_id;
|
2009-03-25 03:41:43 +08:00
|
|
|
avdtp_session_state_t session_state;
|
|
|
|
avdtp_state_t stream_state;
|
2009-03-28 07:15:23 +08:00
|
|
|
sink_state_t state;
|
2007-08-16 23:42:10 +08:00
|
|
|
struct pending_request *connect;
|
|
|
|
struct pending_request *disconnect;
|
2007-08-11 19:05:24 +08:00
|
|
|
DBusConnection *conn;
|
|
|
|
};
|
|
|
|
|
2009-03-28 07:15:23 +08:00
|
|
|
struct sink_state_callback {
|
|
|
|
sink_state_cb cb;
|
|
|
|
void *user_data;
|
|
|
|
unsigned int id;
|
|
|
|
};
|
|
|
|
|
|
|
|
static GSList *sink_callbacks = NULL;
|
|
|
|
|
2009-03-25 03:41:43 +08:00
|
|
|
static unsigned int avdtp_callback_id = 0;
|
|
|
|
|
2009-03-28 07:15:23 +08:00
|
|
|
static const char *state2str(sink_state_t state)
|
2009-03-25 03:41:43 +08:00
|
|
|
{
|
2009-03-28 07:15:23 +08:00
|
|
|
switch (state) {
|
|
|
|
case SINK_STATE_DISCONNECTED:
|
2009-03-25 08:28:28 +08:00
|
|
|
return "disconnected";
|
2009-03-28 07:15:23 +08:00
|
|
|
case SINK_STATE_CONNECTING:
|
|
|
|
return "connecting";
|
|
|
|
case SINK_STATE_CONNECTED:
|
2009-03-25 03:41:43 +08:00
|
|
|
return "connected";
|
2009-03-28 07:15:23 +08:00
|
|
|
case SINK_STATE_PLAYING:
|
2009-03-25 03:41:43 +08:00
|
|
|
return "playing";
|
2009-03-28 07:15:23 +08:00
|
|
|
default:
|
|
|
|
error("Invalid sink state %d", state);
|
|
|
|
return NULL;
|
2009-03-25 03:41:43 +08:00
|
|
|
}
|
2009-03-28 07:15:23 +08:00
|
|
|
}
|
2009-03-25 03:41:43 +08:00
|
|
|
|
2009-03-28 07:15:23 +08:00
|
|
|
static void sink_set_state(struct audio_device *dev, sink_state_t new_state)
|
|
|
|
{
|
|
|
|
struct sink *sink = dev->sink;
|
|
|
|
const char *state_str;
|
|
|
|
sink_state_t old_state = sink->state;
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
sink->state = new_state;
|
|
|
|
|
|
|
|
state_str = state2str(new_state);
|
|
|
|
if (state_str)
|
|
|
|
emit_property_changed(dev->conn, dev->path,
|
|
|
|
AUDIO_SINK_INTERFACE, "State",
|
|
|
|
DBUS_TYPE_STRING, &state_str);
|
|
|
|
|
|
|
|
for (l = sink_callbacks; l != NULL; l = l->next) {
|
|
|
|
struct sink_state_callback *cb = l->data;
|
|
|
|
cb->cb(dev, old_state, new_state, cb->user_data);
|
|
|
|
}
|
2009-03-25 03:41:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void avdtp_state_callback(struct audio_device *dev,
|
2009-03-25 07:45:58 +08:00
|
|
|
struct avdtp *session,
|
2009-03-25 03:41:43 +08:00
|
|
|
avdtp_session_state_t old_state,
|
2009-03-25 07:45:58 +08:00
|
|
|
avdtp_session_state_t new_state,
|
2009-03-25 03:41:43 +08:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct sink *sink = dev->sink;
|
|
|
|
|
2009-03-25 05:14:24 +08:00
|
|
|
if (sink == NULL)
|
|
|
|
return;
|
|
|
|
|
2009-03-25 03:41:43 +08:00
|
|
|
switch (new_state) {
|
|
|
|
case AVDTP_SESSION_STATE_DISCONNECTED:
|
2009-03-28 07:15:23 +08:00
|
|
|
if (sink->state != SINK_STATE_CONNECTING) {
|
2009-03-27 05:44:58 +08:00
|
|
|
gboolean value = FALSE;
|
|
|
|
g_dbus_emit_signal(dev->conn, dev->path,
|
|
|
|
AUDIO_SINK_INTERFACE, "Disconnected",
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
emit_property_changed(dev->conn, dev->path,
|
|
|
|
AUDIO_SINK_INTERFACE, "Connected",
|
|
|
|
DBUS_TYPE_BOOLEAN, &value);
|
2009-05-07 00:30:57 +08:00
|
|
|
if (sink->dc_id) {
|
|
|
|
device_remove_disconnect_watch(dev->btd_dev,
|
|
|
|
sink->dc_id);
|
|
|
|
sink->dc_id = 0;
|
|
|
|
}
|
2009-03-25 03:41:43 +08:00
|
|
|
}
|
2009-03-28 07:15:23 +08:00
|
|
|
sink_set_state(dev, SINK_STATE_DISCONNECTED);
|
2009-03-25 03:41:43 +08:00
|
|
|
break;
|
|
|
|
case AVDTP_SESSION_STATE_CONNECTING:
|
2009-03-28 07:15:23 +08:00
|
|
|
sink_set_state(dev, SINK_STATE_CONNECTING);
|
2009-03-25 03:41:43 +08:00
|
|
|
break;
|
|
|
|
case AVDTP_SESSION_STATE_CONNECTED:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sink->session_state = new_state;
|
|
|
|
}
|
|
|
|
|
2009-04-15 18:32:13 +08:00
|
|
|
static void pending_request_free(struct audio_device *dev,
|
|
|
|
struct pending_request *pending)
|
2007-08-11 19:05:24 +08:00
|
|
|
{
|
2007-08-16 23:42:10 +08:00
|
|
|
if (pending->conn)
|
|
|
|
dbus_connection_unref(pending->conn);
|
|
|
|
if (pending->msg)
|
|
|
|
dbus_message_unref(pending->msg);
|
2009-04-15 18:32:13 +08:00
|
|
|
if (pending->id)
|
2009-07-04 02:23:57 +08:00
|
|
|
a2dp_cancel(dev, pending->id);
|
2009-04-15 18:32:13 +08:00
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
g_free(pending);
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
|
|
|
|
2009-04-27 21:43:29 +08:00
|
|
|
static void disconnect_cb(struct btd_device *btd_dev, gboolean removal,
|
|
|
|
void *user_data)
|
2009-04-23 02:41:53 +08:00
|
|
|
{
|
|
|
|
struct audio_device *device = user_data;
|
|
|
|
struct sink *sink = device->sink;
|
|
|
|
|
|
|
|
debug("Sink: disconnect %s", device->path);
|
|
|
|
|
2010-03-25 05:30:30 +08:00
|
|
|
avdtp_close(sink->session, sink->stream, TRUE);
|
2009-04-23 02:41:53 +08:00
|
|
|
}
|
|
|
|
|
2007-08-17 05:48:33 +08:00
|
|
|
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)
|
2007-08-11 19:05:24 +08:00
|
|
|
{
|
2008-05-29 16:05:16 +08:00
|
|
|
struct audio_device *dev = user_data;
|
2007-08-11 19:05:24 +08:00
|
|
|
struct sink *sink = dev->sink;
|
2008-10-03 03:38:43 +08:00
|
|
|
gboolean value;
|
2007-08-11 19:05:24 +08:00
|
|
|
|
|
|
|
if (err)
|
2007-08-16 23:42:10 +08:00
|
|
|
return;
|
2007-08-11 19:05:24 +08:00
|
|
|
|
|
|
|
switch (new_state) {
|
|
|
|
case AVDTP_STATE_IDLE:
|
2007-08-16 23:42:10 +08:00
|
|
|
if (sink->disconnect) {
|
|
|
|
DBusMessage *reply;
|
|
|
|
struct pending_request *p;
|
|
|
|
|
|
|
|
p = sink->disconnect;
|
|
|
|
sink->disconnect = NULL;
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(p->msg);
|
2008-10-24 20:49:52 +08:00
|
|
|
g_dbus_send_message(p->conn, reply);
|
2009-04-15 18:32:13 +08:00
|
|
|
pending_request_free(dev, p);
|
2007-08-16 23:42:10 +08:00
|
|
|
}
|
|
|
|
|
2009-05-07 00:30:57 +08:00
|
|
|
if (sink->dc_id) {
|
|
|
|
device_remove_disconnect_watch(dev->btd_dev,
|
|
|
|
sink->dc_id);
|
|
|
|
sink->dc_id = 0;
|
|
|
|
}
|
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
if (sink->session) {
|
|
|
|
avdtp_unref(sink->session);
|
|
|
|
sink->session = NULL;
|
|
|
|
}
|
2007-08-13 05:34:50 +08:00
|
|
|
sink->stream = NULL;
|
2007-08-29 20:29:42 +08:00
|
|
|
sink->cb_id = 0;
|
2007-08-11 19:05:24 +08:00
|
|
|
break;
|
|
|
|
case AVDTP_STATE_OPEN:
|
2009-03-28 07:15:23 +08:00
|
|
|
if (old_state == AVDTP_STATE_CONFIGURED &&
|
|
|
|
sink->state == SINK_STATE_CONNECTING) {
|
2008-10-03 03:42:54 +08:00
|
|
|
value = TRUE;
|
2008-06-08 03:30:24 +08:00
|
|
|
g_dbus_emit_signal(dev->conn, dev->path,
|
2008-10-17 03:36:30 +08:00
|
|
|
AUDIO_SINK_INTERFACE,
|
|
|
|
"Connected",
|
|
|
|
DBUS_TYPE_INVALID);
|
2008-10-17 03:03:25 +08:00
|
|
|
emit_property_changed(dev->conn, dev->path,
|
|
|
|
AUDIO_SINK_INTERFACE,
|
|
|
|
"Connected",
|
|
|
|
DBUS_TYPE_BOOLEAN, &value);
|
2009-04-23 02:41:53 +08:00
|
|
|
sink->dc_id = device_add_disconnect_watch(dev->btd_dev,
|
|
|
|
disconnect_cb,
|
|
|
|
dev, NULL);
|
2008-10-03 03:42:54 +08:00
|
|
|
} else if (old_state == AVDTP_STATE_STREAMING) {
|
|
|
|
value = FALSE;
|
2008-06-08 03:30:24 +08:00
|
|
|
g_dbus_emit_signal(dev->conn, dev->path,
|
2008-10-17 03:36:30 +08:00
|
|
|
AUDIO_SINK_INTERFACE,
|
|
|
|
"Stopped",
|
|
|
|
DBUS_TYPE_INVALID);
|
2008-10-17 03:03:25 +08:00
|
|
|
emit_property_changed(dev->conn, dev->path,
|
|
|
|
AUDIO_SINK_INTERFACE,
|
|
|
|
"Playing",
|
|
|
|
DBUS_TYPE_BOOLEAN, &value);
|
2008-10-03 03:42:54 +08:00
|
|
|
}
|
2009-03-28 07:15:23 +08:00
|
|
|
sink_set_state(dev, SINK_STATE_CONNECTED);
|
2007-08-11 19:05:24 +08:00
|
|
|
break;
|
|
|
|
case AVDTP_STATE_STREAMING:
|
2008-10-03 03:42:54 +08:00
|
|
|
value = TRUE;
|
2008-10-17 03:36:30 +08:00
|
|
|
g_dbus_emit_signal(dev->conn, dev->path, AUDIO_SINK_INTERFACE,
|
|
|
|
"Playing", DBUS_TYPE_INVALID);
|
2008-10-17 03:03:25 +08:00
|
|
|
emit_property_changed(dev->conn, dev->path,
|
|
|
|
AUDIO_SINK_INTERFACE, "Playing",
|
|
|
|
DBUS_TYPE_BOOLEAN, &value);
|
2009-03-28 07:15:23 +08:00
|
|
|
sink_set_state(dev, SINK_STATE_PLAYING);
|
2007-08-22 22:15:43 +08:00
|
|
|
break;
|
|
|
|
case AVDTP_STATE_CONFIGURED:
|
2007-08-11 19:05:24 +08:00
|
|
|
case AVDTP_STATE_CLOSING:
|
|
|
|
case AVDTP_STATE_ABORTING:
|
2007-08-16 23:42:10 +08:00
|
|
|
default:
|
2007-08-11 19:05:24 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-03-25 03:41:43 +08:00
|
|
|
sink->stream_state = new_state;
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
|
|
|
|
2008-08-05 03:39:10 +08:00
|
|
|
static DBusHandlerResult error_failed(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, const char * desc)
|
|
|
|
{
|
|
|
|
return error_common_reply(conn, msg, ERROR_INTERFACE ".Failed", desc);
|
|
|
|
}
|
|
|
|
|
2007-10-09 21:36:33 +08:00
|
|
|
static gboolean stream_setup_retry(gpointer user_data)
|
|
|
|
{
|
|
|
|
struct sink *sink = user_data;
|
|
|
|
struct pending_request *pending = sink->connect;
|
|
|
|
|
2009-04-27 16:10:24 +08:00
|
|
|
sink->retry_id = 0;
|
|
|
|
|
2009-03-25 03:41:43 +08:00
|
|
|
if (sink->stream_state >= AVDTP_STATE_OPEN) {
|
2007-10-09 21:36:33 +08:00
|
|
|
debug("Stream successfully created, after XCASE connect:connect");
|
2009-02-03 11:26:23 +08:00
|
|
|
if (pending->msg) {
|
|
|
|
DBusMessage *reply;
|
|
|
|
reply = dbus_message_new_method_return(pending->msg);
|
|
|
|
g_dbus_send_message(pending->conn, reply);
|
|
|
|
}
|
2007-10-09 21:36:33 +08:00
|
|
|
} else {
|
|
|
|
debug("Stream setup failed, after XCASE connect:connect");
|
2009-02-03 11:26:23 +08:00
|
|
|
if (pending->msg)
|
|
|
|
error_failed(pending->conn, pending->msg, "Stream setup failed");
|
2007-10-09 21:36:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sink->connect = NULL;
|
2009-04-15 18:32:13 +08:00
|
|
|
pending_request_free(sink->dev, pending);
|
2007-10-09 21:36:33 +08:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-08-31 18:25:18 +08:00
|
|
|
static void stream_setup_complete(struct avdtp *session, struct a2dp_sep *sep,
|
2007-08-16 23:42:10 +08:00
|
|
|
struct avdtp_stream *stream,
|
2007-12-04 06:41:29 +08:00
|
|
|
struct avdtp_error *err, void *user_data)
|
2007-08-11 19:05:24 +08:00
|
|
|
{
|
2007-08-31 18:25:18 +08:00
|
|
|
struct sink *sink = user_data;
|
2007-08-16 23:42:10 +08:00
|
|
|
struct pending_request *pending;
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
pending = sink->connect;
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2009-04-15 18:32:13 +08:00
|
|
|
pending->id = 0;
|
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
if (stream) {
|
2009-02-03 11:26:23 +08:00
|
|
|
debug("Stream successfully created");
|
|
|
|
|
|
|
|
if (pending->msg) {
|
|
|
|
DBusMessage *reply;
|
|
|
|
reply = dbus_message_new_method_return(pending->msg);
|
|
|
|
g_dbus_send_message(pending->conn, reply);
|
|
|
|
}
|
|
|
|
|
2007-10-09 21:36:33 +08:00
|
|
|
sink->connect = NULL;
|
2009-04-15 18:32:13 +08:00
|
|
|
pending_request_free(sink->dev, pending);
|
2009-02-03 11:26:23 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
avdtp_unref(sink->session);
|
|
|
|
sink->session = NULL;
|
|
|
|
if (avdtp_error_type(err) == AVDTP_ERROR_ERRNO
|
|
|
|
&& avdtp_error_posix_errno(err) != EHOSTDOWN) {
|
|
|
|
debug("connect:connect XCASE detected");
|
2009-04-27 16:10:24 +08:00
|
|
|
sink->retry_id = g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
|
|
|
|
stream_setup_retry,
|
|
|
|
sink);
|
2007-10-03 21:54:14 +08:00
|
|
|
} else {
|
2009-02-03 11:26:23 +08:00
|
|
|
if (pending->msg)
|
2007-11-26 21:43:17 +08:00
|
|
|
error_failed(pending->conn, pending->msg, "Stream setup failed");
|
2009-02-03 11:26:23 +08:00
|
|
|
sink->connect = NULL;
|
2009-04-15 18:32:13 +08:00
|
|
|
pending_request_free(sink->dev, pending);
|
2009-02-03 11:26:23 +08:00
|
|
|
debug("Stream setup failed : %s", avdtp_strerror(err));
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-04 06:41:29 +08:00
|
|
|
static uint8_t default_bitpool(uint8_t freq, uint8_t mode)
|
|
|
|
{
|
|
|
|
switch (freq) {
|
2008-01-23 21:11:07 +08:00
|
|
|
case SBC_SAMPLING_FREQ_16000:
|
|
|
|
case SBC_SAMPLING_FREQ_32000:
|
2007-12-04 06:41:29 +08:00
|
|
|
return 53;
|
2008-01-23 21:11:07 +08:00
|
|
|
case SBC_SAMPLING_FREQ_44100:
|
2007-12-04 06:41:29 +08:00
|
|
|
switch (mode) {
|
2008-01-23 21:11:07 +08:00
|
|
|
case SBC_CHANNEL_MODE_MONO:
|
|
|
|
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
|
2007-12-04 06:41:29 +08:00
|
|
|
return 31;
|
2008-01-23 21:11:07 +08:00
|
|
|
case SBC_CHANNEL_MODE_STEREO:
|
|
|
|
case SBC_CHANNEL_MODE_JOINT_STEREO:
|
2007-12-04 06:41:29 +08:00
|
|
|
return 53;
|
|
|
|
default:
|
|
|
|
error("Invalid channel mode %u", mode);
|
|
|
|
return 53;
|
|
|
|
}
|
2008-01-23 21:11:07 +08:00
|
|
|
case SBC_SAMPLING_FREQ_48000:
|
2007-12-04 06:41:29 +08:00
|
|
|
switch (mode) {
|
2008-01-23 21:11:07 +08:00
|
|
|
case SBC_CHANNEL_MODE_MONO:
|
|
|
|
case SBC_CHANNEL_MODE_DUAL_CHANNEL:
|
2007-12-04 06:41:29 +08:00
|
|
|
return 29;
|
2008-01-23 21:11:07 +08:00
|
|
|
case SBC_CHANNEL_MODE_STEREO:
|
|
|
|
case SBC_CHANNEL_MODE_JOINT_STEREO:
|
2007-12-04 06:41:29 +08:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
unsigned int 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;
|
|
|
|
|
2008-01-23 21:11:07 +08:00
|
|
|
if (supported->frequency & SBC_SAMPLING_FREQ_44100)
|
|
|
|
cap->frequency = SBC_SAMPLING_FREQ_44100;
|
|
|
|
else if (supported->frequency & SBC_SAMPLING_FREQ_48000)
|
|
|
|
cap->frequency = SBC_SAMPLING_FREQ_48000;
|
|
|
|
else if (supported->frequency & SBC_SAMPLING_FREQ_32000)
|
|
|
|
cap->frequency = SBC_SAMPLING_FREQ_32000;
|
|
|
|
else if (supported->frequency & SBC_SAMPLING_FREQ_16000)
|
|
|
|
cap->frequency = SBC_SAMPLING_FREQ_16000;
|
2007-12-04 06:41:29 +08:00
|
|
|
else {
|
|
|
|
error("No supported frequencies");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-23 21:11:07 +08:00
|
|
|
if (supported->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
|
|
|
|
cap->channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
|
|
|
|
else if (supported->channel_mode & SBC_CHANNEL_MODE_STEREO)
|
|
|
|
cap->channel_mode = SBC_CHANNEL_MODE_STEREO;
|
|
|
|
else if (supported->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
|
|
|
|
cap->channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
|
|
|
|
else if (supported->channel_mode & SBC_CHANNEL_MODE_MONO)
|
|
|
|
cap->channel_mode = SBC_CHANNEL_MODE_MONO;
|
2007-12-04 06:41:29 +08:00
|
|
|
else {
|
|
|
|
error("No supported channel modes");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-23 21:11:07 +08:00
|
|
|
if (supported->block_length & SBC_BLOCK_LENGTH_16)
|
|
|
|
cap->block_length = SBC_BLOCK_LENGTH_16;
|
|
|
|
else if (supported->block_length & SBC_BLOCK_LENGTH_12)
|
|
|
|
cap->block_length = SBC_BLOCK_LENGTH_12;
|
|
|
|
else if (supported->block_length & SBC_BLOCK_LENGTH_8)
|
|
|
|
cap->block_length = SBC_BLOCK_LENGTH_8;
|
|
|
|
else if (supported->block_length & SBC_BLOCK_LENGTH_4)
|
|
|
|
cap->block_length = SBC_BLOCK_LENGTH_4;
|
2007-12-04 06:41:29 +08:00
|
|
|
else {
|
|
|
|
error("No supported block lengths");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-23 21:11:07 +08:00
|
|
|
if (supported->subbands & SBC_SUBBANDS_8)
|
|
|
|
cap->subbands = SBC_SUBBANDS_8;
|
|
|
|
else if (supported->subbands & SBC_SUBBANDS_4)
|
|
|
|
cap->subbands = SBC_SUBBANDS_4;
|
2007-12-04 06:41:29 +08:00
|
|
|
else {
|
|
|
|
error("No supported subbands");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-01-23 21:11:07 +08:00
|
|
|
if (supported->allocation_method & SBC_ALLOCATION_LOUDNESS)
|
|
|
|
cap->allocation_method = SBC_ALLOCATION_LOUDNESS;
|
|
|
|
else if (supported->allocation_method & SBC_ALLOCATION_SNR)
|
|
|
|
cap->allocation_method = SBC_ALLOCATION_SNR;
|
2007-12-04 06:41:29 +08:00
|
|
|
|
|
|
|
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 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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2009-10-06 21:41:53 +08:00
|
|
|
if (avdtp_get_delay_reporting(rsep)) {
|
|
|
|
struct avdtp_service_capability *delay_reporting;
|
|
|
|
delay_reporting = avdtp_service_cap_new(AVDTP_DELAY_REPORTING,
|
|
|
|
NULL, 0);
|
|
|
|
*caps = g_slist_append(*caps, delay_reporting);
|
|
|
|
}
|
2007-12-04 06:41:29 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void discovery_complete(struct avdtp *session, GSList *seps, struct avdtp_error *err,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
struct sink *sink = user_data;
|
|
|
|
struct pending_request *pending;
|
|
|
|
struct avdtp_local_sep *lsep;
|
|
|
|
struct avdtp_remote_sep *rsep;
|
2009-03-18 22:20:31 +08:00
|
|
|
struct a2dp_sep *sep;
|
2007-12-04 06:41:29 +08:00
|
|
|
GSList *caps = NULL;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
pending = sink->connect;
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
avdtp_unref(sink->session);
|
|
|
|
sink->session = NULL;
|
|
|
|
if (avdtp_error_type(err) == AVDTP_ERROR_ERRNO
|
|
|
|
&& avdtp_error_posix_errno(err) != EHOSTDOWN) {
|
|
|
|
debug("connect:connect XCASE detected");
|
2009-04-27 16:10:24 +08:00
|
|
|
sink->retry_id =
|
|
|
|
g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
|
|
|
|
stream_setup_retry,
|
|
|
|
sink);
|
2007-12-04 06:41:29 +08:00
|
|
|
} else
|
|
|
|
goto failed;
|
|
|
|
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");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!select_capabilities(session, rsep, &caps)) {
|
|
|
|
error("Unable to select remote SEP capabilities");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2009-07-04 02:23:57 +08:00
|
|
|
sep = a2dp_get(session, rsep);
|
2009-04-16 04:11:26 +08:00
|
|
|
if (!sep) {
|
|
|
|
error("Unable to get a local source SEP");
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2009-07-04 02:23:57 +08:00
|
|
|
id = a2dp_config(sink->session, sep, stream_setup_complete, caps, sink);
|
2007-12-04 06:41:29 +08:00
|
|
|
if (id == 0)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
pending->id = id;
|
|
|
|
return;
|
|
|
|
|
|
|
|
failed:
|
2009-02-03 11:26:23 +08:00
|
|
|
if (pending->msg)
|
|
|
|
error_failed(pending->conn, pending->msg, "Stream setup failed");
|
2009-04-15 18:32:13 +08:00
|
|
|
pending_request_free(sink->dev, pending);
|
2007-12-04 06:41:29 +08:00
|
|
|
sink->connect = NULL;
|
|
|
|
avdtp_unref(sink->session);
|
|
|
|
sink->session = NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-03 11:26:23 +08:00
|
|
|
gboolean sink_setup_stream(struct sink *sink, struct avdtp *session)
|
|
|
|
{
|
|
|
|
if (sink->connect || sink->disconnect)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (session && !sink->session)
|
|
|
|
sink->session = avdtp_ref(session);
|
|
|
|
|
2009-03-25 06:16:32 +08:00
|
|
|
if (!sink->session)
|
|
|
|
return FALSE;
|
|
|
|
|
2009-04-02 22:30:31 +08:00
|
|
|
avdtp_set_auto_disconnect(sink->session, FALSE);
|
|
|
|
|
2009-02-03 11:26:23 +08:00
|
|
|
if (avdtp_discover(sink->session, discovery_complete, sink) < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
sink->connect = g_new0(struct pending_request, 1);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
static DBusMessage *sink_connect(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *data)
|
2007-08-11 19:05:24 +08:00
|
|
|
{
|
2008-05-29 16:05:16 +08:00
|
|
|
struct audio_device *dev = data;
|
2007-08-11 19:05:24 +08:00
|
|
|
struct sink *sink = dev->sink;
|
2007-08-16 23:42:10 +08:00
|
|
|
struct pending_request *pending;
|
2007-08-11 19:05:24 +08:00
|
|
|
|
|
|
|
if (!sink->session)
|
|
|
|
sink->session = avdtp_get(&dev->src, &dev->dst);
|
|
|
|
|
2007-08-21 14:48:14 +08:00
|
|
|
if (!sink->session)
|
2008-06-02 22:34:58 +08:00
|
|
|
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
|
|
|
|
"Unable to get a session");
|
2007-08-21 14:48:14 +08:00
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
if (sink->connect || sink->disconnect)
|
2008-06-02 22:34:58 +08:00
|
|
|
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
|
|
|
|
"%s", strerror(EBUSY));
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2009-03-25 03:41:43 +08:00
|
|
|
if (sink->stream_state >= AVDTP_STATE_OPEN)
|
2008-06-02 22:34:58 +08:00
|
|
|
return g_dbus_create_error(msg, ERROR_INTERFACE
|
|
|
|
".AlreadyConnected",
|
|
|
|
"Device Already Connected");
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2009-02-03 11:26:23 +08:00
|
|
|
if (!sink_setup_stream(sink, NULL))
|
|
|
|
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
|
|
|
|
"Failed to create a stream");
|
|
|
|
|
2009-03-28 01:10:04 +08:00
|
|
|
dev->auto_connect = FALSE;
|
|
|
|
|
2009-02-03 11:26:23 +08:00
|
|
|
pending = sink->connect;
|
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
pending->conn = dbus_connection_ref(conn);
|
|
|
|
pending->msg = dbus_message_ref(msg);
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2007-08-24 20:15:20 +08:00
|
|
|
debug("stream creation in progress");
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
return NULL;
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
static DBusMessage *sink_disconnect(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *data)
|
2007-08-11 19:05:24 +08:00
|
|
|
{
|
2008-05-29 16:05:16 +08:00
|
|
|
struct audio_device *device = data;
|
2007-08-11 19:05:24 +08:00
|
|
|
struct sink *sink = device->sink;
|
2007-08-16 23:42:10 +08:00
|
|
|
struct pending_request *pending;
|
2007-08-11 19:05:24 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!sink->session)
|
2008-06-02 22:34:58 +08:00
|
|
|
return g_dbus_create_error(msg, ERROR_INTERFACE
|
|
|
|
".NotConnected",
|
|
|
|
"Device not Connected");
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
if (sink->connect || sink->disconnect)
|
2008-06-02 22:34:58 +08:00
|
|
|
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
|
|
|
|
"%s", strerror(EBUSY));
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2009-03-25 03:41:43 +08:00
|
|
|
if (sink->stream_state < AVDTP_STATE_OPEN) {
|
2007-08-16 23:42:10 +08:00
|
|
|
DBusMessage *reply = dbus_message_new_method_return(msg);
|
|
|
|
if (!reply)
|
2008-06-02 22:34:58 +08:00
|
|
|
return NULL;
|
2007-08-11 19:05:24 +08:00
|
|
|
avdtp_unref(sink->session);
|
|
|
|
sink->session = NULL;
|
2008-06-02 22:34:58 +08:00
|
|
|
return reply;
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
|
|
|
|
2010-03-25 05:30:30 +08:00
|
|
|
err = avdtp_close(sink->session, sink->stream, FALSE);
|
2007-08-16 23:42:10 +08:00
|
|
|
if (err < 0)
|
2008-06-02 22:34:58 +08:00
|
|
|
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
|
|
|
|
"%s", strerror(-err));
|
2007-08-16 23:42:10 +08:00
|
|
|
|
|
|
|
pending = g_new0(struct pending_request, 1);
|
|
|
|
pending->conn = dbus_connection_ref(conn);
|
|
|
|
pending->msg = dbus_message_ref(msg);
|
|
|
|
sink->disconnect = pending;
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
return NULL;
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
static DBusMessage *sink_is_connected(DBusConnection *conn,
|
|
|
|
DBusMessage *msg,
|
|
|
|
void *data)
|
2007-08-11 19:05:24 +08:00
|
|
|
{
|
2008-05-29 16:05:16 +08:00
|
|
|
struct audio_device *device = data;
|
2007-08-11 19:05:24 +08:00
|
|
|
struct sink *sink = device->sink;
|
|
|
|
DBusMessage *reply;
|
|
|
|
dbus_bool_t connected;
|
|
|
|
|
|
|
|
reply = dbus_message_new_method_return(msg);
|
|
|
|
if (!reply)
|
2008-06-02 22:34:58 +08:00
|
|
|
return NULL;
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2009-03-25 03:41:43 +08:00
|
|
|
connected = (sink->stream_state >= AVDTP_STATE_CONFIGURED);
|
2007-08-11 19:05:24 +08:00
|
|
|
|
|
|
|
dbus_message_append_args(reply, DBUS_TYPE_BOOLEAN, &connected,
|
|
|
|
DBUS_TYPE_INVALID);
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
return reply;
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
|
|
|
|
2008-10-03 03:38:43 +08:00
|
|
|
static DBusMessage *sink_get_properties(DBusConnection *conn,
|
|
|
|
DBusMessage *msg, void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *device = data;
|
2009-03-25 08:28:28 +08:00
|
|
|
struct sink *sink = device->sink;
|
2008-10-03 03:38:43 +08:00
|
|
|
DBusMessage *reply;
|
|
|
|
DBusMessageIter iter;
|
|
|
|
DBusMessageIter dict;
|
2009-03-25 08:28:28 +08:00
|
|
|
const char *state;
|
2008-10-03 03:38:43 +08:00
|
|
|
gboolean value;
|
|
|
|
|
2008-10-03 14:29:51 +08:00
|
|
|
reply = dbus_message_new_method_return(msg);
|
|
|
|
if (!reply)
|
|
|
|
return NULL;
|
|
|
|
|
2008-10-03 03:38:43 +08:00
|
|
|
dbus_message_iter_init_append(reply, &iter);
|
|
|
|
|
|
|
|
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
|
|
|
|
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
|
|
|
|
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
|
|
|
|
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
|
|
|
|
|
|
|
|
/* Playing */
|
2009-03-25 08:28:28 +08:00
|
|
|
value = (sink->stream_state == AVDTP_STATE_STREAMING);
|
2008-10-17 02:23:43 +08:00
|
|
|
dict_append_entry(&dict, "Playing", DBUS_TYPE_BOOLEAN, &value);
|
2008-10-03 03:38:43 +08:00
|
|
|
|
|
|
|
/* Connected */
|
2009-03-25 08:28:28 +08:00
|
|
|
value = (sink->stream_state >= AVDTP_STATE_CONFIGURED);
|
2008-10-17 02:23:43 +08:00
|
|
|
dict_append_entry(&dict, "Connected", DBUS_TYPE_BOOLEAN, &value);
|
2008-10-03 03:38:43 +08:00
|
|
|
|
2009-03-25 08:28:28 +08:00
|
|
|
/* State */
|
2009-03-28 07:15:23 +08:00
|
|
|
state = state2str(sink->state);
|
2009-03-25 08:28:28 +08:00
|
|
|
if (state)
|
|
|
|
dict_append_entry(&dict, "State", DBUS_TYPE_STRING, &state);
|
|
|
|
|
2008-10-03 03:38:43 +08:00
|
|
|
dbus_message_iter_close_container(&iter, &dict);
|
|
|
|
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
static GDBusMethodTable sink_methods[] = {
|
|
|
|
{ "Connect", "", "", sink_connect,
|
|
|
|
G_DBUS_METHOD_FLAG_ASYNC },
|
|
|
|
{ "Disconnect", "", "", sink_disconnect,
|
|
|
|
G_DBUS_METHOD_FLAG_ASYNC },
|
2008-10-03 06:16:39 +08:00
|
|
|
{ "IsConnected", "", "b", sink_is_connected,
|
|
|
|
G_DBUS_METHOD_FLAG_DEPRECATED },
|
2008-10-03 03:38:43 +08:00
|
|
|
{ "GetProperties", "", "a{sv}",sink_get_properties },
|
2007-08-11 19:05:24 +08:00
|
|
|
{ NULL, NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2008-06-02 22:34:58 +08:00
|
|
|
static GDBusSignalTable sink_signals[] = {
|
2008-10-03 06:16:39 +08:00
|
|
|
{ "Connected", "", G_DBUS_SIGNAL_FLAG_DEPRECATED },
|
|
|
|
{ "Disconnected", "", G_DBUS_SIGNAL_FLAG_DEPRECATED },
|
|
|
|
{ "Playing", "", G_DBUS_SIGNAL_FLAG_DEPRECATED },
|
|
|
|
{ "Stopped", "", G_DBUS_SIGNAL_FLAG_DEPRECATED },
|
2008-10-03 03:38:43 +08:00
|
|
|
{ "PropertyChanged", "sv" },
|
2007-08-11 19:05:24 +08:00
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2008-07-31 04:57:50 +08:00
|
|
|
static void sink_free(struct audio_device *dev)
|
2007-08-11 19:05:24 +08:00
|
|
|
{
|
|
|
|
struct sink *sink = dev->sink;
|
|
|
|
|
2007-08-29 20:29:42 +08:00
|
|
|
if (sink->cb_id)
|
|
|
|
avdtp_stream_remove_cb(sink->session, sink->stream,
|
|
|
|
sink->cb_id);
|
|
|
|
|
2009-05-04 21:13:59 +08:00
|
|
|
if (sink->dc_id)
|
|
|
|
device_remove_disconnect_watch(dev->btd_dev, sink->dc_id);
|
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
if (sink->session)
|
|
|
|
avdtp_unref(sink->session);
|
|
|
|
|
2007-08-16 23:42:10 +08:00
|
|
|
if (sink->connect)
|
2009-04-15 18:32:13 +08:00
|
|
|
pending_request_free(dev, sink->connect);
|
2007-08-16 23:42:10 +08:00
|
|
|
|
|
|
|
if (sink->disconnect)
|
2009-04-15 18:32:13 +08:00
|
|
|
pending_request_free(dev, sink->disconnect);
|
2007-08-11 19:05:24 +08:00
|
|
|
|
2009-04-27 16:10:24 +08:00
|
|
|
if (sink->retry_id)
|
|
|
|
g_source_remove(sink->retry_id);
|
|
|
|
|
2007-08-11 19:05:24 +08:00
|
|
|
g_free(sink);
|
|
|
|
dev->sink = NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-31 04:57:50 +08:00
|
|
|
static void path_unregister(void *data)
|
|
|
|
{
|
|
|
|
struct audio_device *dev = data;
|
|
|
|
|
2009-03-24 18:31:10 +08:00
|
|
|
debug("Unregistered interface %s on path %s",
|
2008-07-31 04:57:50 +08:00
|
|
|
AUDIO_SINK_INTERFACE, dev->path);
|
|
|
|
|
|
|
|
sink_free(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sink_unregister(struct audio_device *dev)
|
|
|
|
{
|
|
|
|
g_dbus_unregister_interface(dev->conn, dev->path,
|
|
|
|
AUDIO_SINK_INTERFACE);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sink *sink_init(struct audio_device *dev)
|
|
|
|
{
|
2009-04-15 18:32:13 +08:00
|
|
|
struct sink *sink;
|
|
|
|
|
2008-07-31 04:57:50 +08:00
|
|
|
if (!g_dbus_register_interface(dev->conn, dev->path,
|
|
|
|
AUDIO_SINK_INTERFACE,
|
|
|
|
sink_methods, sink_signals, NULL,
|
|
|
|
dev, path_unregister))
|
|
|
|
return NULL;
|
|
|
|
|
2009-03-24 18:31:10 +08:00
|
|
|
debug("Registered interface %s on path %s",
|
2008-07-31 04:57:50 +08:00
|
|
|
AUDIO_SINK_INTERFACE, dev->path);
|
|
|
|
|
2009-03-25 03:41:43 +08:00
|
|
|
if (avdtp_callback_id == 0)
|
|
|
|
avdtp_callback_id = avdtp_add_state_cb(avdtp_state_callback,
|
|
|
|
NULL);
|
|
|
|
|
2009-04-15 18:32:13 +08:00
|
|
|
sink = g_new0(struct sink, 1);
|
|
|
|
|
|
|
|
sink->dev = dev;
|
|
|
|
|
|
|
|
return sink;
|
2008-07-31 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2008-05-29 16:05:16 +08:00
|
|
|
gboolean sink_is_active(struct audio_device *dev)
|
2007-08-11 19:05:24 +08:00
|
|
|
{
|
|
|
|
struct sink *sink = dev->sink;
|
|
|
|
|
|
|
|
if (sink->session)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-05-29 16:05:16 +08:00
|
|
|
avdtp_state_t sink_get_state(struct audio_device *dev)
|
2007-08-11 19:05:24 +08:00
|
|
|
{
|
|
|
|
struct sink *sink = dev->sink;
|
|
|
|
|
2009-03-25 03:41:43 +08:00
|
|
|
return sink->stream_state;
|
2007-08-11 19:05:24 +08:00
|
|
|
}
|
2007-08-13 05:34:50 +08:00
|
|
|
|
2008-05-29 16:05:16 +08:00
|
|
|
gboolean sink_new_stream(struct audio_device *dev, struct avdtp *session,
|
2007-08-13 07:58:15 +08:00
|
|
|
struct avdtp_stream *stream)
|
2007-08-13 05:34:50 +08:00
|
|
|
{
|
2007-08-13 07:58:15 +08:00
|
|
|
struct sink *sink = dev->sink;
|
2007-08-13 05:34:50 +08:00
|
|
|
|
|
|
|
if (sink->stream)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!sink->session)
|
|
|
|
sink->session = avdtp_ref(session);
|
|
|
|
|
|
|
|
sink->stream = stream;
|
|
|
|
|
2007-08-29 20:29:42 +08:00
|
|
|
sink->cb_id = avdtp_stream_add_cb(session, stream,
|
|
|
|
stream_state_changed, dev);
|
2007-08-13 05:34:50 +08:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-03-28 03:20:07 +08:00
|
|
|
|
|
|
|
gboolean sink_shutdown(struct sink *sink)
|
|
|
|
{
|
|
|
|
if (!sink->stream)
|
|
|
|
return FALSE;
|
|
|
|
|
2010-03-25 05:30:30 +08:00
|
|
|
if (avdtp_close(sink->session, sink->stream, FALSE) < 0)
|
2009-03-28 03:20:07 +08:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2009-03-28 07:15:23 +08:00
|
|
|
|
|
|
|
unsigned int sink_add_state_cb(sink_state_cb cb, void *user_data)
|
|
|
|
{
|
|
|
|
struct sink_state_callback *state_cb;
|
|
|
|
static unsigned int id = 0;
|
|
|
|
|
|
|
|
state_cb = g_new(struct sink_state_callback, 1);
|
|
|
|
state_cb->cb = cb;
|
|
|
|
state_cb->user_data = user_data;
|
|
|
|
state_cb->id = ++id;
|
|
|
|
|
|
|
|
sink_callbacks = g_slist_append(sink_callbacks, state_cb);
|
|
|
|
|
|
|
|
return state_cb->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean sink_remove_state_cb(unsigned int id)
|
|
|
|
{
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
for (l = sink_callbacks; l != NULL; l = l->next) {
|
|
|
|
struct sink_state_callback *cb = l->data;
|
|
|
|
if (cb && cb->id == id) {
|
|
|
|
sink_callbacks = g_slist_remove(sink_callbacks, cb);
|
|
|
|
g_free(cb);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|