Debian Codegraph @

plugins/input/pulseaudio/input_pulseaudio.c

raw
1 /* This file is part of Xmms2-libvisual.
2 *
3 * Xmms2-libvisual is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * Xmms2-libvisual is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with Xmms2-libvisual. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 #include <libvisual/libvisual.h>
18 #include <pulse/mainloop.h>
19 #include <pulse/stream.h>
20 #include <pulse/thread-mainloop.h>
21
22 VISUAL_PLUGIN_API_VERSION_VALIDATOR
23
24 #define SAMPLE_RATE 44100
25 #define SAMPLE_RATE_TYPE_LV VISUAL_AUDIO_SAMPLE_RATE_44100
26 #define CHANNELS 2
27 #define CHANNELS_TYPE_LV VISUAL_AUDIO_SAMPLE_CHANNEL_STEREO
28 #define SAMPLE_TYPE int16_t
29 #define SAMPLE_FORMAT_PA PA_SAMPLE_S16LE
30 #define SAMPLE_FORMAT_LV VISUAL_AUDIO_SAMPLE_FORMAT_S16
31
32 #define FRAMES 512
33 #define CHUNK_SIZE_BYTES (FRAMES * CHANNELS * sizeof(SAMPLE_TYPE))
34 #define CHUNKS (2 * SAMPLE_RATE / FRAMES + 1) // i.e. 2+ seconds of audio
35
36 #if defined(__clang__) || defined(__GNUC__)
37 # define ATOMIC_CONSUMER_LOAD_64(source, target) __atomic_load(&source, &target, __ATOMIC_ACQUIRE)
38 # define ATOMIC_PRODUCER_STORE_64(target, source) __atomic_store(&target, &source, __ATOMIC_RELEASE)
39 #else
40 # error We need GCC or Clang for __atomic_load and __atomic_store
41 #endif
42
43 pa_sample_spec sample_spec = {
44 .format = SAMPLE_FORMAT_PA,
45 .rate = SAMPLE_RATE,
46 .channels = CHANNELS
47 };
48
49 typedef struct {
50 pa_threaded_mainloop * mainloop;
51 pa_context * context;
52 pa_stream * input_stream;
53
54 SAMPLE_TYPE pcm_data[CHUNKS][FRAMES * CHANNELS]; // ringbuffer of chunks
55 uint64_t chunks_written;
56 uint64_t chunk_write_offset_bytes;
57 uint64_t chunks_read;
58 } pulseaudio_priv_t;
59
60 static int inp_pulseaudio_init (VisPluginData *plugin);
61 static int inp_pulseaudio_cleanup (VisPluginData *plugin);
62 static int inp_pulseaudio_upload (VisPluginData *plugin, VisAudio *audio);
63 static void on_input_stream_data (pa_stream *p, size_t nbytes, void *userdata);
64
65 const VisPluginInfo *get_plugin_info( void ) {
66 static VisInputPlugin input = {
67 .upload = inp_pulseaudio_upload
68 };
69
70 static VisPluginInfo info = {
71 .type = VISUAL_PLUGIN_TYPE_INPUT,
72
73 .plugname = "pulseaudio",
74 .name = "Pulseaudio input plugin",
75 .author = "Scott Sibley <[email protected]>"
76 ", "
77 "Sebastian Pipping <[email protected]>",
78 .version = "2.0",
79 .about = "Use input data from pulseaudio",
80 .help = "",
81 .license = VISUAL_PLUGIN_LICENSE_GPL, // v3 or later, see header
82
83 .init = inp_pulseaudio_init,
84 .cleanup = inp_pulseaudio_cleanup,
85 .plugin = VISUAL_OBJECT (&input)
86 };
87
88 return &info;
89 }
90
91 static int inp_pulseaudio_init( VisPluginData *plugin ) {
92 pulseaudio_priv_t *priv = visual_mem_new0(pulseaudio_priv_t, 1);
93 visual_object_set_private(VISUAL_OBJECT (plugin), priv);
94
95 priv->mainloop = pa_threaded_mainloop_new();
96 visual_log_return_val_if_fail( priv->mainloop != NULL, -1 );
97
98 pa_mainloop_api *const mainloop_api = pa_threaded_mainloop_get_api(priv->mainloop);
99 visual_log_return_val_if_fail( mainloop_api != NULL, -2 );
100
101 priv->context = pa_context_new(mainloop_api, "lv-pulseaudio");
102 visual_log_return_val_if_fail( priv->context != NULL, -3 );
103
104 const int connect_res =
105 pa_context_connect(priv->context, NULL, PA_CONTEXT_NOFLAGS, NULL);
106 visual_log_return_val_if_fail( connect_res == 0, -4 );
107
108 // NOTE: Starting the main loop prior to pa_context_connect would get us into
109 // this situation:
110 // Assertion 'c->callback' failed at
111 // ../pulseaudio-16.1/src/pulsecore/socket-client.c:126, function
112 // do_call(). Aborting.
113 // https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/issues/991
114 const int mainloop_start_ret = pa_threaded_mainloop_start(priv->mainloop);
115 visual_log_return_val_if_fail( mainloop_start_ret == 0, -5 );
116 while (pa_context_get_state(priv->context) < PA_CONTEXT_READY) {
117 visual_time_usleep(1000);
118 }
119
120 priv->input_stream = pa_stream_new(priv->context, "Recording", &sample_spec, NULL);
121 visual_log_return_val_if_fail( priv->input_stream != NULL, -6 );
122 pa_stream_set_read_callback(priv->input_stream, on_input_stream_data, priv);
123
124 const pa_buffer_attr input_buffer_attr = {
125 .fragsize = CHUNK_SIZE_BYTES,
126 .maxlength = CHUNK_SIZE_BYTES,
127 };
128 const int input_connect_res = pa_stream_connect_record(
129 priv->input_stream, NULL, &input_buffer_attr, PA_STREAM_ADJUST_LATENCY);
130 visual_log_return_val_if_fail( input_connect_res == 0, -7 );
131
132 return 0;
133 }
134
135 static int inp_pulseaudio_cleanup( VisPluginData *plugin )
136 {
137 pulseaudio_priv_t *priv = visual_object_get_private(VISUAL_OBJECT (plugin));
138 visual_log_return_val_if_fail( priv != NULL, -1 );
139
140 pa_stream_disconnect(priv->input_stream);
141 pa_stream_unref(priv->input_stream);
142 pa_context_disconnect(priv->context);
143 pa_context_unref(priv->context);
144 pa_threaded_mainloop_stop(priv->mainloop);
145 pa_threaded_mainloop_free(priv->mainloop);
146
147 visual_mem_free (priv);
148
149 return 0;
150 }
151
152 static int inp_pulseaudio_upload( VisPluginData *plugin, VisAudio *audio )
153 {
154 pulseaudio_priv_t *priv = visual_object_get_private(VISUAL_OBJECT (plugin));
155 visual_log_return_val_if_fail( priv != NULL, -1 );
156
157 // `priv->chunks_written` is monotonically increasing in another thread in parallel,
158 // so we make a snapshot to work with a single consistent value below.
159 // Also, plain reads to 64bit are not atomic on 32bit platforms, so we add protection.
160 // This is attomic `priv->chunks_written = frozen_chunks_written`.
161 uint64_t frozen_chunks_written;
162 ATOMIC_CONSUMER_LOAD_64(priv->chunks_written, frozen_chunks_written);
163
164 assert(priv->chunks_read <= frozen_chunks_written);
165 if (priv->chunks_read == frozen_chunks_written) {
166 return 0;
167 }
168
169 // The writing head keeps moving "in parallel" in another thread
170 // and without locking. So if the reader get too far behind, the writer
171 // overtakes the reader, in theory. It is not likely to happen because
172 // (1) the reader drains all available bytes at once, (2) the buffer
173 // is large enough to protect against temporary jittering, and (3)
174 // because `.upload` is guaranteed to be called at least at rendering
175 // FPS frequency.
176 const uint64_t tolerable_behind_by = CHUNKS / 2;
177 uint64_t behind_by = frozen_chunks_written - priv->chunks_read;
178 if (behind_by > tolerable_behind_by) {
179 priv->chunks_read = frozen_chunks_written - tolerable_behind_by;
180 behind_by = tolerable_behind_by;
181 }
182 const uint64_t target_chunks_read = priv->chunks_read + behind_by;
183
184 while (priv->chunks_read < target_chunks_read) {
185 void *const data = priv->pcm_data[priv->chunks_read % CHUNKS];
186 VisBuffer visbuffer;
187 visual_buffer_init(&visbuffer, data, CHUNK_SIZE_BYTES, NULL);
188 visual_audio_samplepool_input(audio->samplepool, &visbuffer, SAMPLE_RATE_TYPE_LV,
189 SAMPLE_FORMAT_LV, CHANNELS_TYPE_LV);
190 priv->chunks_read++;
191 }
192
193 return 0;
194 }
195
196 static void on_input_stream_data(pa_stream *p, size_t nbytes, void *userdata) {
197 pulseaudio_priv_t * const priv = (pulseaudio_priv_t *)userdata;
198 visual_log_return_if_fail( priv != NULL );
199
200 const void *source = NULL;
201 const int peek_res = pa_stream_peek(p, &source, &nbytes);
202 visual_log_return_if_fail( peek_res == 0 );
203 visual_log_return_if_fail( source != 0 );
204
205 const int drop_res = pa_stream_drop(p);
206 visual_log_return_if_fail( drop_res == 0 );
207
208 // Copy all readable bytes from `source` to the right place in `priv->pcm_data`.
209 while (nbytes > 0) {
210 void *const target = (void *)priv->pcm_data[priv->chunks_written % CHUNKS] + priv->chunk_write_offset_bytes;
211 size_t round_nbytes = nbytes;
212
213 // Would a full write overflow the current chunk?
214 if (priv->chunk_write_offset_bytes + round_nbytes > CHUNK_SIZE_BYTES) {
215 // Cut down to fit the chunk
216 round_nbytes = CHUNK_SIZE_BYTES - priv->chunk_write_offset_bytes;
217 }
218
219 visual_mem_copy(target, source, round_nbytes);
220
221 // Figure out write offset location for the next round
222 if (priv->chunk_write_offset_bytes + round_nbytes < CHUNK_SIZE_BYTES) {
223 // Same chunk but further behind
224 priv->chunk_write_offset_bytes += round_nbytes;
225 } else {
226 // Start of the next chunk
227 priv->chunk_write_offset_bytes = 0;
228
229 // This is atomic `priv->chunks_written++`
230 const uint64_t new_chunks_written = priv->chunks_written + 1;
231 ATOMIC_PRODUCER_STORE_64(priv->chunks_written, new_chunks_written);
232 }
233
234 nbytes -= round_nbytes;
235 }
236 }