2 #include "alsa_input.h"
6 ALSAInput::ALSAInput(const char *device, unsigned sample_rate, unsigned num_channels, audio_callback_t audio_callback)
7 : device(device), sample_rate(sample_rate), num_channels(num_channels), audio_callback(audio_callback)
9 die_on_error(device, snd_pcm_open(&pcm_handle,device, SND_PCM_STREAM_CAPTURE, 0));
12 snd_pcm_hw_params_t *hw_params;
13 snd_pcm_hw_params_alloca(&hw_params);
14 die_on_error("snd_pcm_hw_params_any()", snd_pcm_hw_params_any(pcm_handle, hw_params));
15 die_on_error("snd_pcm_hw_params_set_access()", snd_pcm_hw_params_set_access(pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED));
16 snd_pcm_format_mask_t *format_mask;
17 snd_pcm_format_mask_alloca(&format_mask);
18 snd_pcm_format_mask_set(format_mask, SND_PCM_FORMAT_S16_LE);
19 snd_pcm_format_mask_set(format_mask, SND_PCM_FORMAT_S24_LE);
20 snd_pcm_format_mask_set(format_mask, SND_PCM_FORMAT_S32_LE);
21 die_on_error("snd_pcm_hw_params_set_format()", snd_pcm_hw_params_set_format_mask(pcm_handle, hw_params, format_mask));
22 die_on_error("snd_pcm_hw_params_set_rate_near()", snd_pcm_hw_params_set_rate_near(pcm_handle, hw_params, &sample_rate, 0));
23 die_on_error("snd_pcm_hw_params_set_channels()", snd_pcm_hw_params_set_channels(pcm_handle, hw_params, num_channels));
25 die_on_error("snd_pcm_hw_params_set_channels()", snd_pcm_hw_params_set_channels(pcm_handle, hw_params, num_channels));
27 // Fragment size of 64 samples (about 1 ms at 48 kHz; a frame at 60
28 // fps/48 kHz is 800 samples.) We ask for 64 such periods in our buffer
29 // (~85 ms buffer); more than that, and our jitter is probably so high
30 // that the resampling queue can't keep up anyway.
31 // The entire thing with periods and such is a bit mysterious to me;
32 // seemingly I can get 96 frames at a time with no problems even if
33 // the period size is 64 frames. And if I set num_periods to e.g. 1,
34 // I can't have a big buffer.
37 die_on_error("snd_pcm_hw_params_set_periods_near()", snd_pcm_hw_params_set_periods_near(pcm_handle, hw_params, &num_periods, &dir));
40 die_on_error("snd_pcm_hw_params_set_period_size_near()", snd_pcm_hw_params_set_period_size_near(pcm_handle, hw_params, &period_size, &dir));
41 buffer_frames = 64 * 64;
42 die_on_error("snd_pcm_hw_params_set_buffer_size_near()", snd_pcm_hw_params_set_buffer_size_near(pcm_handle, hw_params, &buffer_frames));
43 die_on_error("snd_pcm_hw_params()", snd_pcm_hw_params(pcm_handle, hw_params));
44 //snd_pcm_hw_params_free(hw_params);
46 // Figure out which format the card actually chose.
47 die_on_error("snd_pcm_hw_params_current()", snd_pcm_hw_params_current(pcm_handle, hw_params));
48 snd_pcm_format_t chosen_format;
49 die_on_error("snd_pcm_hw_params_get_format()", snd_pcm_hw_params_get_format(hw_params, &chosen_format));
51 audio_format.num_channels = num_channels;
52 audio_format.bits_per_sample = 0;
53 switch (chosen_format) {
54 case SND_PCM_FORMAT_S16_LE:
55 audio_format.bits_per_sample = 16;
57 case SND_PCM_FORMAT_S24_LE:
58 audio_format.bits_per_sample = 24;
60 case SND_PCM_FORMAT_S32_LE:
61 audio_format.bits_per_sample = 32;
66 //printf("num_periods=%u period_size=%u buffer_frames=%u sample_rate=%u bits_per_sample=%d\n",
67 // num_periods, unsigned(period_size), unsigned(buffer_frames), sample_rate, audio_format.bits_per_sample);
69 buffer.reset(new uint8_t[buffer_frames * num_channels * audio_format.bits_per_sample / 8]);
71 snd_pcm_sw_params_t *sw_params;
72 snd_pcm_sw_params_alloca(&sw_params);
73 die_on_error("snd_pcm_sw_params_current()", snd_pcm_sw_params_current(pcm_handle, sw_params));
74 die_on_error("snd_pcm_sw_params_set_start_threshold", snd_pcm_sw_params_set_start_threshold(pcm_handle, sw_params, num_periods * period_size / 2));
75 die_on_error("snd_pcm_sw_params()", snd_pcm_sw_params(pcm_handle, sw_params));
77 die_on_error("snd_pcm_nonblock()", snd_pcm_nonblock(pcm_handle, 1));
78 die_on_error("snd_pcm_prepare()", snd_pcm_prepare(pcm_handle));
81 ALSAInput::~ALSAInput()
83 die_on_error("snd_pcm_close()", snd_pcm_close(pcm_handle));
86 void ALSAInput::start_capture_thread()
89 capture_thread = thread(&ALSAInput::capture_thread_func, this);
92 void ALSAInput::stop_capture_thread()
95 capture_thread.join();
98 void ALSAInput::capture_thread_func()
100 die_on_error("snd_pcm_start()", snd_pcm_start(pcm_handle));
101 uint64_t num_frames_output = 0;
102 while (!should_quit) {
103 int ret = snd_pcm_wait(pcm_handle, /*timeout=*/100);
104 if (ret == 0) continue; // Timeout.
106 fprintf(stderr, "[%s] ALSA overrun\n", device.c_str());
107 snd_pcm_prepare(pcm_handle);
108 snd_pcm_start(pcm_handle);
111 die_on_error("snd_pcm_wait()", ret);
113 snd_pcm_sframes_t frames = snd_pcm_readi(pcm_handle, buffer.get(), buffer_frames);
114 if (frames == -EPIPE) {
115 fprintf(stderr, "[%s] ALSA overrun\n", device.c_str());
116 snd_pcm_prepare(pcm_handle);
117 snd_pcm_start(pcm_handle);
121 fprintf(stderr, "snd_pcm_readi() returned 0\n");
124 die_on_error("snd_pcm_readi()", frames);
126 const int64_t prev_pts = frames_to_pts(num_frames_output);
127 const int64_t pts = frames_to_pts(num_frames_output + frames);
128 audio_callback(buffer.get(), frames, audio_format, pts - prev_pts);
129 num_frames_output += frames;
133 int64_t ALSAInput::frames_to_pts(uint64_t n) const
135 return (n * TIMEBASE) / sample_rate;
138 void ALSAInput::die_on_error(const char *func_name, int err)
141 fprintf(stderr, "[%s] %s: %s\n", device.c_str(), func_name, snd_strerror(err));
146 vector<ALSAInput::Device> ALSAInput::enumerate_devices()
150 // Enumerate all cards.
151 for (int card_index = -1; snd_card_next(&card_index) == 0 && card_index >= 0; ) {
153 snprintf(address, sizeof(address), "hw:%d", card_index);
156 int err = snd_ctl_open(&ctl, address, 0);
158 printf("%s: %s\n", address, snd_strerror(err));
161 snd_ctl_card_info_t *card_info;
162 snd_ctl_card_info_alloca(&card_info);
163 snd_ctl_card_info(ctl, card_info);
165 string card_name = snd_ctl_card_info_get_name(card_info);
167 // Enumerate all devices on this card.
168 for (int dev_index = -1; snd_ctl_pcm_next_device(ctl, &dev_index) == 0 && dev_index >= 0; ) {
169 snd_pcm_info_t *pcm_info;
170 snd_pcm_info_alloca(&pcm_info);
171 snd_pcm_info_set_device(pcm_info, dev_index);
172 snd_pcm_info_set_subdevice(pcm_info, 0);
173 snd_pcm_info_set_stream(pcm_info, SND_PCM_STREAM_CAPTURE);
174 if (snd_ctl_pcm_info(ctl, pcm_info) < 0) {
175 // Not available for capture.
179 snprintf(address, sizeof(address), "hw:%d,%d", card_index, dev_index);
181 // Find all channel maps for this device, and pick out the one
182 // with the most channels.
183 snd_pcm_chmap_query_t **cmaps = snd_pcm_query_chmaps_from_hw(card_index, dev_index, 0, SND_PCM_STREAM_CAPTURE);
184 unsigned num_channels = 0;
185 for (snd_pcm_chmap_query_t **ptr = cmaps; *ptr; ++ptr) {
186 num_channels = max(num_channels, (*ptr)->map.channels);
189 snd_pcm_free_chmaps(cmaps);
191 if (num_channels == 0) {
192 printf("%s: No channel maps with channels\n", address);
197 dev.address = address;
198 dev.name = card_name;
199 dev.info = snd_pcm_info_get_name(pcm_info);
200 dev.num_channels = num_channels;