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#include "qffmpegmuxer_p.h"
4#include "qffmpegrecordingengine_p.h"
5#include "qffmpegrecordingengineutils_p.h"
6#include <QtCore/qloggingcategory.h>
7
8QT_BEGIN_NAMESPACE
9
10namespace QFFmpeg {
11
12static Q_LOGGING_CATEGORY(qLcFFmpegMuxer, "qt.multimedia.ffmpeg.muxer");
13
14Muxer::Muxer(RecordingEngine *encoder) : m_encoder(encoder)
15{
16 setObjectName(QLatin1String("Muxer"));
17}
18
19void Muxer::addPacket(AVPacketUPtr packet)
20{
21 {
22 QMutexLocker locker = lockLoopData();
23 m_packetQueue.push(x: std::move(packet));
24 }
25
26 // qCDebug(qLcFFmpegEncoder) << "Muxer::addPacket" << packet->pts << packet->stream_index;
27 dataReady();
28}
29
30AVPacketUPtr Muxer::takePacket()
31{
32 QMutexLocker locker = lockLoopData();
33 return dequeueIfPossible(queue&: m_packetQueue);
34}
35
36bool Muxer::init()
37{
38 qCDebug(qLcFFmpegMuxer) << "Muxer::init started thread.";
39 return true;
40}
41
42void Muxer::cleanup()
43{
44 while (!m_packetQueue.empty())
45 processOne();
46}
47
48bool QFFmpeg::Muxer::hasData() const
49{
50 return !m_packetQueue.empty();
51}
52
53void Muxer::processOne()
54{
55 auto packet = takePacket();
56 // qCDebug(qLcFFmpegEncoder) << "writing packet to file" << packet->pts << packet->duration <<
57 // packet->stream_index;
58
59 // the function takes ownership for the packet
60 av_interleaved_write_frame(s: m_encoder->avFormatContext(), pkt: packet.release());
61}
62
63} // namespace QFFmpeg
64
65QT_END_NAMESPACE
66

source code of qtmultimedia/src/plugins/multimedia/ffmpeg/recordingengine/qffmpegmuxer.cpp