Skip to content

Commit

Permalink
Fix pending tracks logic for incoming and outgoing
Browse files Browse the repository at this point in the history
  • Loading branch information
singpolyma committed Dec 8, 2024
1 parent 1028cd3 commit 4003725
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
21 changes: 13 additions & 8 deletions snikket/jingle/PeerConnection.cpp.hx
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,8 @@ class PeerConnection {
final stateChangeListeners = [];
final mainLoop: sys.thread.EventLoop;
var hasLocal = false;
var pendingTracks = [];
var hasRemote = false;
var pendingTracks: Array<MediaStreamTrack> = [];

public function new(?configuration : Configuration, ?constraints : Dynamic) {
if (Sys.getEnv("SNIKKET_WEBRTC_DEBUG") != null) {
Expand Down Expand Up @@ -812,6 +813,7 @@ class PeerConnection {
private function onLocalDescription() {
untyped __cpp__("int base = 0; hx::SetTopOfStack(&base, true);"); // allow running haxe code on foreign thread
mainLoop.run(() -> {
addPendingTracks();
if (waitingOnLocal != null) waitingOnLocal(null);
waitingOnLocal = null;
});
Expand All @@ -821,13 +823,6 @@ class PeerConnection {
private function onLocalCandidate(candidate: Candidate) {
untyped __cpp__("int base = 0; hx::SetTopOfStack(&base, true);"); // allow running haxe code on foreign thread
mainLoop.run(() -> {
if (!hasLocal) {
hasLocal = true;
var track;
while ((track = pendingTracks.shift()) != null) {
addTrack(track, null);
}
}
for (cb in localCandidateListeners) {
cb({ candidate: {
candidate: (candidate.candidate() : String),
Expand Down Expand Up @@ -897,12 +892,14 @@ class PeerConnection {
public function setLocalDescription(sdpType: Null<SdpType>): Promise<Any> {
return new Promise((resolve, reject) -> {
waitingOnLocal = resolve;
if (!hasRemote) addPendingTracks();
pc.ref.setLocalDescription(sdpType ?? SdpType.UNSPEC);
});
}

public function setRemoteDescription(description : SessionDescriptionInit): Promise<Any> {
pc.ref.setRemoteDescription(new Description(cpp.StdString.ofString(description.sdp), description.type));
hasRemote = true;
return Promise.resolve(null);
}

Expand All @@ -911,6 +908,14 @@ class PeerConnection {
return Promise.resolve(null);
}

private function addPendingTracks() {
hasLocal = true;
var track;
while ((track = pendingTracks.shift()) != null) {
addTrack(track, null);
}
}

public function addTrack(track : MediaStreamTrack, stream : MediaStream) {
if (hasLocal) {
track.track = pc.ref.addTrack(track.media.value());
Expand Down
11 changes: 5 additions & 6 deletions snikket/jingle/Session.hx
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ class InitiatedSession implements Session {
});
}

private function setupLocalDescription(type: String, ?filterMedia: Array<String>, ?filterOut: Bool = false, ?beforeSend: (SessionDescription)->Void) {
private function setupLocalDescription(type: String, ?filterMedia: Array<String>, ?filterOut: Bool = false, ?beforeSend: (SessionDescription)->Void): Promise<Any> {
return pc.setLocalDescription(null).then((_) -> {
final caps = client.getDirectChat(counterpart.asBare().asString()).getResourceCaps(counterpart.resource);
return if ((type == "session-initiate" || type == "session-accept") && caps.features.contains("urn:ietf:rfc:3264")) {
Expand Down Expand Up @@ -595,18 +595,17 @@ class InitiatedSession implements Session {
});
}

private function onPeerConnection() {
private function onPeerConnection(): Promise<Any> {
return pc.setRemoteDescription({ type: SdpType.OFFER, sdp: remoteDescription.toSdp() })
.then((_) -> {
final inboundTransportInfo = queuedInboundTransportInfo.copy();
queuedInboundTransportInfo.resize(0);
return inboundTransportInfo.map(transportInfo);
})
.then((_) -> {
setupLocalDescription("session-accept");
}).then((x) -> {
.then((_) ->
setupLocalDescription("session-accept")
).then((_) -> {
peerDtlsSetup = localDescription.getDtlsSetup() == "active" ? "passive" : "active";
return;
});
}
}
Expand Down

0 comments on commit 4003725

Please sign in to comment.