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