-
Notifications
You must be signed in to change notification settings - Fork 9
/
useRemoteVideoTracks.ts
170 lines (159 loc) · 5.63 KB
/
useRemoteVideoTracks.ts
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
import type {
IAgoraRTCClient,
IAgoraRTCError,
IAgoraRTCRemoteUser,
IRemoteVideoTrack,
} from "agora-rtc-sdk-ng";
import { useRef, useState } from "react";
import { AgoraRTCReactError } from "../error";
import { useRTCClient } from "../hooks/useRTCClient";
import { listen } from "../misc/listen";
import { joinDisposers } from "../misc/utils";
import { useAsyncEffect, useIsUnmounted } from "./tools";
import type { massUserProps } from "./types";
import { useIsConnected } from "./useIsConnected";
/**
* This hook lets you automatically subscribe to and retrieve remote users' video tracks.
* When the component is unmounted, this hook stops subscribing to the video tracks of the specified users.
* This hook updates the subscribed video tracks when the users parameter changes.
*
* @param users - The list of remote users.
* @param client - Created using the Web SDK's [`IAgoraRTC.createClient`](https://api-ref.agora.io/en/video-sdk/web/4.x/interfaces/iagorartc.html#createclient) method.
* @example
* ```jsx
* import { useRemoteUsers, useRemoteVideoTracks } from "agora-rtc-react";
*
* function App() {
* const remoteUsers = useRemoteUsers();
* const videoTracks = useRemoteVideoTracks(remoteUsers);
*
* return <></>;
* }
* ```
*/
export function useRemoteVideoTracks(
users: IAgoraRTCRemoteUser[] | undefined,
client?: IAgoraRTCClient | null,
): { videoTracks: IRemoteVideoTrack[]; isLoading: boolean; error: AgoraRTCReactError | null } {
const resolvedClient = useRTCClient(client);
const [tracks, setTracks] = useState<IRemoteVideoTrack[]>([]);
const isConnected = useIsConnected();
const nextTracks = useRef<IRemoteVideoTrack[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<AgoraRTCReactError | null>(null);
const isUnmountRef = useIsUnmounted();
useAsyncEffect(async () => {
if (!isUnmountRef.current) {
setError(null);
}
if (!Array.isArray(users) || !isConnected) return;
const subscribe = async (user: IAgoraRTCRemoteUser) => {
if (!user.videoTrack && users.some(({ uid }) => user.uid === uid)) {
try {
if (!isUnmountRef.current) {
setIsLoading(true);
}
await resolvedClient.subscribe(user, "video");
} catch (err) {
console.error(err);
if (!isUnmountRef.current) {
setError(new AgoraRTCReactError("IAgoraRTCClient.subscribe", err as IAgoraRTCError));
}
}
if (user.videoTrack && !nextTracks.current.some(track => track.getUserId() === user.uid)) {
nextTracks.current.push(user.videoTrack);
}
// when hot update mode, track will change every time, so need to update nextTracks
nextTracks.current = nextTracks.current.map(track => {
if (
user.videoTrack &&
track.getUserId() === user.uid &&
track.getTrackId() !== user.videoTrack.getTrackId()
) {
return user.videoTrack;
} else {
return track;
}
});
if (!isUnmountRef.current) {
setTracks(nextTracks.current);
setIsLoading(false);
}
}
};
const unsubscribe = async (user: IAgoraRTCRemoteUser): Promise<void> => {
if (users.some(({ uid }) => user.uid === uid)) {
if (!isUnmountRef.current) {
nextTracks.current = nextTracks.current.filter(track => track.getUserId() !== user.uid);
setTracks(nextTracks.current);
}
try {
if (!isUnmountRef.current) {
setIsLoading(true);
}
await resolvedClient.unsubscribe(user, "video");
} catch (err) {
console.error(err);
if (!isUnmountRef.current) {
setError(new AgoraRTCReactError("IAgoraRTCClient.unsubscribe", err as IAgoraRTCError));
}
}
if (!isUnmountRef.current) {
setIsLoading(false);
}
}
};
users.map(user => {
if (!user.videoTrack && user.hasVideo) {
subscribe(user);
}
});
const unsubscribeList: massUserProps[] = [];
for (let i = 0; i < nextTracks.current.length; i++) {
const track = nextTracks.current[i];
if (!users.some(user => user.uid === track.getUserId())) {
const user = resolvedClient.remoteUsers.find(user => user.uid === track.getUserId());
if (user) {
unsubscribeList.push({
user,
mediaType: "video",
});
}
nextTracks.current.splice(i, 1);
i--;
}
}
if (unsubscribeList.length > 0) {
try {
if (!isUnmountRef.current) {
setIsLoading(true);
}
await resolvedClient.massUnsubscribe(unsubscribeList);
} catch (err) {
console.error(err);
if (!isUnmountRef.current) {
setError(
new AgoraRTCReactError("IAgoraRTCClient.massUnsubscribe", err as IAgoraRTCError),
);
}
}
if (!isUnmountRef.current) {
setTracks(nextTracks.current.slice());
setIsLoading(false);
}
}
return joinDisposers([
listen(resolvedClient, "user-published", (pubUser, pubMediaType) => {
if (users.find(user => user.uid === pubUser.uid) && pubMediaType === "video") {
subscribe(pubUser);
}
}),
listen(resolvedClient, "user-unpublished", (pubUser, pubMediaType) => {
if (users.find(user => user.uid === pubUser.uid) && pubMediaType === "video") {
unsubscribe(pubUser);
}
}),
]);
}, [isConnected, resolvedClient, users]);
return { videoTracks: tracks, isLoading: isLoading, error: error };
}