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 | // |
7 | // W A R N I N G |
8 | // ------------- |
9 | // |
10 | // This file is not part of the Qt API. It exists purely as an |
11 | // implementation detail. This header file may change from version to |
12 | // version without notice, or even be removed. |
13 | // |
14 | // We mean it. |
15 | // |
16 | |
17 | #include <QtFFmpegMediaPluginImpl/private/qffmpeg_p.h> |
18 | #include <QtFFmpegMediaPluginImpl/private/qffmpegencoderthread_p.h> |
19 | #include <QtMultimedia/private/qplatformmediarecorder_p.h> |
20 | #include <qaudiobuffer.h> |
21 | #include <queue> |
22 | #include <chrono> |
23 | |
24 | QT_BEGIN_NAMESPACE |
25 | |
26 | class QMediaEncoderSettings; |
27 | |
28 | namespace QFFmpeg { |
29 | |
30 | class AudioEncoder : public EncoderThread |
31 | { |
32 | public: |
33 | AudioEncoder(RecordingEngine &recordingEngine, const QAudioFormat &sourceFormat, |
34 | const QMediaEncoderSettings &settings); |
35 | |
36 | void addBuffer(const QAudioBuffer &buffer); |
37 | |
38 | protected: |
39 | bool checkIfCanPushFrame() const override; |
40 | |
41 | private: |
42 | QAudioBuffer takeBuffer(); |
43 | void retrievePackets(); |
44 | bool updateResampler(const QAudioFormat &sourceFormat); |
45 | |
46 | bool init() override; |
47 | void cleanup() override; |
48 | bool hasData() const override; |
49 | void processOne() override; |
50 | |
51 | void handleAudioData(const uchar *data, int &samplesOffset, int samplesCount); |
52 | |
53 | void ensurePendingFrame(int availableSamplesCount); |
54 | |
55 | void writeDataToPendingFrame(const uchar *data, int &samplesOffset, int samplesCount); |
56 | |
57 | void sendPendingFrameToAVCodec(); |
58 | |
59 | private: |
60 | std::queue<QAudioBuffer> m_audioBufferQueue; |
61 | |
62 | // Arbitrarily chosen to limit audio queue duration |
63 | const std::chrono::microseconds m_maxQueueDuration = std::chrono::seconds(5); |
64 | |
65 | std::chrono::microseconds m_queueDuration{ 0 }; |
66 | |
67 | AVStream *m_stream = nullptr; |
68 | AVCodecContextUPtr m_codecContext; |
69 | QAudioFormat m_sourceFormat; |
70 | |
71 | SwrContextUPtr m_resampler; |
72 | qint64 m_samplesWritten = 0; |
73 | QMediaEncoderSettings m_settings; |
74 | |
75 | AVFrameUPtr m_avFrame; |
76 | int m_avFrameSamplesOffset = 0; |
77 | std::vector<uint8_t *> m_avFramePlanesData; |
78 | }; |
79 | |
80 | |
81 | } // namespace QFFmpeg |
82 | |
83 | QT_END_NAMESPACE |
84 | |
85 | #endif |
86 |