| 1 |
|
| 2 |
/ |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#include <alsa/asoundlib.h> |
| 12 |
#include <pthread.h> |
| 13 |
#include <stdbool.h> |
| 14 |
#include <stdio.h> |
| 15 |
|
| 16 |
#define DEFAULT_PCM "pipewire" |
| 17 |
#define DEFAULT_RATE 44100 |
| 18 |
#define DEFAULT_CHANNELS 2 |
| 19 |
#define N_THREADS 20 |
| 20 |
|
| 21 |
static void * |
| 22 |
thread_func(void *data) |
| 23 |
{ |
| 24 |
snd_pcm_t *pcm = NULL; |
| 25 |
snd_pcm_hw_params_t *params; |
| 26 |
int res; |
| 27 |
long n = (long)data; |
| 28 |
unsigned int sample_rate = DEFAULT_RATE; |
| 29 |
|
| 30 |
res = snd_pcm_open(&pcm, DEFAULT_PCM, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK); |
| 31 |
if (res < 0) { |
| 32 |
fprintf(stderr, "open failed: %s\n", snd_strerror(res)); |
| 33 |
pcm = NULL; |
| 34 |
goto fail; |
| 35 |
} |
| 36 |
printf("opened %ld\n", n); |
| 37 |
|
| 38 |
snd_pcm_hw_params_alloca(¶ms); |
| 39 |
res = snd_pcm_hw_params_any(pcm, params); |
| 40 |
if (res < 0) { |
| 41 |
fprintf(stderr, "params_any failed: %s\n", snd_strerror(res)); |
| 42 |
goto fail; |
| 43 |
} |
| 44 |
|
| 45 |
res = snd_pcm_hw_params_set_access(pcm, params, SND_PCM_ACCESS_RW_INTERLEAVED); |
| 46 |
if (res < 0) { |
| 47 |
fprintf(stderr, "set_access failed: %s\n", snd_strerror(res)); |
| 48 |
goto fail; |
| 49 |
} |
| 50 |
|
| 51 |
res = snd_pcm_hw_params_set_format(pcm, params, SND_PCM_FORMAT_S32_LE); |
| 52 |
if (res < 0) { |
| 53 |
fprintf(stderr, "set_format failed: %s\n", snd_strerror(res)); |
| 54 |
goto fail; |
| 55 |
} |
| 56 |
|
| 57 |
res = snd_pcm_hw_params_set_rate_near(pcm, params, &sample_rate, 0); |
| 58 |
if (res < 0) { |
| 59 |
fprintf(stderr, "set_rate_near failed: %s\n", snd_strerror(res)); |
| 60 |
goto fail; |
| 61 |
} |
| 62 |
|
| 63 |
res = snd_pcm_hw_params_set_channels(pcm, params, DEFAULT_CHANNELS); |
| 64 |
if (res < 0) { |
| 65 |
fprintf(stderr, "set_channels failed: %s\n", snd_strerror(res)); |
| 66 |
goto fail; |
| 67 |
} |
| 68 |
|
| 69 |
res = snd_pcm_hw_params(pcm, params); |
| 70 |
if (res < 0) { |
| 71 |
fprintf(stderr, "params failed: %s\n", snd_strerror(res)); |
| 72 |
goto fail; |
| 73 |
} |
| 74 |
|
| 75 |
res = snd_pcm_prepare(pcm); |
| 76 |
if (res < 0) { |
| 77 |
fprintf(stderr, "prepare failed: %s (%d)\n", snd_strerror(res), res); |
| 78 |
goto fail; |
| 79 |
} |
| 80 |
printf("prepared %ld\n", n); |
| 81 |
|
| 82 |
res = snd_pcm_close(pcm); |
| 83 |
if (res < 0) { |
| 84 |
fprintf(stderr, "close failed: %s\n", snd_strerror(res)); |
| 85 |
pcm = NULL; |
| 86 |
goto fail; |
| 87 |
} |
| 88 |
printf("closed %ld\n", n); |
| 89 |
|
| 90 |
return NULL; |
| 91 |
|
| 92 |
fail: |
| 93 |
if (pcm != NULL) { |
| 94 |
res = snd_pcm_close(pcm); |
| 95 |
if (res < 0) { |
| 96 |
fprintf(stderr, "close failed: %s\n", snd_strerror(res)); |
| 97 |
} |
| 98 |
} |
| 99 |
exit(EXIT_FAILURE); |
| 100 |
} |
| 101 |
|
| 102 |
int |
| 103 |
main(int argc, char *argv[]) |
| 104 |
{ |
| 105 |
pthread_t t[N_THREADS] = { 0 }; |
| 106 |
long n; |
| 107 |
int s; |
| 108 |
|
| 109 |
|
| 110 |
setenv("PIPEWIRE_CONFIG_NAME", "client.conf", false); |
| 111 |
|
| 112 |
while (true) { |
| 113 |
for (n=0; n < N_THREADS; n++) { |
| 114 |
if ((s = pthread_create(&(t[n]), NULL, thread_func, (void *)n)) != 0) { |
| 115 |
fprintf(stderr, "pthread_create: %s\n", strerror(s)); |
| 116 |
exit(EXIT_FAILURE); |
| 117 |
} |
| 118 |
printf("created %ld\n", n); |
| 119 |
} |
| 120 |
for (n=0; n < N_THREADS; n++) { |
| 121 |
if (t[n] != 0 && (s = pthread_join(t[n], NULL)) != 0) { |
| 122 |
fprintf(stderr, "pthread_join: %s\n", strerror(s)); |
| 123 |
exit(EXIT_FAILURE); |
| 124 |
} |
| 125 |
printf("joined %ld\n", n); |
| 126 |
t[n] = 0; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return EXIT_SUCCESS; |
| 131 |
} |