diff options
Diffstat (limited to 'pica-client')
| -rw-r--r-- | pica-client/audiovideocallcontroller.cpp | 27 | ||||
| -rw-r--r-- | pica-client/audiovideocallcontroller.h | 15 | ||||
| -rw-r--r-- | pica-client/callsettings.cpp | 61 | ||||
| -rw-r--r-- | pica-client/callsettings.h | 40 | ||||
| -rw-r--r-- | pica-client/dialogs/settingsdialog.cpp | 82 | ||||
| -rw-r--r-- | pica-client/dialogs/settingsdialog.h | 9 | ||||
| -rw-r--r-- | pica-client/skynet.cpp | 45 | ||||
| -rw-r--r-- | pica-client/skynet.h | 26 | ||||
| -rw-r--r-- | pica-client/videodevice.cpp | 82 | ||||
| -rw-r--r-- | pica-client/videodevice.h | 11 |
10 files changed, 359 insertions, 39 deletions
diff --git a/pica-client/audiovideocallcontroller.cpp b/pica-client/audiovideocallcontroller.cpp index 62cb0e0..55bcf3a 100644 --- a/pica-client/audiovideocallcontroller.cpp +++ b/pica-client/audiovideocallcontroller.cpp @@ -32,9 +32,9 @@ AudioVideoCallController::AudioVideoCallController(QObject *parent) m_videoSeq(0), m_videoFrameTimestamp(0), m_videoFrameStarted(false), m_requestedFrameRate(0) { - connect(skynet, SIGNAL(IncomingCall(QByteArray)), this, SLOT(call_from(QByteArray))); + connect(skynet, SIGNAL(IncomingCall(QByteArray,QSize)), this, SLOT(call_from(QByteArray,QSize))); connect(skynet, SIGNAL(CallFailed(QByteArray,QString)), this, SLOT(call_failed(QByteArray,QString))); - connect(skynet, SIGNAL(CallAccepted(QByteArray)), this, SLOT(call_accepted(QByteArray))); + connect(skynet, SIGNAL(CallAccepted(QByteArray,QSize)), this, SLOT(call_accepted(QByteArray,QSize))); connect(skynet, SIGNAL(CallRejected(QByteArray)), this, SLOT(call_rejected(QByteArray))); connect(skynet, SIGNAL(CallHungup(QByteArray)), this, SLOT(call_hungup(QByteArray))); connect(skynet, SIGNAL(c2cClosed(QByteArray)), this, SLOT(peer_disconnected(QByteArray))); @@ -150,6 +150,13 @@ void AudioVideoCallController::playRingTone() QMetaObject::invokeMethod(ringtoneplayer, "playClassicRingtone", Qt::QueuedConnection); } +QSize AudioVideoCallController::recommendedResolution() const +{ + // The window the peer's video will be shown in: its screen is the one + // that matters with several of them. + return callRecommendedResolution(CallSettings::load(), callwindow); +} + void AudioVideoCallController::stopTones() { toneplayer->stop(); @@ -311,6 +318,11 @@ void AudioVideoCallController::startVideoPipeline() Q_ARG(QString, cs.videoCodec), Q_ARG(int, cs.videoBitrateKbps * 1000), Q_ARG(int, cs.videoEncodingAcceleration), Q_ARG(QByteArray, m_caller_id), Q_ARG(QByteArray, m_callee_id)); + // The configured size is only where the camera starts from: what the peer + // wants to receive caps it. + QMetaObject::invokeMethod(cam, "limitCaptureSize", Qt::QueuedConnection, + Q_ARG(int, m_peerRecommendedResolution.width()), + Q_ARG(int, m_peerRecommendedResolution.height())); QMetaObject::invokeMethod(cam, "Capture", Qt::QueuedConnection); // remotevideo is configured lazily, once the peer's own 0x75 (see @@ -367,7 +379,7 @@ void AudioVideoCallController::call_failed(QByteArray peer_id, QString reason) playEarpieceTone(&TonePlayer::playUnreachable); } -void AudioVideoCallController::call_accepted(QByteArray peer_id) +void AudioVideoCallController::call_accepted(QByteArray peer_id, QSize recommended_resolution) { /* SkyNet lives in its own thread, so this signal is queued: by the time it is delivered the local side may have already hung up and closed the @@ -376,6 +388,8 @@ void AudioVideoCallController::call_accepted(QByteArray peer_id) if (peer_id != m_peer_id || m_state != CallDialing || !callwindow) return; + m_peerRecommendedResolution = recommended_resolution; + stopTones(); m_state = CallActive; callwindow->call_started(); @@ -473,7 +487,7 @@ void AudioVideoCallController::start_call(QByteArray peer_id) callwindow->show(); } -void AudioVideoCallController::call_from(QByteArray peer_id) +void AudioVideoCallController::call_from(QByteArray peer_id, QSize recommended_resolution) { /* One call at a time: busy. The peer hears the busy tone and our own call is not disturbed. */ @@ -490,6 +504,7 @@ void AudioVideoCallController::call_from(QByteArray peer_id) m_state = CallRinging; m_peer_id = peer_id; + m_peerRecommendedResolution = recommended_resolution; m_caller_id = peer_id; m_callee_id = Accounts::GetCurrentAccount().id; callwindow = new CallWindow(peer_id, true); @@ -516,7 +531,7 @@ void AudioVideoCallController::initiate_call() return; m_state = CallDialing; - skynet->StartCall(m_peer_id); + skynet->StartCall(m_peer_id, recommendedResolution()); /* play ringing tone */ playEarpieceTone(&TonePlayer::playCallInProgress); } @@ -527,7 +542,7 @@ void AudioVideoCallController::accept_call() return; stopTones(); - skynet->AcceptCall(m_peer_id); + skynet->AcceptCall(m_peer_id, recommendedResolution()); m_state = CallActive; callwindow->call_started(); startAudioPipeline(); diff --git a/pica-client/audiovideocallcontroller.h b/pica-client/audiovideocallcontroller.h index ae1d422..b7640fa 100644 --- a/pica-client/audiovideocallcontroller.h +++ b/pica-client/audiovideocallcontroller.h @@ -36,9 +36,11 @@ public: void processCall(); public slots: void start_call(QByteArray peer_id); - void call_from(QByteArray peer_id); + // recommended_resolution - the largest video the peer wants to receive, + // from its 0x70/0x71 + void call_from(QByteArray peer_id, QSize recommended_resolution); void call_failed(QByteArray peer_id, QString reason); - void call_accepted(QByteArray peer_id); + void call_accepted(QByteArray peer_id, QSize recommended_resolution); void call_rejected(QByteArray peer_id); void call_hungup(QByteArray peer_id); // the connection to the peer is gone, and whatever call it carried with it @@ -83,6 +85,15 @@ private: bool m_audioPlaybackStarted; bool m_videoPlaybackStarted; + // The recommended resolution from the peer's 0x70/0x71: the camera is + // stepped down to fit it, see VideoDevice::limitCaptureSize(). A dimension + // of 0 is not limited. + QSize m_peerRecommendedResolution; + + // What we send in our own 0x70/0x71, worked out from the settings and the + // screen the call window is on. + QSize recommendedResolution() const; + // Earpiece tones (ringback, busy, special information/unreachable), // played through the "audio.playback_device" setting. TonePlayer *toneplayer; diff --git a/pica-client/callsettings.cpp b/pica-client/callsettings.cpp index dff72f4..10529c1 100644 --- a/pica-client/callsettings.cpp +++ b/pica-client/callsettings.cpp @@ -17,6 +17,10 @@ #include "callsettings.h" #include "settings.h" #include "globals.h" +#include <QGuiApplication> +#include <QScreen> +#include <QWidget> +#include <QWindow> const CallCodecInfo kCallVideoCodecs[] = { @@ -36,6 +40,15 @@ const CallCodecInfo kCallAudioCodecs[] = 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++) @@ -109,6 +122,43 @@ int callAudioFixedBitrateKbps(const QString &codec) 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); @@ -124,6 +174,12 @@ CallSettings CallSettings::load() 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; @@ -168,5 +224,10 @@ CallSettings CallSettings::load() 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; } diff --git a/pica-client/callsettings.h b/pica-client/callsettings.h index 81d04f3..fab19c3 100644 --- a/pica-client/callsettings.h +++ b/pica-client/callsettings.h @@ -18,6 +18,9 @@ #define CALLSETTINGS_H #include <QString> +#include <QSize> + +class QWidget; // The parameters an audio/video call is started with: what the settings // dialog's "Call Settings" and "Video Devices" tabs write, and what @@ -82,6 +85,23 @@ enum VideoAcceleration VideoAccelerationDxva2 = 0x10 }; +// Where the largest video we want to receive comes from - the recommended +// resolution sent to the peer in 0x70/0x71, which the peer steps its camera +// down to (see VideoDevice::limitCaptureSize()). One per radio button of +// "Maximum incoming video resolution" on the "Call Settings" tab. +enum IncomingVideoLimit +{ + // The resolution of the screen the call window is on, in physical pixels. + IncomingVideoLimitScreen, + // CallSettings::incomingVideoLimitSize. + IncomingVideoLimitCustom +}; + +// The sizes the custom limit can be set to, largest first. The settings +// dialog lists them after "No limit", which is stored as 0x0. +extern const QSize kIncomingVideoLimitSizes[]; +extern const int kIncomingVideoLimitSizeCount; + struct CallSettings { // FFmpeg codec names, e.g. "h264" and "opus". @@ -105,6 +125,15 @@ struct CallSettings int videoEncodingAcceleration; int videoDecodingAcceleration; + // The largest video we want to receive. incomingVideoLimitSize only means + // something for IncomingVideoLimitCustom, and is 0x0 for no limit. + IncomingVideoLimit incomingVideoLimit; + QSize incomingVideoLimitSize; + // A screen resolution taller than it is wide goes to the peer with its + // width and height swapped, for a screen the user can turn round to show + // landscape video at full size. + bool incomingVideoLandscape; + // Reads the stored settings, filling in the defaults above for anything // that was never set. Must be called from a thread that may touch the // settings database - not from a capture or playback loop, see @@ -138,4 +167,15 @@ int callAudioFixedBitrateKbps(const QString &codec); bool callAudioCodecAccepted(const QString &codec); bool callVideoCodecAccepted(const QString &codec); +// The resolution of the screen `widget` is shown on, in physical pixels rather +// than the device independent ones Qt lays windows out in - with display +// scaling the two differ, and it is the physical ones a video frame fills. +// The primary screen for a widget that is not on one yet. Invalid if there +// is no screen at all. +QSize callScreenResolution(const QWidget *widget); + +// The recommended resolution to send in 0x70/0x71 for a call shown in +// `window`: the largest video we want to receive, 0x0 for no limit. +QSize callRecommendedResolution(const CallSettings &cs, const QWidget *window); + #endif // CALLSETTINGS_H diff --git a/pica-client/dialogs/settingsdialog.cpp b/pica-client/dialogs/settingsdialog.cpp index 661cf5b..1698a97 100644 --- a/pica-client/dialogs/settingsdialog.cpp +++ b/pica-client/dialogs/settingsdialog.cpp @@ -663,8 +663,69 @@ SettingsDialog::SettingsDialog(QWidget *parent) : "deliver them."), this); lbCallHint->setWordWrap(true); + // The other direction: the largest video the peer is asked to send, which + // goes to it as the recommended resolution of 0x70/0x71. + QGroupBox *gbIncomingVideo = new QGroupBox(tr("Maximum incoming video resolution"), this); + gbIncomingVideo->setToolTip(tr("The largest video the other side is asked to send. If its " + "camera is set to a larger picture size, it steps down to the " + "largest one the camera offers that fits - or to the smallest " + "one it has, if none does.")); + + // Parented to the group box, for the same reason as the echo cancellation + // buttons. + const QSize screen = callScreenResolution(this); + + rbIncomingVideoScreen = new QRadioButton(screen.isValid() + ? tr("Limit to my screen resolution (%1x%2)") + .arg(screen.width()).arg(screen.height()) + : tr("Limit to my screen resolution"), + gbIncomingVideo); + rbIncomingVideoScreen->setToolTip(tr("The resolution of the screen the call window opens on, " + "taken when the call starts.")); + + // Only means something for the screen: the custom sizes are all landscape. + cbIncomingVideoLandscape = new QCheckBox(tr("Treat a portrait screen as landscape (I can rotate it)"), + gbIncomingVideo); + cbIncomingVideoLandscape->setToolTip(tr("A screen taller than it is wide is announced with its width " + "and height swapped, e.g. 1080x1920 as 1920x1080, so that " + "landscape video is not reduced to fit the screen's width.")); + cbIncomingVideoLandscape->setEnabled(false); + connect(rbIncomingVideoScreen, SIGNAL(toggled(bool)), cbIncomingVideoLandscape, SLOT(setEnabled(bool))); + + rbIncomingVideoCustom = new QRadioButton(tr("Custom limit:"), gbIncomingVideo); + + incomingVideoLimit = new QComboBox(gbIncomingVideo); + incomingVideoLimit->addItem(tr("No limit"), QSize(0, 0)); + + for (int i = 0; i < kIncomingVideoLimitSizeCount; i++) + incomingVideoLimit->addItem(QString(QLatin1String("%1x%2")) + .arg(kIncomingVideoLimitSizes[i].width()) + .arg(kIncomingVideoLimitSizes[i].height()), + kIncomingVideoLimitSizes[i]); + + incomingVideoLimit->setEnabled(false); + connect(rbIncomingVideoCustom, SIGNAL(toggled(bool)), incomingVideoLimit, SLOT(setEnabled(bool))); + + QHBoxLayout *incomingVideoCustomLayout = new QHBoxLayout(); + incomingVideoCustomLayout->addWidget(rbIncomingVideoCustom); + incomingVideoCustomLayout->addWidget(incomingVideoLimit); + incomingVideoCustomLayout->addStretch(1); + + QVBoxLayout *incomingVideoLayout = new QVBoxLayout(); + // Indented under the screen option it belongs to. + QHBoxLayout *incomingVideoLandscapeLayout = new QHBoxLayout(); + incomingVideoLandscapeLayout->addSpacing(style()->pixelMetric(QStyle::PM_ExclusiveIndicatorWidth) + + style()->pixelMetric(QStyle::PM_RadioButtonLabelSpacing)); + incomingVideoLandscapeLayout->addWidget(cbIncomingVideoLandscape); + + incomingVideoLayout->addWidget(rbIncomingVideoScreen); + incomingVideoLayout->addLayout(incomingVideoLandscapeLayout); + incomingVideoLayout->addLayout(incomingVideoCustomLayout); + gbIncomingVideo->setLayout(incomingVideoLayout); + callLayout->addLayout(callFormLayout); callLayout->addWidget(lbCallHint); + callLayout->addWidget(gbIncomingVideo); callLayout->addStretch(1); calltab->setLayout(callLayout); @@ -2113,6 +2174,18 @@ void SettingsDialog::loadSettings() // actually changes - and the stored codec is often the one already showing. callAudioCodecChanged(); + if (cs.incomingVideoLimit == IncomingVideoLimitCustom) + rbIncomingVideoCustom->setChecked(true); + else + rbIncomingVideoScreen->setChecked(true); + + cbIncomingVideoLandscape->setChecked(cs.incomingVideoLandscape); + + // A size stored by hand that is not in the list shows as no limit rather + // than as whatever happens to be first. + int incomingVideoLimitItem = incomingVideoLimit->findData(cs.incomingVideoLimitSize); + incomingVideoLimit->setCurrentIndex(incomingVideoLimitItem >= 0 ? incomingVideoLimitItem : 0); + #ifdef HAVE_VAAPI cbVaapiEncoding->setChecked(st.loadValue("video.vaapi_encoding", 0).toBool()); cbVaapiDecoding->setChecked(st.loadValue("video.vaapi_decoding", 0).toBool()); @@ -2232,6 +2305,15 @@ void SettingsDialog::storeSettings() audioBitrateKbps = callAudioBitrate->value(); st.storeValue("call.audio_bitrate_kbps", QString::number(audioBitrateKbps)); + + // The custom size is kept while the screen is selected, so that switching + // back finds it as it was. + const QSize incomingVideoLimitSize = incomingVideoLimit->itemData(incomingVideoLimit->currentIndex()).toSize(); + + st.storeValue("call.incoming_video_limit", rbIncomingVideoCustom->isChecked() ? "custom" : "screen"); + st.storeValue("call.incoming_video_limit_width", QString::number(incomingVideoLimitSize.width())); + st.storeValue("call.incoming_video_limit_height", QString::number(incomingVideoLimitSize.height())); + st.storeValue("call.incoming_video_landscape", cbIncomingVideoLandscape->isChecked() ? "1" : "0"); #ifdef HAVE_VAAPI st.storeValue("video.vaapi_encoding", cbVaapiEncoding->isChecked() ? "1" : "0"); st.storeValue("video.vaapi_decoding", cbVaapiDecoding->isChecked() ? "1" : "0"); diff --git a/pica-client/dialogs/settingsdialog.h b/pica-client/dialogs/settingsdialog.h index ab1c7e7..d5b624c 100644 --- a/pica-client/dialogs/settingsdialog.h +++ b/pica-client/dialogs/settingsdialog.h @@ -148,6 +148,15 @@ private: QSpinBox *callVideoBitrate; QSpinBox *callAudioBitrate; + // "Maximum incoming video resolution": the recommended resolution a call + // sends to the peer in 0x70/0x71, see IncomingVideoLimit. The combo box + // carries the size as item data, 0x0 for "No limit". + QRadioButton *rbIncomingVideoScreen; + // see CallSettings::incomingVideoLandscape + QCheckBox *cbIncomingVideoLandscape; + QRadioButton *rbIncomingVideoCustom; + QComboBox *incomingVideoLimit; + // The audio bitrate the user chose, which is not always what // callAudioBitrate shows: selecting a fixed rate codec puts that codec's // rate in the box and disables it, and the chosen value has to survive diff --git a/pica-client/skynet.cpp b/pica-client/skynet.cpp index 1973b9b..2fbe21d 100644 --- a/pica-client/skynet.cpp +++ b/pica-client/skynet.cpp @@ -1014,20 +1014,28 @@ void SkyNet::do_SendMessage(QByteArray to, QString msg) } -void SkyNet::AcceptCall(QByteArray from) +// One dimension of the recommended resolution, as the 0x70/0x71 field holds +// it: anything that does not fit means no limit rather than a wrapped one. +static uint16_t recommended_dimension(int value) +{ + return (value > 0 && value <= 0xFFFF) ? (uint16_t)value : 0; +} + +void SkyNet::AcceptCall(QByteArray from, QSize recommended_resolution) { QMetaObject::invokeMethod(this, "do_AcceptCall", Qt::AutoConnection, - Q_ARG(QByteArray, from)); + Q_ARG(QByteArray, from), Q_ARG(QSize, recommended_resolution)); } -void SkyNet::do_AcceptCall(QByteArray from) +void SkyNet::do_AcceptCall(QByteArray from, QSize recommended_resolution) { int ret = PICA_OK; struct PICA_c2c *iptr; if ((iptr = find_active_chan(from))) { - ret = PICA_pickup_call(iptr); + ret = PICA_pickup_call(iptr, recommended_dimension(recommended_resolution.width()), + recommended_dimension(recommended_resolution.height())); if (ret != PICA_OK) emit CallFailed(from, QString(tr("Failed to pick up the call: %1")).arg(ret)); @@ -1162,13 +1170,13 @@ void SkyNet::do_SendVideoPacket(QByteArray to, quint16 seq_num, quint32 timestam } } -void SkyNet::StartCall(QByteArray to) +void SkyNet::StartCall(QByteArray to, QSize recommended_resolution) { QMetaObject::invokeMethod(this, "do_StartCall", Qt::AutoConnection, - Q_ARG(QByteArray, to)); + Q_ARG(QByteArray, to), Q_ARG(QSize, recommended_resolution)); } -void SkyNet::do_StartCall(QByteArray to) +void SkyNet::do_StartCall(QByteArray to, QSize recommended_resolution) { if (!self_aware) { @@ -1181,7 +1189,8 @@ void SkyNet::do_StartCall(QByteArray to) if ((iptr = find_active_chan(to))) { - ret = PICA_start_call(iptr); + ret = PICA_start_call(iptr, recommended_dimension(recommended_resolution.width()), + recommended_dimension(recommended_resolution.height())); if (ret != PICA_OK) emit CallFailed(to, QString(tr("Failed to initiate the call: %1")).arg(ret)); @@ -1191,6 +1200,7 @@ void SkyNet::do_StartCall(QByteArray to) call_waiting_c2c_connect = true; call_waiting_to = to; + call_waiting_resolution = recommended_resolution; struct PICA_c2c *chan = NULL; @@ -1222,7 +1232,8 @@ void SkyNet::continue_start_call() if ((iptr = find_active_chan(call_waiting_to))) { - ret = PICA_start_call(iptr); + ret = PICA_start_call(iptr, recommended_dimension(call_waiting_resolution.width()), + recommended_dimension(call_waiting_resolution.height())); } if (ret != PICA_OK) @@ -1427,14 +1438,14 @@ void SkyNet::emit_CallFailed(QByteArray peer_id, QString reason) emit CallFailed(peer_id, reason); } -void SkyNet::emit_IncomingCall(QByteArray from) +void SkyNet::emit_IncomingCall(QByteArray from, QSize recommended_resolution) { - emit IncomingCall(from); + emit IncomingCall(from, recommended_resolution); } -void SkyNet::emit_CallAccepted(QByteArray by) +void SkyNet::emit_CallAccepted(QByteArray by, QSize recommended_resolution) { - emit CallAccepted(by); + emit CallAccepted(by, recommended_resolution); } void SkyNet::emit_CallRejected(QByteArray by) @@ -1688,17 +1699,17 @@ void SkyNet::direct_c2c_established_cb(const unsigned char *peer_id, const char skynet->emit_ConnectionStatusUpdated(QByteArray((const char*)peer_id, PICA_ID_SIZE), QString("🔐: %1 direct c2c").arg(ciphersuitename)); } -void SkyNet::incoming_call_cb(const unsigned char *peer_id) +void SkyNet::incoming_call_cb(const unsigned char *peer_id, uint16_t width, uint16_t height) { - skynet->emit_IncomingCall(QByteArray((const char*)peer_id, PICA_ID_SIZE)); + skynet->emit_IncomingCall(QByteArray((const char*)peer_id, PICA_ID_SIZE), QSize(width, height)); } -void SkyNet::call_picked_up_cb(const unsigned char *peer_id) +void SkyNet::call_picked_up_cb(const unsigned char *peer_id, uint16_t width, uint16_t height) { QByteArray peer((const char*)peer_id, PICA_ID_SIZE); skynet->call_active_set(true, peer); - skynet->emit_CallAccepted(peer); + skynet->emit_CallAccepted(peer, QSize(width, height)); } void SkyNet::call_rejected_cb(const unsigned char *peer_id) diff --git a/pica-client/skynet.h b/pica-client/skynet.h index 263167a..b766d28 100644 --- a/pica-client/skynet.h +++ b/pica-client/skynet.h @@ -30,6 +30,7 @@ #include <QVector> #include <QPair> #include <QSet> +#include <QSize> class SkyNet : public QObject @@ -52,8 +53,10 @@ public: void ResumeFile(QByteArray peer_id, bool resume_sending); void CancelFile(QByteArray peer_id, bool cancel_sending); - void StartCall(QByteArray to); - void AcceptCall(QByteArray from); + // recommended_resolution is the largest video we want to receive, sent to + // the peer in 0x70/0x71; a dimension of 0 is not limited. + void StartCall(QByteArray to, QSize recommended_resolution); + void AcceptCall(QByteArray from, QSize recommended_resolution); void RejectCall(QByteArray from); void HangupCall(QByteArray with); @@ -93,8 +96,10 @@ signals: void c2cClosed(QByteArray peer_id); void CallFailed(QByteArray peer_id, QString reason); - void IncomingCall(QByteArray from); - void CallAccepted(QByteArray by); + // recommended_resolution - the largest video the peer wants to receive, + // from its 0x70/0x71; a dimension of 0 is not limited. + void IncomingCall(QByteArray from, QSize recommended_resolution); + void CallAccepted(QByteArray by, QSize recommended_resolution); void CallRejected(QByteArray by); void CallHungup(QByteArray by); @@ -119,6 +124,7 @@ private: bool self_aware; bool call_waiting_c2c_connect; QByteArray call_waiting_to; + QSize call_waiting_resolution; QString status; struct PICA_c2n *active_nodelink; struct PICA_acc *acc; @@ -205,8 +211,8 @@ private: void emit_ConnectionStatusUpdated(QByteArray peer_id, QString status); void emit_CallFailed(QByteArray peer_id, QString reason); - void emit_IncomingCall(QByteArray from); - void emit_CallAccepted(QByteArray by); + void emit_IncomingCall(QByteArray from, QSize recommended_resolution); + void emit_CallAccepted(QByteArray by, QSize recommended_resolution); void emit_CallRejected(QByteArray by); void emit_CallHungup(QByteArray by); @@ -260,9 +266,9 @@ private: static void direct_c2c_established_cb(const unsigned char *peer_id, const char *ciphersuitename); - static void incoming_call_cb(const unsigned char *peer_id); + static void incoming_call_cb(const unsigned char *peer_id, uint16_t width, uint16_t height); - static void call_picked_up_cb(const unsigned char *peer_id); + static void call_picked_up_cb(const unsigned char *peer_id, uint16_t width, uint16_t height); static void call_rejected_cb(const unsigned char *peer_id); @@ -337,8 +343,8 @@ private slots: void do_ResumeFile(QByteArray peer_id, bool resume_sending); void do_CancelFile(QByteArray peer_id, bool cancel_sending); - void do_StartCall(QByteArray to); - void do_AcceptCall(QByteArray from); + void do_StartCall(QByteArray to, QSize recommended_resolution); + void do_AcceptCall(QByteArray from, QSize recommended_resolution); void do_RejectCall(QByteArray from); void do_HangupCall(QByteArray with); diff --git a/pica-client/videodevice.cpp b/pica-client/videodevice.cpp index 2ece29c..f7d080c 100644 --- a/pica-client/videodevice.cpp +++ b/pica-client/videodevice.cpp @@ -729,6 +729,7 @@ QByteArray VideoFrameAssembler::addFragment(quint16 seq_num, quint32 timestamp, VideoDevice::VideoDevice(QObject *parent) : QObject(parent), m_width(kDefaultCaptureWidth), m_height(kDefaultCaptureHeight), + m_maxWidth(0), m_maxHeight(0), m_frameRate(kDefaultCaptureFrameRate), m_bitrate(kDefaultVideoBitrateKbps * 1000), m_preferCompressed(false), m_acceleration(0), m_useVaapiRender(false), m_reconfigure(0), @@ -790,6 +791,9 @@ void VideoDevice::configureCapture(QString deviceName, int width, int height, in m_deviceName = deviceName; m_width = width; m_height = height; + // a limit from the previous call does not carry over to this one + m_maxWidth = 0; + m_maxHeight = 0; m_frameRate = frameRate > 0 ? frameRate : kDefaultCaptureFrameRate; m_codec = codec; m_bitrate = bitrate > 0 ? bitrate : kDefaultVideoBitrateKbps * 1000; @@ -797,6 +801,12 @@ void VideoDevice::configureCapture(QString deviceName, int width, int height, in m_acceleration = acceleration; } +void VideoDevice::limitCaptureSize(int width, int height) +{ + m_maxWidth = qMax(0, width); + m_maxHeight = qMax(0, height); +} + void VideoDevice::configurePlayback(QString codec, int width, int height, int acceleration, bool useVaapiRender, QByteArray caller_id, QByteArray callee_id) @@ -960,7 +970,11 @@ static bool formatHasRate(const VideoPixelFormat &format, int frameRate) // separate claims and cameras routinely honour the first without the others. // Whatever eventually opens is what gets announced to the peer, so relaxing // these changes what is sent, not what is claimed. -static QList<CaptureAttempt> buildCaptureAttempts(const QString &device, int width, int height, +// +// formats is what VideoDevice::CaptureFormats() reported for the device. +static QList<CaptureAttempt> buildCaptureAttempts(const QString &device, + const QList<VideoCaptureFormat> &formats, + int width, int height, int frameRate, bool preferCompressed, const QString &configuredCodec) { @@ -989,8 +1003,6 @@ static QList<CaptureAttempt> buildCaptureAttempts(const QString &device, int wid atSize.width = 0; atSize.height = 0; - const QList<VideoCaptureFormat> formats = VideoDevice::CaptureFormats(device); - for (int i = 0; i < formats.size(); i++) { if (formats.at(i).width == width && formats.at(i).height == height) @@ -1088,6 +1100,49 @@ static QList<CaptureAttempt> buildCaptureAttempts(const QString &device, int wid return attempts; } +// Whether a picture size is within the peer's recommended resolution, 0 +// leaving a dimension unlimited. +static bool sizeWithinLimit(const QSize &size, const QSize &limit) +{ + return (limit.width() <= 0 || size.width() <= limit.width()) && + (limit.height() <= 0 || size.height() <= limit.height()); +} + +// The size to capture at: the configured one if it is within the limit, +// otherwise the next smaller one the camera offers that is, stepping down by +// area. A camera that offers nothing within the limit still sends video, at +// the smallest size it has - or the configured one if it has nothing smaller, +// a camera with a single size being the usual case. A camera that could not +// be enumerated keeps the configured size, there being nothing to step to. +static QSize captureSizeWithin(const QList<VideoCaptureFormat> &formats, + const QSize &configured, const QSize &limit) +{ + if (sizeWithinLimit(configured, limit)) + return configured; + + const qint64 configuredArea = (qint64)configured.width() * configured.height(); + QSize best; + QSize smallest = configured; + + for (int i = 0; i < formats.size(); i++) + { + const QSize size(formats.at(i).width, formats.at(i).height); + const qint64 area = (qint64)size.width() * size.height(); + + if (area >= configuredArea) + continue; + + if (area < (qint64)smallest.width() * smallest.height()) + smallest = size; + + if (sizeWithinLimit(size, limit) && + (!best.isValid() || area > (qint64)best.width() * best.height())) + best = size; + } + + return best.isValid() ? best : smallest; +} + // One attempt at opening the camera. // // inputFormat empty asks for whatever the camera gives; a width of zero @@ -1214,11 +1269,30 @@ void VideoDevice::Capture() return; } + const QList<VideoCaptureFormat> formats = VideoDevice::CaptureFormats(m_deviceName); + + // Brought down to what the peer wants to receive before anything is asked + // of the camera, so that the size below is the one every attempt is made + // at and the one the encoder is opened for. + const QSize size = captureSizeWithin(formats, QSize(m_width, m_height), + QSize(m_maxWidth, m_maxHeight)); + + if (size != QSize(m_width, m_height)) + { + qDebug() << QString("Camera '%1': %2x%3 exceeds the peer's recommended %4x%5, capturing at %6x%7") + .arg(m_deviceName).arg(m_width).arg(m_height) + .arg(m_maxWidth).arg(m_maxHeight) + .arg(size.width()).arg(size.height()); + + m_width = size.width(); + m_height = size.height(); + } + // Which formats to try, in which order, and what to do with each - worked // out from what the camera said it can deliver at this size and rate. See // buildCaptureAttempts(). const QList<CaptureAttempt> attempts = - buildCaptureAttempts(m_deviceName, m_width, m_height, m_frameRate, + buildCaptureAttempts(m_deviceName, formats, m_width, m_height, m_frameRate, m_preferCompressed, m_codec); QString passthroughCodec; diff --git a/pica-client/videodevice.h b/pica-client/videodevice.h index 4b9780a..14a906a 100644 --- a/pica-client/videodevice.h +++ b/pica-client/videodevice.h @@ -185,6 +185,14 @@ public: int acceleration, QByteArray caller_id, QByteArray callee_id); + // The largest picture the peer wants to receive - the recommended + // resolution of its 0x70/0x71 message - with 0 leaving a dimension + // unlimited. When the size given to configureCapture() exceeds it, + // Capture() steps down through the camera's smaller sizes to the largest + // one that fits, and with none fitting takes the smallest of them, see + // captureSizeWithin(). Queued after configureCapture(), which clears it. + Q_INVOKABLE void limitCaptureSize(int width, int height); + // Allows Capture()/Play() to run for the call this pair of ids names, // until Close() - the same arrangement as AudioDevice::openSession(). To // be called directly, before the calls that configure and start the @@ -353,6 +361,9 @@ private: QString m_codec; int m_width; int m_height; + // see limitCaptureSize() + int m_maxWidth; + int m_maxHeight; int m_frameRate; int m_bitrate; bool m_preferCompressed; |
