]> code.delx.au - pulseaudio/blob - src/pulsecore/device-port.c
device-port: Introduce pa_device_port_new_data
[pulseaudio] / src / pulsecore / device-port.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6 Copyright 2011 David Henningsson, Canonical Ltd.
7
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2.1 of the License,
11 or (at your option) any later version.
12
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 USA.
22 ***/
23
24 #include "device-port.h"
25 #include <pulsecore/card.h>
26
27 PA_DEFINE_PUBLIC_CLASS(pa_device_port, pa_object);
28
29 pa_device_port_new_data *pa_device_port_new_data_init(pa_device_port_new_data *data) {
30 pa_assert(data);
31
32 pa_zero(*data);
33 data->available = PA_AVAILABLE_UNKNOWN;
34 return data;
35 }
36
37 void pa_device_port_new_data_set_name(pa_device_port_new_data *data, const char *name) {
38 pa_assert(data);
39
40 pa_xfree(data->name);
41 data->name = pa_xstrdup(name);
42 }
43
44 void pa_device_port_new_data_set_description(pa_device_port_new_data *data, const char *description) {
45 pa_assert(data);
46
47 pa_xfree(data->description);
48 data->description = pa_xstrdup(description);
49 }
50
51 void pa_device_port_new_data_set_available(pa_device_port_new_data *data, pa_available_t available) {
52 pa_assert(data);
53
54 data->available = available;
55 }
56
57 void pa_device_port_new_data_set_direction(pa_device_port_new_data *data, pa_direction_t direction) {
58 pa_assert(data);
59
60 data->direction = direction;
61 }
62
63 void pa_device_port_new_data_done(pa_device_port_new_data *data) {
64 pa_assert(data);
65
66 pa_xfree(data->name);
67 pa_xfree(data->description);
68 }
69
70 void pa_device_port_set_available(pa_device_port *p, pa_available_t status)
71 {
72 pa_core *core;
73
74 pa_assert(p);
75
76 if (p->available == status)
77 return;
78
79 /* pa_assert(status != PA_AVAILABLE_UNKNOWN); */
80
81 p->available = status;
82 pa_log_debug("Setting port %s to status %s", p->name, status == PA_AVAILABLE_YES ? "yes" :
83 status == PA_AVAILABLE_NO ? "no" : "unknown");
84
85 /* Post subscriptions to the card which owns us */
86 pa_assert_se(core = p->core);
87 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, p->card->index);
88
89 pa_hook_fire(&core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED], p);
90 }
91
92 static void device_port_free(pa_object *o) {
93 pa_device_port *p = PA_DEVICE_PORT(o);
94
95 pa_assert(p);
96 pa_assert(pa_device_port_refcnt(p) == 0);
97
98 if (p->proplist)
99 pa_proplist_free(p->proplist);
100
101 if (p->profiles)
102 pa_hashmap_free(p->profiles, NULL);
103
104 pa_xfree(p->name);
105 pa_xfree(p->description);
106 pa_xfree(p);
107 }
108
109
110 pa_device_port *pa_device_port_new(pa_core *c, pa_device_port_new_data *data, size_t extra) {
111 pa_device_port *p;
112
113 pa_assert(data);
114 pa_assert(data->name);
115 pa_assert(data->direction == PA_DIRECTION_OUTPUT || data->direction == PA_DIRECTION_INPUT);
116
117 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));
118 p->parent.free = device_port_free;
119
120 p->name = data->name;
121 data->name = NULL;
122 p->description = data->description;
123 data->description = NULL;
124 p->core = c;
125 p->card = NULL;
126 p->priority = 0;
127 p->available = data->available;
128 p->profiles = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
129 p->direction = data->direction;
130
131 p->latency_offset = 0;
132 p->proplist = pa_proplist_new();
133
134 return p;
135 }
136
137 void pa_device_port_set_latency_offset(pa_device_port *p, int64_t offset) {
138 uint32_t state;
139 pa_core *core;
140
141 pa_assert(p);
142
143 if (offset == p->latency_offset)
144 return;
145
146 p->latency_offset = offset;
147
148 switch (p->direction) {
149 case PA_DIRECTION_OUTPUT: {
150 pa_sink *sink;
151
152 PA_IDXSET_FOREACH(sink, p->core->sinks, state) {
153 if (sink->active_port == p) {
154 pa_sink_set_latency_offset(sink, p->latency_offset);
155 break;
156 }
157 }
158
159 break;
160 }
161
162 case PA_DIRECTION_INPUT: {
163 pa_source *source;
164
165 PA_IDXSET_FOREACH(source, p->core->sources, state) {
166 if (source->active_port == p) {
167 pa_source_set_latency_offset(source, p->latency_offset);
168 break;
169 }
170 }
171
172 break;
173 }
174 }
175
176 pa_assert_se(core = p->core);
177 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_CARD|PA_SUBSCRIPTION_EVENT_CHANGE, p->card->index);
178 pa_hook_fire(&core->hooks[PA_CORE_HOOK_PORT_LATENCY_OFFSET_CHANGED], p);
179 }