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