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