-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff3.txt
More file actions
191 lines (181 loc) · 6.91 KB
/
Copy pathdiff3.txt
File metadata and controls
191 lines (181 loc) · 6.91 KB
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
diff --git a/components/video/video-stream.tsx b/components/video/video-stream.tsx
index dc7ffaf7..26dba77e 100644
--- a/components/video/video-stream.tsx
+++ b/components/video/video-stream.tsx
@@ -11,50 +11,53 @@ const VideoStream: React.FC<VideoStreamProps> = ({ channelId }) => {
const localVideoRef = useRef<HTMLVideoElement>(null);
const remoteVideoRef = useRef<HTMLVideoElement>(null);
const peerConnection = useRef<RTCPeerConnection | null>(null);
+ const iceCandidatesBuffer = useRef<RTCIceCandidate[]>([]);
- useEffect(() => {
- if (!isConnected || !socket) {
- console.log('Socket is not connected yet.');
- return;
- }
+ const createPeerConnection = () => {
+ peerConnection.current = new RTCPeerConnection({
+ iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
+ });
- console.log('Socket connected, initializing video stream logic.');
- }, [isConnected]);
+ peerConnection.current.onicecandidate = (event) => {
+ if (event.candidate) {
+ console.log('Emitting ICE candidate', event.candidate);
+ socket?.emit('ice-candidate', { channelId, candidate: event.candidate });
+ }
+ };
+ peerConnection.current.ontrack = (event) => {
+ console.log('Received remote track:', event.streams[0]);
+ if (remoteVideoRef.current) {
+ remoteVideoRef.current.srcObject = event.streams[0];
+ console.log('Remote video stream set successfully.');
+ } else {
+ console.warn('Remote video element not available.');
+ }
+ };
+
+ return peerConnection.current;
+ };
+
+ const handleIceCandidate = async (candidate: RTCIceCandidate) => {
+ if (peerConnection.current?.remoteDescription) {
+ await peerConnection.current.addIceCandidate(candidate);
+ } else {
+ iceCandidatesBuffer.current.push(candidate);
+ }
+ };
useEffect(() => {
if (!socket || !isConnected) return;
- const initializePeerConnection = async () => {
- peerConnection.current = new RTCPeerConnection({
- iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
- });
-
- const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
- if (localVideoRef.current) {
- localVideoRef.current.srcObject = stream;
- }
-
- stream.getTracks().forEach((track) => {
- peerConnection.current?.addTrack(track, stream);
- });
-
- peerConnection.current.ontrack = (event) => {
- if (remoteVideoRef.current) {
- remoteVideoRef.current.srcObject = event.streams[0];
- }
- };
-
- peerConnection.current.onicecandidate = (event) => {
- if (event.candidate) {
- console.log('Emitting ICE candidate', event.candidate);
- socket.emit('ice-candidate', { channelId, candidate: event.candidate });
+ const initializeMediaStream = async () => {
+ try {
+ const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
+ if (localVideoRef.current) {
+ localVideoRef.current.srcObject = stream;
}
- };
-
- const offer = await peerConnection.current.createOffer();
- await peerConnection.current.setLocalDescription(offer);
- console.log('Emitting offer', offer);
- socket.emit('offer', { channelId, offer });
+ return stream;
+ } catch (error) {
+ console.error('Error accessing media devices:', error);
+ }
};
const joinChannel = () => {
@@ -62,45 +65,57 @@ const VideoStream: React.FC<VideoStreamProps> = ({ channelId }) => {
socket.emit('join-channel', channelId);
};
- const leaveChannel = () => {
- console.log(`Leaving channel ${channelId}`);
- socket.emit('leave-channel', channelId);
- };
-
-
joinChannel();
+
socket.on('user-joined', async () => {
console.log('Another user joined, creating offer');
- await initializePeerConnection();
+ const pc = createPeerConnection();
+ const stream = await initializeMediaStream();
+ stream?.getTracks().forEach((track) => pc.addTrack(track, stream));
+
+ const offer = await pc.createOffer();
+ await pc.setLocalDescription(offer);
+ console.log('Emitting offer', offer);
+ socket.emit('offer', { channelId, offer });
});
-
-
+
socket.on('offer', async ({ offer }) => {
- console.log(`offer happened ${offer}`)
- if (!peerConnection.current) await initializePeerConnection();
- await peerConnection.current?.setRemoteDescription(new RTCSessionDescription(offer));
- const answer = await peerConnection.current?.createAnswer();
- await peerConnection.current?.setLocalDescription(answer);
+ console.log(`Offer received:`, offer);
+ const pc = createPeerConnection();
+ const stream = await initializeMediaStream();
+ stream?.getTracks().forEach((track) => pc.addTrack(track, stream));
+
+ await pc.setRemoteDescription(new RTCSessionDescription(offer));
+ const answer = await pc.createAnswer();
+ await pc.setLocalDescription(answer);
+ console.log('Emitting answer:', answer);
socket.emit('answer', { channelId, answer });
- });
+ // Process buffered ICE candidates
+ while (iceCandidatesBuffer.current.length) {
+ const candidate = iceCandidatesBuffer.current.shift();
+ await pc.addIceCandidate(candidate!);
+ }
+ });
socket.on('answer', async ({ answer }) => {
- console.log(`answer happened ${answer}`)
- await peerConnection.current?.setRemoteDescription(new RTCSessionDescription(answer));
+ console.log(`Answer received:`, answer);
+ if (peerConnection.current?.signalingState === 'have-remote-offer') {
+ await peerConnection.current.setRemoteDescription(new RTCSessionDescription(answer));
+ } else {
+ console.warn('Ignoring answer because connection is in stable state.');
+ }
});
+
socket.on('ice-candidate', async ({ candidate }) => {
-
- console.log('Received ICE candidate', candidate);
-
- console.log(`ice-candidate happened ${answer}`)
-
- await peerConnection.current?.addIceCandidate(new RTCIceCandidate(candidate));
+ console.log('Received ICE candidate', candidate);
+ await handleIceCandidate(new RTCIceCandidate(candidate));
});
-
return () => {
- leaveChannel();
+ console.log(`Leaving channel ${channelId}`);
+ socket.emit('leave-channel', channelId);
+ socket.off('user-joined');
socket.off('offer');
socket.off('answer');
socket.off('ice-candidate');
@@ -111,9 +126,9 @@ const VideoStream: React.FC<VideoStreamProps> = ({ channelId }) => {
return (
<div>
<video ref={localVideoRef} autoPlay muted playsInline />
- <video ref={remoteVideoRef} autoPlay playsInline />
+ <video ref={remoteVideoRef} autoPlay playsInline muted />
</div>
);
};
-export default VideoStream;
+export default VideoStream;
\ No newline at end of file