diff options
Diffstat (limited to 'pica-client/callquality.h')
| -rw-r--r-- | pica-client/callquality.h | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/pica-client/callquality.h b/pica-client/callquality.h new file mode 100644 index 0000000..a147433 --- /dev/null +++ b/pica-client/callquality.h @@ -0,0 +1,184 @@ +/* + (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/>. +*/ +#ifndef CALLQUALITY_H +#define CALLQUALITY_H + +#include "callsettings.h" +#include <QList> +#include <QMetaType> +#include <QSize> +#include <QString> + +// What the call quality presets other than CallQualityCustom turn into: the +// codec, hardware encoder, picture size, frame rate and bitrate a call starts +// with, and for CallQualityAdaptive how those follow the connection during the +// call. +// +// Nothing in here touches FFmpeg, a camera or the settings database. Its inputs +// are what VideoDevice found out - the camera's sizes and rates, the hardware +// encoders that work - so that the decisions can be tested on their own, see +// tests/test_callquality. + +// One hardware encoder that opened and encoded a frame on this machine, as +// found by VideoDevice::ProbeHardwareEncoders(). +struct CallHardwareEncoder +{ + // FFmpeg codec name, one of kCallVideoCodecs. + QString codec; + // The flag that reaches it. + VideoAcceleration acceleration; + // FFmpeg encoder name, e.g. "h264_vaapi", for showing to the user. + QString encoder; +}; + +// One picture size a camera offers and the frame rates it offers it at - the +// part of VideoCaptureFormat the planning needs. +struct CallCameraSize +{ + QSize size; + // Highest first; empty when the camera would not say. + QList<int> frameRates; +}; + +struct CallVideoPlan +{ + // FFmpeg codec name. + QString codec; + // The one VideoAcceleration flag to encode through, 0 for software. + int acceleration; + + // What the camera is asked for. + QSize captureSize; + int frameRate; + + // What gets encoded and sent at the start of the call: captureSize, or the + // captured picture scaled down. + QSize encodeSize; + int bitrateKbps; + + // CallQualityAdaptive: the bitrate follows the peer's 0x79 reports, see + // CallRateController, and the picture is scaled to one of these sizes, + // largest first and starting with captureSize, as the bitrate goes down + // and up. Empty for the other presets, whose plan holds for the whole + // call. + QList<QSize> ladder; + // The most the bitrate goes up to: what CallQualityMax would use for + // captureSize. + int maxBitrateKbps; + + bool adaptive() const { return !ladder.isEmpty(); } +}; + +struct CallAudioPlan +{ + // FFmpeg codec name. + QString codec; + // Only means something for a codec callAudioBitrateAdjustable() is true + // for. + int bitrateKbps; +}; + +// The bitrate that gives a picture of this size and rate good quality in the +// given codec, without spending bits on detail a call does not show - the +// target of CallQualityMax, and the ceiling CallQualityAdaptive climbs back +// to. Grows slower than the pixel rate, since a larger or faster picture has +// more in it that can be predicted. hardware is for a GPU encoder, which +// needs somewhat more than a software one for the same picture. +int callVideoBitrateKbps(const QSize &size, double frameRate, const QString &codec, bool hardware); + +// The sizes CallQualityAdaptive steps between for a picture captured at top: +// top itself, then the common sizes with a shorter side of 1440, 1080, 720, +// 540, 360, 270 and 180 that are smaller than it, at top's aspect ratio. +QList<QSize> callVideoSizeLadder(const QSize &top); + +// What a call made with `preset` sends. camera is what the camera offers, +// empty when it could not be asked; peerLimit the recommended resolution of +// the peer's 0x70/0x71, a dimension of 0 being unlimited; hardware the +// encoders that work here, in the order VideoDevice would try them. +// +// Not for CallQualityCustom, whose settings are used as they are. +CallVideoPlan callPlanVideo(CallQualityPreset preset, const QList<CallCameraSize> &camera, + const QSize &peerLimit, const QList<CallHardwareEncoder> &hardware); + +// The audio codec and bitrate a call is made with: the stored ones for +// CallQualityCustom, the preset's otherwise. +CallAudioPlan callPlanAudio(const CallSettings &cs); + +// One 0x79 message: what the peer received of our media over one period, see +// doc/protocol-specification.txt. +struct CallReceptionReport +{ + int periodMs; + int audioReceived; + int audioLost; + int videoReceived; + int videoLost; + quint32 audioOctets; + quint32 videoOctets; + // How much later than usual our audio has been arriving, -1 when the peer + // received none to tell by. + int queuingDelayMs; +}; + +Q_DECLARE_METATYPE(CallReceptionReport) + +// Follows the connection for CallQualityAdaptive: takes the peer's reception +// reports and works out the bitrate and picture size to encode at. +// +// The bitrate goes down when packets get lost or start queueing up on the way +// - multiplicatively, and to below what actually got through when it is a +// queue - and creeps back up a few percent per report while the connection +// keeps up, to the plan's maximum. The picture size follows the bitrate: a +// step down once the bitrate is too low for the current size to look good, +// and a step up once it is well into what the next size up needs. The two +// thresholds are far enough apart that the size does not flap, and steps up +// are spaced out on top of that, each one costing a keyframe. +class CallRateController +{ +public: + CallRateController(); + + // Begins at plan.encodeSize and plan.bitrateKbps. + void start(const CallVideoPlan &plan, qint64 nowMs); + + // One report from the peer. videoPacketsSent is how many video packets + // went out since the previous one, which tells a connection that lets + // nothing through from a camera that stopped. True when bitrateKbps() or + // size() changed. + bool update(const CallReceptionReport &report, int videoPacketsSent, qint64 nowMs); + + int bitrateKbps() const { return qRound(m_bitrate); } + QSize size() const { return m_ladder.isEmpty() ? QSize() : m_ladder.at(m_rung); } + + // Why the last change was made, for the log. + QString reason() const { return m_reason; } + +private: + QList<QSize> m_ladder; + // callVideoBitrateKbps() for each size of m_ladder. + QList<int> m_rungKbps; + int m_rung; + double m_bitrate; + int m_maxKbps; + // No increase before this, after a decrease: the reports of the next + // second or two still describe the time before it took effect. + qint64 m_holdUntil; + qint64 m_lastRungChange; + QString m_reason; +}; + +#endif // CALLQUALITY_H |
