~ubuntu-branches/ubuntu/wily/bluez/wily

« back to all changes in this revision

Viewing changes to audio/sink.c

ImportĀ upstreamĀ versionĀ 4.81

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *
3
3
 *  BlueZ - Bluetooth protocol stack for Linux
4
4
 *
5
 
 *  Copyright (C) 2006-2007  Nokia Corporation
6
 
 *  Copyright (C) 2004-2009  Marcel Holtmann <marcel@holtmann.org>
 
5
 *  Copyright (C) 2006-2010  Nokia Corporation
 
6
 *  Copyright (C) 2004-2010  Marcel Holtmann <marcel@holtmann.org>
7
7
 *
8
8
 *
9
9
 *  This program is free software; you can redistribute it and/or modify
30
30
#include <errno.h>
31
31
 
32
32
#include <bluetooth/bluetooth.h>
 
33
#include <bluetooth/sdp.h>
33
34
 
34
35
#include <glib.h>
35
36
#include <dbus/dbus.h>
36
37
#include <gdbus.h>
37
38
 
38
 
#include "logging.h"
 
39
#include "log.h"
39
40
 
40
41
#include "device.h"
41
42
#include "avdtp.h"
 
43
#include "media.h"
42
44
#include "a2dp.h"
43
45
#include "error.h"
44
46
#include "sink.h"
45
47
#include "dbus-common.h"
 
48
#include "../src/adapter.h"
 
49
#include "../src/device.h"
46
50
 
47
51
#define STREAM_SETUP_RETRY_TIMER 2
48
52
 
53
57
};
54
58
 
55
59
struct sink {
 
60
        struct audio_device *dev;
56
61
        struct avdtp *session;
57
62
        struct avdtp_stream *stream;
58
63
        unsigned int cb_id;
 
64
        guint retry_id;
59
65
        avdtp_session_state_t session_state;
60
66
        avdtp_state_t stream_state;
61
67
        sink_state_t state;
74
80
 
75
81
static unsigned int avdtp_callback_id = 0;
76
82
 
 
83
static char *str_state[] = {
 
84
        "SINK_STATE_DISCONNECTED",
 
85
        "SINK_STATE_CONNECTING",
 
86
        "SINK_STATE_CONNECTED",
 
87
        "SINK_STATE_PLAYING",
 
88
};
 
89
 
77
90
static const char *state2str(sink_state_t state)
78
91
{
79
92
        switch (state) {
106
119
                                        AUDIO_SINK_INTERFACE, "State",
107
120
                                        DBUS_TYPE_STRING, &state_str);
108
121
 
 
122
        DBG("State changed %s: %s -> %s", dev->path, str_state[old_state],
 
123
                str_state[new_state]);
 
124
 
109
125
        for (l = sink_callbacks; l != NULL; l = l->next) {
110
126
                struct sink_state_callback *cb = l->data;
111
127
                cb->cb(dev, old_state, new_state, cb->user_data);
146
162
        sink->session_state = new_state;
147
163
}
148
164
 
149
 
static void pending_request_free(struct pending_request *pending)
 
165
static void pending_request_free(struct audio_device *dev,
 
166
                                        struct pending_request *pending)
150
167
{
151
168
        if (pending->conn)
152
169
                dbus_connection_unref(pending->conn);
153
170
        if (pending->msg)
154
171
                dbus_message_unref(pending->msg);
 
172
        if (pending->id)
 
173
                a2dp_cancel(dev, pending->id);
 
174
 
155
175
        g_free(pending);
156
176
}
157
177
 
179
199
 
180
200
                        reply = dbus_message_new_method_return(p->msg);
181
201
                        g_dbus_send_message(p->conn, reply);
182
 
                        pending_request_free(p);
 
202
                        pending_request_free(dev, p);
183
203
                }
184
204
 
185
205
                if (sink->session) {
244
264
        struct sink *sink = user_data;
245
265
        struct pending_request *pending = sink->connect;
246
266
 
 
267
        sink->retry_id = 0;
 
268
 
247
269
        if (sink->stream_state >= AVDTP_STATE_OPEN) {
248
 
                debug("Stream successfully created, after XCASE connect:connect");
 
270
                DBG("Stream successfully created, after XCASE connect:connect");
249
271
                if (pending->msg) {
250
272
                        DBusMessage *reply;
251
273
                        reply = dbus_message_new_method_return(pending->msg);
252
274
                        g_dbus_send_message(pending->conn, reply);
253
275
                }
254
276
        } else {
255
 
                debug("Stream setup failed, after XCASE connect:connect");
 
277
                DBG("Stream setup failed, after XCASE connect:connect");
256
278
                if (pending->msg)
257
279
                        error_failed(pending->conn, pending->msg, "Stream setup failed");
258
280
        }
259
281
 
260
282
        sink->connect = NULL;
261
 
        pending_request_free(pending);
 
283
        pending_request_free(sink->dev, pending);
262
284
 
263
285
        return FALSE;
264
286
}
272
294
 
273
295
        pending = sink->connect;
274
296
 
 
297
        pending->id = 0;
 
298
 
275
299
        if (stream) {
276
 
                debug("Stream successfully created");
 
300
                DBG("Stream successfully created");
277
301
 
278
302
                if (pending->msg) {
279
303
                        DBusMessage *reply;
282
306
                }
283
307
 
284
308
                sink->connect = NULL;
285
 
                pending_request_free(pending);
 
309
                pending_request_free(sink->dev, pending);
286
310
 
287
311
                return;
288
312
        }
289
313
 
290
314
        avdtp_unref(sink->session);
291
315
        sink->session = NULL;
292
 
        if (avdtp_error_type(err) == AVDTP_ERROR_ERRNO
 
316
        if (avdtp_error_category(err) == AVDTP_ERRNO
293
317
                        && avdtp_error_posix_errno(err) != EHOSTDOWN) {
294
 
                debug("connect:connect XCASE detected");
295
 
                g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
296
 
                                stream_setup_retry, sink);
 
318
                DBG("connect:connect XCASE detected");
 
319
                sink->retry_id = g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
 
320
                                                        stream_setup_retry,
 
321
                                                        sink);
297
322
        } else {
298
323
                if (pending->msg)
299
324
                        error_failed(pending->conn, pending->msg, "Stream setup failed");
300
325
                sink->connect = NULL;
301
 
                pending_request_free(pending);
302
 
                debug("Stream setup failed : %s", avdtp_strerror(err));
303
 
        }
304
 
}
305
 
 
306
 
static uint8_t default_bitpool(uint8_t freq, uint8_t mode)
307
 
{
308
 
        switch (freq) {
309
 
        case SBC_SAMPLING_FREQ_16000:
310
 
        case SBC_SAMPLING_FREQ_32000:
311
 
                return 53;
312
 
        case SBC_SAMPLING_FREQ_44100:
313
 
                switch (mode) {
314
 
                case SBC_CHANNEL_MODE_MONO:
315
 
                case SBC_CHANNEL_MODE_DUAL_CHANNEL:
316
 
                        return 31;
317
 
                case SBC_CHANNEL_MODE_STEREO:
318
 
                case SBC_CHANNEL_MODE_JOINT_STEREO:
319
 
                        return 53;
320
 
                default:
321
 
                        error("Invalid channel mode %u", mode);
322
 
                        return 53;
323
 
                }
324
 
        case SBC_SAMPLING_FREQ_48000:
325
 
                switch (mode) {
326
 
                case SBC_CHANNEL_MODE_MONO:
327
 
                case SBC_CHANNEL_MODE_DUAL_CHANNEL:
328
 
                        return 29;
329
 
                case SBC_CHANNEL_MODE_STEREO:
330
 
                case SBC_CHANNEL_MODE_JOINT_STEREO:
331
 
                        return 51;
332
 
                default:
333
 
                        error("Invalid channel mode %u", mode);
334
 
                        return 51;
335
 
                }
336
 
        default:
337
 
                error("Invalid sampling freq %u", freq);
338
 
                return 53;
339
 
        }
340
 
}
341
 
 
342
 
static gboolean select_sbc_params(struct sbc_codec_cap *cap,
343
 
                                        struct sbc_codec_cap *supported)
344
 
{
345
 
        unsigned int max_bitpool, min_bitpool;
346
 
 
347
 
        memset(cap, 0, sizeof(struct sbc_codec_cap));
348
 
 
349
 
        cap->cap.media_type = AVDTP_MEDIA_TYPE_AUDIO;
350
 
        cap->cap.media_codec_type = A2DP_CODEC_SBC;
351
 
 
352
 
        if (supported->frequency & SBC_SAMPLING_FREQ_44100)
353
 
                cap->frequency = SBC_SAMPLING_FREQ_44100;
354
 
        else if (supported->frequency & SBC_SAMPLING_FREQ_48000)
355
 
                cap->frequency = SBC_SAMPLING_FREQ_48000;
356
 
        else if (supported->frequency & SBC_SAMPLING_FREQ_32000)
357
 
                cap->frequency = SBC_SAMPLING_FREQ_32000;
358
 
        else if (supported->frequency & SBC_SAMPLING_FREQ_16000)
359
 
                cap->frequency = SBC_SAMPLING_FREQ_16000;
360
 
        else {
361
 
                error("No supported frequencies");
362
 
                return FALSE;
363
 
        }
364
 
 
365
 
        if (supported->channel_mode & SBC_CHANNEL_MODE_JOINT_STEREO)
366
 
                cap->channel_mode = SBC_CHANNEL_MODE_JOINT_STEREO;
367
 
        else if (supported->channel_mode & SBC_CHANNEL_MODE_STEREO)
368
 
                cap->channel_mode = SBC_CHANNEL_MODE_STEREO;
369
 
        else if (supported->channel_mode & SBC_CHANNEL_MODE_DUAL_CHANNEL)
370
 
                cap->channel_mode = SBC_CHANNEL_MODE_DUAL_CHANNEL;
371
 
        else if (supported->channel_mode & SBC_CHANNEL_MODE_MONO)
372
 
                cap->channel_mode = SBC_CHANNEL_MODE_MONO;
373
 
        else {
374
 
                error("No supported channel modes");
375
 
                return FALSE;
376
 
        }
377
 
 
378
 
        if (supported->block_length & SBC_BLOCK_LENGTH_16)
379
 
                cap->block_length = SBC_BLOCK_LENGTH_16;
380
 
        else if (supported->block_length & SBC_BLOCK_LENGTH_12)
381
 
                cap->block_length = SBC_BLOCK_LENGTH_12;
382
 
        else if (supported->block_length & SBC_BLOCK_LENGTH_8)
383
 
                cap->block_length = SBC_BLOCK_LENGTH_8;
384
 
        else if (supported->block_length & SBC_BLOCK_LENGTH_4)
385
 
                cap->block_length = SBC_BLOCK_LENGTH_4;
386
 
        else {
387
 
                error("No supported block lengths");
388
 
                return FALSE;
389
 
        }
390
 
 
391
 
        if (supported->subbands & SBC_SUBBANDS_8)
392
 
                cap->subbands = SBC_SUBBANDS_8;
393
 
        else if (supported->subbands & SBC_SUBBANDS_4)
394
 
                cap->subbands = SBC_SUBBANDS_4;
395
 
        else {
396
 
                error("No supported subbands");
397
 
                return FALSE;
398
 
        }
399
 
 
400
 
        if (supported->allocation_method & SBC_ALLOCATION_LOUDNESS)
401
 
                cap->allocation_method = SBC_ALLOCATION_LOUDNESS;
402
 
        else if (supported->allocation_method & SBC_ALLOCATION_SNR)
403
 
                cap->allocation_method = SBC_ALLOCATION_SNR;
404
 
 
405
 
        min_bitpool = MAX(MIN_BITPOOL, supported->min_bitpool);
406
 
        max_bitpool = MIN(default_bitpool(cap->frequency, cap->channel_mode),
407
 
                                                        supported->max_bitpool);
408
 
 
409
 
        cap->min_bitpool = min_bitpool;
410
 
        cap->max_bitpool = max_bitpool;
411
 
 
412
 
        return TRUE;
413
 
}
414
 
 
415
 
static gboolean select_capabilities(struct avdtp *session,
416
 
                                        struct avdtp_remote_sep *rsep,
417
 
                                        GSList **caps)
418
 
{
419
 
        struct avdtp_service_capability *media_transport, *media_codec;
420
 
        struct sbc_codec_cap sbc_cap;
421
 
 
422
 
        media_codec = avdtp_get_codec(rsep);
423
 
        if (!media_codec)
424
 
                return FALSE;
425
 
 
426
 
        select_sbc_params(&sbc_cap, (struct sbc_codec_cap *) media_codec->data);
427
 
 
428
 
        media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
429
 
                                                NULL, 0);
430
 
 
431
 
        *caps = g_slist_append(*caps, media_transport);
432
 
 
433
 
        media_codec = avdtp_service_cap_new(AVDTP_MEDIA_CODEC, &sbc_cap,
434
 
                                                sizeof(sbc_cap));
435
 
 
436
 
        *caps = g_slist_append(*caps, media_codec);
437
 
 
438
 
 
439
 
        return TRUE;
 
326
                pending_request_free(sink->dev, pending);
 
327
                DBG("Stream setup failed : %s", avdtp_strerror(err));
 
328
        }
 
329
}
 
330
 
 
331
static void select_complete(struct avdtp *session, struct a2dp_sep *sep,
 
332
                        GSList *caps, void *user_data)
 
333
{
 
334
        struct sink *sink = user_data;
 
335
        struct pending_request *pending;
 
336
        int id;
 
337
 
 
338
        pending = sink->connect;
 
339
        pending->id = 0;
 
340
 
 
341
        id = a2dp_config(session, sep, stream_setup_complete, caps, sink);
 
342
        if (id == 0)
 
343
                goto failed;
 
344
 
 
345
        pending->id = id;
 
346
        return;
 
347
 
 
348
failed:
 
349
        if (pending->msg)
 
350
                error_failed(pending->conn, pending->msg, "Stream setup failed");
 
351
        pending_request_free(sink->dev, pending);
 
352
        sink->connect = NULL;
 
353
        avdtp_unref(sink->session);
 
354
        sink->session = NULL;
440
355
}
441
356
 
442
357
static void discovery_complete(struct avdtp *session, GSList *seps, struct avdtp_error *err,
444
359
{
445
360
        struct sink *sink = user_data;
446
361
        struct pending_request *pending;
447
 
        struct avdtp_local_sep *lsep;
448
 
        struct avdtp_remote_sep *rsep;
449
 
        struct a2dp_sep *sep;
450
 
        GSList *caps = NULL;
451
362
        int id;
452
363
 
 
364
        if (!sink->connect) {
 
365
                avdtp_unref(sink->session);
 
366
                sink->session = NULL;
 
367
                return;
 
368
        }
 
369
 
453
370
        pending = sink->connect;
454
371
 
455
372
        if (err) {
456
373
                avdtp_unref(sink->session);
457
374
                sink->session = NULL;
458
 
                if (avdtp_error_type(err) == AVDTP_ERROR_ERRNO
 
375
                if (avdtp_error_category(err) == AVDTP_ERRNO
459
376
                                && avdtp_error_posix_errno(err) != EHOSTDOWN) {
460
 
                        debug("connect:connect XCASE detected");
461
 
                        g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
462
 
                                        stream_setup_retry, sink);
 
377
                        DBG("connect:connect XCASE detected");
 
378
                        sink->retry_id =
 
379
                                g_timeout_add_seconds(STREAM_SETUP_RETRY_TIMER,
 
380
                                                        stream_setup_retry,
 
381
                                                        sink);
463
382
                } else
464
383
                        goto failed;
465
384
                return;
466
385
        }
467
386
 
468
 
        debug("Discovery complete");
469
 
 
470
 
        if (avdtp_get_seps(session, AVDTP_SEP_TYPE_SINK, AVDTP_MEDIA_TYPE_AUDIO,
471
 
                                A2DP_CODEC_SBC, &lsep, &rsep) < 0) {
472
 
                error("No matching ACP and INT SEPs found");
473
 
                goto failed;
474
 
        }
475
 
 
476
 
        if (!select_capabilities(session, rsep, &caps)) {
477
 
                error("Unable to select remote SEP capabilities");
478
 
                goto failed;
479
 
        }
480
 
 
481
 
        sep = a2dp_source_get(session, rsep);
482
 
        id = a2dp_source_config(sink->session, sep, stream_setup_complete,
483
 
                                caps, sink);
 
387
        DBG("Discovery complete");
 
388
 
 
389
        id = a2dp_select_capabilities(sink->session, AVDTP_SEP_TYPE_SINK, NULL,
 
390
                                                select_complete, sink);
484
391
        if (id == 0)
485
392
                goto failed;
486
393
 
490
397
failed:
491
398
        if (pending->msg)
492
399
                error_failed(pending->conn, pending->msg, "Stream setup failed");
493
 
        pending_request_free(pending);
 
400
        pending_request_free(sink->dev, pending);
494
401
        sink->connect = NULL;
495
402
        avdtp_unref(sink->session);
496
403
        sink->session = NULL;
507
414
        if (!sink->session)
508
415
                return FALSE;
509
416
 
 
417
        avdtp_set_auto_disconnect(sink->session, FALSE);
 
418
 
510
419
        if (avdtp_discover(sink->session, discovery_complete, sink) < 0)
511
420
                return FALSE;
512
421
 
538
447
                                                ".AlreadyConnected",
539
448
                                                "Device Already Connected");
540
449
 
541
 
        avdtp_set_auto_disconnect(sink->session, FALSE);
542
 
 
543
450
        if (!sink_setup_stream(sink, NULL))
544
451
                return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
545
452
                                                "Failed to create a stream");
551
458
        pending->conn = dbus_connection_ref(conn);
552
459
        pending->msg = dbus_message_ref(msg);
553
460
 
554
 
        debug("stream creation in progress");
 
461
        DBG("stream creation in progress");
555
462
 
556
463
        return NULL;
557
464
}
582
489
                return reply;
583
490
        }
584
491
 
585
 
        err = avdtp_close(sink->session, sink->stream);
 
492
        err = avdtp_close(sink->session, sink->stream, FALSE);
586
493
        if (err < 0)
587
494
                return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
588
495
                                                "%s", strerror(-err));
688
595
                avdtp_unref(sink->session);
689
596
 
690
597
        if (sink->connect)
691
 
                pending_request_free(sink->connect);
 
598
                pending_request_free(dev, sink->connect);
692
599
 
693
600
        if (sink->disconnect)
694
 
                pending_request_free(sink->disconnect);
 
601
                pending_request_free(dev, sink->disconnect);
 
602
 
 
603
        if (sink->retry_id)
 
604
                g_source_remove(sink->retry_id);
695
605
 
696
606
        g_free(sink);
697
607
        dev->sink = NULL;
701
611
{
702
612
        struct audio_device *dev = data;
703
613
 
704
 
        debug("Unregistered interface %s on path %s",
 
614
        DBG("Unregistered interface %s on path %s",
705
615
                AUDIO_SINK_INTERFACE, dev->path);
706
616
 
707
617
        sink_free(dev);
715
625
 
716
626
struct sink *sink_init(struct audio_device *dev)
717
627
{
 
628
        struct sink *sink;
 
629
 
718
630
        if (!g_dbus_register_interface(dev->conn, dev->path,
719
631
                                        AUDIO_SINK_INTERFACE,
720
632
                                        sink_methods, sink_signals, NULL,
721
633
                                        dev, path_unregister))
722
634
                return NULL;
723
635
 
724
 
        debug("Registered interface %s on path %s",
 
636
        DBG("Registered interface %s on path %s",
725
637
                AUDIO_SINK_INTERFACE, dev->path);
726
638
 
727
639
        if (avdtp_callback_id == 0)
728
640
                avdtp_callback_id = avdtp_add_state_cb(avdtp_state_callback,
729
641
                                                                        NULL);
730
642
 
731
 
        return g_new0(struct sink, 1);
 
643
        sink = g_new0(struct sink, 1);
 
644
 
 
645
        sink->dev = dev;
 
646
 
 
647
        return sink;
732
648
}
733
649
 
734
650
gboolean sink_is_active(struct audio_device *dev)
769
685
 
770
686
gboolean sink_shutdown(struct sink *sink)
771
687
{
 
688
        if (!sink->session)
 
689
                return FALSE;
 
690
 
 
691
        avdtp_set_device_disconnect(sink->session, TRUE);
 
692
 
 
693
        /* cancel pending connect */
 
694
        if (sink->connect) {
 
695
                struct pending_request *pending = sink->connect;
 
696
 
 
697
                if (pending->msg)
 
698
                        error_failed(pending->conn, pending->msg,
 
699
                                                        "Stream setup failed");
 
700
                pending_request_free(sink->dev, pending);
 
701
                sink->connect = NULL;
 
702
 
 
703
                avdtp_unref(sink->session);
 
704
                sink->session = NULL;
 
705
 
 
706
                return TRUE;
 
707
        }
 
708
 
 
709
        /* disconnect already ongoing */
 
710
        if (sink->disconnect)
 
711
                return TRUE;
 
712
 
772
713
        if (!sink->stream)
773
714
                return FALSE;
774
715
 
775
 
        if (avdtp_close(sink->session, sink->stream) < 0)
 
716
        if (avdtp_close(sink->session, sink->stream, FALSE) < 0)
776
717
                return FALSE;
777
718
 
778
719
        return TRUE;