7#include "pipewirerecord.h"
10#include "pipewirerecord_p.h"
11#include <logging_record.h>
13#include <QGuiApplication>
16#include <qpa/qplatformnativeinterface.h>
22#include <libavcodec/avcodec.h>
23#include <libavformat/avformat.h>
24#include <libavutil/timestamp.h>
31char buf[AV_TS_MAX_STRING_SIZE];
32#define av_ts2str(ts) av_ts_make_string(buf, ts)
37char timebuf[AV_TS_MAX_STRING_SIZE];
38#define av_ts2timestr(ts, tb) av_ts_make_time_string(timebuf, ts, tb)
41static void log_packet(
const AVFormatContext *fmt_ctx,
const AVPacket *pkt)
43 AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
45 qCDebug(PIPEWIRERECORD_LOGGING,
46 "pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s "
49 av_ts2timestr(pkt->pts, time_base),
51 av_ts2timestr(pkt->dts, time_base),
52 av_ts2str(pkt->duration),
53 av_ts2timestr(pkt->duration, time_base),
57PipeWireRecord::PipeWireRecord(
QObject *parent)
58 : PipeWireBaseEncodedStream(parent)
59 , d(new PipeWireRecordPrivate)
63PipeWireRecord::~PipeWireRecord() =
default;
65void PipeWireRecord::setOutput(
const QString &_output)
69 if (d->m_output == output)
74 Q_EMIT outputChanged(output);
77QString PipeWireRecord::output()
const
82QString PipeWireRecord::extension()
const
85 {PipeWireBaseEncodedStream::H264Main, QStringLiteral(
"mp4")},
86 {PipeWireBaseEncodedStream::H264Baseline, QStringLiteral(
"mp4")},
87 {PipeWireBaseEncodedStream::VP8, QStringLiteral(
"webm")},
88 {PipeWireBaseEncodedStream::VP9, QStringLiteral(
"webm")},
90 return s_extensions.
value(encoder());
93PipeWireRecordProduce::PipeWireRecordProduce(PipeWireBaseEncodedStream::Encoder encoder, uint nodeId, uint fd,
const Fraction &framerate,
const QString &output)
94 : PipeWireProduce(encoder, nodeId, fd, framerate)
99bool PipeWireRecordProduce::setupFormat()
101 avformat_alloc_output_context2(&m_avFormatContext,
nullptr,
nullptr, m_output.toUtf8().constData());
102 if (!m_avFormatContext) {
103 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Could not deduce output format from file: using WebM." << m_output;
104 avformat_alloc_output_context2(&m_avFormatContext,
nullptr,
"webm", m_output.toUtf8().constData());
106 if (!m_avFormatContext) {
107 qCDebug(PIPEWIRERECORD_LOGGING) <<
"could not set stream up";
111 const Fraction framerate = m_stream->framerate();
114 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Could not open" << m_output << av_err2str(ret);
118 auto avStream = avformat_new_stream(m_avFormatContext,
nullptr);
119 avStream->start_time = 0;
121 avStream->r_frame_rate.num = framerate.numerator;
122 avStream->r_frame_rate.den = framerate.denominator;
123 avStream->avg_frame_rate.num = framerate.numerator;
124 avStream->avg_frame_rate.den = framerate.denominator;
127 ret = avcodec_parameters_from_context(avStream->codecpar, m_encoder->avCodecContext());
129 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Error occurred when passing the codec:" << av_err2str(ret);
133 ret = avformat_write_header(m_avFormatContext,
nullptr);
135 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Error occurred when writing header:" << av_err2str(ret);
142void PipeWireRecordProduce::processFrame(
const PipeWireFrame &frame)
144 PipeWireProduce::processFrame(frame);
145 if (frame.cursor && !frame.dmabuf && !frame.dataFrame && m_frameWithoutMetadataCursor.dataFrame) {
146 m_encoder->filterFrame(m_frameWithoutMetadataCursor);
150void PipeWireRecordProduce::aboutToEncode(PipeWireFrame &frame)
152 if (!frame.dataFrame) {
156 if (m_cursor.position && !m_cursor.texture.isNull()) {
157 auto image = frame.dataFrame->toImage();
159 if (m_frameWithoutMetadataCursor.dataFrame->cleanup != frame.dataFrame->cleanup) {
160 m_frameWithoutMetadataCursor.dataFrame = frame.dataFrame->copy();
163 p.drawImage(*m_cursor.position, m_cursor.texture);
167void PipeWireRecordProduce::processPacket(AVPacket *packet)
169 packet->stream_index = (*m_avFormatContext->streams)->index;
170 av_packet_rescale_ts(packet, m_encoder->avCodecContext()->time_base, (*m_avFormatContext->streams)->time_base);
171 log_packet(m_avFormatContext, packet);
172 auto ret = av_interleaved_write_frame(m_avFormatContext, packet);
174 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Error while writing output packet:" << av_err2str(ret);
178std::unique_ptr<PipeWireProduce> PipeWireRecord::makeProduce()
180 return std::make_unique<PipeWireRecordProduce>(encoder(), nodeId(), fd(), maxFramerate(), d->m_output);
183int64_t PipeWireRecordProduce::framePts(
const std::optional<std::chrono::nanoseconds> &presentationTimestamp)
185 const auto current = std::chrono::duration_cast<std::chrono::milliseconds>(*presentationTimestamp).count();
186 if ((*m_avFormatContext->streams)->start_time == 0) {
187 (*m_avFormatContext->streams)->start_time = current;
190 return current - (*m_avFormatContext->streams)->start_time;
193void PipeWireRecordProduce::cleanup()
195 if (m_avFormatContext) {
196 if (
auto result = av_write_trailer(m_avFormatContext); result < 0) {
197 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Could not write trailer";
200 avio_closep(&m_avFormatContext->pb);
201 avformat_free_context(m_avFormatContext);
205#include "moc_pipewirerecord.cpp"
207#include "moc_pipewirerecord_p.cpp"
KCOREADDONS_EXPORT QString tildeExpand(const QString &path)
const char * constData() const const
QByteArray encodeName(const QString &fileName)
T value(const Key &key) const const