Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qffmpegmuxer.cpp
Go to the documentation of this file.
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
6#include <QtCore/qloggingcategory.h>
7
9
10namespace QFFmpeg {
11
12Q_STATIC_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(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(m_packetQueue);
34}
35
36bool Muxer::init()
37{
38 qCDebug(qLcFFmpegMuxer) << "Muxer::init started thread.";
39 return true;
40}
41
43{
44 while (!m_packetQueue.empty())
46
47 av_interleaved_write_frame(m_encoder->avFormatContext(), nullptr);
48}
49
50bool QFFmpeg::Muxer::hasData() const
51{
52 return !m_packetQueue.empty();
53}
54
56{
57 auto packet = takePacket();
58 // qCDebug(qLcFFmpegEncoder) << "writing packet to file" << packet->pts << packet->duration <<
59 // packet->stream_index;
60
61 // from AVPacket Struct Reference:
62 // "The semantics of data ownership depends on the buf field. If it is set, the packet data is
63 // dynamically allocated and is valid indefinitely until a call to av_packet_unref() reduces the
64 // reference count to 0."
65 //
66 // from av_interleaved_write_frame:
67 // "If the packet is reference-counted, this function will take ownership of this reference and
68 // unreference it later when it sees fit. The caller must not access the data through this
69 // reference after this function returns. If the packet is not reference-counted, libavformat
70 // will make a copy."
71 //
72 // This means that av_interleaved_write_frame takes ownership of the AVBufferRef field, not the
73 // whole AVPacket.
74
75 av_interleaved_write_frame(m_encoder->avFormatContext(), packet.get());
76 Q_ASSERT(packet->buf == nullptr); // av_interleaved_write_frame took ownership of the buffer
77}
78
79} // namespace QFFmpeg
80
81QT_END_NAMESPACE
bool init() override
Called on this thread when thread starts.
void cleanup() override
Called on this thread before thread exits.
Muxer(RecordingEngine *encoder)
void processOne() override
Process one work item.
void addPacket(AVPacketUPtr packet)
bool hasData() const override
Must return true when data is available for processing.
Q_STATIC_LOGGING_CATEGORY(qLcEncodingFormatContext, "qt.multimedia.ffmpeg.encodingformatcontext")
std::conditional_t< QT_FFMPEG_AVIO_WRITE_CONST, const uint8_t *, uint8_t * > AvioWriteBufferType