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