File indexing completed on 2024-05-05 05:30:17

0001 /*
0002     SPDX-FileCopyrightText: 2022 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QObject>
0010 
0011 #include <kpipewire_export.h>
0012 
0013 struct Fraction;
0014 struct PipeWireEncodedStreamPrivate;
0015 class PipeWireProduce;
0016 
0017 class KPIPEWIRE_EXPORT PipeWireBaseEncodedStream : public QObject
0018 {
0019     Q_OBJECT
0020     /// Specify the pipewire node id that we want to record
0021     Q_PROPERTY(uint nodeId READ nodeId WRITE setNodeId NOTIFY nodeIdChanged)
0022     /**
0023      * Specifies the file descriptor we are connected to, if none 0 will be returned
0024      *
0025      * Transfers the ownership of the fd, will close it when it's done with it.
0026      */
0027     Q_PROPERTY(uint fd READ fd WRITE setFd NOTIFY fdChanged)
0028     Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activeChanged)
0029     Q_PROPERTY(State state READ state NOTIFY stateChanged)
0030     Q_PROPERTY(Encoder encoder READ encoder WRITE setEncoder NOTIFY encoderChanged)
0031 
0032 public:
0033     enum Encoder {
0034         NoEncoder,
0035         VP8,
0036         VP9,
0037         H264Main,
0038         H264Baseline,
0039     };
0040     Q_ENUM(Encoder)
0041 
0042     PipeWireBaseEncodedStream(QObject *parent = nullptr);
0043     ~PipeWireBaseEncodedStream() override;
0044 
0045     void setNodeId(uint nodeId);
0046     uint nodeId() const;
0047 
0048     void setFd(uint fd);
0049     uint fd() const;
0050 
0051     Fraction maxFramerate() const;
0052     void setMaxFramerate(const Fraction &framerate);
0053     void setMaxFramerate(quint32 numerator, quint32 denominator = 1);
0054 
0055     bool isActive() const;
0056     void setActive(bool active);
0057 
0058     /**
0059      * The quality used for encoding.
0060      */
0061     std::optional<quint8> quality() const;
0062     /**
0063      * Set the quality to use for encoding.
0064      *
0065      * This uses a range of 0-100 as a percentage, with 0 being lowest quality
0066      * and 100 being highest. This is internally converted to a value relevant to
0067      * the encoder.
0068      *
0069      * @param quality The quality level to use.
0070      */
0071     void setQuality(quint8 quality);
0072 
0073     enum State {
0074         Idle, //< ready to get started
0075         Recording, //< actively recording
0076         Rendering, //< recording is over but there are still frames being processed.
0077     };
0078     Q_ENUM(State)
0079     State state() const;
0080 
0081     /**
0082      * Set the FFmpeg @p encoder that will be used to create the video
0083      *
0084      * They can be inspected using:
0085      * ffmpeg -encoders | grep "^ V"
0086      */
0087     void setEncoder(Encoder encoder);
0088     Encoder encoder() const;
0089 
0090     /// Returns the encoders that are tested to work, sorted by preference
0091     QList<PipeWireBaseEncodedStream::Encoder> suggestedEncoders() const;
0092 
0093 Q_SIGNALS:
0094     void activeChanged(bool active);
0095     void nodeIdChanged(uint nodeId);
0096     void fdChanged(uint fd);
0097     void errorFound(const QString &error);
0098     void maxFramerateChanged();
0099     void stateChanged();
0100     void encoderChanged();
0101 
0102 protected:
0103     virtual std::unique_ptr<PipeWireProduce> makeProduce() = 0;
0104 
0105     void refresh();
0106     QScopedPointer<PipeWireEncodedStreamPrivate> d;
0107 };