@@ -95,6 +95,15 @@ export const HomeScreen: React.FC<Props> = () => {
95
95
// init PeerConnection
96
96
const peerConnection = new RTCPeerConnection ( peerConstraints )
97
97
98
+ // get location of connection between requester and current user
99
+ const connectionsCollection = roomRef . collection ( FirestoreCollections . connections )
100
+ const connectionRef = connectionsCollection . doc ( `${ data . requester } -${ createdUserName } ` )
101
+
102
+ // create answerCandidates collection
103
+ const answerCandidatesCollection = connectionRef . collection (
104
+ FirestoreCollections . answerCandidates ,
105
+ )
106
+
98
107
// add current user's stream to created PC (Peer Connection)
99
108
localStream ?. getTracks ( ) . forEach ( track => {
100
109
peerConnection . addTrack ( track , localStream )
@@ -114,9 +123,12 @@ export const HomeScreen: React.FC<Props> = () => {
114
123
} )
115
124
} )
116
125
117
- // get location of connection between requester and current user
118
- const connectionsCollection = roomRef . collection ( FirestoreCollections . connections )
119
- const connectionRef = connectionsCollection . doc ( `${ data . requester } -${ createdUserName } ` )
126
+ // add current user's ICE Candidates to answerCandidates collection
127
+ peerConnection . addEventListener ( 'icecandidate' , event => {
128
+ if ( event . candidate ) {
129
+ answerCandidatesCollection . add ( event . candidate . toJSON ( ) )
130
+ }
131
+ } )
120
132
121
133
// get data from requester-user's connection
122
134
const connectionData = ( await connectionRef . get ( ) ) . data ( )
@@ -136,18 +148,6 @@ export const HomeScreen: React.FC<Props> = () => {
136
148
}
137
149
await connectionRef . update ( { answer} )
138
150
139
- // create answerCandidates collection
140
- const answerCandidatesCollection = connectionRef . collection (
141
- FirestoreCollections . answerCandidates ,
142
- )
143
-
144
- // add current user's ICE Candidates to answerCandidates collection
145
- peerConnection . addEventListener ( 'icecandidate' , event => {
146
- if ( event . candidate ) {
147
- answerCandidatesCollection . add ( event . candidate . toJSON ( ) )
148
- }
149
- } )
150
-
151
151
// collect Offer's ICE candidates from offerCandidates collection and add in PC
152
152
connectionRef
153
153
. collection ( FirestoreCollections . offerCandidates )
@@ -191,9 +191,22 @@ export const HomeScreen: React.FC<Props> = () => {
191
191
} ,
192
192
} ) )
193
193
194
+ // init participant's remoteStream to add track data from Peer Connection later
195
+ setRemoteStreams ( prev => ( {
196
+ ...prev ,
197
+ [ participant . name ] : new MediaStream ( [ ] ) ,
198
+ } ) )
199
+
194
200
// init peer connection
195
201
const peerConnection = new RTCPeerConnection ( peerConstraints )
196
202
203
+ // create connection between current user and participant
204
+ const connectionsCollection = roomRef . collection ( FirestoreCollections . connections )
205
+ const connectionRef = connectionsCollection . doc ( `${ createdUserName } -${ participant . name } ` )
206
+
207
+ // create offerCandidates collection
208
+ const offerCandidatesCollection = connectionRef . collection ( FirestoreCollections . offerCandidates )
209
+
197
210
// add current user's stream to created PC (Peer Connection)
198
211
localStream ?. getTracks ( ) . forEach ( track => {
199
212
peerConnection . addTrack ( track , localStream )
@@ -213,15 +226,12 @@ export const HomeScreen: React.FC<Props> = () => {
213
226
} )
214
227
} )
215
228
216
- // init participant's remoteStream to add track data from Peer Connection later
217
- setRemoteStreams ( prev => ( {
218
- ...prev ,
219
- [ participant . name ] : new MediaStream ( [ ] ) ,
220
- } ) )
221
-
222
- // create connection between current user and participant
223
- const connectionsCollection = roomRef . collection ( FirestoreCollections . connections )
224
- const connectionRef = connectionsCollection . doc ( `${ createdUserName } -${ participant . name } ` )
229
+ // add current user's ICE Candidates to offerCandidates collection
230
+ peerConnection . addEventListener ( 'icecandidate' , event => {
231
+ if ( event . candidate ) {
232
+ offerCandidatesCollection . add ( event . candidate . toJSON ( ) )
233
+ }
234
+ } )
225
235
226
236
// create offer SDP and set localDescription
227
237
const offerDescription = await peerConnection . createOffer ( sessionConstraints )
@@ -249,16 +259,6 @@ export const HomeScreen: React.FC<Props> = () => {
249
259
}
250
260
} )
251
261
252
- // create offerCandidates collection
253
- const offerCandidatesCollection = connectionRef . collection ( FirestoreCollections . offerCandidates )
254
-
255
- // add current user's ICE Candidates to offerCandidates collection
256
- peerConnection . addEventListener ( 'icecandidate' , event => {
257
- if ( event . candidate ) {
258
- offerCandidatesCollection . add ( event . candidate . toJSON ( ) )
259
- }
260
- } )
261
-
262
262
// add listener to answerCandidates collection to participant's ICE Candidates
263
263
connectionRef . collection ( FirestoreCollections . answerCandidates ) . onSnapshot ( iceCandidatesSnapshot => {
264
264
iceCandidatesSnapshot . docChanges ( ) . forEach ( async change => {
0 commit comments