pulseaudio: Implemented audio capture support!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.c b/src/audio/pulseaudio/SDL_pulseaudio.c
index 4fed809..cc9a4db 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.c
+++ b/src/audio/pulseaudio/SDL_pulseaudio.c
@@ -100,12 +100,19 @@ static pa_stream * (*PULSEAUDIO_pa_stream_new) (pa_context *, const char *,
const pa_sample_spec *, const pa_channel_map *);
static int (*PULSEAUDIO_pa_stream_connect_playback) (pa_stream *, const char *,
const pa_buffer_attr *, pa_stream_flags_t, pa_cvolume *, pa_stream *);
+static int (*PULSEAUDIO_pa_stream_connect_record) (pa_stream *, const char *,
+ const pa_buffer_attr *, pa_stream_flags_t);
static pa_stream_state_t (*PULSEAUDIO_pa_stream_get_state) (pa_stream *);
static size_t (*PULSEAUDIO_pa_stream_writable_size) (pa_stream *);
+static size_t (*PULSEAUDIO_pa_stream_readable_size) (pa_stream *);
static int (*PULSEAUDIO_pa_stream_write) (pa_stream *, const void *, size_t,
pa_free_cb_t, int64_t, pa_seek_mode_t);
static pa_operation * (*PULSEAUDIO_pa_stream_drain) (pa_stream *,
pa_stream_success_cb_t, void *);
+static int (*PULSEAUDIO_pa_stream_peek) (pa_stream *, const void **, size_t *);
+static int (*PULSEAUDIO_pa_stream_drop) (pa_stream *);
+static pa_operation * (*PULSEAUDIO_pa_stream_flush) (pa_stream *,
+ pa_stream_success_cb_t, void *);
static int (*PULSEAUDIO_pa_stream_disconnect) (pa_stream *);
static void (*PULSEAUDIO_pa_stream_unref) (pa_stream *);
@@ -206,11 +213,16 @@ load_pulseaudio_syms(void)
SDL_PULSEAUDIO_SYM(pa_context_unref);
SDL_PULSEAUDIO_SYM(pa_stream_new);
SDL_PULSEAUDIO_SYM(pa_stream_connect_playback);
+ SDL_PULSEAUDIO_SYM(pa_stream_connect_record);
SDL_PULSEAUDIO_SYM(pa_stream_get_state);
SDL_PULSEAUDIO_SYM(pa_stream_writable_size);
+ SDL_PULSEAUDIO_SYM(pa_stream_readable_size);
SDL_PULSEAUDIO_SYM(pa_stream_write);
SDL_PULSEAUDIO_SYM(pa_stream_drain);
SDL_PULSEAUDIO_SYM(pa_stream_disconnect);
+ SDL_PULSEAUDIO_SYM(pa_stream_peek);
+ SDL_PULSEAUDIO_SYM(pa_stream_drop);
+ SDL_PULSEAUDIO_SYM(pa_stream_flush);
SDL_PULSEAUDIO_SYM(pa_stream_unref);
SDL_PULSEAUDIO_SYM(pa_channel_map_init_auto);
SDL_PULSEAUDIO_SYM(pa_strerror);
@@ -240,6 +252,12 @@ getAppName(void)
}
static void
+stream_operation_complete_no_op(pa_stream *s, int success, void *userdata)
+{
+ /* no-op for pa_stream_drain(), etc, to use for callback. */
+}
+
+static void
WaitForPulseOperation(pa_mainloop *mainloop, pa_operation *o)
{
/* This checks for NO errors currently. Either fix that, check results elsewhere, or do things you don't care about. */
@@ -352,17 +370,11 @@ PULSEAUDIO_PlayDevice(_THIS)
}
static void
-stream_drain_complete(pa_stream *s, int success, void *userdata)
-{
- /* no-op for pa_stream_drain() to use for callback. */
-}
-
-static void
PULSEAUDIO_WaitDone(_THIS)
{
if (SDL_AtomicGet(&this->enabled)) {
struct SDL_PrivateAudioData *h = this->hidden;
- pa_operation *o = PULSEAUDIO_pa_stream_drain(h->stream, stream_drain_complete, NULL);
+ pa_operation *o = PULSEAUDIO_pa_stream_drain(h->stream, stream_operation_complete_no_op, NULL);
if (o) {
while (PULSEAUDIO_pa_operation_get_state(o) != PA_OPERATION_DONE) {
if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
@@ -386,10 +398,75 @@ PULSEAUDIO_GetDeviceBuf(_THIS)
}
+static int
+PULSEAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
+{
+ struct SDL_PrivateAudioData *h = this->hidden;
+ const void *data = NULL;
+ size_t nbytes = 0;
+
+ while (SDL_AtomicGet(&this->enabled)) {
+ if (h->capturebuf != NULL) {
+ const int cpy = SDL_min(buflen, h->capturelen);
+ SDL_memcpy(buffer, h->capturebuf, cpy);
+ /*printf("PULSEAUDIO: fed %d captured bytes\n", cpy);*/
+ h->capturebuf += cpy;
+ h->capturelen -= cpy;
+ if (h->capturelen == 0) {
+ h->capturebuf = NULL;
+ PULSEAUDIO_pa_stream_drop(h->stream); /* done with this fragment. */
+ }
+ return cpy; /* new data, return it. */
+ }
+
+ if (PULSEAUDIO_pa_context_get_state(h->context) != PA_CONTEXT_READY ||
+ PULSEAUDIO_pa_stream_get_state(h->stream) != PA_STREAM_READY ||
+ PULSEAUDIO_pa_mainloop_iterate(h->mainloop, 1, NULL) < 0) {
+ SDL_OpenedAudioDeviceDisconnected(this);
+ return -1; /* uhoh, pulse failed! */
+ }
+
+ if (PULSEAUDIO_pa_stream_readable_size(h->stream) == 0) {
+ continue; /* no data available yet. */
+ }
+
+ /* a new fragment is available! */
+ PULSEAUDIO_pa_stream_peek(h->stream, &data, &nbytes);
+ SDL_assert(nbytes > 0);
+ if (data == NULL) { /* NULL==buffer had a hole. Ignore that. */
+ PULSEAUDIO_pa_stream_drop(h->stream); /* drop this fragment. */
+ } else {
+ /* store this fragment's data, start feeding it to SDL. */
+ /*printf("PULSEAUDIO: captured %d new bytes\n", (int) nbytes);*/
+ h->capturebuf = (const Uint8 *) data;
+ h->capturelen = nbytes;
+ }
+ }
+
+ return -1; /* not enabled? */
+}
+
+static void
+PULSEAUDIO_FlushCapture(_THIS)
+{
+ struct SDL_PrivateAudioData *h = this->hidden;
+
+ if (h->capturebuf != NULL) {
+ PULSEAUDIO_pa_stream_drop(h->stream);
+ h->capturebuf = NULL;
+ h->capturelen = 0;
+ }
+
+ WaitForPulseOperation(h->mainloop, PULSEAUDIO_pa_stream_flush(h->stream, stream_operation_complete_no_op, NULL));
+}
+
static void
PULSEAUDIO_CloseDevice(_THIS)
{
if (this->hidden != NULL) {
+ if (this->hidden->capturebuf != NULL) {
+ PULSEAUDIO_pa_stream_drop(this->hidden->stream);
+ }
SDL_FreeAudioMem(this->hidden->mixbuf);
SDL_free(this->hidden->device_name);
if (this->hidden->stream) {
@@ -403,7 +480,16 @@ PULSEAUDIO_CloseDevice(_THIS)
}
static void
-DeviceNameCallback(pa_context *c, const pa_sink_info *i, int is_last, void *data)
+SinkDeviceNameCallback(pa_context *c, const pa_sink_info *i, int is_last, void *data)
+{
+ if (i) {
+ char **devname = (char **) data;
+ *devname = SDL_strdup(i->name);
+ }
+}
+
+static void
+SourceDeviceNameCallback(pa_context *c, const pa_source_info *i, int is_last, void *data)
{
if (i) {
char **devname = (char **) data;
@@ -412,7 +498,7 @@ DeviceNameCallback(pa_context *c, const pa_sink_info *i, int is_last, void *data
}
static SDL_bool
-FindDeviceName(struct SDL_PrivateAudioData *h, void *handle)
+FindDeviceName(struct SDL_PrivateAudioData *h, const int iscapture, void *handle)
{
const uint32_t idx = ((uint32_t) ((size_t) handle)) - 1;
@@ -420,7 +506,16 @@ FindDeviceName(struct SDL_PrivateAudioData *h, void *handle)
return SDL_TRUE;
}
- WaitForPulseOperation(h->mainloop, PULSEAUDIO_pa_context_get_sink_info_by_index(h->context, idx, DeviceNameCallback, &h->device_name));
+ if (iscapture) {
+ WaitForPulseOperation(h->mainloop,
+ PULSEAUDIO_pa_context_get_source_info_by_index(h->context, idx,
+ SourceDeviceNameCallback, &h->device_name));
+ } else {
+ WaitForPulseOperation(h->mainloop,
+ PULSEAUDIO_pa_context_get_sink_info_by_index(h->context, idx,
+ SinkDeviceNameCallback, &h->device_name));
+ }
+
return (h->device_name != NULL);
}
@@ -434,6 +529,7 @@ PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
pa_channel_map pacmap;
pa_stream_flags_t flags = 0;
int state = 0;
+ int rc = 0;
/* Initialize all variables that we clean on shutdown */
this->hidden = (struct SDL_PrivateAudioData *)
@@ -495,13 +591,15 @@ PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
SDL_CalculateAudioSpec(&this->spec);
/* Allocate mixing buffer */
- h->mixlen = this->spec.size;
- h->mixbuf = (Uint8 *) SDL_AllocAudioMem(h->mixlen);
- if (h->mixbuf == NULL) {
- PULSEAUDIO_CloseDevice(this);
- return SDL_OutOfMemory();
+ if (!iscapture) {
+ h->mixlen = this->spec.size;
+ h->mixbuf = (Uint8 *) SDL_AllocAudioMem(h->mixlen);
+ if (h->mixbuf == NULL) {
+ PULSEAUDIO_CloseDevice(this);
+ return SDL_OutOfMemory();
+ }
+ SDL_memset(h->mixbuf, this->spec.silence, this->spec.size);
}
- SDL_memset(h->mixbuf, this->spec.silence, this->spec.size);
paspec.channels = this->spec.channels;
paspec.rate = this->spec.freq;
@@ -527,9 +625,9 @@ PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
return SDL_SetError("Could not connect to PulseAudio server");
}
- if (!FindDeviceName(h, handle)) {
+ if (!FindDeviceName(h, iscapture, handle)) {
PULSEAUDIO_CloseDevice(this);
- return SDL_SetError("Requested PulseAudio sink missing?");
+ return SDL_SetError("Requested PulseAudio sink/source missing?");
}
/* The SDL ALSA output hints us that we use Windows' channel mapping */
@@ -555,8 +653,13 @@ PULSEAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
flags |= PA_STREAM_DONT_MOVE;
}
- if (PULSEAUDIO_pa_stream_connect_playback(h->stream, h->device_name, &paattr, flags,
- NULL, NULL) < 0) {
+ if (iscapture) {
+ rc = PULSEAUDIO_pa_stream_connect_record(h->stream, h->device_name, &paattr, flags);
+ } else {
+ rc = PULSEAUDIO_pa_stream_connect_playback(h->stream, h->device_name, &paattr, flags, NULL, NULL);
+ }
+
+ if (rc < 0) {
PULSEAUDIO_CloseDevice(this);
return SDL_SetError("Could not connect PulseAudio stream");
}
@@ -687,6 +790,10 @@ PULSEAUDIO_Init(SDL_AudioDriverImpl * impl)
impl->CloseDevice = PULSEAUDIO_CloseDevice;
impl->WaitDone = PULSEAUDIO_WaitDone;
impl->Deinitialize = PULSEAUDIO_Deinitialize;
+ impl->CaptureFromDevice = PULSEAUDIO_CaptureFromDevice;
+ impl->FlushCapture = PULSEAUDIO_FlushCapture;
+
+ impl->HasCaptureSupport = SDL_TRUE;
return 1; /* this audio target is available. */
}
diff --git a/src/audio/pulseaudio/SDL_pulseaudio.h b/src/audio/pulseaudio/SDL_pulseaudio.h
index c57ab71..e12000f 100644
--- a/src/audio/pulseaudio/SDL_pulseaudio.h
+++ b/src/audio/pulseaudio/SDL_pulseaudio.h
@@ -42,6 +42,9 @@ struct SDL_PrivateAudioData
/* Raw mixing buffer */
Uint8 *mixbuf;
int mixlen;
+
+ const Uint8 *capturebuf;
+ int capturelen;
};
#endif /* _SDL_pulseaudio_h */