Repository navigation
Bundle the publisher offer with the /rtc/v1 join request - #1070
Conversation
In single peer connection mode the client is the only offerer, so the publisher, its three data channels and the first offer are built before the signal socket opens and the offer travels in JoinRequest.publisher_offer. The server answers it over the signal channel right after the JoinResponse, which removes one offer/answer round trip from the connect path. PeerConnectionTransport.createInitialOffer creates and munges the offer but does not apply it: setLocalDescription would start ICE gathering before the server's ICE servers are known. The offer stays pending and setRemoteDescription applies it just before the answer. While it is pending, negotiate queues a renegotiation the same way as for an offer already in flight. Offer creation and munging are shared with createAndSendOffer through createMungedOffer. RTCEngine builds the early publisher with a client-side configuration (makeRTCConfig with no server response) and configure adopts it, installing the server's ICE servers and relay policy through updateRTCConfig before the pending offer can be applied. The eager negotiate on join is skipped while the initial offer is pending. Not yet handled: closing the early publisher when the join fails, and the /rtc/v1 404 fallback, which still adopts it. The two fallback reconnect tests in SinglePeerConnectionFallbackMockE2ETest fail until then.
The publisher created ahead of a single peer connection join lived in locals of joinImpl, so a join that failed before configure ran left it with no owner, and the /rtc/v1 404 fallback handed the already-closed publisher to configure, which adopted it into the dual peer connection transport. RTCEngine now holds the early publisher and its observer in fields. configure adopts them into the transport and clears the fields once the data channels are in place; closeResources closes a publisher that was never adopted, which covers a failed or cancelled join through the engine's normal teardown. The 404 fallback calls closeEarlyPublisher before the legacy join, so that join starts from nothing. The helper also disposes the publisher's data channels, and closeResources reuses it for that side of the teardown. The two SinglePeerConnectionFallbackMockE2ETest reconnect tests pass again.
SinglePeerConnectionMockE2ETest answered the last offer sent over the signal connection after joining. With the publisher offer bundled in the /rtc/v1 join request there is no such offer, so every test that connects failed on an empty list. The connect helper now decodes the offer from the join request URL and answers that one by id. publisherOfferRidesInTheJoinRequest checks that the offer travels with the join request and that nothing is offered over the signal connection before or after its answer. The detekt baseline entry for makeRTCConfig takes the nullable server response the function has had since the early publisher was added.
createInitialOffer bundled the munged description as is. The ordinary offer path sends whatever setMungedSdp managed to apply, so an empty munge falls back to the offer libwebrtc created, but the initial offer is sent before anything is applied and had no such step: an empty munge went to the server as an empty publisher offer. createInitialOffer now bundles the unmunged description in that case. The pending offer is the bundled one, so what the server answers is what the answer path sets locally. In the mock E2E suite createOffer returns a placeholder that munges to nothing, so publisherOfferRidesInTheJoinRequest can now also check that the bundled SDP is not empty and becomes the local description once it is answered.
🦋 Changeset detectedLatest commit: 1fa3830 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Diffuse output: AARJAR |
RTCEngine counts as closed until a join response arrives, and close returns early in that state. The early publisher is created before the response, so a rejected or cancelled first join left it in its field with nothing to dispose it: Room's close after the failure did nothing, and a retry against another region replaced the field and dropped the only reference to the first peer connection. close now disposes an unadopted early publisher even when the engine is already closed, and joinImpl disposes one that an earlier join left behind before creating the next. SinglePeerConnectionFailedJoinMockE2ETest covers both paths. MockPeerConnectionFactory records the peer connections it creates so the tests can reach the early publisher.
| return@launchRTCIfNotClosed Either.Right("Old offer, ignoring. Expected: ${latestOfferId.get()}, actual: $offerId") | ||
| } | ||
| initialOffer?.let { | ||
| peerConnection.setLocalDescription(it.sessionDescription) |
There was a problem hiding this comment.
[P2] Handle rejection of the pending local offer before applying its answer
The result of setLocalDescription() is discarded. If libwebrtc rejects the bundled local offer, we still attempt the remote answer while signaling remains stable, then clear initialOffer. This loses the original error and prevents a later answer from applying the saved offer.
Please check the local result and handle failure explicitly through the join/reconnect failure path. Falling back to different SDP here would be unsafe because the server already answered the bundled bytes.
I reproduced this with native setter failure injection: the remote setter was called despite local rejection, the returned error was the remote error, and the pending offer was cleared. Please add regression coverage for this path.
xianshijing-lk
left a comment
There was a problem hiding this comment.
one comment to a corner case, please address it before merging the PR
| * Drops the offer kept by [createInitialOffer], when the join it was bundled with did not | ||
| * succeed, so [negotiate] offers normally again. | ||
| */ | ||
| fun clearPendingInitialOffer() { |
There was a problem hiding this comment.
This doesn't seem to be called anywhere?
| var effectiveOptions = options | ||
| if (effectiveOptions.singlePeerConnection) { | ||
| if (earlyPublisher != null) { | ||
| closeEarlyPublisher() |
There was a problem hiding this comment.
nit: this cleanup should be moved outside, since it's possible for the same room object to go from a single peer connection join to be later reused on a double peer connection join.
setRemoteDescription discarded the result of setting the pending initial offer as the local description. When libwebrtc rejected the offer, the answer was still applied in the stable state, failed with an unrelated error, and cleared the pending offer, leaving the room connecting with nothing to recover it. The answer is now not applied when the offer is rejected, the error names the offer, and the offer stays pending. No other description can stand in for it, because the server answered the bytes that were bundled with the join request. RTCEngine treats a failed answer with the offer still pending as a peer connection that cannot be negotiated and starts a full reconnect, which rejoins with a new one. MockPeerConnection.localDescriptionError injects the rejection for the transport and E2E tests.
Nothing in the SDK called it. A join that fails with a bundled offer closes the early publisher, which drops the pending offer with it.
joinImpl closed the early publisher of an earlier join only when the new join was also single peer connection. A dual peer connection join on the same engine would have created an initial offer on the leftover publisher, and configure would have adopted it as the publisher. The cleanup now runs before the single peer connection check.
When a track callback arrives before the participant update that announces its publication, addSubscribedMediaTrack retries every 150 ms until the publication exists. The participant update also attaches the bound track right away, and the pending retry then attached it a second time: a second RemoteTrack around the same rtcTrack, a second TrackSubscribed, and the first track never stopped. addSubscribedMediaTrack now returns when the publication already holds that rtcTrack. Every attach goes through it, the retry included, so the same check in Room.attachTrack is gone.
PeerConnectionTransport conflicted with the Opus NACK change (#1072), which edited the offer munge inline in createAndSendOffer and added setAnswerWithOpusNack next to it. On this branch the munge lives in createMungedOffer, so the Opus NACK step for audio sections moves there, and both functions are kept. The single peer connection publisher is created in joinImpl on this branch, ahead of the join, so that call now passes singlePeerConnection = true. Without it the publisher's offers would not carry Opus NACK. audioOffersAdvertiseOpusNack covers this through the engine.
In single peer connection mode the client makes every offer, so the first one no longer waits for the join to finish. The publisher peer connection, its three data channels and the initial offer are created before the signal socket opens, and the offer travels in
JoinRequest.publisher_offer. The server answers it over the signal channel right after theJoinResponse, which removes one offer/answer round trip from connecting. Same approach as livekit/client-sdk-js#1846, livekit/rust-sdks#996 and livekit/client-sdk-swift#1111.Follows #1066. Only joins with
ConnectOptions.singlePeerConnectionare affected.PeerConnectionTransportcreateInitialOffercreates and munges the offer but does not apply it:setLocalDescriptionwould start ICE gathering before the server's ICE servers are known. The offer stays pending, andsetRemoteDescriptionapplies it as the local description just before the answer.negotiatequeues a renegotiation, the same way it does for an offer already in flight. A publish that races the join therefore waits for the answer instead of offering over the bundled offer.createAndSendOfferthroughcreateMungedOffer.RTCEngineconfigureadopts them into the transport and installs the server's ICE servers and relay policy throughupdateRTCConfig.SignalClientholds responses untilconfigureis done, so the answer is applied after that.closedisposes it even when the join ended before its response, a point at which the engine still counts as closed. A new join disposes one left by an earlier attempt, such as a region retry.closeResourcesdisposes it on teardown and before a full reconnect's join./rtc/v1404 fallback the early publisher and its data channels are closed before the legacy join, so that join starts from nothing.makeRTCConfigaccepts a missing server response, for the configuration the early publisher starts with.Signal
SignalClient.jointakes an optionalpublisherOfferand passes it toSignalUrlBuilder.buildJoinRequestUrl.SinglePeerConnectionTransportskips the eager negotiate on join while the initial offer is pending.Not included
Tests
PeerConnectionTransportTest(new) covers the initial offer: offer id, constraints, munging, the empty-munge case, not being applied or sent, being applied before the answer, and queued negotiation.SinglePeerConnectionMockE2ETestnow answers the offer decoded from the join request and checks that nothing is offered over the signal connection.SinglePeerConnectionFallbackMockE2ETestcovers the 404 fallback and the reconnects after it.SinglePeerConnectionFailedJoinMockE2ETest(new) covers a rejected join and a join that follows a rejected one: in both, the earlier publisher is closed.Not yet run on a device against a live server.