@@ -1807,6 +1807,14 @@ class CallStore extends EventEmitter {
1807
1807
}
1808
1808
if ( TG_CALLS_SDP ) {
1809
1809
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
+ // }
1810
1818
} else {
1811
1819
if ( is_outgoing ) {
1812
1820
this . p2pAppendInputStream ( inputStream ) ;
@@ -1833,6 +1841,7 @@ class CallStore extends EventEmitter {
1833
1841
if ( ! call ) return ;
1834
1842
1835
1843
const { is_outgoing } = call ;
1844
+ if ( ! connection . localDescription && ! connection . remoteDescription && ! is_outgoing ) return ;
1836
1845
// if (!is_outgoing) return;
1837
1846
1838
1847
let offer = await connection . createOffer ( {
@@ -1845,20 +1854,9 @@ class CallStore extends EventEmitter {
1845
1854
if ( TG_CALLS_SDP ) {
1846
1855
const initialSetup = p2pParseSdp ( offer . sdp ) ;
1847
1856
initialSetup [ '@type' ] = 'InitialSetup' ;
1857
+ currentCall . offerSent = true ;
1848
1858
1849
1859
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
- }
1862
1860
} else {
1863
1861
this . p2pSendSdp ( callId , offer ) ;
1864
1862
}
@@ -1965,6 +1963,48 @@ class CallStore extends EventEmitter {
1965
1963
1966
1964
let type = TG_CALLS ? data [ '@type' ] || data . type : data . type ;
1967
1965
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
+ }
1968
2008
case 'answer' :
1969
2009
case 'offer' :{
1970
2010
let description = data ;
@@ -2022,57 +2062,6 @@ class CallStore extends EventEmitter {
2022
2062
}
2023
2063
break ;
2024
2064
}
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
- }
2076
2065
case 'Candidates' : {
2077
2066
const candidates = [ ] ;
2078
2067
let candidate = data ;
@@ -2202,13 +2191,22 @@ class CallStore extends EventEmitter {
2202
2191
}
2203
2192
2204
2193
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
+
2206
2204
TdLibController . send ( {
2207
2205
'@type' : 'discardCall' ,
2208
2206
call_id : callId ,
2209
2207
is_disconnected : false ,
2210
2208
duration : 0 ,
2211
- is_video : true ,
2209
+ is_video : isVideo ,
2212
2210
connection_id : 0
2213
2211
} ) ;
2214
2212
}
0 commit comments