-
Notifications
You must be signed in to change notification settings - Fork 9
/
useRemoteUserTrack.ts
149 lines (136 loc) · 4.75 KB
/
useRemoteUserTrack.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
import type {
IAgoraRTCClient,
IAgoraRTCError,
IAgoraRTCRemoteUser,
IRemoteAudioTrack,
IRemoteVideoTrack,
} from "agora-rtc-sdk-ng";
import { useEffect, useRef, useState } from "react";
import { AgoraRTCReactError } from "../error";
import { useRTCClient } from "../hooks/useRTCClient";
import { listen } from "../misc/listen";
import type { AsyncTaskRunner } from "../misc/utils";
import { createAsyncTaskRunner, joinDisposers } from "../misc/utils";
import { useIsConnected } from "./useIsConnected";
/**
* This hook lets you retrieve the audio or video track of a remote user.
*
* @param user - The remote user.
* @param mediaType - The media type. Pass `"video"` or `"audio"`.
* @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, useRemoteUserTrack } from "agora-rtc-react";
*
* function App() {
* const remoteUsers = useRemoteUsers();
*
* const videoTrack = useRemoteUserTrack(remoteUsers[0], "video");
* const audioTrack = useRemoteUserTrack(remoteUsers[0], "audio");
*
* return <></>;
* }
* ```
*/
export function useRemoteUserTrack(
user: IAgoraRTCRemoteUser | undefined,
mediaType: "video",
client?: IAgoraRTCClient | null,
): { track: IRemoteVideoTrack | undefined; isLoading: boolean; error: AgoraRTCReactError | null };
export function useRemoteUserTrack(
user: IAgoraRTCRemoteUser | undefined,
mediaType: "audio",
client?: IAgoraRTCClient | null,
): { track: IRemoteAudioTrack | undefined; isLoading: boolean; error: AgoraRTCReactError | null };
export function useRemoteUserTrack(
user: IAgoraRTCRemoteUser | undefined,
mediaType: "video" | "audio",
client?: IAgoraRTCClient | null,
): {
track: IRemoteVideoTrack | IRemoteAudioTrack | undefined;
isLoading: boolean;
error: AgoraRTCReactError | null;
} {
const resolvedClient = useRTCClient(client);
const trackName = mediaType === "audio" ? "audioTrack" : "videoTrack";
const [track, setTrack] = useState(user && user[trackName]);
const isConnected = useIsConnected();
const runnerRef = useRef<AsyncTaskRunner | undefined>();
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<AgoraRTCReactError | null>(null);
useEffect(() => {
if (!user || !isConnected) return;
let isUnmounted = false;
if (!isUnmounted) {
setError(null);
}
const hasTrack = mediaType === "audio" ? "hasAudio" : "hasVideo";
const uid = user.uid;
const unsubscribe = async (
user: IAgoraRTCRemoteUser,
mediaType: "audio" | "video",
): Promise<void> => {
if (user[trackName] && resolvedClient.remoteUsers.some(({ uid }) => user.uid === uid)) {
try {
if (!isUnmounted) {
setIsLoading(true);
}
await resolvedClient.unsubscribe(user, mediaType);
} catch (err) {
if (!isUnmounted) {
setError(new AgoraRTCReactError("IAgoraRTCClient.unsubscribe", err as IAgoraRTCError));
}
console.error(err);
}
}
if (!isUnmounted) {
setTrack(void 0);
setIsLoading(false);
}
};
const subscribe = async (user: IAgoraRTCRemoteUser, mediaType: "audio" | "video") => {
try {
if (!user[trackName] && resolvedClient.remoteUsers.some(({ uid }) => user.uid === uid)) {
if (!isUnmounted) {
setIsLoading(true);
}
await resolvedClient.subscribe(user, mediaType);
}
if (!isUnmounted) {
setTrack(user[trackName]);
}
} catch (err) {
if (!isUnmounted) {
setError(new AgoraRTCReactError("IAgoraRTCClient.subscribe", err as IAgoraRTCError));
}
console.error(err);
}
if (!isUnmounted) {
setIsLoading(false);
}
};
const runner = (runnerRef.current ||= createAsyncTaskRunner());
if (!user[trackName] && user[hasTrack]) {
runner.run(() => subscribe(user, mediaType));
} else {
setTrack(user[trackName]);
}
return joinDisposers([
() => {
isUnmounted = true;
runner.dispose();
},
listen(resolvedClient, "user-published", (pubUser, pubMediaType) => {
if (pubUser.uid === uid && pubMediaType === mediaType) {
runner.run(() => subscribe(pubUser, mediaType));
}
}),
listen(resolvedClient, "user-unpublished", (pubUser, pubMediaType) => {
if (pubUser.uid === uid && pubMediaType === mediaType) {
runner.run(() => unsubscribe(pubUser, mediaType));
}
}),
]);
}, [isConnected, resolvedClient, user, mediaType, trackName]);
return { track: track, isLoading: isLoading, error: error };
}