]> code.delx.au - pulseaudio/blob - src/polypcore/source.c
Reorganised the source tree. We now have src/ with a couple of subdirs:
[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 "source.h"
32 #include "source-output.h"
33 #include "namereg.h"
34 #include "xmalloc.h"
35 #include "subscribe.h"
36 #include "log.h"
37
38 pa_source* pa_source_new(
39 pa_core *core,
40 const char *driver,
41 const char *name,
42 int fail,
43 const pa_sample_spec *spec,
44 const pa_channel_map *map) {
45
46 pa_source *s;
47 char st[256];
48 int r;
49
50 assert(core);
51 assert(name);
52 assert(*name);
53 assert(spec);
54
55 s = pa_xnew(pa_source, 1);
56
57 if (!(name = pa_namereg_register(core, name, PA_NAMEREG_SOURCE, s, fail))) {
58 pa_xfree(s);
59 return NULL;
60 }
61
62 s->ref = 1;
63 s->core = core;
64 s->state = PA_SOURCE_RUNNING;
65 s->name = pa_xstrdup(name);
66 s->description = NULL;
67 s->driver = pa_xstrdup(driver);
68 s->owner = NULL;
69
70 s->sample_spec = *spec;
71 if (map)
72 s->channel_map = *map;
73 else
74 pa_channel_map_init_auto(&s->channel_map, spec->channels);
75
76 s->outputs = pa_idxset_new(NULL, NULL);
77 s->monitor_of = NULL;
78
79 s->get_latency = NULL;
80 s->notify = NULL;
81 s->userdata = NULL;
82
83 r = pa_idxset_put(core->sources, s, &s->index);
84 assert(s->index != PA_IDXSET_INVALID && r >= 0);
85
86 pa_sample_spec_snprint(st, sizeof(st), spec);
87 pa_log_info(__FILE__": created %u \"%s\" with sample spec \"%s\"\n", s->index, s->name, st);
88
89 pa_subscription_post(core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_NEW, s->index);
90
91 return s;
92 }
93
94 void pa_source_disconnect(pa_source *s) {
95 pa_source_output *o, *j = NULL;
96
97 assert(s);
98 assert(s->state == PA_SOURCE_RUNNING);
99
100 pa_namereg_unregister(s->core, s->name);
101
102 while ((o = pa_idxset_first(s->outputs, NULL))) {
103 assert(o != j);
104 pa_source_output_kill(o);
105 j = o;
106 }
107
108 pa_idxset_remove_by_data(s->core->sources, s, NULL);
109
110 s->get_latency = NULL;
111 s->notify = NULL;
112
113 s->state = PA_SOURCE_DISCONNECTED;
114 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE | PA_SUBSCRIPTION_EVENT_REMOVE, s->index);
115 }
116
117 static void source_free(pa_source *s) {
118 assert(s);
119 assert(!s->ref);
120
121 if (s->state != PA_SOURCE_DISCONNECTED)
122 pa_source_disconnect(s);
123
124 pa_log_info(__FILE__": freed %u \"%s\"\n", s->index, s->name);
125
126 pa_idxset_free(s->outputs, NULL, NULL);
127
128 pa_xfree(s->name);
129 pa_xfree(s->description);
130 pa_xfree(s->driver);
131 pa_xfree(s);
132 }
133
134 void pa_source_unref(pa_source *s) {
135 assert(s);
136 assert(s->ref >= 1);
137
138 if (!(--s->ref))
139 source_free(s);
140 }
141
142 pa_source* pa_source_ref(pa_source *s) {
143 assert(s);
144 assert(s->ref >= 1);
145
146 s->ref++;
147 return s;
148 }
149
150 void pa_source_notify(pa_source*s) {
151 assert(s);
152 assert(s->ref >= 1);
153
154 if (s->notify)
155 s->notify(s);
156 }
157
158 static int do_post(void *p, PA_GCC_UNUSED uint32_t idx, int *del, void*userdata) {
159 pa_source_output *o = p;
160 const pa_memchunk *chunk = userdata;
161
162 assert(o);
163 assert(chunk);
164
165 pa_source_output_push(o, chunk);
166 return 0;
167 }
168
169 void pa_source_post(pa_source*s, const pa_memchunk *chunk) {
170 assert(s);
171 assert(s->ref >= 1);
172 assert(chunk);
173
174 pa_source_ref(s);
175 pa_idxset_foreach(s->outputs, do_post, (void*) chunk);
176 pa_source_unref(s);
177 }
178
179 void pa_source_set_owner(pa_source *s, pa_module *m) {
180 assert(s);
181 assert(s->ref >= 1);
182
183 s->owner = m;
184 }
185
186 pa_usec_t pa_source_get_latency(pa_source *s) {
187 assert(s);
188 assert(s->ref >= 1);
189
190 if (!s->get_latency)
191 return 0;
192
193 return s->get_latency(s);
194 }
195