]> git.sesse.net Git - nageru/blob - audio_encoder.h
If a dead device comes back, put it into the right slot.
[nageru] / audio_encoder.h
1 // A class to encode audio (using ffmpeg) and send it to a Mux.
2
3 #ifndef _AUDIO_ENCODER_H
4 #define _AUDIO_ENCODER_H 1
5
6 #include <memory>
7 #include <string>
8 #include <vector>
9
10 extern "C" {
11 #include <libavcodec/avcodec.h>
12 #include <libavresample/avresample.h>
13 #include <libavutil/frame.h>
14 }
15
16 #include "ffmpeg_raii.h"
17 #include "mux.h"
18
19 class AudioEncoder {
20 public:
21         AudioEncoder(const std::string &codec_name, int bit_rate, const AVOutputFormat *oformat);
22         ~AudioEncoder();
23
24         void add_mux(Mux *mux) {  // Does not take ownership.
25                 muxes.push_back(mux);
26         }
27         void encode_audio(const std::vector<float> &audio, int64_t audio_pts);
28         void encode_last_audio();
29
30         AVCodecParametersWithDeleter get_codec_parameters();
31
32 private:
33         void encode_audio_one_frame(const float *audio, size_t num_samples, int64_t audio_pts);
34
35         std::vector<float> audio_queue;
36         int64_t last_pts = 0;  // The first pts after all audio we've encoded.
37
38         AVCodecContext *ctx;
39         AVAudioResampleContext *resampler;
40         AVFrame *audio_frame = nullptr;
41         std::vector<Mux *> muxes;
42 };
43
44 #endif  // !defined(_AUDIO_ENCODER_H)