]> git.sesse.net Git - nageru/blob - alsa_input.h
Support audio-only FFmpeg inputs. Somewhat wonky, though.
[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 <stdint.h>
14 #include <sys/types.h>
15 #include <atomic>
16 #include <chrono>
17 #include <functional>
18 #include <memory>
19 #include <string>
20 #include <thread>
21
22 #include "bmusb/bmusb.h"
23 #include "quittable_sleeper.h"
24
25 class ALSAPool;
26
27 class ALSAInput {
28 public:
29         typedef std::function<bool(const uint8_t *data, unsigned num_samples, bmusb::AudioFormat audio_format, int64_t frame_length, std::chrono::steady_clock::time_point ts)> audio_callback_t;
30
31         ALSAInput(const char *device, unsigned sample_rate, unsigned num_channels, audio_callback_t audio_callback, ALSAPool *parent_pool, unsigned internal_dev_index);
32         ~ALSAInput();
33
34         // If not called before start_capture_thread(), the capture thread
35         // will call it until it succeeds.
36         bool open_device();
37
38         // Not valid before the device has been successfully opened.
39         // NOTE: Might very well be different from the sample rate given to the
40         // constructor, since the card might not support the one you wanted.
41         unsigned get_sample_rate() const { return sample_rate; }
42
43         void start_capture_thread();
44         void stop_capture_thread();
45
46         // Set access, sample rate and format parameters on the given ALSA PCM handle.
47         // Returns the computed parameter set and the chosen sample rate. Note that
48         // sample_rate is an in/out parameter; you send in the desired rate,
49         // and ALSA picks one as close to that as possible.
50         static bool set_base_params(const char *device_name, snd_pcm_t *pcm_handle, snd_pcm_hw_params_t *hw_params, unsigned *sample_rate);
51
52 private:
53         void capture_thread_func();
54         int64_t frames_to_pts(uint64_t n) const;
55
56         enum class CaptureEndReason {
57                 REQUESTED_QUIT,
58                 DEVICE_GONE,
59                 OTHER_ERROR
60         };
61         CaptureEndReason do_capture();
62
63         std::string device;
64         unsigned sample_rate, num_channels, num_periods;
65         snd_pcm_uframes_t period_size;
66         snd_pcm_uframes_t buffer_frames;
67         bmusb::AudioFormat audio_format;
68         audio_callback_t audio_callback;
69
70         snd_pcm_t *pcm_handle = nullptr;
71         std::thread capture_thread;
72         QuittableSleeper should_quit;
73         std::unique_ptr<uint8_t[]> buffer;
74         ALSAPool *parent_pool;
75         unsigned internal_dev_index;
76 };
77
78 #endif  // !defined(_ALSA_INPUT_H)