]> code.delx.au - pulseaudio/blob - src/sink-input.c
include config.h in every file
[pulseaudio] / src / sink-input.c
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4
5 #include <stdio.h>
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "sink-input.h"
11 #include "sample-util.h"
12
13 #define CONVERT_BUFFER_LENGTH 4096
14
15 struct pa_sink_input* pa_sink_input_new(struct pa_sink *s, const char *name, const struct pa_sample_spec *spec) {
16 struct pa_sink_input *i;
17 struct pa_resampler *resampler = NULL;
18 int r;
19 char st[256];
20 assert(s && spec);
21
22 if (!pa_sample_spec_equal(spec, &s->sample_spec))
23 if (!(resampler = pa_resampler_new(spec, &s->sample_spec)))
24 return NULL;
25
26 i = malloc(sizeof(struct pa_sink_input));
27 assert(i);
28 i->name = name ? strdup(name) : NULL;
29 i->client = NULL;
30 i->owner = NULL;
31 i->sink = s;
32 i->sample_spec = *spec;
33
34 i->peek = NULL;
35 i->drop = NULL;
36 i->kill = NULL;
37 i->get_latency = NULL;
38 i->userdata = NULL;
39
40 i->volume = PA_VOLUME_NORM;
41
42 i->resampled_chunk.memblock = NULL;
43 i->resampled_chunk.index = i->resampled_chunk.length = 0;
44 i->resampler = resampler;
45
46 assert(s->core);
47 r = pa_idxset_put(s->core->sink_inputs, i, &i->index);
48 assert(r == 0 && i->index != PA_IDXSET_INVALID);
49 r = pa_idxset_put(s->inputs, i, NULL);
50 assert(r == 0);
51
52 pa_sample_snprint(st, sizeof(st), spec);
53 fprintf(stderr, "sink-input: created %u \"%s\" on %u with sample spec \"%s\"\n", i->index, i->name, s->index, st);
54
55 return i;
56 }
57
58 void pa_sink_input_free(struct pa_sink_input* i) {
59 assert(i);
60
61 assert(i->sink && i->sink->core);
62 pa_idxset_remove_by_data(i->sink->core->sink_inputs, i, NULL);
63 pa_idxset_remove_by_data(i->sink->inputs, i, NULL);
64
65 if (i->resampled_chunk.memblock)
66 pa_memblock_unref(i->resampled_chunk.memblock);
67 if (i->resampler)
68 pa_resampler_free(i->resampler);
69
70 free(i->name);
71 free(i);
72 }
73
74 void pa_sink_input_kill(struct pa_sink_input*i) {
75 assert(i);
76
77 if (i->kill)
78 i->kill(i);
79 }
80
81 uint32_t pa_sink_input_get_latency(struct pa_sink_input *i) {
82 uint32_t l = 0;
83
84 assert(i);
85 if (i->get_latency)
86 l += i->get_latency(i);
87
88 assert(i->sink);
89 l += pa_sink_get_latency(i->sink);
90
91 return l;
92 }
93
94 int pa_sink_input_peek(struct pa_sink_input *i, struct pa_memchunk *chunk) {
95 assert(i && chunk && i->peek && i->drop);
96
97 if (!i->resampler)
98 return i->peek(i, chunk);
99
100 if (!i->resampled_chunk.memblock) {
101 struct pa_memchunk tchunk;
102 size_t l;
103 int ret;
104
105 if ((ret = i->peek(i, &tchunk)) < 0)
106 return ret;
107
108 l = pa_resampler_request(i->resampler, CONVERT_BUFFER_LENGTH);
109 if (tchunk.length > l)
110 tchunk.length = l;
111
112 i->drop(i, tchunk.length);
113
114 pa_resampler_run(i->resampler, &tchunk, &i->resampled_chunk);
115 pa_memblock_unref(tchunk.memblock);
116 }
117
118 assert(i->resampled_chunk.memblock && i->resampled_chunk.length);
119 *chunk = i->resampled_chunk;
120 pa_memblock_ref(i->resampled_chunk.memblock);
121 return 0;
122 }
123
124 void pa_sink_input_drop(struct pa_sink_input *i, size_t length) {
125 assert(i && length);
126
127 if (!i->resampler) {
128 i->drop(i, length);
129 return;
130 }
131
132 assert(i->resampled_chunk.memblock && i->resampled_chunk.length >= length);
133
134 i->resampled_chunk.index += length;
135 i->resampled_chunk.length -= length;
136
137 if (!i->resampled_chunk.length) {
138 pa_memblock_unref(i->resampled_chunk.memblock);
139 i->resampled_chunk.memblock = NULL;
140 i->resampled_chunk.index = i->resampled_chunk.length = 0;
141 }
142 }