-
Notifications
You must be signed in to change notification settings - Fork 0
/
oldStudent.jsx
242 lines (199 loc) · 9.12 KB
/
oldStudent.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// import React, {useEffect,useRef} from "react";
// import { Device } from 'mediasoup-client';
// const setupDeviceAndJoin = async (ws) => {
// try {
// const device = new Device();
// // Fetch the router RTP capabilities from the server
// const response = await fetch('http://localhost:4000/rtpCapabilities');
// const routerRtpCapabilities = await response.json();
// console.log(routerRtpCapabilities)
// // Load the device with the router's RTP capabilities
// await device.load({ routerRtpCapabilities });
// // Get the client's RTP capabilities
// const rtpCapabilities = device.rtpCapabilities;
// // Send the client's RTP capabilities to the server to join
// ws.send(JSON.stringify({
// type: 'join',
// peerId: 'student', // or 'teacher'
// rtpCapabilities,
// }));
// } catch (error) {
// console.error('Error setting up device:', error);
// }
// };
// export const StudentsScreenReceiver = () => {
// const videoRef = useRef(null);
// const deviceRef = useRef(null);
// useEffect(() => {
// const ws = new WebSocket('ws://localhost:4000');
// ws.onopen = () => {
// setupDeviceAndJoin(ws,(device)=>(deviceRef.current = device));
// console.log("student connected to websocket server");
// }
// ws.onmessage = async (event) => {
// const data = JSON.parse(event.data)
// console.log("Bhallaladev",data);
// if(data.type === 'newScreenShare'){
// console.log("i ma in the if",data)
// const { producerId,id,kind,rtpParameters:rtpParametersJson } = data;
// const rtpParameters = rtpParametersJson ? JSON.parse(rtpParametersJson):undefined;
// console.log(`Producer id : ${producerId} kind:${kind} rtpParameters:${rtpParameters}`)
// console.log("kkkkkeeeeerrrrthhi");
// console.log(deviceRef.current)
// if (deviceRef.current && kind === 'video'){
// try{
// // Create a transport to receive the screen share stream
// const transport = await deviceRef.current.createRecvTransport({
// id:id,
// iceParameters : data.iceParameters,
// iceCandidates : data.iceCandidates,
// dtlsParameters : data.dtlsParameters,
// });
// // consume the screen share stream.
// const consumer = await transport.consume({
// id,
// producerId,
// kind,
// rtpParameters
// });
// // Attach the received stream to the video element.
// const stream = new MediaStream();
// stream.addTrack(consumer.track);
// videoRef.current.srcObject = stream;
// }catch(err){
// console.error('Error Consuming Screen Share:', err);
// }
// }
// }
// }
// return () => ws.close(); // cleanup websocket
// },[])
// return (
// <div>
// <h2>
// Student View.
// </h2>
// <video ref={videoRef} autoPlay playsInline muted />
// </div>
// )
// }
import React, { useEffect, useRef } from "react";
import { Device } from 'mediasoup-client';
const setupDeviceAndJoin = async (ws, deviceRef) => {
try {
const device = new Device();
// Fetch the router RTP capabilities from the server
const response = await fetch('http://localhost:4000/rtpCapabilities');
const routerRtpCapabilities = await response.json();
console.log('Fetched routerRtpCapabilities:', routerRtpCapabilities);
// Load the device with the router's RTP capabilities
await device.load({ routerRtpCapabilities });
// Set deviceRef.current to the loaded device
deviceRef.current = device;
// Get the client's RTP capabilities
const rtpCapabilities = device.rtpCapabilities;
// Send the client's RTP capabilities to the server to join
ws.send(JSON.stringify({
type: 'join',
peerId: 'student',
rtpCapabilities,
}));
console.log("Device setup complete:", device);
} catch (error) {
console.error('Error setting up device:', error);
}
};
export const StudentsScreenReceiver = () => {
const videoRef = useRef(null);
const deviceRef = useRef(null);
const messageQueue = useRef([]); // Queue to hold WebSocket messages
useEffect(() => {
const ws = new WebSocket('ws://localhost:4000');
ws.onopen = () => {
setupDeviceAndJoin(ws, deviceRef);
console.log("Student connected to WebSocket server");
};
ws.onmessage = async (event) => {
const data = JSON.parse(event.data);
// Queue the message if deviceRef.current is not yet ready
if (!deviceRef.current) {
messageQueue.current.push(data);
return;
}
// Process the message directly if deviceRef.current is ready
await handleScreenShareMessage(data);
};
// Function to handle screen share messages
const handleScreenShareMessage = async (data) => {
if (data.type === 'newScreenShare') {
const { producerId, kind, rtpParameters: rtpParametersJson } = data;
const rtpParameters = rtpParametersJson ? JSON.parse(rtpParametersJson) : undefined;
console.log("id",data.peerId)
console.log(`Producer id: ${producerId}, kind: ${kind}, rtpParameters: ${JSON.stringify(rtpParameters, null, 2)}`);
if (deviceRef.current && kind === 'video') {
try {
console.log("hiiiiiiiiiiiiiiiii")
// Create a transport to receive the screen share stream
const transport = await deviceRef.current.createRecvTransport({
id: data.peerId,
iceParameters: data.iceParameters,
iceCandidates: data.iceCandidates,
dtlsParameters: data.dtlsParameters,
});
// Add the required 'connect' listener to the transport
transport.on('connect', ({ dtlsParameters }, callback, errback) => {
// Send the DTLS parameters to the server to complete the connection
ws.send(JSON.stringify({
type: 'connectTransport',
transportId: transport.id,
dtlsParameters
}));
// Use callback to signal success, errback for failure
ws.onmessage = (event) => {
const response = JSON.parse(event.data);
if (response.type === 'connectTransportSuccess') {
callback();
} else if (response.type === 'connectTransportError') {
errback(new Error('Failed to connect transport on server'));
}
};
});
// Consume the screen share stream
const consumer = await transport.consume({
id: data.peerId,
producerId,
kind,
rtpParameters
});
// Attach the received stream to the video element
const stream = new MediaStream();
stream.addTrack(consumer.track);
videoRef.current.srcObject = stream;
} catch (err) {
console.error('Error Consuming Screen Share:', err);
}
}
}
};
// Process any queued messages once deviceRef.current is ready
const processQueuedMessages = () => {
while (messageQueue.current.length > 0) {
const message = messageQueue.current.shift();
handleScreenShareMessage(message);
}
};
// Watch deviceRef to process queued messages as soon as device is ready
useEffect(() => {
if (deviceRef.current) {
processQueuedMessages();
}
}, [deviceRef.current]);
return () => ws.close(); // Cleanup WebSocket on component unmount
}, []);
return (
<div>
<h2>Student View</h2>
<video ref={videoRef} autoPlay controls />
</div>
);
};