@@ -36,6 +36,8 @@ const globalLog = logger.init("global", "blue");
36
36
const shareLog = logger . init ( "share" , "yellow" ) ;
37
37
const localLog = logger . init ( "local" , "green" ) ;
38
38
39
+ // Initialize the options from cookies (set from index.js and precall.js)
40
+
39
41
const optionsInit = ( ) => {
40
42
let options = {
41
43
videoProfile : Cookies . get ( "videoProfile" ) . split ( "," ) [ 0 ] || "480p_4" ,
@@ -60,6 +62,8 @@ const optionsInit = () => {
60
62
return options ;
61
63
} ;
62
64
65
+ // Initalizes the User Interface
66
+
63
67
const uiInit = options => {
64
68
document . querySelector (
65
69
".ag-header-lead span"
@@ -92,11 +96,17 @@ const uiInit = options => {
92
96
}
93
97
} ;
94
98
99
+ // Intializes the Agora client object
100
+
95
101
const clientInit = ( client , options ) => {
96
102
return new Promise ( ( resolve , reject ) => {
103
+ // Initialize the agora client object with appid
97
104
client . init ( options . key , ( ) => {
98
105
globalLog ( "AgoraRTC client initialized" ) ;
106
+ // Set low stream parameter
107
+ // Read more here https://docs.agora.io/en/Video/API%20Reference/web/interfaces/agorartc.client.html#setlowstreamparameter
99
108
let lowStreamParam = RESOLUTION_ARR [ options . videoProfileLow ] ;
109
+ // Join the channel
100
110
client . join (
101
111
options . token ,
102
112
options . channel ,
@@ -122,8 +132,8 @@ const clientInit = (client, options) => {
122
132
} ;
123
133
124
134
/**
125
- *
126
- * @param {* } uid
135
+ * @description Initializes the local stream
136
+ * @param {* } uid User Id
127
137
* @param {* } options global option
128
138
* @param {* } config stream config
129
139
*/
@@ -135,6 +145,7 @@ const streamInit = (uid, options, config) => {
135
145
screen : false
136
146
} ;
137
147
148
+ // Modify default config based on attendee mode
138
149
switch ( options . attendeeMode ) {
139
150
case "audio-only" :
140
151
defaultConfig . video = false ;
@@ -147,12 +158,15 @@ const streamInit = (uid, options, config) => {
147
158
case "video" :
148
159
break ;
149
160
}
161
+ // Create an Agora stream using the above parameters
150
162
// eslint-disable-next-line
151
163
let stream = AgoraRTC . createStream ( merge ( defaultConfig , config ) ) ;
164
+ // Set the selected video profile
152
165
stream . setVideoProfile ( options . videoProfile ) ;
153
166
return stream ;
154
167
} ;
155
168
169
+ // A function to stop and reset screen sharing related features.
156
170
const shareEnd = ( ) => {
157
171
try {
158
172
shareClient && shareClient . unpublish ( shareStream ) ;
@@ -172,26 +186,38 @@ const shareEnd = () => {
172
186
}
173
187
} ;
174
188
189
+
190
+ // Start screen sharing
175
191
const shareStart = ( ) => {
176
192
ButtonControl . disable ( ".shareScreenBtn" ) ;
193
+
194
+ // Create a new client for the screen sharing stream.
195
+
177
196
// eslint-disable-next-line
178
197
shareClient = AgoraRTC . createClient ( {
179
198
mode : options . transcode
180
199
} ) ;
200
+
201
+ // Create a new options object for screen sharing.
181
202
let shareOptions = merge ( options , {
182
203
uid : SHARE_ID
183
204
} ) ;
205
+
206
+ // Initalializes the client with the screen sharing options.
184
207
clientInit ( shareClient , shareOptions ) . then ( uid => {
208
+ // New config for screen sharing stream
185
209
let config = {
186
210
screen : true ,
187
211
video : false ,
188
212
audio : false ,
189
213
extensionId : "minllpmhdgpndnkomcoccfekfegnlikg" ,
190
214
mediaSource : "application"
191
215
} ;
216
+ // Intialize the screen sharing stream with the necessary parameters.
192
217
shareStream = streamInit ( uid , shareOptions , config ) ;
193
218
shareStream . init (
194
219
( ) => {
220
+ // Once the stream is intialized, update the relevant ui and publish the stream.
195
221
ButtonControl . enable ( ".shareScreenBtn" ) ;
196
222
shareStream . on ( "stopScreenSharing" , ( ) => {
197
223
shareEnd ( ) ;
@@ -203,6 +229,7 @@ const shareStart = () => {
203
229
} ) ;
204
230
} ,
205
231
err => {
232
+ // Appropriate error handling if screen sharing doesn't work.
206
233
ButtonControl . enable ( ".shareScreenBtn" ) ;
207
234
shareLog ( "getUserMedia failed" , err ) ;
208
235
shareEnd ( ) ;
@@ -221,6 +248,7 @@ const shareStart = () => {
221
248
} ) ;
222
249
} ;
223
250
251
+ // Relevant code if you are using the screen sharing extension for previous versions of chrome.
224
252
window . installSuccess = ( ...args ) => {
225
253
globalLog ( ...args ) ;
226
254
} ;
@@ -233,6 +261,7 @@ window.installError = (...args) => {
233
261
) ;
234
262
} ;
235
263
264
+ // Helper function to remove stream from UI
236
265
const removeStream = id => {
237
266
streamList . map ( ( item , index ) => {
238
267
if ( item . getId ( ) === id ) {
@@ -249,6 +278,7 @@ const removeStream = id => {
249
278
Renderer . customRender ( streamList , options . displayMode , mainId ) ;
250
279
} ;
251
280
281
+ // Helper function to add stream to UI
252
282
const addStream = ( stream , push = false ) => {
253
283
let id = stream . getId ( ) ;
254
284
// Check for redundant
@@ -267,12 +297,14 @@ const addStream = (stream, push = false) => {
267
297
Renderer . customRender ( streamList , options . displayMode , mainId ) ;
268
298
} ;
269
299
300
+ // Helper function to fetch video streams from the data structure
270
301
const getStreamById = id => {
271
302
return streamList . filter ( item => {
272
303
return item . getId ( ) === id ;
273
304
} ) [ 0 ] ;
274
305
} ;
275
306
307
+ // enable dual stream
276
308
const enableDualStream = ( ) => {
277
309
client . enableDualStream (
278
310
function ( ) {
@@ -284,6 +316,7 @@ const enableDualStream = () => {
284
316
) ;
285
317
} ;
286
318
319
+ // for setting the high stream in dual stream configuration.
287
320
const setHighStream = ( prev , next ) => {
288
321
if ( prev === next ) {
289
322
return ;
@@ -306,6 +339,7 @@ const setHighStream = (prev, next) => {
306
339
// Set next stream to high
307
340
nextStream && client . setRemoteVideoStreamType ( nextStream , 0 ) ;
308
341
} ;
342
+
309
343
/**
310
344
* Add callback for client event to control streams
311
345
* @param {* } client
@@ -341,6 +375,7 @@ const subscribeStreamEvents = () => {
341
375
} ) ;
342
376
} ) ;
343
377
378
+ // When a peer leaves
344
379
client . on ( "peer-leave" , function ( evt ) {
345
380
let id = evt . uid ;
346
381
localLog ( "Peer has left: " + id ) ;
@@ -362,6 +397,7 @@ const subscribeStreamEvents = () => {
362
397
removeStream ( evt . uid ) ;
363
398
} ) ;
364
399
400
+ // when a stream is subscribed
365
401
client . on ( "stream-subscribed" , function ( evt ) {
366
402
let stream = evt . stream ;
367
403
localLog ( "Got stream-subscribed event" ) ;
@@ -370,6 +406,7 @@ const subscribeStreamEvents = () => {
370
406
addStream ( stream ) ;
371
407
} ) ;
372
408
409
+ // when a stream is removed
373
410
client . on ( "stream-removed" , function ( evt ) {
374
411
let stream = evt . stream ;
375
412
let id = stream . getId ( ) ;
@@ -393,6 +430,7 @@ const subscribeStreamEvents = () => {
393
430
} ) ;
394
431
} ;
395
432
433
+ // Subscribe the various mouse events in the UI like clicks
396
434
const subscribeMouseEvents = ( ) => {
397
435
$ ( ".displayModeBtn" ) . on ( "click" , function ( e ) {
398
436
if (
@@ -522,6 +560,7 @@ const subscribeMouseEvents = () => {
522
560
} ) ;
523
561
} ;
524
562
563
+ // Get various scheduled network stats
525
564
const infoDetectSchedule = ( ) => {
526
565
let no = streamList . length ;
527
566
for ( let i = 0 ; i < no ; i ++ ) {
0 commit comments