From a50c28da704fbf8b9e71ec92054f325a33b9765f Mon Sep 17 00:00:00 2001
From: Fabian Vogt <fabian@ritter-vogt.de>
Date: Sat, 6 Jul 2024 16:27:28 +0200
Subject: [PATCH 1/2] Simpler yet more useful handling of
 KPIPEWIRE_FORCE_ENCODER

Previously, it always overrode the encoder type and profile.

Now just force a specific encoder by inlining the encoder selection
in the switch cases.

This means it's no longer possible to force a different encoder type
than the application requested, but it's arguably not that useful to
e.g. force VP9 if the application expects H.264 packets.

(cherry picked from commit 0c3f8b4f9de7d4dcd24d952184dabdbda74b4c35)
---
 src/pipewireproduce.cpp | 45 +++++++++--------------------------------
 1 file changed, 9 insertions(+), 36 deletions(-)

diff --git a/src/pipewireproduce.cpp b/src/pipewireproduce.cpp
index 3452ce9..416bcd3 100644
--- a/src/pipewireproduce.cpp
+++ b/src/pipewireproduce.cpp
@@ -266,46 +266,19 @@ void PipeWireProduce::stateChanged(pw_stream_state state)
 
 std::unique_ptr<Encoder> PipeWireProduce::makeEncoder()
 {
-    auto encoderType = m_encoderType;
-    bool forceSoftware = false;
-    bool forceHardware = false;
-
-    if (qEnvironmentVariableIsSet("KPIPEWIRE_FORCE_ENCODER")) {
-        auto forcedEncoder = qEnvironmentVariable("KPIPEWIRE_FORCE_ENCODER");
-        if (forcedEncoder == u"libvpx") {
-            qCWarning(PIPEWIRERECORD_LOGGING) << "Forcing VP8 Software encoding";
-            encoderType = PipeWireBaseEncodedStream::VP8;
-            forceSoftware = true;
-        } else if (forcedEncoder == u"libvpx-vp9") {
-            qCWarning(PIPEWIRERECORD_LOGGING) << "Forcing VP9 Software encoding";
-            encoderType = PipeWireBaseEncodedStream::VP9;
-            forceSoftware = true;
-        } else if (forcedEncoder == u"libx264") {
-            qCWarning(PIPEWIRERECORD_LOGGING) << "Forcing H264 Software encoding, main profile";
-            encoderType = PipeWireBaseEncodedStream::H264Main;
-            forceSoftware = true;
-        } else if (forcedEncoder == u"h264_vaapi") {
-            qCWarning(PIPEWIRERECORD_LOGGING) << "Forcing H264 Hardware encoding, main profile";
-            encoderType = PipeWireBaseEncodedStream::H264Main;
-            forceHardware = true;
-        } else if (forcedEncoder == u"libx264_baseline") {
-            qCWarning(PIPEWIRERECORD_LOGGING) << "Forcing H264 Software encoding, baseline profile";
-            encoderType = PipeWireBaseEncodedStream::H264Baseline;
-            forceSoftware = true;
-        } else if (forcedEncoder == u"h264_vaapi_baseline") {
-            qCWarning(PIPEWIRERECORD_LOGGING) << "Forcing H264 Hardware encoding, baseline profile";
-            encoderType = PipeWireBaseEncodedStream::H264Baseline;
-            forceHardware = true;
-        }
+    auto forcedEncoder = qEnvironmentVariable("KPIPEWIRE_FORCE_ENCODER");
+    if (!forcedEncoder.isNull()) {
+        qCWarning(PIPEWIRERECORD_LOGGING) << "Forcing encoder to" << forcedEncoder;
     }
 
     auto size = m_stream->size();
 
-    switch (encoderType) {
+    switch (m_encoderType) {
     case PipeWireBaseEncodedStream::H264Baseline:
     case PipeWireBaseEncodedStream::H264Main: {
         auto profile = m_encoderType == PipeWireBaseEncodedStream::H264Baseline ? Encoder::H264Profile::Baseline : Encoder::H264Profile::Main;
-        if (!forceSoftware) {
+
+        if (forcedEncoder.isNull() || forcedEncoder == u"h264_vaapi") {
             auto hardwareEncoder = std::make_unique<H264VAAPIEncoder>(profile, this);
             hardwareEncoder->setQuality(m_quality);
             hardwareEncoder->setEncodingPreference(m_encodingPreference);
@@ -314,7 +287,7 @@ std::unique_ptr<Encoder> PipeWireProduce::makeEncoder()
             }
         }
 
-        if (!forceHardware) {
+        if (forcedEncoder.isNull() || forcedEncoder == u"libx264") {
             auto softwareEncoder = std::make_unique<LibX264Encoder>(profile, this);
             softwareEncoder->setQuality(m_quality);
             softwareEncoder->setEncodingPreference(m_encodingPreference);
@@ -325,7 +298,7 @@ std::unique_ptr<Encoder> PipeWireProduce::makeEncoder()
         break;
     }
     case PipeWireBaseEncodedStream::VP8: {
-        if (!forceHardware) {
+        if (forcedEncoder.isNull() || forcedEncoder == u"libvpx") {
             auto encoder = std::make_unique<LibVpxEncoder>(this);
             encoder->setQuality(m_quality);
             if (encoder->initialize(size)) {
@@ -335,7 +308,7 @@ std::unique_ptr<Encoder> PipeWireProduce::makeEncoder()
         break;
     }
     case PipeWireBaseEncodedStream::VP9: {
-        if (!forceHardware) {
+        if (forcedEncoder.isNull() || forcedEncoder == u"libvpx-vp9") {
             auto encoder = std::make_unique<LibVpxVp9Encoder>(this);
             encoder->setQuality(m_quality);
             if (encoder->initialize(size)) {
-- 
2.45.2