]> code.delx.au - pulseaudio/blob - polyp/sink-input.c
rename src to polyp
[pulseaudio] / polyp / sink-input.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 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 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 "sink-input.h"
32 #include "sample-util.h"
33
34 #define CONVERT_BUFFER_LENGTH 4096
35
36 struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec) {
37 struct pa_sink_input *i;
38 struct pa_resampler *resampler = NULL;
39 int r;
40 char st[256];
41 assert(s && spec);
42
43 if (!pa_sample_spec_equal(spec, &s->sample_spec))
44 if (!(resampler = pa_resampler_new(spec, &s->sample_spec)))
45 return NULL;
46
47 i = malloc(sizeof(struct pa_sink_input));
48 assert(i);
49 i->name = name ? strdup(name) : NULL;
50 i->client = NULL;
51 i->owner = NULL;
52 i->sink = s;
53 i->sample_spec = *spec;
54
55 i->peek = NULL;
56 i->drop = NULL;
57 i->kill = NULL;
58 i->get_latency = NULL;
59 i->userdata = NULL;
60
61 i->volume = PA_VOLUME_NORM;
62
63 i->resampled_chunk.memblock = NULL;
64 i->resampled_chunk.index = i->resampled_chunk.length = 0;
65 i->resampler = resampler;
66
67 assert(s->core);
68 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
69 assert(r == 0 && i->index != PA_IDXSET_INVALID);
70 r = pa_idxset_put(s->inputs, i, NULL);
71 assert(r == 0);
72
73 pa_sample_snprint(st, sizeof(st), spec);
74 fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
75
76 return i;
77 }
78
79 void pa_sink_input_free(struct pa_sink_input* i) {
80 assert(i);
81
82 assert(i->sink && i->sink->core);
83 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
84 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
85
86 if (i->resampled_chunk.memblock)
87 pa_memblock_unref(i->resampled_chunk.memblock);
88 if (i->resampler)
89 pa_resampler_free(i->resampler);
90
91 free(i->name);
92 free(i);
93 }
94
95 void pa_sink_input_kill(struct pa_sink_input*i) {
96 assert(i);
97
98 if (i->kill)
99 i->kill(i);
100 }
101
102 uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) {
103 uint32_t l = 0;
104
105 assert(i);
106 if (i->get_latency)
107 l += i->get_latency(i);
108
109 assert(i->sink);
110 l += pa_sink_get_latency(i->sink);
111
112 return l;
113 }
114
115 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
116 assert(i && chunk && i->peek && i->drop);
117
118 if (!i->resampler)
119 return i->peek(i, chunk);
120
121 if (!i->resampled_chunk.memblock) {
122 struct pa_memchunk tchunk;
123 size_t l;
124 int ret;
125
126 if ((ret = i->peek(i, &tchunk)) < 0)
127 return ret;
128
129 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
130 if (tchunk.length > l)
131 tchunk.length = l;
132
133 i->drop(i, tchunk.length);
134
135 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
136 pa_memblock_unref(tchunk.memblock);
137 }
138
139 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
140 *chunk = i->resampled_chunk;
141 pa_memblock_ref(i->resampled_chunk.memblock);
142 return 0;
143 }
144
145 void pa_sink_input_drop(struct pa_sink_input *i, size_t length) {
146 assert(i && length);
147
148 if (!i->resampler) {
149 i->drop(i, length);
150 return;
151 }
152
153 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
154
155 i->resampled_chunk.index += length;
156 i->resampled_chunk.length -= length;
157
158 if (!i->resampled_chunk.length) {
159 pa_memblock_unref(i->resampled_chunk.memblock);
160 i->resampled_chunk.memblock = NULL;
161 i->resampled_chunk.index = i->resampled_chunk.length = 0;
162 }
163 }