]> git.sesse.net Git - nageru/blob - audio_encoder.h
Remove some use of the AVStream::codec parameter (not all). Fixes some deprecation...
[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 "mux.h"
17
18 static inline void avcodec_parameters_free_unique(AVCodecParameters *codec_par)
19 {
20         avcodec_parameters_free(&codec_par);
21 }
22
23 typedef std::unique_ptr<AVCodecParameters, decltype(avcodec_parameters_free_unique)*>
24 AVCodecParametersWithDeleter;
25
26 class AudioEncoder {
27 public:
28         AudioEncoder(const std::string &codec_name, int bit_rate, const AVOutputFormat *oformat);
29         ~AudioEncoder();
30
31         void add_mux(Mux *mux) {  // Does not take ownership.
32                 muxes.push_back(mux);
33         }
34         void encode_audio(const std::vector<float> &audio, int64_t audio_pts);
35         void encode_last_audio();
36
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         AVAudioResampleContext *resampler;
47         AVFrame *audio_frame = nullptr;
48         std::vector<Mux *> muxes;
49 };
50
51 #endif  // !defined(_AUDIO_ENCODER_H)