add description field for sinks/sources
[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->client = NULL;
27     i->owner = NULL;
28     i->sink = s;
29     i->sample_spec = *spec;
30
31     i->peek = NULL;
32     i->drop = NULL;
33     i->kill = NULL;
34     i->get_latency = NULL;
35     i->userdata = NULL;
36
37     i->volume = PA_VOLUME_NORM;
38
39     i->resampled_chunk.memblock = NULL;
40     i->resampled_chunk.index = i->resampled_chunk.length = 0;
41     i->resampler = resampler;
42     
43     assert(s->core);
44     r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
45     assert(r == 0 && i->index != PA_IDXSET_INVALID);
46     r = pa_idxset_put(s->inputs, i, NULL);
47     assert(r == 0);
48
49     pa_sample_snprint(st, sizeof(st), spec);
50     fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
51     
52     return i;    
53 }
54
55 void pa_sink_input_free(struct pa_sink_input* i) {
56     assert(i);
57
58     assert(i->sink && i->sink->core);
59     pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
60     pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
61
62     if (i->resampled_chunk.memblock)
63         pa_memblock_unref(i->resampled_chunk.memblock);
64     if (i->resampler)
65         pa_resampler_free(i->resampler);
66     
67     free(i->name);
68     free(i);
69 }
70
71 void pa_sink_input_kill(struct pa_sink_input*i) {
72     assert(i);
73
74     if (i->kill)
75         i->kill(i);
76 }
77
78 char *pa_sink_input_list_to_string(struct pa_core *c) {
79     struct pa_strbuf *s;
80     struct pa_sink_input *i;
81     uint32_t index = PA_IDXSET_INVALID;
82     assert(c);
83
84     s = pa_strbuf_new();
85     assert(s);
86
87     pa_strbuf_printf(s, "%u sink input(s) available.\n", pa_idxset_ncontents(c->sink_inputs));
88
89     for (i = pa_idxset_first(c->sink_inputs, &index); i; i = pa_idxset_next(c->sink_inputs, &index)) {
90         char ss[PA_SAMPLE_SNPRINT_MAX_LENGTH];
91         pa_sample_snprint(ss, sizeof(ss), &i->sample_spec);
92         assert(i->sink);
93         pa_strbuf_printf(
94             s, "    index: %u\n\tname: <%s>\n\tsink: <%u>\n\tvolume: <0x%04x>\n\tlatency: <%u usec>\n\tsample_spec: <%s>\n",
95             i->index,
96             i->name,
97             i->sink->index,
98             (unsigned) i->volume,
99             pa_sink_input_get_latency(i),
100             ss);
101
102         if (i->owner)
103             pa_strbuf_printf(s, "\towner module: <%u>\n", i->owner->index);
104         if (i->client)
105             pa_strbuf_printf(s, "\tclient: <%u>\n", i->client->index);
106     }
107     
108     return pa_strbuf_tostring_free(s);
109 }
110
111 uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) {
112     uint32_t l = 0;
113     
114     assert(i);
115     if (i->get_latency)
116         l += i->get_latency(i);
117
118     assert(i->sink);
119     l += pa_sink_get_latency(i->sink);
120
121     return l;
122 }
123
124
125 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
126     assert(i && chunk && i->peek && i->drop);
127
128     if (!i->resampler)
129         return i->peek(i, chunk);
130
131     if (!i->resampled_chunk.memblock) {
132         struct pa_memchunk tchunk;
133         size_t l;
134         int ret;
135         
136         if ((ret = i->peek(i, &tchunk)) < 0)
137             return ret;
138
139         l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
140         if (tchunk.length > l)
141             tchunk.length = l;
142
143         i->drop(i, tchunk.length);
144         
145         pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
146         pa_memblock_unref(tchunk.memblock);
147     }
148
149     assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
150     *chunk = i->resampled_chunk;
151     pa_memblock_ref(i->resampled_chunk.memblock);
152     return 0;
153 }
154
155 void pa_sink_input_drop(struct pa_sink_input *i, size_t length) {
156     assert(i && length);
157
158     if (!i->resampler) {
159         i->drop(i, length);
160         return;
161     }
162     
163     assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
164
165     i->resampled_chunk.index += length;
166     i->resampled_chunk.length -= length;
167
168     if (!i->resampled_chunk.length) {
169         pa_memblock_unref(i->resampled_chunk.memblock);
170         i->resampled_chunk.memblock = NULL;
171         i->resampled_chunk.index = i->resampled_chunk.length = 0;
172     }
173 }