KPipewire

pipewirebaseencodedstream.h
1/*
2 SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#pragma once
8
9#include <QObject>
10
11#include <kpipewire_export.h>
12
13struct Fraction;
14struct PipeWireEncodedStreamPrivate;
15class PipeWireProduce;
16
17class KPIPEWIRE_EXPORT PipeWireBaseEncodedStream : public QObject
18{
19 Q_OBJECT
20 /// Specify the pipewire node id that we want to record
21 Q_PROPERTY(uint nodeId READ nodeId WRITE setNodeId NOTIFY nodeIdChanged)
22 /**
23 * Specifies the file descriptor we are connected to, if none 0 will be returned
24 *
25 * Transfers the ownership of the fd, will close it when it's done with it.
26 */
27 Q_PROPERTY(uint fd READ fd WRITE setFd NOTIFY fdChanged)
28 Q_PROPERTY(bool active READ isActive NOTIFY activeChanged)
29 Q_PROPERTY(State state READ state NOTIFY stateChanged)
30 Q_PROPERTY(Encoder encoder READ encoder WRITE setEncoder NOTIFY encoderChanged)
31
32public:
33 enum Encoder {
34 NoEncoder,
35 VP8,
36 VP9,
37 H264Main,
38 H264Baseline,
39 WebP,
40 Gif,
41 };
42 Q_ENUM(Encoder)
43
44 PipeWireBaseEncodedStream(QObject *parent = nullptr);
45 ~PipeWireBaseEncodedStream() override;
46
47 void setNodeId(uint nodeId);
48 uint nodeId() const;
49
50 void setFd(uint fd);
51 uint fd() const;
52
53 Fraction maxFramerate() const;
54 void setMaxFramerate(const Fraction &framerate);
55 void setMaxFramerate(quint32 numerator, quint32 denominator = 1);
56
57 /**
58 * Defines how many frames are kept in the encoding buffer.
59 * New frames after the buffer is full will be dropped.
60 *
61 * This needs to be high enough for intra-frame analysis.
62 * The default value is 50.
63 *
64 * There is a minimum value of 3.
65 */
66 void setMaxPendingFrames(int maxBufferSize);
67 int maxBufferSize() const;
68
69 bool isActive() const;
70 /**
71 * Set the active state of recording.
72 *
73 * @deprecated Since 6.4, use the separate `start()`/`stop()`calls instead.
74 * This function now just calls `start()`/`stop()`.
75 *
76 * @note When calling `setActive(false)`, unlike `stop()`, this function will
77 * block until the internal encoding threads are finished.
78 */
79 KPIPEWIRE_DEPRECATED void setActive(bool active);
80
81 /**
82 * Request to start recording.
83 *
84 * This will create everything required to perform recording, like a PipeWire
85 * stream and an encoder, then start receiving frames from the stream and
86 * encoding those.
87 *
88 * This requires a valid node ID to be set and that the current state is Idle.
89 *
90 * Note that recording all happens on separate threads, this method only
91 * starts the process, only when state() returns Recording is recording
92 * actually happening.
93 */
94 Q_INVOKABLE void start();
95 /**
96 * Request to stop recording.
97 *
98 * This will terminate receiving frames from PipeWire and do any cleanup
99 * necessary to fully terminate recording after that.
100 *
101 * Note that after this request, there may still be some processing required
102 * due to internal queues. As long as state() does not return Idle processing
103 * is still happening and teardown has not been completed.
104 */
105 Q_INVOKABLE void stop();
106
107 /**
108 * The quality used for encoding.
109 */
110 std::optional<quint8> quality() const;
111 /**
112 * Set the quality to use for encoding.
113 *
114 * This uses a range of 0-100 as a percentage, with 0 being lowest quality
115 * and 100 being highest. This is internally converted to a value relevant to
116 * the encoder.
117 *
118 * @param quality The quality level to use.
119 */
120 void setQuality(quint8 quality);
121
122 enum State {
123 Idle, //< ready to get started
124 Recording, //< actively recording
125 Rendering, //< recording is over but there are still frames being processed.
126 };
127 Q_ENUM(State)
128 State state() const;
129
130 /**
131 * Set the FFmpeg @p encoder that will be used to create the video
132 *
133 * They can be inspected using:
134 * ffmpeg -encoders | grep "^ V"
135 */
136 void setEncoder(Encoder encoder);
137 Encoder encoder() const;
138
139 /// Returns the encoders that are tested to work, sorted by preference
140 QList<PipeWireBaseEncodedStream::Encoder> suggestedEncoders() const;
141
142 enum EncodingPreference {
143 NoPreference, ///< Default settings, good for most usecases
144 Quality, ///< A bit slower than default, but more consistent bitrate, use for high quality
145 Speed, ///< Encode as fast as possible and use zerolatency tune, good for streaming
146 Size, ///< Slowest encoding but reduces the size of the file
147 };
148 Q_ENUM(EncodingPreference);
149 void setEncodingPreference(EncodingPreference profile);
150
151Q_SIGNALS:
152 void activeChanged(bool active);
153 void nodeIdChanged(uint nodeId);
154 void fdChanged(uint fd);
155 void errorFound(const QString &error);
156 void maxFramerateChanged();
157 void maxPendingFramesChanged();
158 void stateChanged();
159 void encoderChanged();
160
161protected:
162 virtual std::unique_ptr<PipeWireProduce> makeProduce() = 0;
163 EncodingPreference encodingPreference();
164
165 QScopedPointer<PipeWireEncodedStreamPrivate> d;
166};
void stop(Ekos::AlignState mode)
Q_SCRIPTABLE Q_NOREPLY void start()
Q_ENUM(...)
Q_PROPERTY(...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 12:00:27 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.