History

ALSA receiver improvements (#13)

* scream-alsa: add -v option to dump PCM setup info * scream-alsa: remove unnecessary `channels` argument in setup_alsa * scream-alsa: use snd_pcm_set_params to set up the PCM to get a reasonable buffer and start_threshold on a wider variety of hardware * scream-alsa: document -t target latency * scream-alsa: handle short writes by writing again * scream-alsa: add error handling to dump_alsa_info * scream-alsa: remove unused macro

Ivan Kozik committed 8 years ago

Commit 35bc85b1 parent df6b3d12 files · +53 −55Browse files

Unified · Split

2 files
  1. Receivers/alsa/README.md +7 −2
  2. Receivers/alsa/scream-alsa.c +46 −53
Receivers/alsa/README.md+7−2
@@ -29,10 +29,15 @@ $ scream-alsa -i eth0 · 28 unmodified lines
2929
3030 If you experience excessive underruns under normal operating conditions,
3131 lower the process niceness; if it still underruns, raise the default
32−output start threshold (1960) with `-t`:
32+target latency (50 ms) with `-t`:
3333
3434 ```shell
35−$ scream-alsa -t 7840
35+$ scream-alsa -t 100
3636 ```
3737
38+Note that audio hardware typically has small buffers that result in a
39+latency lower than the target latency.
40+
41+Run with `-v` to dump ALSA PCM setup information.
42+
3843 Run with `env LIBASOUND_DEBUG=1` to debug ALSA problems.
Receivers/alsa/scream-alsa.c+46−53
@@ -15,7 +15,6 @@ · 14 unmodified lines
1515 #define MAX_SO_PACKETSIZE 1764
1616 #define BYTES_PER_SAMPLE 2
1717 #define CHANNELS 2
18−#define TYPICAL_SAMPLES_PER_PACKET (980 / (BYTES_PER_SAMPLE * CHANNELS))
1918
2019 #define SNDCHK(call, ret) { \
2120 if (ret < 0) { \
@@ -31,7 +30,7 @@ static void alsa_error(const char *msg, int r) · 9 unmodified lines
3130
3231 static void show_usage(const char *arg0)
3332 {
34− fprintf(stderr, "Usage: %s [-i interface_name_or_address] [-t alsa_output_start_threshold]\n",
33+ fprintf(stderr, "Usage: %s [-v] [-i interface_name_or_address] [-t target_latency_ms]\n",
3534 arg0);
3635 exit(1);
3736 }
@@ -74,68 +73,58 @@ error_exit: · 36 unmodified lines
7473 exit(1);
7574 }
7675
77−static int setup_alsa(snd_pcm_t *pcm, unsigned int rate, unsigned int channels, int start_threshold)
76+static int dump_alsa_info(snd_pcm_t *pcm)
7877 {
7978 int ret;
80− snd_pcm_hw_params_t *hw;
81− snd_pcm_sw_params_t *sw;
79+ snd_output_t *log;
8280
83− snd_pcm_hw_params_alloca(&hw);
81+ ret = snd_output_stdio_attach(&log, stderr, 0);
82+ SNDCHK("snd_output_stdio_attach", ret);
8483
85− ret = snd_pcm_hw_params_any(pcm, hw);
86− SNDCHK("snd_pcm_hw_params_any", ret);
84+ ret = snd_pcm_dump(pcm, log);
85+ SNDCHK("snd_pcm_dump", ret);
8786
88− ret = snd_pcm_hw_params_set_rate_resample(pcm, hw, 1);
89− SNDCHK("snd_pcm_hw_params_set_rate_resample", ret);
87+ ret = snd_output_close(log);
88+ SNDCHK("snd_output_close", ret);
9089
91− ret = snd_pcm_hw_params_set_access(pcm, hw, SND_PCM_ACCESS_RW_INTERLEAVED);
92− SNDCHK("snd_pcm_hw_params_set_access", ret);
93−
94− ret = snd_pcm_hw_params_set_format(pcm, hw, SND_PCM_FORMAT_S16_LE);
95− SNDCHK("snd_pcm_hw_params_set_format", ret);
96−
97− ret = snd_pcm_hw_params_set_rate_near(pcm, hw, &rate, 0);
98− SNDCHK("snd_pcm_hw_params_set_rate_near", ret);
99−
100− ret = snd_pcm_hw_params_set_channels(pcm, hw, channels);
101− SNDCHK("snd_pcm_hw_params_set_channels", ret);
102−
103− ret = snd_pcm_hw_params(pcm, hw);
104− SNDCHK("hw_params", ret);
105−
106− ret = snd_pcm_prepare(pcm);
107− SNDCHK("snd_pcm_prepare", ret);
108−
109− snd_pcm_sw_params_alloca(&sw);
90+ return 0;
91+}
11092
111− ret = snd_pcm_sw_params_current(pcm, sw);
112− SNDCHK("snd_pcm_sw_params_current", ret);
93+static int setup_alsa(snd_pcm_t *pcm, unsigned int rate, int verbosity, unsigned int target_latency_ms)
94+{
95+ int ret;
96+ int soft_resample = 1;
97+ unsigned int latency = target_latency_ms * 1000;
11398
114− // Avoid underruns
115− ret = snd_pcm_sw_params_set_start_threshold(pcm, sw, start_threshold);
116− SNDCHK("snd_pcm_sw_params_set_start_threshold", ret);
99+ ret = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED,
100+ CHANNELS, rate, soft_resample, latency);
101+ SNDCHK("snd_pcm_set_params", ret);
117102
118− ret = snd_pcm_sw_params(pcm, sw);
119− SNDCHK("snd_pcm_sw_params", ret);
103+ if (verbosity > 0) {
104+ return dump_alsa_info(pcm);
105+ }
120106
121107 return 0;
122108 }
123109
124−static int write_frames(snd_pcm_t *snd, void *pcm, int num_frames)
110+static int write_frames(snd_pcm_t *snd, unsigned char buf[MAX_SO_PACKETSIZE], int total_frames)
125111 {
112+ int i = 0;
126113 int ret;
127− snd_pcm_sframes_t f;
128−
129− f = snd_pcm_writei(snd, pcm, num_frames);
130− if (f < 0) {
131− ret = snd_pcm_recover(snd, f, 0);
132− SNDCHK("snd_pcm_recover", ret);
133− return 0;
134− }
135− if (f < num_frames) {
136− fprintf(stderr, "Short write %ld\n", f);
114+ snd_pcm_sframes_t written;
115+
116+ while (i < total_frames) {
117+ written = snd_pcm_writei(snd, &buf[i * BYTES_PER_SAMPLE * CHANNELS], total_frames - i);
118+ if (written < 0) {
119+ ret = snd_pcm_recover(snd, written, 0);
120+ SNDCHK("snd_pcm_recover", ret);
121+ return 0;
122+ } else if (written < total_frames - i) {
123+ fprintf(stderr, "Writing again after short write %ld < %d\n", written, total_frames - i);
124+ }
125+ i += written;
137126 }
138− return num_frames;
127+ return 0;
139128 }
140129
141130 int main(int argc, char *argv[])
@@ -148,17 +137,21 @@ int main(int argc, char *argv[]) · 6 unmodified lines
148137 unsigned char buf[MAX_SO_PACKETSIZE];
149138 in_addr_t interface = INADDR_ANY;
150139 int opt;
140+ int verbosity = 0;
151141 unsigned int rate = 44100;
152142 int samples;
153− int start_threshold = TYPICAL_SAMPLES_PER_PACKET * 8;
143+ int target_latency_ms = 50;
154144
155− while ((opt = getopt(argc, argv, "i:t:")) != -1) {
145+ while ((opt = getopt(argc, argv, "i:t:v")) != -1) {
156146 switch (opt) {
157147 case 'i':
158148 interface = get_interface(optarg);
159149 break;
160150 case 't':
161− start_threshold = atoi(optarg);
151+ target_latency_ms = atoi(optarg);
152+ break;
153+ case 'v':
154+ verbosity += 1;
162155 break;
163156 default:
164157 show_usage(argv[0]);
@@ -173,7 +166,7 @@ int main(int argc, char *argv[]) · 8 unmodified lines
173166 ret = snd_pcm_open(&snd, device, SND_PCM_STREAM_PLAYBACK, 0);
174167 SNDCHK("snd_pcm_open", ret);
175168
176− if (setup_alsa(snd, rate, CHANNELS, start_threshold) == -1) {
169+ if (setup_alsa(snd, rate, verbosity, target_latency_ms) == -1) {
177170 return -1;
178171 }
179172
@@ -194,6 +187,6 @@ int main(int argc, char *argv[]) · 14 unmodified lines
194187 for (;;) {
195188 n = recvfrom(sockfd, &buf, MAX_SO_PACKETSIZE, 0, NULL, 0);
196189 samples = n / (BYTES_PER_SAMPLE * CHANNELS);
197− write_frames(snd, &buf, samples);
190+ write_frames(snd, buf, samples);
198191 }
199192 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t