@@ -1752,7 +1752,7 @@ class CallStore extends EventEmitter {
1752
1752
LOG_P2P_CALL ( '[conn] oniceconnectionstatechange' , connection . iceConnectionState ) ;
1753
1753
} ;
1754
1754
connection . onnegotiationneeded = event => {
1755
- LOG_P2P_CALL ( '[conn] onnegotiationneeded' , connection . signalingState ) ;
1755
+ LOG_P2P_CALL ( '[conn][InitialSetup] onnegotiationneeded' , connection . signalingState ) ;
1756
1756
this . p2pStartNegotiation ( id ) ;
1757
1757
}
1758
1758
connection . onicecandidate = event => {
@@ -1808,6 +1808,22 @@ class CallStore extends EventEmitter {
1808
1808
}
1809
1809
if ( TG_CALLS_SDP ) {
1810
1810
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
+ }
1811
1827
} else {
1812
1828
if ( is_outgoing ) {
1813
1829
this . p2pAppendInputStream ( inputStream ) ;
@@ -1823,10 +1839,10 @@ class CallStore extends EventEmitter {
1823
1839
/// https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Perfect_negotiation
1824
1840
p2pStartNegotiation = async id => {
1825
1841
const { currentCall } = this ;
1826
- LOG_P2P_CALL ( 'p2pStartNegotiation' , currentCall ) ;
1842
+ LOG_P2P_CALL ( '[InitialSetup] p2pStartNegotiation' , currentCall ) ;
1827
1843
if ( ! currentCall ) return ;
1828
1844
1829
- const { callId, connection } = currentCall ;
1845
+ const { callId, connection, offerReceived } = currentCall ;
1830
1846
if ( callId !== id ) return ;
1831
1847
if ( ! connection ) return ;
1832
1848
@@ -1837,21 +1853,38 @@ class CallStore extends EventEmitter {
1837
1853
if ( ! connection . localDescription && ! connection . remoteDescription && ! is_outgoing ) return ;
1838
1854
// if (!is_outgoing) return;
1839
1855
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 ;
1844
1859
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 ) ;
1849
1866
initialSetup [ '@type' ] = 'InitialSetup' ;
1850
- currentCall . offerSent = true ;
1851
1867
1868
+ console . log ( '[InitialSetup] send 2' ) ;
1852
1869
this . p2pSendInitialSetup ( callId , initialSetup ) ;
1853
1870
} 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
+ }
1855
1888
}
1856
1889
} ;
1857
1890
@@ -1867,7 +1900,7 @@ class CallStore extends EventEmitter {
1867
1900
}
1868
1901
1869
1902
p2pAppendInputStream ( inputStream ) {
1870
- LOG_P2P_CALL ( 'p2pAppendInputStream start' , inputStream ) ;
1903
+ LOG_P2P_CALL ( '[InitialSetup] p2pAppendInputStream start' , inputStream ) ;
1871
1904
const { currentCall } = this ;
1872
1905
if ( ! currentCall ) return ;
1873
1906
@@ -1887,7 +1920,7 @@ class CallStore extends EventEmitter {
1887
1920
inputStream && inputStream . getVideoTracks ( ) . forEach ( x => {
1888
1921
connection . addTrack ( x , inputStream ) ;
1889
1922
} ) ;
1890
- LOG_P2P_CALL ( 'p2pAppendInputStream stop' , inputStream ) ;
1923
+ LOG_P2P_CALL ( '[InitialSetup] p2pAppendInputStream stop' , inputStream ) ;
1891
1924
}
1892
1925
1893
1926
async p2pApplyCallDataChannelData ( data ) {
@@ -1971,10 +2004,6 @@ class CallStore extends EventEmitter {
1971
2004
}
1972
2005
}
1973
2006
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
-
1978
2007
console . log ( '[sdp] remote' , description . type , description . sdp )
1979
2008
await connection . setRemoteDescription ( description ) ;
1980
2009
@@ -1986,24 +2015,22 @@ class CallStore extends EventEmitter {
1986
2015
}
1987
2016
1988
2017
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 ) ;
1995
2024
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
+ }
2007
2034
}
2008
2035
break ;
2009
2036
}
0 commit comments