| 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 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | namespace QFFmpeg { |
| 11 | |
| 12 | static Q_LOGGING_CATEGORY(qLcFFmpegMuxer, "qt.multimedia.ffmpeg.muxer"); |
| 13 | |
| 14 | Muxer::Muxer(RecordingEngine *encoder) : m_encoder(encoder) |
| 15 | { |
| 16 | setObjectName(QLatin1String("Muxer")); |
| 17 | } |
| 18 | |
| 19 | void 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 | |
| 30 | AVPacketUPtr Muxer::takePacket() |
| 31 | { |
| 32 | QMutexLocker locker = lockLoopData(); |
| 33 | return dequeueIfPossible(queue&: m_packetQueue); |
| 34 | } |
| 35 | |
| 36 | bool Muxer::init() |
| 37 | { |
| 38 | qCDebug(qLcFFmpegMuxer) << "Muxer::init started thread."; |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | void Muxer::cleanup() |
| 43 | { |
| 44 | while (!m_packetQueue.empty()) |
| 45 | processOne(); |
| 46 | } |
| 47 | |
| 48 | bool QFFmpeg::Muxer::hasData() const |
| 49 | { |
| 50 | return !m_packetQueue.empty(); |
| 51 | } |
| 52 | |
| 53 | void 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 | |
| 65 | QT_END_NAMESPACE |
| 66 |
