]> code.delx.au - pulseaudio/blob - polyp/source-output.c
commit liboil porting changes
[pulseaudio] / polyp / source-output.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-output.h"
32 #include "xmalloc.h"
33 #include "subscribe.h"
34 #include "log.h"
35
36 struct pa_source_output* pa_source_output_new(
37 struct pa_source *s,
38 const char *name,
39 const char *driver,
40 const struct pa_sample_spec *spec,
41 const struct pa_channel_map *map,
42 int resample_method) {
43
44 struct pa_source_output *o;
45 struct pa_resampler *resampler = NULL;
46 int r;
47 char st[256];
48 assert(s && spec);
49
50 if (pa_idxset_ncontents(s->outputs) >= PA_MAX_OUTPUTS_PER_SOURCE) {
51 pa_log(__FILE__": Failed to create source output: too many outputs per source.\n");
52 return NULL;
53 }
54
55 if (resample_method == PA_RESAMPLER_INVALID)
56 resample_method = s->core->resample_method;
57
58 if (!pa_sample_spec_equal(&s->sample_spec, spec))
59 if (!(resampler = pa_resampler_new(&s->sample_spec, &s->channel_map, spec, map, s->core->memblock_stat, resample_method)))
60 return NULL;
61
62 o = pa_xmalloc(sizeof(struct pa_source_output));
63 o->ref = 1;
64 o->state = PA_SOURCE_OUTPUT_RUNNING;
65 o->name = pa_xstrdup(name);
66 o->driver = pa_xstrdup(driver);
67
68 o->client = NULL;
69 o->owner = NULL;
70 o->source = s;
71
72 o->sample_spec = *spec;
73 if (map)
74 c->channel_map = *map;
75 else
76 pa_channel_map_init_auto(&c->channel_map, spec->channels);
77
78 o->push = NULL;
79 o->kill = NULL;
80 o->userdata = NULL;
81 o->get_latency = NULL;
82
83 o->resampler = resampler;
84
85 assert(s->core);
86 r = pa_idxset_put(s->core->source_outputs, o, &o->index);
87 assert(r == 0 && o->index != PA_IDXSET_INVALID);
88 r = pa_idxset_put(s->outputs, o, NULL);
89 assert(r == 0);
90
91 pa_sample_spec_snprint(st, sizeof(st), spec);
92 pa_log_info(__FILE__": created %u \"%s\" on %u with sample spec \"%s\"\n", o->index, o->name, s->index, st);
93
94 pa_subscription_post(s->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_NEW, o->index);
95
96 return o;
97 }
98
99 void pa_source_output_disconnect(struct pa_source_output*o) {
100 assert(o && o->state != PA_SOURCE_OUTPUT_DISCONNECTED && o->source && o->source->core);
101
102 pa_idxset_remove_by_data(o->source->core->source_outputs, o, NULL);
103 pa_idxset_remove_by_data(o->source->outputs, o, NULL);
104
105 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_REMOVE, o->index);
106 o->source = NULL;
107
108 o->push = NULL;
109 o->kill = NULL;
110
111 o->state = PA_SOURCE_OUTPUT_DISCONNECTED;
112 }
113
114 static void source_output_free(struct pa_source_output* o) {
115 assert(o);
116
117 if (o->state != PA_SOURCE_OUTPUT_DISCONNECTED)
118 pa_source_output_disconnect(o);
119
120 pa_log_info(__FILE__": freed %u \"%s\"\n", o->index, o->name);
121
122 if (o->resampler)
123 pa_resampler_free(o->resampler);
124
125 pa_xfree(o->name);
126 pa_xfree(o->driver);
127 pa_xfree(o);
128 }
129
130
131 void pa_source_output_unref(struct pa_source_output* o) {
132 assert(o && o->ref >= 1);
133
134 if (!(--o->ref))
135 source_output_free(o);
136 }
137
138 struct pa_source_output* pa_source_output_ref(struct pa_source_output *o) {
139 assert(o && o->ref >= 1);
140 o->ref++;
141 return o;
142 }
143
144
145 void pa_source_output_kill(struct pa_source_output*o) {
146 assert(o && o->ref >= 1);
147
148 if (o->kill)
149 o->kill(o);
150 }
151
152 void pa_source_output_push(struct pa_source_output *o, const struct pa_memchunk *chunk) {
153 struct pa_memchunk rchunk;
154 assert(o && chunk && chunk->length && o->push);
155
156 if (o->state == PA_SOURCE_OUTPUT_CORKED)
157 return;
158
159 if (!o->resampler) {
160 o->push(o, chunk);
161 return;
162 }
163
164 pa_resampler_run(o->resampler, chunk, &rchunk);
165 if (!rchunk.length)
166 return;
167
168 assert(rchunk.memblock);
169 o->push(o, &rchunk);
170 pa_memblock_unref(rchunk.memblock);
171 }
172
173 void pa_source_output_set_name(struct pa_source_output *o, const char *name) {
174 assert(o && o->ref >= 1);
175 pa_xfree(o->name);
176 o->name = pa_xstrdup(name);
177
178 pa_subscription_post(o->source->core, PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT|PA_SUBSCRIPTION_EVENT_CHANGE, o->index);
179 }
180
181 pa_usec_t pa_source_output_get_latency(struct pa_source_output *o) {
182 assert(o && o->ref >= 1);
183
184 if (o->get_latency)
185 return o->get_latency(o);
186
187 return 0;
188 }
189
190 void pa_source_output_cork(struct pa_source_output *o, int b) {
191 assert(o && o->ref >= 1);
192
193 if (o->state == PA_SOURCE_OUTPUT_DISCONNECTED)
194 return;
195
196 o->state = b ? PA_SOURCE_OUTPUT_CORKED : PA_SOURCE_OUTPUT_RUNNING;
197 }
198
199 enum pa_resample_method pa_source_output_get_resample_method(struct pa_source_output *o) {
200 assert(o && o->ref >= 1);
201
202 if (!o->resampler)
203 return PA_RESAMPLER_INVALID;
204
205 return pa_resampler_get_method(o->resampler);
206 }