]> git.sesse.net Git - nageru/blob - alsa_input.h
Move ALSAPool into its own file; it is pretty large now.
[nageru] / alsa_input.h
1 #ifndef _ALSA_INPUT_H
2 #define _ALSA_INPUT_H 1
3
4 // ALSA sound input, running in a separate thread and sending audio back
5 // in callbacks.
6 //
7 // Note: “frame” here generally refers to the ALSA definition of frame,
8 // which is a set of samples, exactly one for each channel. The only exception
9 // is in frame_length, where it means the TIMEBASE length of the buffer
10 // as a whole, since that's what AudioMixer::add_audio() wants.
11
12 #include <alsa/asoundlib.h>
13 #include <alsa/pcm.h>
14 #include <stdint.h>
15 #include <sys/types.h>
16 #include <atomic>
17 #include <functional>
18 #include <string>
19 #include <thread>
20 #include <unordered_map>
21 #include <vector>
22
23 #include "bmusb/bmusb.h"
24 #include "timebase.h"
25
26 class ALSAPool;
27 class DeviceSpecProto;
28
29 class ALSAInput {
30 public:
31         typedef std::function<bool(const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, int64_t frame_length)> audio_callback_t;
32
33         ALSAInput(const char *device, unsigned sample_rate, unsigned num_channels, audio_callback_t audio_callback, ALSAPool *parent_pool, unsigned internal_dev_index);
34         ~ALSAInput();
35
36         // If not called before start_capture_thread(), the capture thread
37         // will call it until it succeeds.
38         bool open_device();
39
40         // Not valid before the device has been successfully opened.
41         // NOTE: Might very well be different from the sample rate given to the
42         // constructor, since the card might not support the one you wanted.
43         unsigned get_sample_rate() const { return sample_rate; }
44
45         void start_capture_thread();
46         void stop_capture_thread();
47
48         // Set access, sample rate and format parameters on the given ALSA PCM handle.
49         // Returns the computed parameter set and the chosen sample rate. Note that
50         // sample_rate is an in/out parameter; you send in the desired rate,
51         // and ALSA picks one as close to that as possible.
52         static bool set_base_params(const char *device_name, snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hw_params, unsigned *sample_rate);
53
54 private:
55         void capture_thread_func();
56         int64_t frames_to_pts(uint64_t n) const;
57
58         enum class CaptureEndReason {
59                 REQUESTED_QUIT,
60                 DEVICE_GONE,
61                 OTHER_ERROR
62         };
63         CaptureEndReason do_capture();
64
65         std::string device;
66         unsigned sample_rate, num_channels, num_periods;
67         snd_pcm_uframes_t period_size;
68         snd_pcm_uframes_t buffer_frames;
69         bmusb::AudioFormat audio_format;
70         audio_callback_t audio_callback;
71
72         snd_pcm_t *pcm_handle = nullptr;
73         std::thread capture_thread;
74         std::atomic<bool> should_quit{false};
75         std::unique_ptr<uint8_t[]> buffer;
76         ALSAPool *parent_pool;
77         unsigned internal_dev_index;
78 };
79
80 #endif  // !defined(_ALSA_INPUT_H)