2022-05-05 00:09:48 +08:00
|
|
|
/*
|
|
|
|
Copyright 2022 Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-04-08 05:22:36 +08:00
|
|
|
import { useState, useEffect } from "react";
|
|
|
|
import { CallFeedEvent } from "matrix-js-sdk/src/webrtc/callFeed";
|
|
|
|
|
|
|
|
function getCallFeedState(callFeed) {
|
|
|
|
return {
|
|
|
|
member: callFeed ? callFeed.getMember() : null,
|
|
|
|
isLocal: callFeed ? callFeed.isLocal() : false,
|
|
|
|
speaking: callFeed ? callFeed.isSpeaking() : false,
|
|
|
|
noVideo: callFeed
|
|
|
|
? !callFeed.stream || callFeed.stream.getVideoTracks().length === 0
|
|
|
|
: true,
|
|
|
|
videoMuted: callFeed ? callFeed.isVideoMuted() : true,
|
|
|
|
audioMuted: callFeed ? callFeed.isAudioMuted() : true,
|
|
|
|
stream: callFeed ? callFeed.stream : undefined,
|
|
|
|
purpose: callFeed ? callFeed.purpose : undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useCallFeed(callFeed) {
|
|
|
|
const [state, setState] = useState(() => getCallFeedState(callFeed));
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function onSpeaking(speaking) {
|
|
|
|
setState((prevState) => ({ ...prevState, speaking }));
|
|
|
|
}
|
|
|
|
|
|
|
|
function onMuteStateChanged(audioMuted, videoMuted) {
|
|
|
|
setState((prevState) => ({ ...prevState, audioMuted, videoMuted }));
|
|
|
|
}
|
|
|
|
|
|
|
|
function onUpdateCallFeed() {
|
|
|
|
setState(getCallFeedState(callFeed));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (callFeed) {
|
|
|
|
callFeed.on(CallFeedEvent.Speaking, onSpeaking);
|
|
|
|
callFeed.on(CallFeedEvent.MuteStateChanged, onMuteStateChanged);
|
|
|
|
callFeed.on(CallFeedEvent.NewStream, onUpdateCallFeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
onUpdateCallFeed();
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (callFeed) {
|
|
|
|
callFeed.removeListener(CallFeedEvent.Speaking, onSpeaking);
|
|
|
|
callFeed.removeListener(
|
|
|
|
CallFeedEvent.MuteStateChanged,
|
|
|
|
onMuteStateChanged
|
|
|
|
);
|
|
|
|
callFeed.removeListener(CallFeedEvent.NewStream, onUpdateCallFeed);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [callFeed]);
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|