f3e474bcce674a9698ebfd1d1d431ba5f88f09ef
[profile/ivi/pulseaudio.git] / src / sinkinput.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "sinkinput.h"
7 #include "strbuf.h"
8 #include "sample-util.h"
9
10 #define CONVERT_BUFFER_LENGTH 4096
11
12 struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec) {
13     struct pa_sink_input *i;
14     struct pa_resampler *resampler = NULL;
15     int r;
16     char st[256];
17     assert(s && spec);
18
19     if (!pa_sample_spec_equal(spec, &s->sample_spec))
20         if (!(resampler = pa_resampler_new(spec, &s->sample_spec)))
21             return NULL;
22     
23     i = malloc(sizeof(struct pa_sink_input));
24     assert(i);
25     i->name = name ? strdup(name) : NULL;
26     i->sink = s;
27     i->sample_spec = *spec;
28
29     i->peek = NULL;
30     i->drop = NULL;
31     i->kill = NULL;
32     i->get_latency = NULL;
33     i->userdata = NULL;
34
35     i->volume = PA_VOLUME_NORM;
36
37     i->resampled_chunk.memblock = NULL;
38     i->resampled_chunk.index = i->resampled_chunk.length = 0;
39     i->resampler = resampler;
40     
41     assert(s->core);
42     r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
43     assert(r == 0 && i->index != PA_IDXSET_INVALID);
44     r = pa_idxset_put(s->inputs, i, NULL);
45     assert(r == 0);
46
47     pa_sample_snprint(st, sizeof(st), spec);
48     fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
49     
50     return i;    
51 }
52
53 void pa_sink_input_free(struct pa_sink_input* i) {
54     assert(i);
55
56     assert(i->sink && i->sink->core);
57     pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
58     pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
59
60     if (i->resampled_chunk.memblock)
61         pa_memblock_unref(i->resampled_chunk.memblock);
62     if (i->resampler)
63         pa_resampler_free(i->resampler);
64     
65     free(i->name);
66     free(i);
67 }
68
69 void pa_sink_input_kill(struct pa_sink_input*i) {
70     assert(i);
71
72     if (i->kill)
73         i->kill(i);
74 }
75
76 char *pa_sink_input_list_to_string(struct pa_core *c) {
77     struct pa_strbuf *s;
78     struct pa_sink_input *i;
79     uint32_t index = PA_IDXSET_INVALID;
80     assert(c);
81
82     s = pa_strbuf_new();
83     assert(s);
84
85     pa_strbuf_printf(s, "%u sink input(s) available.\n", pa_idxset_ncontents(c->sink_inputs));
86
87     for (i = pa_idxset_first(c->sink_inputs, &index); i; i = pa_idxset_next(c->sink_inputs, &index)) {
88         char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH];
89         pa_sample_snprint(ss, sizeof(ss), &i->sample_spec);
90         assert(i->sink);
91         pa_strbuf_printf(
92             s, "    index: %u\n\tname: <%s>\n\tsink: <%u>\n\tvolume: <0x%04x>\n\tlatency: <%u usec>\n\tsample_spec: <%s>\n",
93             i->index,
94             i->name,
95             i->sink->index,
96             (unsigned) i->volume,
97             pa_sink_input_get_latency(i),
98             ss);
99     }
100     
101     return pa_strbuf_tostring_free(s);
102 }
103
104 uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) {
105     uint32_t l = 0;
106     
107     assert(i);
108     if (i->get_latency)
109         l += i->get_latency(i);
110
111     assert(i->sink);
112     l += pa_sink_get_latency(i->sink);
113
114     return l;
115 }
116
117
118 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
119     assert(i && chunk && i->peek && i->drop);
120
121     if (!i->resampler)
122         return i->peek(i, chunk);
123
124     if (!i->resampled_chunk.memblock) {
125         struct pa_memchunk tchunk;
126         size_t l;
127         int ret;
128         
129         if ((ret = i->peek(i, &tchunk)) < 0)
130             return ret;
131
132         l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
133         if (tchunk.length > l)
134             tchunk.length = l;
135
136         i->drop(i, tchunk.length);
137         
138         pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
139         pa_memblock_unref(tchunk.memblock);
140     }
141
142     assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
143     *chunk = i->resampled_chunk;
144     pa_memblock_ref(i->resampled_chunk.memblock);
145     return 0;
146 }
147
148 void pa_sink_input_drop(struct pa_sink_input *i, size_t length) {
149     assert(i && length);
150
151     if (!i->resampler) {
152         i->drop(i, length);
153         return;
154     }
155     
156     assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
157
158     i->resampled_chunk.index += length;
159     i->resampled_chunk.length -= length;
160
161     if (!i->resampled_chunk.length) {
162         pa_memblock_unref(i->resampled_chunk.memblock);
163         i->resampled_chunk.memblock = NULL;
164         i->resampled_chunk.index = i->resampled_chunk.length = 0;
165     }
166 }