]> code.delx.au - pulseaudio/blob - src/polypcore/source.c
Cleaned up the includes after the restructuring. Indicate which headers are
[pulseaudio] / src / polypcore / source.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio 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 License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <polypcore/source-output.h>
32 #include <polypcore/namereg.h>
33 #include <polypcore/xmalloc.h>
34 #include <polypcore/core-subscribe.h>
35 #include <polypcore/log.h>
36
37 #include "source.h"
38
39 pa_source* pa_source_new(
40 pa_core *core,
41 const char *driver,
42 const char *name,
43 int fail,
44 const pa_sample_spec *spec,
45 const pa_channel_map *map) {
46
47 pa_source *s;
48 char st[256];
49 int r;
50
51 assert(core);
52 assert(name);
53 assert(*name);
54 assert(spec);
55
56 s = pa_xnew(pa_source, 1);
57
58 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
59 pa_xfree(s);
60 return NULL;
61 }
62
63 s->ref = 1;
64 s->core = core;
65 s->state = PA_SOURCE_RUNNING;
66 s->name = pa_xstrdup(name);
67 s->description = NULL;
68 s->driver = pa_xstrdup(driver);
69 s->owner = NULL;
70
71 s->sample_spec = *spec;
72 if (map)
73 s->channel_map = *map;
74 else
75 pa_channel_map_init_auto(&s->channel_map, spec->channels);
76
77 s->outputs = pa_idxset_new(NULL, NULL);
78 s->monitor_of = NULL;
79
80 s->get_latency = NULL;
81 s->notify = NULL;
82 s->userdata = NULL;
83
84 r = pa_idxset_put(core->sources, s, &s->index);
85 assert(s->index != PA_IDXSET_INVALID && r >= 0);
86
87 pa_sample_spec_snprint(st, sizeof(st), spec);
88 pa_log_info(__FILE__": created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
89
90 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
91
92 return s;
93 }
94
95 void pa_source_disconnect(pa_source *s) {
96 pa_source_output *o, *j = NULL;
97
98 assert(s);
99 assert(s->state == PA_SOURCE_RUNNING);
100
101 pa_namereg_unregister(s->core, s->name);
102
103 while ((o = pa_idxset_first(s->outputs, NULL))) {
104 assert(o != j);
105 pa_source_output_kill(o);
106 j = o;
107 }
108
109 pa_idxset_remove_by_data(s->core->sources, s, NULL);
110
111 s->get_latency = NULL;
112 s->notify = NULL;
113
114 s->state = PA_SOURCE_DISCONNECTED;
115 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
116 }
117
118 static void source_free(pa_source *s) {
119 assert(s);
120 assert(!s->ref);
121
122 if (s->state != PA_SOURCE_DISCONNECTED)
123 pa_source_disconnect(s);
124
125 pa_log_info(__FILE__": freed %u \"%s\"\n", s->index, s->name);
126
127 pa_idxset_free(s->outputs, NULL, NULL);
128
129 pa_xfree(s->name);
130 pa_xfree(s->description);
131 pa_xfree(s->driver);
132 pa_xfree(s);
133 }
134
135 void pa_source_unref(pa_source *s) {
136 assert(s);
137 assert(s->ref >= 1);
138
139 if (!(--s->ref))
140 source_free(s);
141 }
142
143 pa_source* pa_source_ref(pa_source *s) {
144 assert(s);
145 assert(s->ref >= 1);
146
147 s->ref++;
148 return s;
149 }
150
151 void pa_source_notify(pa_source*s) {
152 assert(s);
153 assert(s->ref >= 1);
154
155 if (s->notify)
156 s->notify(s);
157 }
158
159 static int do_post(void *p, PA_GCC_UNUSED uint32_t idx, int *del, void*userdata) {
160 pa_source_output *o = p;
161 const pa_memchunk *chunk = userdata;
162
163 assert(o);
164 assert(chunk);
165
166 pa_source_output_push(o, chunk);
167 return 0;
168 }
169
170 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
171 assert(s);
172 assert(s->ref >= 1);
173 assert(chunk);
174
175 pa_source_ref(s);
176 pa_idxset_foreach(s->outputs, do_post, (void*) chunk);
177 pa_source_unref(s);
178 }
179
180 void pa_source_set_owner(pa_source *s, pa_module *m) {
181 assert(s);
182 assert(s->ref >= 1);
183
184 s->owner = m;
185 }
186
187 pa_usec_t pa_source_get_latency(pa_source *s) {
188 assert(s);
189 assert(s->ref >= 1);
190
191 if (!s->get_latency)
192 return 0;
193
194 return s->get_latency(s);
195 }
196