summaryrefslogtreecommitdiff
path: root/src/pulsecore/device-port.c
blob: 8263c9e9bd2299ec608d773957a6dc79320ca0eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/***
  This file is part of PulseAudio.

  Copyright 2004-2006 Lennart Poettering
  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
  Copyright 2011 David Henningsson, Canonical Ltd.

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  PulseAudio 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 Lesser General Public License
  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
***/

#include "device-port.h"
#include <pulsecore/card.h>
#include <pulsecore/core-util.h>

PA_DEFINE_PUBLIC_CLASS(pa_device_port, pa_object);

pa_device_port_new_data *pa_device_port_new_data_init(pa_device_port_new_data *data) {
    pa_assert(data);

    pa_zero(*data);
    data->type = PA_DEVICE_PORT_TYPE_UNKNOWN;
    data->available = PA_AVAILABLE_UNKNOWN;
    return data;
}

void pa_device_port_new_data_set_name(pa_device_port_new_data *data, const char *name) {
    pa_assert(data);

    pa_xfree(data->name);
    data->name = pa_xstrdup(name);
}

void pa_device_port_new_data_set_description(pa_device_port_new_data *data, const char *description) {
    pa_assert(data);

    pa_xfree(data->description);
    data->description = pa_xstrdup(description);
}

void pa_device_port_new_data_set_available(pa_device_port_new_data *data, pa_available_t available) {
    pa_assert(data);

    data->available = available;
}

void pa_device_port_new_data_set_availability_group(pa_device_port_new_data *data, const char *group) {
    pa_assert(data);

    pa_xfree(data->availability_group);
    data->availability_group = pa_xstrdup(group);
}

void pa_device_port_new_data_set_direction(pa_device_port_new_data *data, pa_direction_t direction) {
    pa_assert(data);

    data->direction = direction;
}

void pa_device_port_new_data_set_type(pa_device_port_new_data *data, pa_device_port_type_t type) {
    pa_assert(data);

    data->type = type;
}

void pa_device_port_new_data_done(pa_device_port_new_data *data) {
    pa_assert(data);

    pa_xfree(data->name);
    pa_xfree(data->description);
    pa_xfree(data->availability_group);
}

void pa_device_port_set_preferred_profile(pa_device_port *p, const char *new_pp) {
    pa_assert(p);

    if (!pa_safe_streq(p->preferred_profile, new_pp)) {
        pa_xfree(p->preferred_profile);
        p->preferred_profile = pa_xstrdup(new_pp);
    }
}

void pa_device_port_set_available(pa_device_port *p, pa_available_t status) {
    pa_assert(p);

    if (p->available == status)
        return;

/*    pa_assert(status != PA_AVAILABLE_UNKNOWN); */

    p->available = status;
    pa_log_debug("Setting port %s to status %s", p->name, pa_available_to_string(status));

    /* Post subscriptions to the card which owns us */
    /* XXX: We need to check p->card, because this function may be called
     * before the card object has been created. The card object should probably
     * be created before port objects, and then p->card could be non-NULL for
     * the whole lifecycle of pa_device_port. */
    if (p->card && p->card->linked) {
        pa_sink *sink;
        pa_source *source;

        pa_subscription_post(p->core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, p->card->index);

        sink = pa_device_port_get_sink(p);
        source = pa_device_port_get_source(p);
        if (sink)
            pa_subscription_post(p->core, PA_SUBSCRIPTION_EVENT_SINK|PA_SUBSCRIPTION_EVENT_CHANGE, sink->index);
        if (source)
            pa_subscription_post(p->core, PA_SUBSCRIPTION_EVENT_SOURCE|PA_SUBSCRIPTION_EVENT_CHANGE, source->index);

        pa_hook_fire(&p->core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED], p);

        /* A sink or source whose active port is unavailable can't be the
         * default sink/source, so port availability changes may affect the
         * default sink/source choice. */
        if (p->direction == PA_DIRECTION_OUTPUT)
            pa_core_update_default_sink(p->core);
        else
            pa_core_update_default_source(p->core);

        if (p->direction == PA_DIRECTION_OUTPUT) {
            if (sink && p == sink->active_port) {
                if (sink->active_port->available == PA_AVAILABLE_NO) {
                    if (p->core->rescue_streams)
                        pa_sink_move_streams_to_default_sink(p->core, sink, false);
                } else
                    pa_core_move_streams_to_newly_available_preferred_sink(p->core, sink);
            }
        } else {
            if (source && p == source->active_port) {
                if (source->active_port->available == PA_AVAILABLE_NO) {
                    if (p->core->rescue_streams)
                        pa_source_move_streams_to_default_source(p->core, source, false);
                } else
                    pa_core_move_streams_to_newly_available_preferred_source(p->core, source);
            }
        }
    }
}

static void device_port_free(pa_object *o) {
    pa_device_port *p = PA_DEVICE_PORT(o);

    pa_assert(p);
    pa_assert(pa_device_port_refcnt(p) == 0);

    if (p->impl_free)
        p->impl_free(p);

    if (p->proplist)
        pa_proplist_free(p->proplist);

    if (p->profiles)
        pa_hashmap_free(p->profiles);

    pa_xfree(p->availability_group);
    pa_xfree(p->preferred_profile);
    pa_xfree(p->name);
    pa_xfree(p->description);
    pa_xfree(p);
}

pa_device_port *pa_device_port_new(pa_core *c, pa_device_port_new_data *data, size_t extra) {
    pa_device_port *p;

    pa_assert(data);
    pa_assert(data->name);
    pa_assert(data->description);
    pa_assert(data->direction == PA_DIRECTION_OUTPUT || data->direction == PA_DIRECTION_INPUT);

    p = PA_DEVICE_PORT(pa_object_new_internal(PA_ALIGN(sizeof(pa_device_port)) + extra, pa_device_port_type_id, pa_device_port_check_type));
    p->parent.free = device_port_free;

    p->name = data->name;
    data->name = NULL;
    p->description = data->description;
    data->description = NULL;
    p->core = c;
    p->card = NULL;
    p->priority = 0;
    p->available = data->available;
    p->availability_group = data->availability_group;
    data->availability_group = NULL;
    p->profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
    p->direction = data->direction;
    p->type = data->type;

    p->latency_offset = 0;
    p->proplist = pa_proplist_new();

    return p;
}

void pa_device_port_set_latency_offset(pa_device_port *p, int64_t offset) {
    uint32_t state;
    pa_core *core;

    pa_assert(p);

    if (offset == p->latency_offset)
        return;

    p->latency_offset = offset;

    switch (p->direction) {
        case PA_DIRECTION_OUTPUT: {
            pa_sink *sink;

            PA_IDXSET_FOREACH(sink, p->core->sinks, state) {
                if (sink->active_port == p) {
                    pa_sink_set_port_latency_offset(sink, p->latency_offset);
                    break;
                }
            }

            break;
        }

        case PA_DIRECTION_INPUT: {
            pa_source *source;

            PA_IDXSET_FOREACH(source, p->core->sources, state) {
                if (source->active_port == p) {
                    pa_source_set_port_latency_offset(source, p->latency_offset);
                    break;
                }
            }

            break;
        }
    }

    pa_assert_se(core = p->core);
    pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, p->card->index);
    pa_hook_fire(&core->hooks[PA_CORE_HOOK_PORT_LATENCY_OFFSET_CHANGED], p);
}

pa_device_port *pa_device_port_find_best(pa_hashmap *ports)
{
    void *state;
    pa_device_port *p, *best = NULL;

    if (!ports)
        return NULL;

    /* First run: skip unavailable ports */
    PA_HASHMAP_FOREACH(p, ports, state) {
        if (p->available == PA_AVAILABLE_NO)
            continue;

        if (!best || p->priority > best->priority)
            best = p;
    }

    /* Second run: if only unavailable ports exist, still suggest a port */
    if (!best) {
        PA_HASHMAP_FOREACH(p, ports, state)
            if (!best || p->priority > best->priority)
                best = p;
    }

    return best;
}

pa_sink *pa_device_port_get_sink(pa_device_port *p) {
    pa_sink *rs = NULL;
    pa_sink *sink;
    uint32_t state;

    PA_IDXSET_FOREACH(sink, p->card->sinks, state)
        if (p == pa_hashmap_get(sink->ports, p->name)) {
            rs = sink;
            break;
        }
    return rs;
}

pa_source *pa_device_port_get_source(pa_device_port *p) {
    pa_source *rs = NULL;
    pa_source *source;
    uint32_t state;

    PA_IDXSET_FOREACH(source, p->card->sources, state)
        if (p == pa_hashmap_get(source->ports, p->name)) {
            rs = source;
            break;
        }
    return rs;
}