1// Copyright (C) 2024 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3#ifndef QFFMPEGAUDIOENCODER_P_H
4#define QFFMPEGAUDIOENCODER_P_H
5
6#include "qffmpeg_p.h"
7#include "qffmpegencoderthread_p.h"
8#include "private/qplatformmediarecorder_p.h"
9#include <qaudiobuffer.h>
10#include <queue>
11#include <chrono>
12
13QT_BEGIN_NAMESPACE
14
15class QMediaEncoderSettings;
16
17namespace QFFmpeg {
18
19class AudioEncoder : public EncoderThread
20{
21public:
22 AudioEncoder(RecordingEngine &recordingEngine, const QAudioFormat &sourceFormat,
23 const QMediaEncoderSettings &settings);
24
25 void addBuffer(const QAudioBuffer &buffer);
26
27protected:
28 bool checkIfCanPushFrame() const override;
29
30private:
31 QAudioBuffer takeBuffer();
32 void retrievePackets();
33 bool updateResampler(const QAudioFormat &sourceFormat);
34
35 bool init() override;
36 void cleanup() override;
37 bool hasData() const override;
38 void processOne() override;
39
40 void handleAudioData(const uchar *data, int &samplesOffset, int samplesCount);
41
42 void ensurePendingFrame(int availableSamplesCount);
43
44 void writeDataToPendingFrame(const uchar *data, int &samplesOffset, int samplesCount);
45
46 void sendPendingFrameToAVCodec();
47
48private:
49 std::queue<QAudioBuffer> m_audioBufferQueue;
50
51 // Arbitrarily chosen to limit audio queue duration
52 const std::chrono::microseconds m_maxQueueDuration = std::chrono::seconds(5);
53
54 std::chrono::microseconds m_queueDuration{ 0 };
55
56 AVStream *m_stream = nullptr;
57 AVCodecContextUPtr m_codecContext;
58 QAudioFormat m_sourceFormat;
59
60 SwrContextUPtr m_resampler;
61 qint64 m_samplesWritten = 0;
62 QMediaEncoderSettings m_settings;
63
64 AVFrameUPtr m_avFrame;
65 int m_avFrameSamplesOffset = 0;
66 std::vector<uint8_t *> m_avFramePlanesData;
67};
68
69
70} // namespace QFFmpeg
71
72QT_END_NAMESPACE
73
74#endif
75

source code of qtmultimedia/src/plugins/multimedia/ffmpeg/recordingengine/qffmpegaudioencoder_p.h