]> git.sesse.net Git - nageru/blob - audio_encoder.h
Send the AVCodecContext for audio to the stream mux instead of constructing a fake...
[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 <string>
7 #include <vector>
8
9 extern "C" {
10 #include <libavcodec/avcodec.h>
11 #include <libavresample/avresample.h>
12 #include <libavutil/frame.h>
13 }
14
15 #include "mux.h"
16
17 class AudioEncoder {
18 public:
19         AudioEncoder(const std::string &codec_name, int bit_rate);
20         ~AudioEncoder();
21
22         void add_mux(Mux *mux) {  // Does not take ownership.
23                 muxes.push_back(mux);
24         }
25         void encode_audio(const std::vector<float> &audio, int64_t audio_pts);
26         void encode_last_audio();
27
28         const AVCodecContext *get_ctx() { return ctx; }
29
30 private:
31         void encode_audio_one_frame(const float *audio, size_t num_samples, int64_t audio_pts);
32
33         std::vector<float> audio_queue;
34         int64_t last_pts = 0;  // The first pts after all audio we've encoded.
35
36         AVCodecContext *ctx;
37         AVAudioResampleContext *resampler;
38         AVFrame *audio_frame = nullptr;
39         std::vector<Mux *> muxes;
40 };
41
42 #endif  // !defined(_AUDIO_ENCODER_H)