summaryrefslogtreecommitdiffstats
path: root/pica-client/callsettings.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pica-client/callsettings.cpp')
-rw-r--r--pica-client/callsettings.cpp233
1 files changed, 233 insertions, 0 deletions
diff --git a/pica-client/callsettings.cpp b/pica-client/callsettings.cpp
new file mode 100644
index 0000000..10529c1
--- /dev/null
+++ b/pica-client/callsettings.cpp
@@ -0,0 +1,233 @@
+/*
+ (c) Copyright 2012 - 2026 Anton Sviridenko
+ https://picapica.im
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, version 3.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+#include "callsettings.h"
+#include "settings.h"
+#include "globals.h"
+#include <QGuiApplication>
+#include <QScreen>
+#include <QWidget>
+#include <QWindow>
+
+const CallCodecInfo kCallVideoCodecs[] =
+{
+ { "h264", "H.264" },
+ { "hevc", "H.265/HEVC" },
+ { "vp9", "VP9" }
+};
+
+const int kCallVideoCodecCount = sizeof(kCallVideoCodecs) / sizeof(kCallVideoCodecs[0]);
+
+const CallCodecInfo kCallAudioCodecs[] =
+{
+ { "opus", "Opus" },
+ { "adpcm_g722", "G.722" },
+ { "pcm_alaw", "G.711 A-law" }
+};
+
+const int kCallAudioCodecCount = sizeof(kCallAudioCodecs) / sizeof(kCallAudioCodecs[0]);
+
+const QSize kIncomingVideoLimitSizes[] =
+{
+ QSize(3840, 2160), QSize(2560, 1440), QSize(1920, 1080), QSize(1600, 900),
+ QSize(1280, 720), QSize(960, 540), QSize(800, 600), QSize(640, 480),
+ QSize(640, 360), QSize(320, 240)
+};
+
+const int kIncomingVideoLimitSizeCount = sizeof(kIncomingVideoLimitSizes) / sizeof(kIncomingVideoLimitSizes[0]);
+
+bool callAudioCodecAccepted(const QString &codec)
+{
+ for (int i = 0; i < kCallAudioCodecCount; i++)
+ if (codec == QLatin1String(kCallAudioCodecs[i].name))
+ return true;
+
+ return false;
+}
+
+bool callVideoCodecAccepted(const QString &codec)
+{
+ for (int i = 0; i < kCallVideoCodecCount; i++)
+ if (codec == QLatin1String(kCallVideoCodecs[i].name))
+ return true;
+
+ // A camera's own compressed stream is forwarded untouched, so a sender may
+ // legitimately name a capture format that is not in the encode list.
+ if (codec == QLatin1String("mjpeg"))
+ return true;
+
+ return false;
+}
+
+int callAudioSampleRate(const QString &codec)
+{
+ // G.722 is a 16kHz codec by definition - it subband-codes a 7kHz audio
+ // bandwidth and there is no other rate to run it at.
+ if (codec == QLatin1String("adpcm_g722"))
+ return 16000;
+
+ // A-law is the telephone network's codec and 8kHz is what it is for.
+ // Running it at anything else would only spend twice the bandwidth on a
+ // codec chosen for costing as little as possible.
+ if (codec == QLatin1String("pcm_alaw"))
+ return 8000;
+
+ // Opus, and anything unrecognised: 16kHz rather than 48kHz because it is
+ // what the echo canceller wants. AEC3 does its filtering in the 0-8kHz
+ // band whatever it is handed; at 48kHz it splits the signal into four
+ // bands, cancels in the lowest and gates the rest, which is more work for
+ // a result that is no better on speech.
+ //
+ // Costs nothing worth having for speech: Opus codes this as wideband, an
+ // 8kHz audio bandwidth, which is well past telephone quality and past
+ // where voice intelligibility stops improving.
+ //
+ // Note that this is the capture direction only. Playback follows whatever
+ // the peer announced in its own 0x74 message (see
+ // AudioVideoCallController::incoming_audio_params()), so a call with a
+ // peer that picked a different codec or rate keeps working.
+ return 16000;
+}
+
+bool callAudioBitrateAdjustable(const QString &codec)
+{
+ // Opus is the only one of the three with a rate control worth the name;
+ // the other two are fixed bits per sample.
+ return codec == QLatin1String("opus") || codec.isEmpty();
+}
+
+int callAudioFixedBitrateKbps(const QString &codec)
+{
+ // G.722 is 4 bits per sample at 16kHz, A-law 8 bits per sample at 8kHz -
+ // 64 kbit/s either way, which is no coincidence.
+ if (codec == QLatin1String("adpcm_g722"))
+ return 16000 * 4 / 1000;
+
+ if (codec == QLatin1String("pcm_alaw"))
+ return 8000 * 8 / 1000;
+
+ return kDefaultAudioBitrateKbps;
+}
+
+QSize callScreenResolution(const QWidget *widget)
+{
+ QScreen *screen = nullptr;
+
+ // Only a window that has been shown has a QWindow of its own; a dialog
+ // that is still being built goes by the window it belongs to.
+ for (const QWidget *w = widget; w && !screen; w = w->parentWidget())
+ if (w->windowHandle())
+ screen = w->windowHandle()->screen();
+
+ if (!screen)
+ screen = QGuiApplication::primaryScreen();
+
+ if (!screen)
+ return QSize();
+
+ return screen->size() * screen->devicePixelRatio();
+}
+
+QSize callRecommendedResolution(const CallSettings &cs, const QWidget *window)
+{
+ if (cs.incomingVideoLimit == IncomingVideoLimitCustom)
+ return cs.incomingVideoLimitSize;
+
+ const QSize screen = callScreenResolution(window);
+
+ if (!screen.isValid())
+ return QSize(0, 0);
+
+ // The peer compares width with width, so a portrait screen would hold a
+ // landscape camera down to the screen's width.
+ if (cs.incomingVideoLandscape && screen.height() > screen.width())
+ return screen.transposed();
+
+ return screen;
+}
+
+CallSettings CallSettings::load()
+{
+ Settings st(config_dbname);
+ CallSettings cs;
+
+ cs.videoCodec = st.loadValue("call.video_codec", QLatin1String(kCallVideoCodecs[0].name)).toString();
+ cs.audioCodec = st.loadValue("call.audio_codec", QLatin1String(kCallAudioCodecs[0].name)).toString();
+
+ cs.videoBitrateKbps = st.loadValue("call.video_bitrate_kbps", kDefaultVideoBitrateKbps).toInt();
+ cs.audioBitrateKbps = st.loadValue("call.audio_bitrate_kbps", kDefaultAudioBitrateKbps).toInt();
+
+ cs.captureWidth = st.loadValue("video.capture_width", kDefaultCaptureWidth).toInt();
+ cs.captureHeight = st.loadValue("video.capture_height", kDefaultCaptureHeight).toInt();
+ cs.captureFrameRate = st.loadValue("video.capture_framerate", kDefaultCaptureFrameRate).toInt();
+
+ cs.incomingVideoLimit = st.loadValue("call.incoming_video_limit", "screen").toString() == QLatin1String("custom")
+ ? IncomingVideoLimitCustom : IncomingVideoLimitScreen;
+ cs.incomingVideoLimitSize = QSize(st.loadValue("call.incoming_video_limit_width", 0).toInt(),
+ st.loadValue("call.incoming_video_limit_height", 0).toInt());
+ cs.incomingVideoLandscape = st.loadValue("call.incoming_video_landscape", 0).toBool();
+
+ // Only the keys this build has checkboxes for: a database carried over
+ // from another platform must not switch on a path that cannot exist here.
+ cs.videoEncodingAcceleration = 0;
+ cs.videoDecodingAcceleration = 0;
+#ifdef HAVE_VAAPI
+ if (st.loadValue("video.vaapi_encoding", 0).toBool())
+ cs.videoEncodingAcceleration |= VideoAccelerationVaapi;
+ if (st.loadValue("video.vaapi_decoding", 0).toBool())
+ cs.videoDecodingAcceleration |= VideoAccelerationVaapi;
+#endif
+#ifdef HAVE_D3D12VA
+ if (st.loadValue("video.d3d12va_encoding", 0).toBool())
+ cs.videoEncodingAcceleration |= VideoAccelerationD3d12va;
+ if (st.loadValue("video.d3d12va_decoding", 0).toBool())
+ cs.videoDecodingAcceleration |= VideoAccelerationD3d12va;
+#endif
+#ifdef Q_OS_WIN
+ if (st.loadValue("video.mediafoundation_encoding", 0).toBool())
+ cs.videoEncodingAcceleration |= VideoAccelerationMediaFoundation;
+ if (st.loadValue("video.d3d11va_decoding", 0).toBool())
+ cs.videoDecodingAcceleration |= VideoAccelerationD3d11va;
+ if (st.loadValue("video.dxva2_decoding", 0).toBool())
+ cs.videoDecodingAcceleration |= VideoAccelerationDxva2;
+#endif
+
+ // A stored value that makes no sense - a hand-edited database, or a
+ // setting written by a build that offered something this one does not -
+ // would otherwise be passed to the encoder and fail the call rather than
+ // the setting.
+ if (cs.videoBitrateKbps <= 0)
+ cs.videoBitrateKbps = kDefaultVideoBitrateKbps;
+
+ if (cs.audioBitrateKbps <= 0)
+ cs.audioBitrateKbps = kDefaultAudioBitrateKbps;
+
+ if (cs.captureWidth <= 0 || cs.captureHeight <= 0)
+ {
+ cs.captureWidth = kDefaultCaptureWidth;
+ cs.captureHeight = kDefaultCaptureHeight;
+ }
+
+ if (cs.captureFrameRate <= 0)
+ cs.captureFrameRate = kDefaultCaptureFrameRate;
+
+ // Half a limit, or one that does not fit the 0x70/0x71 field, is no limit.
+ if (cs.incomingVideoLimitSize.width() <= 0 || cs.incomingVideoLimitSize.height() <= 0 ||
+ cs.incomingVideoLimitSize.width() > 0xFFFF || cs.incomingVideoLimitSize.height() > 0xFFFF)
+ cs.incomingVideoLimitSize = QSize(0, 0);
+
+ return cs;
+}