@@ -13,16 +13,13 @@ import EmptyVideoWorker from "./emptyVideoWorker.ts?worker";
13
13
14
14
export type LocalPeerContext = {
15
15
video : UseCameraResult < TrackMetadata > ;
16
- // audio: UseMicrophoneResult<TrackMetadata>;
17
- // screenShare: UseScreenShareResult<TrackMetadata>;
18
- // camera: MembraneStreaming;
19
16
init : ( ) => void ;
20
17
blur : boolean ;
21
18
setBlur : ( status : boolean , restart : boolean ) => void ;
22
19
setDevice : ( cameraId : string | null , microphoneId : string | null , blur : boolean ) => void ;
23
20
toggleCamera : ( value : boolean ) => void ,
24
21
toggleMicrophone : ( value : boolean ) => void ,
25
- restartMicrophone : ( ) => void ;
22
+ restartDevices : ( ) => void ;
26
23
} ;
27
24
28
25
const LocalPeerMediaContext = React . createContext < LocalPeerContext | undefined > ( undefined ) ;
@@ -79,7 +76,6 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
79
76
const blurRef = useRef < boolean > ( false ) ;
80
77
81
78
const [ stream , setStream ] = useState < MediaStream | null > ( null ) ;
82
- // obecnie nadawany stram - może to być ciemność, może to być blur, może to być kamerka
83
79
const streamRef = useRef < MediaStream | null > ( null ) ;
84
80
const metadataActiveRef = useRef < boolean > ( true ) ;
85
81
@@ -92,40 +88,22 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
92
88
const offscreenCanvasRef = useRef < OffscreenCanvas | null > ( null ) ;
93
89
const remoteTrackIdRef = useRef < string | null > ( null ) ;
94
90
95
- // if user start or stop camera during it's decision is reflected in this variables
96
91
const cameraIntentionRef = useRef < boolean > ( true ) ;
97
92
const microphoneIntentionRef = useRef < boolean > ( true ) ;
98
93
const lastCameraIdRef = useRef < null | string > ( null ) ;
99
94
100
- // useEffect(() => {
101
- // const a = setInterval(() => {
102
- // console.log({ lastCameraIdRef: lastCameraIdRef.current, cameraIntention: cameraIntentionRef.current });
103
- // }, 3000);
104
- //
105
- // return () => {
106
- // clearInterval(a);
107
- // };
108
- // }, []);
109
-
110
95
const broadcastedStreamRef = useRef < MediaStream | null > ( null ) ;
111
96
112
97
const changeMediaStream = useCallback ( async ( stream : MediaStream | null , track : MediaStreamTrack | null , blur : boolean , metadataActive : boolean ) => {
113
- console . log ( { stream, track, blur, metadataActive } ) ;
114
98
metadataActiveRef . current = metadataActive ;
115
99
116
100
if ( processor . current && ! blur ) {
117
- console . log ( "Destroying processor" ) ;
118
101
processor . current . destroy ( ) ;
119
102
processor . current = null ;
120
103
}
121
104
122
105
if ( blur && stream ) {
123
- console . log ( { name : "Handle blur" , blur, stream } ) ;
124
- if ( processor . current && streamRef . current ?. id === stream . id ) {
125
- console . log ( "Ignoring blur" ) ;
126
- } else {
127
- console . log ( "Disabling old processor and creating new one" ) ;
128
-
106
+ if ( ! ( processor . current && streamRef . current ?. id === stream . id ) ) {
129
107
processor ?. current ?. destroy ( ) ;
130
108
processor . current = null ;
131
109
processor . current = new BlurProcessor ( stream ) ;
@@ -136,9 +114,7 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
136
114
streamRef . current = processor . current ?. stream || null ;
137
115
trackRef . current = processor . current ?. track || null ;
138
116
}
139
-
140
117
} else {
141
- console . log ( "Set stream and track" ) ;
142
118
setStream ( stream || null ) ;
143
119
setTrack ( track || null ) ;
144
120
@@ -148,7 +124,6 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
148
124
149
125
if ( client . getSnapshot ( ) . status === "joined" ) {
150
126
if ( ! remoteTrackIdRef . current && streamRef . current && trackRef . current ) {
151
- console . log ( "Adding track" ) ;
152
127
const mediaStream = new MediaStream ( ) ;
153
128
mediaStream . addTrack ( trackRef . current ) ;
154
129
@@ -163,8 +138,6 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
163
138
selectBandwidthLimit ( "camera" , simulcastEnabled )
164
139
) ;
165
140
} else if ( remoteTrackIdRef . current && trackRef . current ) {
166
- console . log ( "Replacing track" ) ;
167
-
168
141
// todo add setter as an alternative to setting whole object
169
142
broadcastedStreamRef . current ?. removeTrack ( broadcastedStreamRef . current ?. getVideoTracks ( ) [ 0 ] ) ;
170
143
broadcastedStreamRef . current ?. addTrack ( trackRef . current ) ;
@@ -173,15 +146,9 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
173
146
const newMetadata : TrackMetadata = { active : metadataActive , type : "camera" } ;
174
147
175
148
await client . replaceTrack ( remoteTrackIdRef . current , trackRef . current , newMetadata ) ;
176
- // todo ...
177
- // await client.updateTrackMetadata(remoteTrackIdRef.current, newMetadata);
178
149
} else if ( remoteTrackIdRef . current && ! stream ) {
179
- console . log ( "Removing track" ) ;
180
-
181
150
await client . removeTrack ( remoteTrackIdRef . current ) ;
182
151
remoteTrackIdRef . current = null ;
183
- } else {
184
- console . log ( "Skipping track" ) ;
185
152
}
186
153
}
187
154
} , [ setStream , setTrack ] ) ;
@@ -198,7 +165,6 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
198
165
199
166
const snapshot = client . getSnapshot ( ) ;
200
167
201
- console . log ( { name : "managerInitialized" , event } ) ;
202
168
const cameraId = snapshot ?. media ?. video ?. media ?. deviceInfo ?. deviceId || null ;
203
169
204
170
if ( cameraId ) {
@@ -236,68 +202,30 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
236
202
const stream = snapshot . devices . camera . stream ;
237
203
const track = snapshot . devices . camera . track ;
238
204
239
- console . log ( {
240
- name : "joinedHandler - method start" ,
241
- cameraIntentionRef : cameraIntentionRef . current ,
242
- lastCameraIdRef : lastCameraIdRef . current
243
- } ) ;
244
-
245
205
if ( cameraIntentionRef . current && ! remoteTrackIdRef . current && stream && track ) {
246
- console . log ( "First visit" ) ;
247
-
248
206
await changeMediaStream ( stream , track , blurRef . current , metadataActiveRef . current ) ;
249
207
} else if ( cameraIntentionRef . current && lastCameraIdRef . current ) {
250
- console . log ( "Rejoin" ) ;
251
- console . log ( "joinedHandler - start device if user wants it" ) ;
252
208
await snapshot . deviceManager . start ( { videoDeviceId : lastCameraIdRef . current } ) ;
253
- } else {
254
- console . log ( "joinedHandler - skipped" ) ;
255
209
}
256
210
257
211
const microphoneTrack = snapshot . devices . microphone . track ;
258
212
259
213
if ( microphoneIntentionRef . current && ! microphoneTrack ) {
260
- console . log ( "Restarting microphone" ) ;
261
214
await snapshot . deviceManager . start ( { audioDeviceId : true } ) ;
262
215
}
263
-
264
- // if (stream && track) {
265
- // // naiwnie zakłądam zę jest tylko kamera a nie czarne ekran lub rozmycie
266
- // const cameraId = snapshot?.media?.video?.media?.deviceInfo?.deviceId || null;
267
- // console.log({ name: "setting camera id - joinedHandler", cameraId, stream, track });
268
- //
269
- // lastCameraIdRef.current = cameraId;
270
- //
271
- // await changeMediaStream(stream, track, blurRef.current, !!snapshot.media?.video?.media?.stream);
272
- // } else if (cameraIntentionRef.current && lastCameraIdRef.current) {
273
- // // todo:
274
- // // we need another method that starts the last stopped one
275
- // console.log("Jest intencja uruchomionej kamery");
276
- // await snapshot.deviceManager.start({ videoDeviceId: lastCameraIdRef.current });
277
- // }
278
216
} ;
279
217
280
218
const deviceReady : ClientEvents < PeerMetadata , TrackMetadata > [ "deviceReady" ] = async ( event , client ) => {
281
219
const snapshot = client . getSnapshot ( ) ;
282
220
283
- console . log ( { name : "Device ready" , event } ) ;
284
-
285
221
const cameraId = snapshot . media ?. video ?. media ?. deviceInfo ?. deviceId ;
286
222
if ( event . trackType === "video" && event . mediaDeviceType === "userMedia" && cameraId ) {
287
- // todo remove? what about autost
288
- // cameraIntentionRef.current = true;
289
-
290
- console . log ( { name : "setting camera id - device ready" , cameraId } ) ;
291
223
lastCameraIdRef . current = cameraId ;
292
- // if (event.trackType === "audio") {
293
- // // microphoneIntentionRef.current = true;
294
- // }
295
224
296
225
const stream = snapshot ?. media ?. video ?. media ?. stream ;
297
226
const track = snapshot ?. media ?. video ?. media ?. track ;
298
227
299
228
if ( snapshot . status === "joined" && event . trackType === "video" && stream && track ) {
300
- // why is there remoteTrackIdRef.current?
301
229
workerRef . current ?. postMessage ( { action : "stop" } , [ ] ) ; // todo what is the second parameter
302
230
await changeMediaStream ( stream , track , blurRef . current , track . enabled ) ;
303
231
}
@@ -307,44 +235,30 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
307
235
const devicesReady : ClientEvents < PeerMetadata , TrackMetadata > [ "devicesReady" ] = async ( event , client ) => {
308
236
const snapshot = client . getSnapshot ( ) ;
309
237
310
- console . log ( { name : "devicesReady" , event } ) ;
311
238
const cameraId = snapshot ?. media ?. video ?. media ?. deviceInfo ?. deviceId || null ;
312
239
313
240
if ( cameraId ) {
314
241
lastCameraIdRef . current = cameraId ;
315
242
}
316
243
317
244
if ( event . video . restarted && event . video ?. media ?. stream ) {
318
- console . log ( { name : "devicesReady -> changeMediaStream" , cameraId } ) ;
319
245
lastCameraIdRef . current = cameraId ;
320
246
321
247
await changeMediaStream ( event . video ?. media ?. stream || null , event . video ?. media ?. track || null , blurRef . current , true ) ;
322
248
}
323
249
} ;
324
250
325
251
const deviceStopped : ClientEvents < PeerMetadata , TrackMetadata > [ "deviceStopped" ] = async ( event , client ) => {
326
- console . log ( { name : "device stopped" , event } ) ;
327
-
328
252
const snapshot = client . getSnapshot ( ) ;
329
253
330
- if ( event . trackType === "video" && event . mediaDeviceType === "userMedia" ) {
331
- // todo to nie jest prawda, intencja na stop jest wtedy gdy użytkownik to wciśnie
332
- // cameraIntentionRef.current = false;
333
- }
334
- if ( event . trackType === "audio" && event . mediaDeviceType === "userMedia" ) {
335
- // microphoneIntentionRef.current = false;
336
- }
337
-
338
254
if ( snapshot . status !== "joined" && event . trackType === "video" && event . mediaDeviceType === "userMedia" ) {
339
- // w poczeklani trzeba wynullować tracki
340
255
setStream ( null ) ;
341
256
setTrack ( null ) ;
342
257
343
258
streamRef . current = null ;
344
259
trackRef . current = null ;
345
260
}
346
261
if ( snapshot . status === "joined" && event . trackType === "video" && event . mediaDeviceType === "userMedia" && trackRef . current && remoteTrackIdRef . current ) {
347
- // jeżeli jesteśmy połączeni to trzeba podmienić na blackscreen
348
262
if ( ! workerRef . current ) {
349
263
workerRef . current = new EmptyVideoWorker ( ) ;
350
264
const canvasElement = document . createElement ( "canvas" ) ;
@@ -383,66 +297,31 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
383
297
}
384
298
} ;
385
299
386
- const localTrackMetadataChanged : ClientEvents < PeerMetadata , TrackMetadata > [ "localTrackMetadataChanged" ] = async ( event , client ) => {
387
- const snapshot = client . getSnapshot ( ) ;
388
-
389
- console . log ( { name : "localTrackMetadataChanged" , event, client, snapshot } ) ;
390
- } ;
391
-
392
- const disconnectRequested : ClientEvents < PeerMetadata , TrackMetadata > [ "disconnectRequested" ] = async ( event , client ) => {
393
- const snapshot = client . getSnapshot ( ) ;
394
-
395
- console . log ( { name : "disconnectRequested" , event, client, snapshot } ) ;
396
- } ;
397
-
398
- const authSuccess : ClientEvents < PeerMetadata , TrackMetadata > [ "authSuccess" ] = async ( client ) => {
399
- console . log ( { name : "authSuccess" , client } ) ;
400
- } ;
401
-
402
- const socketOpen : ClientEvents < PeerMetadata , TrackMetadata > [ "socketOpen" ] = async ( client ) => {
403
- console . log ( { name : "socketOpen" , client } ) ;
404
- } ;
405
-
406
300
const disconnected : ClientEvents < PeerMetadata , TrackMetadata > [ "disconnected" ] = async ( client ) => {
407
301
const snapshot = client . getSnapshot ( ) ;
408
302
409
- console . log ( { name : "disconnected" , client, snapshot } ) ;
410
303
remoteTrackIdRef . current = null ;
411
304
412
305
if ( snapshot . devices . microphone . stream ) snapshot . devices . microphone . stop ( ) ;
413
306
if ( snapshot . devices . camera . stream ) snapshot . devices . camera . stop ( ) ;
414
307
if ( snapshot . devices . screenShare . stream ) snapshot . devices . screenShare . stop ( ) ;
415
308
} ;
416
309
417
- const devicesStarted : ClientEvents < PeerMetadata , TrackMetadata > [ "devicesStarted" ] = async ( event , client ) => {
418
- console . log ( { name : "devicesStarted" , event, client } ) ;
419
- } ;
420
-
421
310
422
311
client . on ( "joined" , joinedHandler ) ;
423
- client . on ( "localTrackMetadataChanged" , localTrackMetadataChanged ) ;
424
312
client . on ( "managerInitialized" , managerInitialized ) ;
425
313
client . on ( "deviceReady" , deviceReady ) ;
426
314
client . on ( "devicesReady" , devicesReady ) ;
427
315
client . on ( "deviceStopped" , deviceStopped ) ;
428
- client . on ( "disconnectRequested" , disconnectRequested ) ;
429
- client . on ( "socketOpen" , socketOpen ) ;
430
- client . on ( "authSuccess" , authSuccess ) ;
431
316
client . on ( "disconnected" , disconnected ) ;
432
- client . on ( "devicesStarted" , devicesStarted ) ;
433
317
434
318
return ( ) => {
435
319
client . removeListener ( "joined" , joinedHandler ) ;
436
- client . removeListener ( "localTrackMetadataChanged" , localTrackMetadataChanged ) ;
437
320
client . removeListener ( "managerInitialized" , managerInitialized ) ;
438
321
client . removeListener ( "deviceReady" , deviceReady ) ;
439
322
client . removeListener ( "devicesReady" , devicesReady ) ;
440
323
client . removeListener ( "deviceStopped" , deviceStopped ) ;
441
- client . removeListener ( "disconnectRequested" , disconnectRequested ) ;
442
- client . removeListener ( "socketOpen" , socketOpen ) ;
443
- client . removeListener ( "authSuccess" , authSuccess ) ;
444
324
client . removeListener ( "disconnected" , disconnected ) ;
445
- client . removeListener ( "devicesStarted" , devicesStarted ) ;
446
325
447
326
} ;
448
327
} , [ simulcast . status ] ) ;
@@ -475,10 +354,6 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
475
354
mediaStatus : video . mediaStatus
476
355
} ) , [ stream , track ] ) ;
477
356
478
- useEffect ( ( ) => {
479
- console . log ( { newVideo, stream, track } ) ;
480
- } , [ newVideo , stream , track ] ) ;
481
-
482
357
const microphone = useMicrophone ( ) ;
483
358
484
359
const setDevice = useCallback ( async ( cameraId : string | null , microphoneId : string | null , blur : boolean ) => {
@@ -504,7 +379,6 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
504
379
} , [ video ] ) ;
505
380
506
381
const toggleCamera = useCallback ( ( value : boolean ) => {
507
- console . log ( { name : "Toggle camera" , value } ) ;
508
382
cameraIntentionRef . current = value ;
509
383
510
384
if ( value ) {
@@ -516,7 +390,6 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
516
390
} , [ ] ) ;
517
391
518
392
const toggleMicrophone = useCallback ( ( value : boolean ) => {
519
- console . log ( { name : "Toggle microphone" , value } ) ;
520
393
microphoneIntentionRef . current = value ;
521
394
if ( value ) {
522
395
microphone . start ( ) ;
@@ -525,15 +398,13 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
525
398
}
526
399
} , [ ] ) ;
527
400
528
- const restartMicrophone = useCallback ( ( ) => {
401
+ const restartDevices = useCallback ( ( ) => {
529
402
const micStatus = client . getSnapshot ( ) . media ?. audio ?. mediaStatus ;
530
403
if ( managerInitializedRef . current && microphoneIntentionRef . current && micStatus === "OK" ) {
531
- console . log ( "Restarting mic!" ) ;
532
404
toggleMicrophone ( true ) ;
533
405
}
534
406
const camStatus = client . getSnapshot ( ) . media ?. video ?. mediaStatus ;
535
407
if ( managerInitializedRef . current && cameraIntentionRef . current && camStatus === "OK" ) {
536
- console . log ( "Restarting camStatus!" ) ;
537
408
toggleCamera ( true ) ;
538
409
}
539
410
} , [ toggleMicrophone , toggleCamera ] ) ;
@@ -548,7 +419,7 @@ export const LocalPeerMediaProvider = ({ children }: Props) => {
548
419
setDevice,
549
420
toggleCamera,
550
421
toggleMicrophone,
551
- restartMicrophone
422
+ restartDevices
552
423
} }
553
424
>
554
425
{ children }
0 commit comments