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")},
89 {PipeWireBaseEncodedStream::WebP, QStringLiteral(
"webp")},
90 {PipeWireBaseEncodedStream::Gif, QStringLiteral(
"gif")},
92 return s_extensions.
value(encoder());
95PipeWireRecordProduce::PipeWireRecordProduce(PipeWireBaseEncodedStream::Encoder encoder, uint nodeId, uint fd,
const Fraction &framerate,
const QString &output)
96 : PipeWireProduce(encoder, nodeId, fd, framerate)
101bool PipeWireRecordProduce::setupFormat()
103 avformat_alloc_output_context2(&m_avFormatContext,
nullptr,
nullptr, m_output.toUtf8().constData());
104 if (!m_avFormatContext) {
105 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Could not deduce output format from file: using WebM." << m_output;
106 avformat_alloc_output_context2(&m_avFormatContext,
nullptr,
"webm", m_output.toUtf8().constData());
108 if (!m_avFormatContext) {
109 qCDebug(PIPEWIRERECORD_LOGGING) <<
"could not set stream up";
113 const Fraction framerate = m_stream->framerate();
116 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Could not open" << m_output << av_err2str(ret);
120 auto avStream = avformat_new_stream(m_avFormatContext,
nullptr);
121 avStream->start_time = 0;
123 avStream->r_frame_rate.num = framerate.numerator;
124 avStream->r_frame_rate.den = framerate.denominator;
125 avStream->avg_frame_rate.num = framerate.numerator;
126 avStream->avg_frame_rate.den = framerate.denominator;
129 ret = avcodec_parameters_from_context(avStream->codecpar, m_encoder->avCodecContext());
131 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Error occurred when passing the codec:" << av_err2str(ret);
135 AVDictionary *options =
nullptr;
136 const auto codecId = m_avFormatContext->oformat->video_codec;
137 if (codecId == AV_CODEC_ID_GIF || codecId == AV_CODEC_ID_WEBP) {
138 av_dict_set_int(&options,
"loop", 0, 0);
140 ret = avformat_write_header(m_avFormatContext, &options);
142 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Error occurred when writing header:" << av_err2str(ret);
149void PipeWireRecordProduce::processFrame(
const PipeWireFrame &frame)
151 PipeWireProduce::processFrame(frame);
152 if (frame.cursor && !frame.dmabuf && !frame.dataFrame && m_frameWithoutMetadataCursor.dataFrame) {
153 m_encoder->filterFrame(m_frameWithoutMetadataCursor);
157void PipeWireRecordProduce::aboutToEncode(PipeWireFrame &frame)
159 if (!frame.dataFrame) {
163 if (m_cursor.position && !m_cursor.texture.isNull()) {
164 auto image = frame.dataFrame->toImage();
166 if (m_frameWithoutMetadataCursor.dataFrame->cleanup != frame.dataFrame->cleanup) {
167 m_frameWithoutMetadataCursor.dataFrame = frame.dataFrame->copy();
170 p.drawImage(*m_cursor.position, m_cursor.texture);
174void PipeWireRecordProduce::processPacket(AVPacket *packet)
176 packet->stream_index = (*m_avFormatContext->streams)->index;
177 av_packet_rescale_ts(packet, m_encoder->avCodecContext()->time_base, (*m_avFormatContext->streams)->time_base);
178 log_packet(m_avFormatContext, packet);
179 auto ret = av_interleaved_write_frame(m_avFormatContext, packet);
181 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Error while writing output packet:" << av_err2str(ret);
185std::unique_ptr<PipeWireProduce> PipeWireRecord::makeProduce()
187 return std::make_unique<PipeWireRecordProduce>(encoder(), nodeId(), fd(), maxFramerate(), d->m_output);
190int64_t PipeWireRecordProduce::framePts(
const std::optional<std::chrono::nanoseconds> &presentationTimestamp)
192 const auto current = std::chrono::duration_cast<std::chrono::milliseconds>(*presentationTimestamp).count();
193 if ((*m_avFormatContext->streams)->start_time == 0) {
194 (*m_avFormatContext->streams)->start_time = current;
197 return current - (*m_avFormatContext->streams)->start_time;
200void PipeWireRecordProduce::cleanup()
202 if (m_avFormatContext) {
203 if (
auto result = av_write_trailer(m_avFormatContext); result < 0) {
204 qCWarning(PIPEWIRERECORD_LOGGING) <<
"Could not write trailer";
207 avio_closep(&m_avFormatContext->pb);
208 avformat_free_context(m_avFormatContext);
212#include "moc_pipewirerecord.cpp"
214#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