-
Notifications
You must be signed in to change notification settings - Fork 6
/
TracksProvider.js
163 lines (141 loc) · 4.65 KB
/
TracksProvider.js
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
import React, {
useEffect,
useCallback,
createContext,
useContext,
useMemo,
useState,
} from "react";
import { useParticipants } from "./ParticipantProvider";
import { sortByKey } from "../libs/sortByKey";
// -- Constants
/**
* Maximum amount of concurrently subscribed or staged most recent speakers.
*/
const MAX_RECENT_SPEAKER_COUNT = 6;
export const TrackContext = createContext();
// -- Provider
export const TrackProvider = ({ callObject, children }) => {
const { participants } = useParticipants();
const [videoTracks, setVideoTracks] = useState({});
const recentSpeakerIds = useMemo(
() =>
participants
.filter((p) => Boolean(p.lastActiveDate) && !p.isLocal)
.sort((a, b) => sortByKey(a, b, "lastActiveDate"))
.slice(-MAX_RECENT_SPEAKER_COUNT)
.map((p) => p.id)
.reverse(),
[participants]
);
// All participants except the local client
const remoteParticipantIds = useMemo(
() => participants.filter((p) => !p.isLocal).map((p) => p.id),
[participants]
);
/**
* Updates cam subscriptions based on passed subscribedIds and stagedIds.
* For ids not provided, cam tracks will be unsubscribed from
*/
const updateCamSubscriptions = useCallback(
(subscribedIds, stagedIds = []) => {
if (!callObject) return;
// Append recent speakers to our staged list as we presume they will likely speak again
const stagedIdsFiltered = [
...stagedIds,
...recentSpeakerIds.filter((id) => !subscribedIds.includes(id)),
];
// Assemble updates to get to desired cam subscriptions
const updates = remoteParticipantIds.reduce((u, id) => {
let desiredSubscription;
const currentSubscription =
callObject.participants()?.[id]?.tracks?.video?.subscribed;
// Ignore undefined or local
if (!id || id === "local") return u;
// Decide on desired cam subscription for this participant:
// subscribed, staged, or unsubscribed
if (subscribedIds.includes(id)) {
desiredSubscription = true;
} else if (stagedIdsFiltered.includes(id)) {
desiredSubscription = "staged";
} else {
desiredSubscription = false;
}
// Skip if we already have the desired subscription to this
// participant's cam
if (desiredSubscription === currentSubscription) return u;
// Log out changes
console.log(
`%cParticipant ${id} cam subscription: ${desiredSubscription}`,
`color: ${desiredSubscription ? "green" : "red"}`
);
return {
...u,
[id]: {
setSubscribedTracks: {
video: desiredSubscription,
},
},
};
}, {});
callObject.updateParticipants(updates);
},
[callObject, remoteParticipantIds, recentSpeakerIds]
);
/**
* Listen and keep state of track events
*/
useEffect(() => {
if (!callObject) return;
const handleTrackUpdate = ({ action, participant, track }) => {
if (!participant) return;
const id = participant.local ? "local" : participant.user_id;
switch (action) {
case "track-started":
case "track-stopped":
if (track.kind !== "video") break;
setVideoTracks((prevState) => ({
...prevState,
[id]: participant.tracks.video,
}));
break;
// Listen for participant updated events so we can
// keep our state updated with track subscription status
case "participant-updated":
setVideoTracks((prevState) => ({
...prevState,
[id]: participant.tracks.video,
}));
break;
// Remove tracks we no longer need
case "participant-left":
setVideoTracks((prevState) => {
delete prevState[id];
return prevState;
});
break;
}
};
callObject.on("track-started", handleTrackUpdate);
callObject.on("track-stopped", handleTrackUpdate);
callObject.on("participant-updated", handleTrackUpdate);
callObject.on("participant-left", handleTrackUpdate);
return () => {
callObject.off("track-started", handleTrackUpdate);
callObject.off("track-stopped", handleTrackUpdate);
callObject.off("participant-updated", handleTrackUpdate);
callObject.off("participant-left", handleTrackUpdate);
};
}, [callObject]);
return (
<TrackContext.Provider
value={{
videoTracks,
updateCamSubscriptions,
}}
>
{children}
</TrackContext.Provider>
);
};
export const useTracks = () => useContext(TrackContext);