Skip to content

Commit a85cf79

Browse files
author
evgeny-nadymov
committed
Fix p2p calls from iPhone to Chrome/Safari
1 parent afd90f1 commit a85cf79

File tree

3 files changed

+65
-37
lines changed

3 files changed

+65
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"homepage": "https://evgeny-nadymov.github.io/telegram-react",
33
"name": "telegram_react",
4-
"version": "0.0.990",
4+
"version": "0.0.991",
55
"private": true,
66
"dependencies": {
77
"tdweb": "^1.7.2",

src/Calls/P2P/P2PSdpBuilder.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ export function addExtmap(extmap) {
251251
for (let j = 0; j < extmap.length; j++) {
252252
const ext = extmap[j];
253253
const { id, uri } = ext;
254+
// if (isFirefox() && uri.indexOf(''))
254255
console.log('[extmap] add', id, uri);
255256
sdp += `
256257
a=extmap:${id} ${uri}`;

src/Stores/CallStore.js

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,7 +1752,7 @@ class CallStore extends EventEmitter {
17521752
LOG_P2P_CALL('[conn] oniceconnectionstatechange', connection.iceConnectionState);
17531753
};
17541754
connection.onnegotiationneeded = event => {
1755-
LOG_P2P_CALL('[conn] onnegotiationneeded', connection.signalingState);
1755+
LOG_P2P_CALL('[conn][InitialSetup] onnegotiationneeded', connection.signalingState);
17561756
this.p2pStartNegotiation(id);
17571757
}
17581758
connection.onicecandidate = event => {
@@ -1808,6 +1808,22 @@ class CallStore extends EventEmitter {
18081808
}
18091809
if (TG_CALLS_SDP) {
18101810
this.p2pAppendInputStream(inputStream);
1811+
const { offerReceived } = this.currentCall;
1812+
if (offerReceived) {
1813+
console.log('[InitialSetup] after p2pAppendInputStream');
1814+
this.currentCall.offerReceived = false;
1815+
1816+
const answer = await connection.createAnswer();
1817+
1818+
console.log('[sdp] local', answer.type, answer.sdp);
1819+
await connection.setLocalDescription(answer);
1820+
1821+
const initialSetup = p2pParseSdp(answer.sdp);
1822+
initialSetup['@type'] = 'InitialSetup';
1823+
1824+
console.log('[InitialSetup] send 2');
1825+
this.p2pSendInitialSetup(callId, initialSetup);
1826+
}
18111827
} else {
18121828
if (is_outgoing) {
18131829
this.p2pAppendInputStream(inputStream);
@@ -1823,10 +1839,10 @@ class CallStore extends EventEmitter {
18231839
/// https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Perfect_negotiation
18241840
p2pStartNegotiation = async id => {
18251841
const { currentCall } = this;
1826-
LOG_P2P_CALL('p2pStartNegotiation', currentCall);
1842+
LOG_P2P_CALL('[InitialSetup] p2pStartNegotiation', currentCall);
18271843
if (!currentCall) return;
18281844

1829-
const { callId, connection } = currentCall;
1845+
const { callId, connection, offerReceived } = currentCall;
18301846
if (callId !== id) return;
18311847
if (!connection) return;
18321848

@@ -1837,21 +1853,38 @@ class CallStore extends EventEmitter {
18371853
if (!connection.localDescription && !connection.remoteDescription && !is_outgoing) return;
18381854
// if (!is_outgoing) return;
18391855

1840-
let offer = await connection.createOffer({
1841-
offerToReceiveAudio: true,
1842-
offerToReceiveVideo: true
1843-
});
1856+
LOG_P2P_CALL('[InitialSetup] p2pStartNegotiation 1', offerReceived);
1857+
if (offerReceived) {
1858+
currentCall.offerReceived = false;
18441859

1845-
console.log('[sdp] local', offer.sdp);
1846-
await connection.setLocalDescription(offer);
1847-
if (TG_CALLS_SDP) {
1848-
const initialSetup = p2pParseSdp(offer.sdp);
1860+
const answer = await connection.createAnswer();
1861+
1862+
console.log('[sdp] local', answer.type, answer.sdp);
1863+
await connection.setLocalDescription(answer);
1864+
1865+
const initialSetup = p2pParseSdp(answer.sdp);
18491866
initialSetup['@type'] = 'InitialSetup';
1850-
currentCall.offerSent = true;
18511867

1868+
console.log('[InitialSetup] send 2');
18521869
this.p2pSendInitialSetup(callId, initialSetup);
18531870
} else {
1854-
this.p2pSendSdp(callId, offer);
1871+
let offer = await connection.createOffer({
1872+
offerToReceiveAudio: true,
1873+
offerToReceiveVideo: true
1874+
});
1875+
1876+
console.log('[sdp] local', offer.sdp);
1877+
await connection.setLocalDescription(offer);
1878+
if (TG_CALLS_SDP) {
1879+
const initialSetup = p2pParseSdp(offer.sdp);
1880+
initialSetup['@type'] = 'InitialSetup';
1881+
currentCall.offerSent = true;
1882+
1883+
console.log('[InitialSetup] send 0');
1884+
this.p2pSendInitialSetup(callId, initialSetup);
1885+
} else {
1886+
this.p2pSendSdp(callId, offer);
1887+
}
18551888
}
18561889
};
18571890

@@ -1867,7 +1900,7 @@ class CallStore extends EventEmitter {
18671900
}
18681901

18691902
p2pAppendInputStream(inputStream) {
1870-
LOG_P2P_CALL('p2pAppendInputStream start', inputStream);
1903+
LOG_P2P_CALL('[InitialSetup] p2pAppendInputStream start', inputStream);
18711904
const { currentCall } = this;
18721905
if (!currentCall) return;
18731906

@@ -1887,7 +1920,7 @@ class CallStore extends EventEmitter {
18871920
inputStream && inputStream.getVideoTracks().forEach(x => {
18881921
connection.addTrack(x, inputStream);
18891922
});
1890-
LOG_P2P_CALL('p2pAppendInputStream stop', inputStream);
1923+
LOG_P2P_CALL('[InitialSetup] p2pAppendInputStream stop', inputStream);
18911924
}
18921925

18931926
async p2pApplyCallDataChannelData(data) {
@@ -1971,10 +2004,6 @@ class CallStore extends EventEmitter {
19712004
}
19722005
}
19732006

1974-
// if (description.type === 'offer' && description.sdp.indexOf('a=setup:active')) {
1975-
// description.sdp = description.sdp.replaceAll('a=setup:active', 'a=setup:actpass')
1976-
// }
1977-
19782007
console.log('[sdp] remote', description.type, description.sdp)
19792008
await connection.setRemoteDescription(description);
19802009

@@ -1986,24 +2015,22 @@ class CallStore extends EventEmitter {
19862015
}
19872016

19882017
if (!isAnswer) {
1989-
const answer = await connection.createAnswer();
1990-
// if (description.sdp.indexOf('a=setup:active') && answer.sdp.indexOf('a=setup:active')) {
1991-
// answer.sdp = answer.sdp.replaceAll('a=setup:active', 'a=setup:passive');
1992-
// }
1993-
console.log('[sdp] local', answer.type, answer.sdp);
1994-
await connection.setLocalDescription(answer);
2018+
const { inputStream } = currentCall;
2019+
if (inputStream) {
2020+
const answer = await connection.createAnswer();
2021+
2022+
console.log('[sdp] local', answer.type, answer.sdp);
2023+
await connection.setLocalDescription(answer);
19952024

1996-
// LOG_P2P_CALL('2 try invoke p2pAppendInputStream', inputStream, is_outgoing);
1997-
// const { inputStream } = currentCall;
1998-
// if (inputStream && !is_outgoing) {
1999-
// this.p2pAppendInputStream(inputStream);
2000-
// }
2001-
const initialSetup = p2pParseSdp(answer.sdp);
2002-
initialSetup['@type'] = 'InitialSetup';
2003-
2004-
console.log('[sdp] InitialSetup 4', callId, initialSetup);
2005-
this.p2pSendInitialSetup(callId, initialSetup);
2006-
// this.p2pSendSdp(callId, answer);
2025+
const initialSetup = p2pParseSdp(answer.sdp);
2026+
initialSetup['@type'] = 'InitialSetup';
2027+
2028+
console.log('[InitialSetup] send 1');
2029+
this.p2pSendInitialSetup(callId, initialSetup);
2030+
} else {
2031+
console.log('[InitialSetup] offerReceived=true');
2032+
currentCall.offerReceived = true;
2033+
}
20072034
}
20082035
break;
20092036
}

0 commit comments

Comments
 (0)