Skip to content

Commit 9183964

Browse files
refactor send settings & receive settings
1 parent 9f7008f commit 9183964

File tree

12 files changed

+459
-282
lines changed

12 files changed

+459
-282
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useCallback } from 'react';
2+
import { useVideoLayer } from '@/states/videoLayerState';
3+
import { DailyEventObject } from '@daily-co/daily-js';
4+
import { useThrottledDailyEvent } from '@daily-co/daily-react';
5+
6+
import { VIDEO_QUALITY_LAYERS } from '@/types/videoQualityLayers';
7+
8+
export function CPULoadListener() {
9+
const [, setVideoLayer] = useVideoLayer();
10+
11+
useThrottledDailyEvent(
12+
'cpu-load-change',
13+
useCallback(
14+
(events: DailyEventObject<'cpu-load-change'>[]) => {
15+
events.forEach((ev) => {
16+
const { cpuLoadState, cpuLoadStateReason } = ev;
17+
switch (cpuLoadState) {
18+
case 'high':
19+
setVideoLayer((prev) => {
20+
return {
21+
...prev,
22+
receive: {
23+
...prev.receive,
24+
layerBasedOnCPU:
25+
cpuLoadStateReason === 'decode'
26+
? VIDEO_QUALITY_LAYERS.LOW
27+
: VIDEO_QUALITY_LAYERS.MEDIUM,
28+
},
29+
};
30+
});
31+
break;
32+
case 'low':
33+
setVideoLayer((prev) => {
34+
return {
35+
...prev,
36+
receive: {
37+
...prev.receive,
38+
layerBasedOnCPU: VIDEO_QUALITY_LAYERS.HIGH,
39+
},
40+
};
41+
});
42+
break;
43+
}
44+
});
45+
},
46+
[setVideoLayer],
47+
),
48+
);
49+
50+
return null;
51+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { useCallback } from 'react';
2+
import { useVideoLayer } from '@/states/videoLayerState';
3+
import { DailyEventObject } from '@daily-co/daily-js';
4+
import { useThrottledDailyEvent } from '@daily-co/daily-react';
5+
6+
import { VIDEO_QUALITY_LAYERS } from '@/types/videoQualityLayers';
7+
8+
export function NetworkThresholdListener() {
9+
const [, setVideoLayer] = useVideoLayer();
10+
11+
useThrottledDailyEvent(
12+
'network-quality-change',
13+
useCallback(
14+
(events: DailyEventObject<'network-quality-change'>[]) => {
15+
events.forEach((ev) => {
16+
const { threshold } = ev;
17+
switch (threshold) {
18+
case 'good':
19+
setVideoLayer((prev) => {
20+
return {
21+
...prev,
22+
receive: {
23+
...prev.receive,
24+
layerBasedOnNetwork: VIDEO_QUALITY_LAYERS.HIGH,
25+
},
26+
};
27+
});
28+
break;
29+
case 'low':
30+
setVideoLayer((prev) => {
31+
return {
32+
...prev,
33+
receive: {
34+
...prev.receive,
35+
layerBasedOnNetwork: VIDEO_QUALITY_LAYERS.MEDIUM,
36+
},
37+
};
38+
});
39+
break;
40+
case 'very-low':
41+
setVideoLayer((prev) => {
42+
return {
43+
...prev,
44+
receive: {
45+
...prev.receive,
46+
layerBasedOnNetwork: VIDEO_QUALITY_LAYERS.LOW,
47+
},
48+
};
49+
});
50+
break;
51+
}
52+
});
53+
},
54+
[setVideoLayer],
55+
),
56+
);
57+
58+
return null;
59+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { useEffect } from 'react';
2+
import { useVideoLayer } from '@/states/videoLayerState';
3+
4+
import { VIDEO_QUALITY_LAYERS } from '@/types/videoQualityLayers';
5+
import { useStage } from '@/hooks/useStage';
6+
7+
export function ParticipantsListener() {
8+
const { participantIds } = useStage();
9+
const [, setVideoLayer] = useVideoLayer();
10+
11+
useEffect(() => {
12+
const participantCount = participantIds.length;
13+
const participantCountLayer =
14+
participantCount < 5
15+
? VIDEO_QUALITY_LAYERS.HIGH
16+
: participantCount < 10
17+
? VIDEO_QUALITY_LAYERS.MEDIUM
18+
: VIDEO_QUALITY_LAYERS.LOW;
19+
20+
setVideoLayer((prev) => {
21+
return {
22+
...prev,
23+
receive: {
24+
...prev.receive,
25+
layerBasedOnParticipantCount: participantCountLayer,
26+
},
27+
};
28+
});
29+
}, [participantIds, setVideoLayer]);
30+
31+
return null;
32+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { useCallback, useEffect } from 'react';
2+
import { useVideoLayer } from '@/states/videoLayerState';
3+
import { DailyReceiveSettings } from '@daily-co/daily-js';
4+
import { useDaily, useLocalSessionId } from '@daily-co/daily-react';
5+
import { dequal } from 'dequal';
6+
7+
import { useStage } from '@/hooks/useStage';
8+
import { CPULoadListener } from '@/components/Listeners/ReceiveSettings/CPULoadListener';
9+
import { NetworkThresholdListener } from '@/components/Listeners/ReceiveSettings/NetworkThresholdListener';
10+
import { ParticipantsListener } from '@/components/Listeners/ReceiveSettings/ParticipantsListener';
11+
12+
export function ReceiveSettingsListener() {
13+
const daily = useDaily();
14+
const localSessionId = useLocalSessionId();
15+
const { participantIds, waitingParticipantIds } = useStage();
16+
17+
const [{ receive }] = useVideoLayer();
18+
19+
const updateReceiveSettings = useCallback(async () => {
20+
if (!daily || daily.meetingState() !== 'joined-meeting') return;
21+
22+
const updatedReceiveSettings: DailyReceiveSettings = {};
23+
const receiveSettings = await daily.getReceiveSettings();
24+
25+
const layer = Math.min(
26+
receive.layerBasedOnCPU,
27+
receive.layerBasedOnNetwork,
28+
receive.layerBasedOnParticipantCount,
29+
);
30+
31+
const updateSettingsForParticipant = (
32+
participantId: string,
33+
layer: number,
34+
) => {
35+
if (participantId === localSessionId) return;
36+
updatedReceiveSettings[participantId] = { video: { layer } };
37+
};
38+
39+
participantIds.forEach((participantId) =>
40+
updateSettingsForParticipant(participantId, layer),
41+
);
42+
43+
waitingParticipantIds.forEach((participantId) =>
44+
updateSettingsForParticipant(participantId, 0),
45+
);
46+
47+
if (
48+
Object.keys(updatedReceiveSettings).length === 0 ||
49+
dequal(receiveSettings, updatedReceiveSettings)
50+
) {
51+
return;
52+
}
53+
54+
await daily.updateReceiveSettings(updatedReceiveSettings);
55+
}, [
56+
daily,
57+
localSessionId,
58+
participantIds,
59+
receive.layerBasedOnCPU,
60+
receive.layerBasedOnNetwork,
61+
receive.layerBasedOnParticipantCount,
62+
waitingParticipantIds,
63+
]);
64+
65+
useEffect(() => {
66+
updateReceiveSettings();
67+
}, [updateReceiveSettings]);
68+
69+
return (
70+
<>
71+
<CPULoadListener />
72+
<NetworkThresholdListener />
73+
<ParticipantsListener />
74+
</>
75+
);
76+
}

components/Listeners/ReceiveSettingsListener.tsx

Lines changed: 0 additions & 151 deletions
This file was deleted.

0 commit comments

Comments
 (0)