]> git.sesse.net Git - nageru/blob - nageru/audio_encoder.h
IWYU-fix nageru/*.h.
[nageru] / 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 <assert.h>
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
11
12 extern "C" {
13 #include <libavcodec/codec.h>
14 #include <libavcodec/avcodec.h>
15 #include <libavformat/avformat.h>
16 #include <libswresample/swresample.h>
17 #include <libavutil/frame.h>
18 }
19
20 #include "shared/ffmpeg_raii.h"
21
22 class Mux;
23
24 class AudioEncoder {
25 public:
26         AudioEncoder(const std::string &codec_name, int bit_rate, const AVOutputFormat *oformat);
27         ~AudioEncoder();
28
29         void add_mux(Mux *mux) {  // Does not take ownership.
30                 assert(mux != nullptr);
31                 muxes.push_back(mux);
32         }
33         void encode_audio(const std::vector<float> &audio, int64_t audio_pts);
34         void encode_last_audio();
35
36         const AVCodec *get_codec() const { return ctx->codec; }
37         AVCodecParametersWithDeleter get_codec_parameters();
38
39 private:
40         void encode_audio_one_frame(const float *audio, size_t num_samples, int64_t audio_pts);
41
42         std::vector<float> audio_queue;
43         int64_t last_pts = 0;  // The first pts after all audio we've encoded.
44
45         AVCodecContext *ctx;
46         SwrContext *resampler;
47         AVFrame *audio_frame = nullptr;
48         std::vector<Mux *> muxes;
49 };
50
51 #endif  // !defined(_AUDIO_ENCODER_H)