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));
82 ALSAInput::~ALSAInput()
84 die_on_error("snd_pcm_close()", snd_pcm_close(pcm_handle));
87 void ALSAInput::start_capture_thread()
90 capture_thread = thread(&ALSAInput::capture_thread_func, this);
93 void ALSAInput::stop_capture_thread()
96 capture_thread.join();
99 void ALSAInput::capture_thread_func()
101 die_on_error("snd_pcm_start()", snd_pcm_start(pcm_handle));
102 uint64_t num_frames_output = 0;
103 while (!should_quit) {
104 int ret = snd_pcm_wait(pcm_handle, /*timeout=*/100);
105 if (ret == 0) continue; // Timeout.
107 fprintf(stderr, "[%s] ALSA overrun\n", device.c_str());
108 snd_pcm_prepare(pcm_handle);
109 snd_pcm_start(pcm_handle);
112 die_on_error("snd_pcm_wait()", ret);
114 snd_pcm_sframes_t frames = snd_pcm_readi(pcm_handle, buffer.get(), buffer_frames);
115 if (frames == -EPIPE) {
116 fprintf(stderr, "[%s] ALSA overrun\n", device.c_str());
117 snd_pcm_prepare(pcm_handle);
118 snd_pcm_start(pcm_handle);
122 fprintf(stderr, "snd_pcm_readi() returned 0\n");
125 die_on_error("snd_pcm_readi()", frames);
127 const int64_t prev_pts = frames_to_pts(num_frames_output);
128 const int64_t pts = frames_to_pts(num_frames_output + frames);
129 audio_callback(buffer.get(), frames, audio_format, pts - prev_pts);
130 num_frames_output += frames;
134 int64_t ALSAInput::frames_to_pts(uint64_t n) const
136 return (n * TIMEBASE) / sample_rate;
139 void ALSAInput::die_on_error(const char *func_name, int err)
142 fprintf(stderr, "[%s] %s: %s\n", device.c_str(), func_name, snd_strerror(err));