~binli/ubuntu/vivid/pulseaudio/load-extcon-module

« back to all changes in this revision

Viewing changes to .pc/0506-bluetooth-bluez5-prevent-SCO-sink-source-to-be-suspe.patch/src/modules/bluetooth/module-bluez5-discover.c

  • Committer: Bin Li
  • Date: 2016-01-23 15:04:48 UTC
  • Revision ID: bin.li@canonical.com-20160123150448-5ockvw4p5xxntda4
init the 1:6.0-0ubuntu9.15 from silo 12

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***
 
2
  This file is part of PulseAudio.
 
3
 
 
4
  Copyright 2008-2013 João Paulo Rechi Vita
 
5
 
 
6
  PulseAudio is free software; you can redistribute it and/or modify
 
7
  it under the terms of the GNU Lesser General Public License as
 
8
  published by the Free Software Foundation; either version 2.1 of the
 
9
  License, or (at your option) any later version.
 
10
 
 
11
  PulseAudio is distributed in the hope that it will be useful, but
 
12
  WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
14
  General Public License for more details.
 
15
 
 
16
  You should have received a copy of the GNU Lesser General Public
 
17
  License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
 
18
***/
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include <config.h>
 
22
#endif
 
23
 
 
24
#include <pulsecore/core.h>
 
25
#include <pulsecore/core-util.h>
 
26
#include <pulsecore/macro.h>
 
27
#include <pulsecore/module.h>
 
28
#include <pulsecore/modargs.h>
 
29
#include <pulsecore/shared.h>
 
30
 
 
31
#include "bluez5-util.h"
 
32
 
 
33
#include "module-bluez5-discover-symdef.h"
 
34
 
 
35
PA_MODULE_AUTHOR("João Paulo Rechi Vita");
 
36
PA_MODULE_DESCRIPTION("Detect available BlueZ 5 Bluetooth audio devices and load BlueZ 5 Bluetooth audio drivers");
 
37
PA_MODULE_VERSION(PACKAGE_VERSION);
 
38
PA_MODULE_LOAD_ONCE(true);
 
39
PA_MODULE_USAGE(
 
40
    "headset=ofono|native|auto|both "
 
41
    "sco_sink=<name of sink> "
 
42
    "sco_source=<name of source> "
 
43
);
 
44
 
 
45
static const char* const valid_modargs[] = {
 
46
    "headset",
 
47
    "sco_sink",
 
48
    "sco_source",
 
49
    NULL
 
50
};
 
51
 
 
52
struct userdata {
 
53
    pa_module *module;
 
54
    pa_modargs *modargs;
 
55
    pa_core *core;
 
56
    pa_hashmap *loaded_device_paths;
 
57
    pa_hook_slot *device_connection_changed_slot;
 
58
    pa_bluetooth_discovery *discovery;
 
59
};
 
60
 
 
61
static pa_hook_result_t device_connection_changed_cb(pa_bluetooth_discovery *y, const pa_bluetooth_device *d, struct userdata *u) {
 
62
    bool module_loaded;
 
63
 
 
64
    pa_assert(d);
 
65
    pa_assert(u);
 
66
 
 
67
    module_loaded = pa_hashmap_get(u->loaded_device_paths, d->path) ? true : false;
 
68
 
 
69
    if (module_loaded && !pa_bluetooth_device_any_transport_connected(d)) {
 
70
        /* disconnection, the module unloads itself */
 
71
        pa_log_debug("Unregistering module for %s", d->path);
 
72
        pa_hashmap_remove(u->loaded_device_paths, d->path);
 
73
        return PA_HOOK_OK;
 
74
    }
 
75
 
 
76
    if (!module_loaded && pa_bluetooth_device_any_transport_connected(d)) {
 
77
        /* a new device has been connected */
 
78
        pa_module *m;
 
79
        char *args = pa_sprintf_malloc("path=%s", d->path);
 
80
 
 
81
        if (pa_modargs_get_value(u->modargs, "sco_sink", NULL) &&
 
82
            pa_modargs_get_value(u->modargs, "sco_source", NULL)) {
 
83
            char *tmp;
 
84
 
 
85
            tmp = pa_sprintf_malloc("%s sco_sink=\"%s\" sco_source=\"%s\"", args,
 
86
                                    pa_modargs_get_value(u->modargs, "sco_sink", NULL),
 
87
                                    pa_modargs_get_value(u->modargs, "sco_source", NULL));
 
88
            pa_xfree(args);
 
89
            args = tmp;
 
90
        }
 
91
        pa_log_debug("Loading module-bluez5-device %s", args);
 
92
        m = pa_module_load(u->module->core, "module-bluez5-device", args);
 
93
        pa_xfree(args);
 
94
 
 
95
        if (m)
 
96
            /* No need to duplicate the path here since the device object will
 
97
             * exist for the whole hashmap entry lifespan */
 
98
            pa_hashmap_put(u->loaded_device_paths, d->path, d->path);
 
99
        else
 
100
            pa_log_warn("Failed to load module for device %s", d->path);
 
101
 
 
102
        return PA_HOOK_OK;
 
103
    }
 
104
 
 
105
    return PA_HOOK_OK;
 
106
}
 
107
 
 
108
#ifdef HAVE_BLUEZ_5_NATIVE_HEADSET
 
109
#ifdef HAVE_BLUEZ_5_OFONO_HEADSET
 
110
const char *default_headset_backend = "both";
 
111
#else
 
112
const char *default_headset_backend = "native";
 
113
#endif
 
114
#else
 
115
const char *default_headset_backend = "ofono";
 
116
#endif
 
117
 
 
118
int pa__init(pa_module *m) {
 
119
    struct userdata *u;
 
120
    pa_modargs *ma;
 
121
    const char *headset_str;
 
122
    int headset_backend;
 
123
 
 
124
    pa_assert(m);
 
125
 
 
126
    if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
 
127
        pa_log("failed to parse module arguments.");
 
128
        goto fail;
 
129
    }
 
130
 
 
131
    pa_assert_se(headset_str = pa_modargs_get_value(ma, "headset", default_headset_backend));
 
132
    if (pa_streq(headset_str, "ofono"))
 
133
        headset_backend = HEADSET_BACKEND_OFONO;
 
134
    else if (pa_streq(headset_str, "native"))
 
135
        headset_backend = HEADSET_BACKEND_NATIVE;
 
136
    else if (pa_streq(headset_str, "auto"))
 
137
        headset_backend = HEADSET_BACKEND_AUTO;
 
138
    else if (pa_streq(headset_str, "both"))
 
139
        headset_backend = HEADSET_BACKEND_BOTH;
 
140
    else {
 
141
        pa_log("headset parameter must be either ofono, native, auto or both (found %s)", headset_str);
 
142
        goto fail;
 
143
    }
 
144
 
 
145
    m->userdata = u = pa_xnew0(struct userdata, 1);
 
146
    u->module = m;
 
147
    u->modargs = ma;
 
148
    u->core = m->core;
 
149
    u->loaded_device_paths = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
150
 
 
151
    if (!(u->discovery = pa_bluetooth_discovery_get(u->core, headset_backend)))
 
152
        goto fail;
 
153
 
 
154
    u->device_connection_changed_slot =
 
155
        pa_hook_connect(pa_bluetooth_discovery_hook(u->discovery, PA_BLUETOOTH_HOOK_DEVICE_CONNECTION_CHANGED),
 
156
                        PA_HOOK_NORMAL, (pa_hook_cb_t) device_connection_changed_cb, u);
 
157
 
 
158
    return 0;
 
159
 
 
160
fail:
 
161
    pa_modargs_free(ma);
 
162
    pa__done(m);
 
163
    return -1;
 
164
}
 
165
 
 
166
void pa__done(pa_module *m) {
 
167
    struct userdata *u;
 
168
 
 
169
    pa_assert(m);
 
170
 
 
171
    if (!(u = m->userdata))
 
172
        return;
 
173
 
 
174
    if (u->device_connection_changed_slot)
 
175
        pa_hook_slot_free(u->device_connection_changed_slot);
 
176
 
 
177
    if (u->discovery)
 
178
        pa_bluetooth_discovery_unref(u->discovery);
 
179
 
 
180
    if (u->loaded_device_paths)
 
181
        pa_hashmap_free(u->loaded_device_paths);
 
182
 
 
183
    if (u->modargs)
 
184
        pa_modargs_free(u->modargs);
 
185
    pa_xfree(u);
 
186
}