Skip to content

Commit 68a8561

Browse files
author
evgeny-nadymov
committed
Offer/answer webrtc algorithm
Fix is_video flag on call discarding
1 parent 28149a6 commit 68a8561

File tree

2 files changed

+64
-66
lines changed

2 files changed

+64
-66
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.985",
4+
"version": "0.0.988",
55
"private": true,
66
"dependencies": {
77
"tdweb": "^1.7.2",

src/Stores/CallStore.js

Lines changed: 63 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,14 @@ class CallStore extends EventEmitter {
18071807
}
18081808
if (TG_CALLS_SDP) {
18091809
this.p2pAppendInputStream(inputStream);
1810+
// if (is_outgoing) {
1811+
// this.p2pAppendInputStream(inputStream);
1812+
// } else {
1813+
// LOG_P2P_CALL('try invoke p2pAppendInputStream', connection.localDescription, connection.remoteDescription);
1814+
// if (connection.localDescription && connection.remoteDescription) {
1815+
// this.p2pAppendInputStream(inputStream);
1816+
// }
1817+
// }
18101818
} else {
18111819
if (is_outgoing) {
18121820
this.p2pAppendInputStream(inputStream);
@@ -1833,6 +1841,7 @@ class CallStore extends EventEmitter {
18331841
if (!call) return;
18341842

18351843
const { is_outgoing } = call;
1844+
if (!connection.localDescription && !connection.remoteDescription && !is_outgoing) return;
18361845
// if (!is_outgoing) return;
18371846

18381847
let offer = await connection.createOffer({
@@ -1845,20 +1854,9 @@ class CallStore extends EventEmitter {
18451854
if (TG_CALLS_SDP) {
18461855
const initialSetup = p2pParseSdp(offer.sdp);
18471856
initialSetup['@type'] = 'InitialSetup';
1857+
currentCall.offerSent = true;
18481858

18491859
this.p2pSendInitialSetup(callId, initialSetup);
1850-
if (currentCall.answer) {
1851-
await connection.setRemoteDescription(currentCall.answer);
1852-
currentCall.answer = null;
1853-
if (currentCall.candidates) {
1854-
currentCall.candidates.forEach(x => {
1855-
this.p2pAddIceCandidate(connection, x);
1856-
// LOG_P2P_CALL('[candidate] add postpone', x);
1857-
// connection.addIceCandidate(x);
1858-
});
1859-
currentCall.candidates = [];
1860-
}
1861-
}
18621860
} else {
18631861
this.p2pSendSdp(callId, offer);
18641862
}
@@ -1965,6 +1963,48 @@ class CallStore extends EventEmitter {
19651963

19661964
let type = TG_CALLS ? data['@type'] || data.type : data.type;
19671965
switch (type) {
1966+
case 'InitialSetup': {
1967+
console.log('[sdp] InitialSetup', data);
1968+
const isAnswer = currentCall.offerSent;
1969+
currentCall.offerSent = false;
1970+
1971+
let description = data;
1972+
if (UNIFY_SDP) {
1973+
description = {
1974+
type: isAnswer ? 'answer' : 'offer',
1975+
sdp: isAnswer ?
1976+
P2PSdpBuilder.generateAnswer(data) :
1977+
P2PSdpBuilder.generateOffer(data)
1978+
}
1979+
}
1980+
1981+
await connection.setRemoteDescription(description);
1982+
1983+
if (currentCall.candidates) {
1984+
currentCall.candidates.forEach(x => {
1985+
this.p2pAddIceCandidate(connection, x);
1986+
});
1987+
currentCall.candidates = [];
1988+
}
1989+
1990+
if (!isAnswer) {
1991+
const answer = await connection.createAnswer();
1992+
await connection.setLocalDescription(answer);
1993+
1994+
// LOG_P2P_CALL('2 try invoke p2pAppendInputStream', inputStream, is_outgoing);
1995+
// const { inputStream } = currentCall;
1996+
// if (inputStream && !is_outgoing) {
1997+
// this.p2pAppendInputStream(inputStream);
1998+
// }
1999+
const initialSetup = p2pParseSdp(answer.sdp);
2000+
initialSetup['@type'] = 'InitialSetup';
2001+
2002+
console.log('[sdp] InitialSetup 4', callId, initialSetup);
2003+
this.p2pSendInitialSetup(callId, initialSetup);
2004+
// this.p2pSendSdp(callId, answer);
2005+
}
2006+
break;
2007+
}
19682008
case 'answer':
19692009
case 'offer':{
19702010
let description = data;
@@ -2022,57 +2062,6 @@ class CallStore extends EventEmitter {
20222062
}
20232063
break;
20242064
}
2025-
case 'InitialSetup': {
2026-
console.log('[sdp] InitialSetup');
2027-
let description = data;
2028-
if (UNIFY_SDP) {
2029-
const { fingerprints } = description;
2030-
if (fingerprints) {
2031-
fingerprints.forEach(x => {
2032-
if (x.setup === 'actpass') {
2033-
if (is_outgoing) {
2034-
x.setup = 'active';
2035-
} else {
2036-
x.setup = 'passive';
2037-
}
2038-
}
2039-
})
2040-
}
2041-
description = {
2042-
type: 'answer',
2043-
sdp: P2PSdpBuilder.generateAnswer(data)
2044-
}
2045-
}
2046-
2047-
console.log('[sdp] remote', description.sdp);
2048-
if (connection.localDescription) {
2049-
await connection.setRemoteDescription(description);
2050-
if (currentCall.candidates) {
2051-
currentCall.candidates.forEach(x => {
2052-
this.p2pAddIceCandidate(connection, x);
2053-
// LOG_P2P_CALL('[candidate] add postpone', x);
2054-
// connection.addIceCandidate(x);
2055-
});
2056-
currentCall.candidates = [];
2057-
}
2058-
} else {
2059-
currentCall.answer = description;
2060-
}
2061-
2062-
2063-
2064-
// const answer = await connection.createAnswer();
2065-
// await connection.setLocalDescription(answer);
2066-
//
2067-
// LOG_P2P_CALL('2 try invoke p2pAppendInputStream', inputStream, is_outgoing);
2068-
// const { inputStream } = currentCall;
2069-
// if (inputStream && !is_outgoing) {
2070-
// this.p2pAppendInputStream(inputStream);
2071-
// }
2072-
2073-
// this.p2pSendSdp(callId, answer);
2074-
break;
2075-
}
20762065
case 'Candidates': {
20772066
const candidates = [];
20782067
let candidate = data;
@@ -2202,13 +2191,22 @@ class CallStore extends EventEmitter {
22022191
}
22032192

22042193
if (discard) {
2205-
LOG_P2P_CALL('[tdlib] discardCall', callId);
2194+
const inputMediaState = this.p2pGetMediaState(callId, 'input');
2195+
const outputMediaState = this.p2pGetMediaState(callId, 'output');
2196+
2197+
const call = this.p2pGet(callId);
2198+
2199+
const isVideo = inputMediaState && inputMediaState.videoState === 'active'
2200+
|| outputMediaState && outputMediaState.videoState === 'active'
2201+
|| call && call.is_video;
2202+
LOG_P2P_CALL('[tdlib] discardCall', callId, isVideo);
2203+
22062204
TdLibController.send({
22072205
'@type': 'discardCall',
22082206
call_id: callId,
22092207
is_disconnected: false,
22102208
duration: 0,
2211-
is_video: true,
2209+
is_video: isVideo,
22122210
connection_id: 0
22132211
});
22142212
}

0 commit comments

Comments
 (0)