Skip to content

Commit d554b81

Browse files
committed
refactor rtc
1 parent c613b74 commit d554b81

File tree

6 files changed

+53
-60
lines changed

6 files changed

+53
-60
lines changed

src/components/rtc/components/PeerConnectionSession.ts

+30-31
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,37 @@ import io from 'socket.io-client';
44
const { RTCPeerConnection, RTCSessionDescription } = window;
55

66
class PeerConnectionSession {
7-
mOnConnected: any;
8-
mOnDisconnected: any;
9-
mRoom: any;
10-
peerConnections: { [key: string]: any } = {};
11-
senders: any = [];
12-
listeners: { [key: string]: any } = {};
13-
socket: any;
14-
15-
constructor(socket: any) {
7+
mOnConnected;
8+
mOnDisconnected;
9+
mRoom;
10+
peerConnections = {};
11+
senders = [];
12+
listeners = {};
13+
socket;
14+
15+
constructor(socket) {
1616
this.socket = socket;
1717
this.onCallMade();
1818
}
1919

20-
addPeerConnection(id: any, stream: any, callback: any) {
21-
console.log('mOnConnected', this.mOnConnected);
20+
addPeerConnection(id, stream, callback) {
2221
this.peerConnections[id] = new RTCPeerConnection({
2322
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
2423
});
25-
stream.getTracks().forEach((track: any) => {
24+
stream.getTracks().forEach((track) => {
2625
this.senders.push(this.peerConnections[id].addTrack(track, stream));
2726
});
2827

29-
this.listeners[id] = (event: any) => {
28+
this.listeners[id] = (event) => {
3029
let fn;
3130
if (this.peerConnections[id].connectionState === 'connected') {
31+
console.log('mOnConnected', this.mOnConnected);
3232
fn = this.mOnConnected;
3333
} else if (this.peerConnections[id].connectionState === 'disconnected') {
34+
console.log('mOnDisconnected', this.mOnDisconnected);
3435
fn = this.mOnDisconnected;
3536
} else {
37+
console.log('mRoom', this.mRoom);
3638
fn = this.mRoom;
3739
}
3840
if (fn === null) {
@@ -48,7 +50,7 @@ class PeerConnectionSession {
4850
this.peerConnections[id].ontrack = function ({
4951
streams: [stream],
5052
}: {
51-
streams: any;
53+
streams;
5254
}) {
5355
console.log({ id, stream });
5456
callback(stream);
@@ -79,21 +81,21 @@ class PeerConnectionSession {
7981
}
8082
}
8183

82-
onConnected(callback: any) {
84+
onConnected(callback) {
8385
this.mOnConnected = callback;
8486
}
8587

86-
onDisconnected(callback: any) {
88+
onDisconnected(callback) {
8789
this.mOnDisconnected = callback;
8890
}
8991

90-
joinRoom(room: any) {
92+
joinRoom(room) {
9193
this.mRoom = room;
9294
this.socket.emit('joinRoom', room);
9395
}
9496

9597
onCallMade() {
96-
this.socket.on('call-made', async (data: any) => {
98+
this.socket.on('call-made', async (data) => {
9799
await this.peerConnections[data.socket].setRemoteDescription(
98100
new RTCSessionDescription(data.offer),
99101
);
@@ -109,29 +111,26 @@ class PeerConnectionSession {
109111
});
110112
}
111113

112-
onAddUser(callback: any) {
113-
this.socket.on(`${this.mRoom}-add-user`, async ({ user }: any) => {
114+
onAddUser(callback) {
115+
this.socket.on(`${this.mRoom}-add-user`, async ({ user }) => {
114116
callback(user);
115117
});
116118
}
117119

118-
onRemoveUser(callback: any) {
119-
this.socket.on(`${this.mRoom}-remove-user`, ({ socketId }: any) => {
120+
onRemoveUser(callback) {
121+
this.socket.on(`${this.mRoom}-remove-user`, ({ socketId }) => {
120122
callback(socketId);
121123
});
122124
}
123125

124-
onUpdateUserList(callback: any) {
125-
this.socket.on(
126-
`${this.mRoom}-update-user-list`,
127-
({ users, current }: any) => {
128-
callback(users, current);
129-
},
130-
);
126+
onUpdateUserList(callback) {
127+
this.socket.on(`${this.mRoom}-update-user-list`, ({ users, current }) => {
128+
callback(users, current);
129+
});
131130
}
132131

133-
onAnswerMade(callback: any) {
134-
this.socket.on('answer-made', async (data: any) => {
132+
onAnswerMade(callback) {
133+
this.socket.on('answer-made', async (data) => {
135134
await this.peerConnections[data.socket].setRemoteDescription(
136135
new RTCSessionDescription(data.answer),
137136
);

src/components/rtc/components/RemoteVideo.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import React, { useEffect, useState } from 'react';
44
import { Video, VideoContainer } from './Video';
55

6-
export function RemoteVideo(props: any) {
6+
export function RemoteVideo(props) {
77
type MediaProvider = MediaStream | MediaSource | Blob;
88
const [mediaStream, setMediaStream] = useState<MediaProvider>();
99

src/components/rtc/hooks/useStartPeerSession.ts

+18-24
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,26 @@ import { RefObject, useEffect, useMemo, useState } from 'react';
33
import { createPeerConnectionContext } from '../components/PeerConnectionSession';
44

55
export const useStartPeerSession = (
6-
room: any,
7-
userMediaStream: any,
6+
room: string,
7+
userMediaStream: MediaStream,
88
localVideoRef: RefObject<HTMLVideoElement>,
99
) => {
10-
const peerVideoConnection: any = useMemo(
11-
() => createPeerConnectionContext(),
12-
[],
13-
);
10+
const peerVideoConnection = useMemo(() => createPeerConnectionContext(), []);
1411

1512
const [displayMediaStream, setDisplayMediaStream] = useState<MediaStream>();
1613
const [connectedUsers, setConnectedUsers] = useState([]);
1714

1815
useEffect(() => {
1916
if (userMediaStream) {
17+
console.log('mediaStream', userMediaStream);
2018
peerVideoConnection.joinRoom(room);
21-
peerVideoConnection.onAddUser((user: any) => {
22-
setConnectedUsers((users: any): any => [...users, user]);
19+
peerVideoConnection.onAddUser((user: string) => {
20+
setConnectedUsers((users) => [...users, user]);
2321

2422
peerVideoConnection.addPeerConnection(
2523
`${user}`,
2624
userMediaStream,
27-
(_stream: any) => {
25+
(_stream) => {
2826
if (user) {
2927
const box = <HTMLVideoElement>document.getElementById(user);
3028
box.srcObject = _stream;
@@ -40,13 +38,13 @@ export const useStartPeerSession = (
4038
peerVideoConnection.removePeerConnection(socketId);
4139
});
4240

43-
peerVideoConnection.onUpdateUserList(async (users: any) => {
41+
peerVideoConnection.onUpdateUserList(async (users) => {
4442
setConnectedUsers(users);
45-
users.forEach((user: any) => {
43+
users.forEach((user: string) => {
4644
peerVideoConnection.addPeerConnection(
4745
`${user}`,
4846
userMediaStream,
49-
(_stream: any) => {
47+
(_stream) => {
5048
if (user) {
5149
const box = <HTMLVideoElement>document.getElementById(user);
5250
box.srcObject = _stream;
@@ -56,7 +54,7 @@ export const useStartPeerSession = (
5654
});
5755
});
5856

59-
peerVideoConnection.onAnswerMade((socket: any) =>
57+
peerVideoConnection.onAnswerMade((socket) =>
6058
peerVideoConnection.callUser(socket),
6159
);
6260
console.log(peerVideoConnection);
@@ -65,22 +63,20 @@ export const useStartPeerSession = (
6563
return () => {
6664
if (userMediaStream) {
6765
peerVideoConnection.clearConnections();
68-
userMediaStream?.getTracks()?.forEach((track: any) => track.stop());
66+
userMediaStream?.getTracks()?.forEach((track) => track.stop());
6967
}
7068
};
7169
}, [peerVideoConnection, room, userMediaStream]);
7270

7371
const cancelScreenSharing = async () => {
7472
const senders = await peerVideoConnection.senders.filter(
75-
(sender: any) => sender.track.kind === 'video',
73+
(sender) => sender.track.kind === 'video',
7674
);
7775

7876
if (senders) {
79-
senders.forEach((sender: any) =>
77+
senders.forEach((sender) =>
8078
sender.replaceTrack(
81-
userMediaStream
82-
.getTracks()
83-
.find((track: any) => track.kind === 'video'),
79+
userMediaStream.getTracks().find((track) => track.kind === 'video'),
8480
),
8581
);
8682
}
@@ -89,7 +85,7 @@ export const useStartPeerSession = (
8985
localVideoRef.current.srcObject = userMediaStream;
9086
}
9187
if (displayMediaStream) {
92-
displayMediaStream.getTracks().forEach((track: any) => track.stop());
88+
displayMediaStream.getTracks().forEach((track) => track.stop());
9389
}
9490
setDisplayMediaStream(undefined);
9591
};
@@ -99,13 +95,11 @@ export const useStartPeerSession = (
9995
displayMediaStream || (await navigator.mediaDevices.getDisplayMedia());
10096

10197
const senders = await peerVideoConnection.senders.filter(
102-
(sender: any) => sender.track.kind === 'video',
98+
(sender) => sender.track.kind === 'video',
10399
);
104100

105101
if (senders) {
106-
senders.forEach((sender: any) =>
107-
sender.replaceTrack(stream.getTracks()[0]),
108-
);
102+
senders.forEach((sender) => sender.replaceTrack(stream.getTracks()[0]));
109103
}
110104

111105
stream.getVideoTracks()[0].addEventListener('ended', () => {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { RefObject } from 'react';
22

33
export default interface useStartPeerSession {
4-
room: any;
5-
userMediaStream: any;
4+
room;
5+
userMediaStream;
66
localVideoRef: RefObject<HTMLVideoElement> | null;
77
}

src/pages/Room.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default function MainPage() {
3131
<LocalVideo ref={localVideoRef} />
3232
{connectedUsers.map((user: string) => (
3333
<>
34-
<p>{user}</p>
34+
<p>USER : {user}</p>
3535
<RemoteVideo key={user} id={user} autoPlay playsInline />
3636
</>
3737
))}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
7777

7878
/* Type Checking */
79-
"strict": true /* Enable all strict type-checking options. */,
79+
"strict": false /* Enable all strict type-checking options. */,
8080
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
8181
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
8282
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */

0 commit comments

Comments
 (0)