]> git.sesse.net Git - nageru/blobdiff - audio_encoder.h
Move audio encoding from QuickSyncEncoder into its own class.
[nageru] / audio_encoder.h
diff --git a/audio_encoder.h b/audio_encoder.h
new file mode 100644 (file)
index 0000000..c49cf9f
--- /dev/null
@@ -0,0 +1,39 @@
+// A class to encode audio (using ffmpeg) and send it to a Mux.
+
+#ifndef _AUDIO_ENCODER_H
+#define _AUDIO_ENCODER_H 1
+
+#include <string>
+#include <vector>
+
+extern "C" {
+#include <libavcodec/avcodec.h>
+#include <libavresample/avresample.h>
+#include <libavutil/frame.h>
+}
+
+#include "mux.h"
+
+class AudioEncoder {
+public:
+       AudioEncoder(const std::string &codec_name, int bit_rate, const std::vector<Mux *> &muxes);
+       ~AudioEncoder();
+
+       void encode_audio(const std::vector<float> &audio, int64_t audio_pts);
+       void encode_last_audio();
+
+       const AVCodec *get_codec() { return ctx->codec; }
+
+private:
+       void encode_audio_one_frame(const float *audio, size_t num_samples, int64_t audio_pts);
+
+       std::vector<float> audio_queue;
+       int64_t last_pts = 0;  // The first pts after all audio we've encoded.
+
+       AVCodecContext *ctx;
+       AVAudioResampleContext *resampler;
+       AVFrame *audio_frame = nullptr;
+       std::vector<Mux *> muxes;
+};
+
+#endif  // !defined(_AUDIO_ENCODER_H)