mirror of
https://github.com/TelegramMessenger/tgcalls.git
synced 2026-11-18 21:35:28 +00:00
v2: fold the initial camera into the first offer; defer capture renegotiation
CallCoreHost re-applied descriptor.videoCapture after the core's start had set _didBeginNegotiation, and the core's video_capture handler offered unconditionally. In every video call the caller issued a second CreateOffer while the first was in flight (SetLocalDescription failed) and the callee sent an unsolicited offer; the caller applied the colliding offer, rolled its first offer back, and WebRTC's orphaned sctp_mid wedged the call in an endless offer/answer loop (one round per signaling RTT, video send stream stopped twice and audio channel flapped per round). Production: 32 of 39 v19 video calls in the 2026-09-17 not_inited sample, and the arm's shorter call durations in the A/B. - host passes config.hasInitialVideoCapture; the core adds video0 in start, before the initial offer (stock ordering: InstanceV2ReferenceImpl.cpp:843 precedes beginSignaling at :872), and the host re-applies the capture only if the core did not add video0. - core video_capture handler and native setVideoCapture go through requestRenegotiation(): offer only from a quiet stable state, otherwise defer to flushPendingRenegotiation() after each SLD/SRD. - tools/cli: --video now attaches a capture to both p2p descriptors, the shape that reproduces the loop (19/19: 96 SLD in 8 s -> 4 after). Needs the webrtc seam "release an unnegotiated sctp_mid on rollback" as the backstop for any future glare. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
2c94eb6d1d
commit
a6ae18bc08
7 files changed
+181
-16
No files matched your search
@@ -1117,12 +1117,50 @@ public:
|
||||
strong->doSendLocalDescription();
|
||||
|
||||
strong->maybeCommitPendingIceCandidates();
|
||||
|
||||
// An answer just took us back to stable; after an offer this is
|
||||
// a no-op until its answer lands.
|
||||
strong->flushPendingRenegotiation();
|
||||
});
|
||||
}));
|
||||
RTC_LOG(LS_INFO) << "Calling SetLocalDescription";
|
||||
_peerConnection->SetLocalDescription(observer);
|
||||
}
|
||||
|
||||
void requestRenegotiation() {
|
||||
// A capture change wants a new offer, but only from a quiet stable
|
||||
// state: never while our own offer is in flight, never while we owe or
|
||||
// await an answer, and on the callee never before the caller's first
|
||||
// offer has arrived - an unsolicited callee offer collides with it and
|
||||
// rolls it back, and a rollback of the very first offer orphans the
|
||||
// data-channel mid in WebRTC (an endless offer/answer loop; this is
|
||||
// what wedged every 18/19 video call). Otherwise remember it and send
|
||||
// once the exchange settles, rather than trusting WebRTC to re-raise
|
||||
// OnRenegotiationNeeded past its negotiation-needed latch.
|
||||
if (!_didBeginNegotiation) {
|
||||
return;
|
||||
}
|
||||
_pendingRenegotiation = true;
|
||||
flushPendingRenegotiation();
|
||||
}
|
||||
|
||||
void flushPendingRenegotiation() {
|
||||
if (!_pendingRenegotiation) {
|
||||
return;
|
||||
}
|
||||
if (_isMakingOffer || _isSettingRemoteAnswerPending) {
|
||||
return;
|
||||
}
|
||||
if (_peerConnection->signaling_state() != webrtc::PeerConnectionInterface::SignalingState::kStable) {
|
||||
return;
|
||||
}
|
||||
if (!_encryptionKey.isOutgoing && !_peerConnection->remote_description()) {
|
||||
return;
|
||||
}
|
||||
_pendingRenegotiation = false;
|
||||
sendLocalDescription();
|
||||
}
|
||||
|
||||
void sendIceCandidate(const webrtc::IceCandidateInterface *iceCandidate) {
|
||||
std::string sdp;
|
||||
iceCandidate->ToString(&sdp);
|
||||
@@ -1384,6 +1422,9 @@ public:
|
||||
if (type == "offer") {
|
||||
strong->sendLocalDescription();
|
||||
}
|
||||
// After an offer we are now making the answer (no-op); after an
|
||||
// answer we are stable and a deferred capture change may go out.
|
||||
strong->flushPendingRenegotiation();
|
||||
});
|
||||
}));
|
||||
RTC_LOG(LS_INFO) << "Calling SetRemoteDescription";
|
||||
@@ -1611,7 +1652,7 @@ public:
|
||||
|
||||
if (_didBeginNegotiation) {
|
||||
sendMediaState();
|
||||
sendLocalDescription();
|
||||
requestRenegotiation();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1863,6 +1904,9 @@ private:
|
||||
bool _didBeginNegotiation = false;
|
||||
bool _isMakingOffer = false;
|
||||
bool _isSettingRemoteAnswerPending = false;
|
||||
// setVideoCapture() asked for a new offer while one was in flight, an
|
||||
// answer was pending, or (callee) the first remote offer had not arrived.
|
||||
bool _pendingRenegotiation = false;
|
||||
bool _isPerformingConfiguration = false;
|
||||
bool _useAddTrack = false;
|
||||
bool _useMtProto = false;
|
||||
|
||||
@@ -99,6 +99,28 @@ the `exp0` ping/pong demo).
|
||||
|
||||
## Invariants & gotchas
|
||||
|
||||
- **The initial camera must be part of the first offer, never a second one.**
|
||||
Stock `start()` applies `descriptor.videoCapture` (InstanceV2ReferenceImpl.cpp:843)
|
||||
*before* `beginSignaling()` (:872), so a video call's first offer already
|
||||
carries video. The host cannot reproduce that ordering — the core sets
|
||||
`_didBeginNegotiation` inside its own start — so it passes
|
||||
`config.hasInitialVideoCapture` and the core adds `video0` in start, ahead of
|
||||
the initial `requestSetLocalDescription()`; the host then re-applies the
|
||||
capture after create only if the core did NOT add `video0` (an external
|
||||
module ignoring the key). Until 2026-09-17 the host re-applied it
|
||||
unconditionally and the core's `video_capture` handler offered
|
||||
unconditionally: the caller issued a second `CreateOffer` while the first was
|
||||
in flight (`SetLocalDescription failed`), the callee sent an unsolicited offer
|
||||
(glare), the caller rolled its first offer back, and WebRTC's orphaned
|
||||
`sctp_mid` wedged the call in an endless offer/answer loop — every 18/19
|
||||
video call in production. The handler now goes through
|
||||
`requestRenegotiation()`: a capture change offers only from a quiet stable
|
||||
state (no offer in flight, no answer pending, callee only after the first
|
||||
remote offer) and is otherwise deferred to `flushPendingRenegotiation()`,
|
||||
which runs after each `pc_set_local_done` / `pc_set_remote_done`. Native got
|
||||
the same deferral. Verify: `tgcalls_cli --mode p2p --version 19.0.0
|
||||
--version2 19.0.0 --video` → 4 `SetLocalDescription` per call, none failed.
|
||||
|
||||
- **Wire parity is frozen.** Signaling JSON keys/values are copied verbatim
|
||||
from stock (`InstanceV2ReferenceImpl.cpp` / `Signaling.cpp`); both sides use
|
||||
json11 (std::map-backed → key-sorted dump), so identical keys ⇒ identical
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
// ≲10 Hz / KB-scale envelope (enforcement: Phase-4 metering).
|
||||
//
|
||||
// Config (tgcalls_core_create):
|
||||
// { "abiVersion": 1, "isOutgoing": bool,
|
||||
// { "abiVersion": 1, "isOutgoing": bool, "hasInitialVideoCapture": bool,
|
||||
// "enableP2P": bool, "customParameters": string,
|
||||
// "rtcServers": [ { "host": s, "port": n, "login": s, "password": s,
|
||||
// "isTurn": bool, "isTcp": bool } ] }
|
||||
|
||||
@@ -633,9 +633,17 @@ void CallCoreHost::start() {
|
||||
{"isTcp", server.isTcp},
|
||||
});
|
||||
}
|
||||
// Stock start() applies descriptor.videoCapture before beginSignaling(), so
|
||||
// the camera is part of the first offer; the core reproduces that from this
|
||||
// flag (see the post-create check below).
|
||||
bool hasInitialVideoCapture = false;
|
||||
if (const auto captureImpl = GetVideoCaptureAssumingSameThread(_videoCapture.get())) {
|
||||
hasInitialVideoCapture = !captureImpl->isScreenCapture();
|
||||
}
|
||||
const std::string configJson = json11::Json(json11::Json::object{
|
||||
{"abiVersion", 1},
|
||||
{"isOutgoing", _encryptionKey.isOutgoing},
|
||||
{"hasInitialVideoCapture", hasInitialVideoCapture},
|
||||
{"enableP2P", _enableP2P},
|
||||
{"customParameters", _customParameters},
|
||||
{"rtcServers", std::move(rtcServers)},
|
||||
@@ -686,10 +694,15 @@ void CallCoreHost::start() {
|
||||
}
|
||||
processPendingCommands();
|
||||
|
||||
if (_videoCapture) {
|
||||
// Stock start() re-applies descriptor.videoCapture (InstanceV2ReferenceImpl.cpp:726-728).
|
||||
// The core has already set _didBeginNegotiation, so this also triggers the
|
||||
// MediaState + renegotiation it would have folded into initial negotiation.
|
||||
if (_videoCapture && _coreTransceivers.find("video0") == _coreTransceivers.end()) {
|
||||
// A core that honours config.hasInitialVideoCapture has already added
|
||||
// video0 inside its start, before _didBeginNegotiation - where stock
|
||||
// start() applies descriptor.videoCapture (InstanceV2ReferenceImpl.cpp:843,
|
||||
// ahead of beginSignaling() at :872). Re-applying it here landed after
|
||||
// the flag and cost a second offer on the caller plus an unsolicited
|
||||
// one on the callee: the start glare that wedged every 18/19 video
|
||||
// call in an offer/answer loop. Only a core that ignored the key (an
|
||||
// external module) still needs the stock re-apply.
|
||||
setVideoCapture(_videoCapture);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,6 +103,20 @@ _emit(std::move(emit)) {
|
||||
{"sendEncodings", json11::Json::array{ json11::Json::object{ {"maxBitrateBps", kAudioMaxBitrateBps} } }},
|
||||
});
|
||||
|
||||
// Parity with stock start(): setVideoCapture(_videoCapture) runs at
|
||||
// InstanceV2ReferenceImpl.cpp:843, BEFORE beginSignaling() at :872, so an
|
||||
// initial camera folds into the first offer. The host reports it here
|
||||
// instead of re-applying the capture after create; re-applying would land
|
||||
// after _didBeginNegotiation and cost a second offer on the caller plus an
|
||||
// unsolicited one on the callee - the start glare whose rollback orphaned
|
||||
// the data-channel mid and wedged 18/19 video calls in an endless
|
||||
// offer/answer loop (analysis/FINDINGS-v19-video-loop.md in getlogstgcalls).
|
||||
if (config["hasInitialVideoCapture"].bool_value()) {
|
||||
emitAddVideoTransceiver();
|
||||
_hasVideoCapture = true;
|
||||
_hasVideoTrack = true;
|
||||
}
|
||||
|
||||
// stock beginSignaling()
|
||||
_didBeginNegotiation = true;
|
||||
if (_isOutgoing) {
|
||||
@@ -211,6 +225,11 @@ void ReferenceCallCore::onEvent(json11::Json const &event) {
|
||||
emitLog("SetLocalDescription failed");
|
||||
}
|
||||
flushPendingRemoteCandidates();
|
||||
if (event["ok"].bool_value()) {
|
||||
// An answer just took us back to stable; an offer leaves us in
|
||||
// have-local-offer and the flush is a no-op until its answer lands.
|
||||
flushPendingRenegotiation();
|
||||
}
|
||||
} else if (type == "pc_set_remote_done") {
|
||||
_isSettingRemoteAnswerPending = false;
|
||||
if (event["ok"].bool_value()) {
|
||||
@@ -219,6 +238,9 @@ void ReferenceCallCore::onEvent(json11::Json const &event) {
|
||||
if (stringField(event, "sdpType") == "offer") {
|
||||
requestSetLocalDescription();
|
||||
}
|
||||
// After an offer we are now making the answer (no-op here); after
|
||||
// an answer we are stable and a deferred capture change may go out.
|
||||
flushPendingRenegotiation();
|
||||
} else {
|
||||
emitLog("SetRemoteDescription failed");
|
||||
}
|
||||
@@ -304,20 +326,12 @@ void ReferenceCallCore::onEvent(json11::Json const &event) {
|
||||
}
|
||||
_hasVideoCapture = event["active"].bool_value() && !event["screencast"].bool_value();
|
||||
if (_hasVideoCapture) {
|
||||
emit({
|
||||
{"@type", "pc_add_transceiver"},
|
||||
{"id", "video0"},
|
||||
{"kind", "video"},
|
||||
{"direction", "sendrecv"},
|
||||
{"trackSource", "camera"},
|
||||
{"codecPreferences", json11::Json::array{ "H265", "H264" }},
|
||||
{"sendEncodings", json11::Json::array{ json11::Json::object{ {"maxBitrateBps", kVideoMaxBitrateBps} } }},
|
||||
});
|
||||
emitAddVideoTransceiver();
|
||||
_hasVideoTrack = true;
|
||||
}
|
||||
if (_didBeginNegotiation) {
|
||||
sendMediaState();
|
||||
requestSetLocalDescription();
|
||||
requestRenegotiation();
|
||||
}
|
||||
} else if (type == "pc_track") {
|
||||
if (stringField(event, "kind") == "video") {
|
||||
@@ -349,6 +363,51 @@ void ReferenceCallCore::requestSetLocalDescription() {
|
||||
emit({ {"@type", asAnswer ? "pc_create_answer" : "pc_create_offer"} });
|
||||
}
|
||||
|
||||
void ReferenceCallCore::emitAddVideoTransceiver() {
|
||||
emit({
|
||||
{"@type", "pc_add_transceiver"},
|
||||
{"id", "video0"},
|
||||
{"kind", "video"},
|
||||
{"direction", "sendrecv"},
|
||||
{"trackSource", "camera"},
|
||||
{"codecPreferences", json11::Json::array{ "H265", "H264" }},
|
||||
{"sendEncodings", json11::Json::array{ json11::Json::object{ {"maxBitrateBps", kVideoMaxBitrateBps} } }},
|
||||
});
|
||||
}
|
||||
|
||||
void ReferenceCallCore::requestRenegotiation() {
|
||||
// A capture change wants a new offer, but only from a quiet stable state:
|
||||
// never while our own offer is in flight (that was the caller's second
|
||||
// CreateOffer in the 18/19 start-glare loop), never while we owe or await
|
||||
// an answer, and on the callee never before the caller's first offer has
|
||||
// arrived (an unsolicited callee offer collides with it and rolls it
|
||||
// back). Otherwise remember it and send once the exchange settles. Stock
|
||||
// sends unconditionally and leans on WebRTC re-raising
|
||||
// OnRenegotiationNeeded, which its negotiation-needed latch can swallow.
|
||||
if (!_didBeginNegotiation) {
|
||||
return;
|
||||
}
|
||||
_pendingRenegotiation = true;
|
||||
flushPendingRenegotiation();
|
||||
}
|
||||
|
||||
void ReferenceCallCore::flushPendingRenegotiation() {
|
||||
if (!_pendingRenegotiation) {
|
||||
return;
|
||||
}
|
||||
if (_isMakingOffer || _isSettingRemoteAnswerPending) {
|
||||
return;
|
||||
}
|
||||
if (_signalingState != "stable") {
|
||||
return;
|
||||
}
|
||||
if (!_isOutgoing && !_haveRemoteDescription) {
|
||||
return;
|
||||
}
|
||||
_pendingRenegotiation = false;
|
||||
requestSetLocalDescription();
|
||||
}
|
||||
|
||||
void ReferenceCallCore::sendSignalingMessage(json11::Json::object &&message) {
|
||||
mungeOutgoingSignalingMessage(message);
|
||||
const std::string data = json11::Json(std::move(message)).dump();
|
||||
|
||||
@@ -55,6 +55,9 @@ protected:
|
||||
void emit(json11::Json::object &&command);
|
||||
void emitLog(std::string const &message);
|
||||
void requestSetLocalDescription();
|
||||
void emitAddVideoTransceiver();
|
||||
void requestRenegotiation();
|
||||
void flushPendingRenegotiation();
|
||||
void handleSignalingData(std::string const &data);
|
||||
void handleRemoteSdp(std::string const &type, std::string const &sdp);
|
||||
void handleMediaStateMessage(json11::Json const &message);
|
||||
@@ -80,6 +83,9 @@ protected:
|
||||
bool _didBeginNegotiation = false;
|
||||
bool _isMakingOffer = false;
|
||||
bool _isSettingRemoteAnswerPending = false;
|
||||
// A capture change asked for a new offer while one was in flight, an
|
||||
// answer was pending, or (callee) the first remote offer had not arrived.
|
||||
bool _pendingRenegotiation = false;
|
||||
bool _haveLocalDescription = false;
|
||||
bool _haveRemoteDescription = false;
|
||||
std::string _signalingState = "stable";
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "Instance.h"
|
||||
#include "FakeAudioDeviceModule.h"
|
||||
#include "VideoCaptureInterface.h"
|
||||
#include "StaticThreads.h"
|
||||
#include "v2/InstanceV2Impl.h"
|
||||
#include "v2/InstanceV2CompatImpl.h"
|
||||
#include "v2/InstanceV2ReferenceImpl.h"
|
||||
@@ -475,6 +476,18 @@ int main(int argc, char* argv[]) {
|
||||
std::string callerStatsPath = "/tmp/tgcalls_cli_caller_" + std::to_string(getpid()) + ".json";
|
||||
std::string calleeStatsPath = "/tmp/tgcalls_cli_callee_" + std::to_string(getpid()) + ".json";
|
||||
|
||||
// --video in p2p/reflector mode: both sides start with the camera already on,
|
||||
// the app's descriptor.videoCapture path (the join-time capture in group mode).
|
||||
// This is the shape that exposed the 18/19 start-glare renegotiation loop.
|
||||
std::shared_ptr<tgcalls::VideoCaptureInterface> callerVideoCapture;
|
||||
std::shared_ptr<tgcalls::VideoCaptureInterface> calleeVideoCapture;
|
||||
if (enableVideo) {
|
||||
auto threads = tgcalls::StaticThreads::getThreads();
|
||||
callerVideoCapture = tgcalls::VideoCaptureInterface::Create(threads, "caller");
|
||||
calleeVideoCapture = tgcalls::VideoCaptureInterface::Create(threads, "callee");
|
||||
logMsg("Main", "video capture attached to both descriptors");
|
||||
}
|
||||
|
||||
// --- Caller descriptor ---
|
||||
auto callerDesc = (tgcalls::Descriptor){
|
||||
.version = version,
|
||||
@@ -492,6 +505,7 @@ int main(int argc, char* argv[]) {
|
||||
? std::vector<tgcalls::RtcServer>{makeReflectorServer(reflectorHost, reflectorPort, callerPeerTag)}
|
||||
: std::vector<tgcalls::RtcServer>{},
|
||||
.encryptionKey = tgcalls::EncryptionKey(keyData, true),
|
||||
.videoCapture = callerVideoCapture,
|
||||
.stateUpdated = [callState](tgcalls::State state) {
|
||||
logMsg("Caller", "state -> %s", stateName(state));
|
||||
std::lock_guard<std::mutex> lock(callState->mutex);
|
||||
@@ -528,6 +542,7 @@ int main(int argc, char* argv[]) {
|
||||
? std::vector<tgcalls::RtcServer>{makeReflectorServer(reflectorHost, reflectorPort, calleePeerTag)}
|
||||
: std::vector<tgcalls::RtcServer>{},
|
||||
.encryptionKey = tgcalls::EncryptionKey(keyData, false),
|
||||
.videoCapture = calleeVideoCapture,
|
||||
.stateUpdated = [callState](tgcalls::State state) {
|
||||
logMsg("Callee", "state -> %s", stateName(state));
|
||||
std::lock_guard<std::mutex> lock(callState->mutex);
|
||||
@@ -593,6 +608,12 @@ int main(int argc, char* argv[]) {
|
||||
};
|
||||
};
|
||||
|
||||
if (callerVideoCapture) {
|
||||
callerVideoCapture->setState(tgcalls::VideoState::Inactive);
|
||||
}
|
||||
if (calleeVideoCapture) {
|
||||
calleeVideoCapture->setState(tgcalls::VideoState::Inactive);
|
||||
}
|
||||
callerInstance->stop(onStopped("Caller"));
|
||||
calleeInstance->stop(onStopped("Callee"));
|
||||
|
||||
|
||||
Reference in new issue
Block a user