From 201861c8b93bba6858d20dfac4a05e78014f8992 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Mon, 27 Mar 2023 23:41:20 -0700 Subject: [PATCH] rendering logo when video off --- net/web/src/context/useRingContext.hook.js | 34 ++++++++++++++------- net/web/src/session/Session.jsx | 35 ++++++++++++++++------ net/web/src/session/Session.styled.js | 9 ++++++ net/web/src/session/useSession.hook.js | 9 ++++-- 4 files changed, 65 insertions(+), 22 deletions(-) diff --git a/net/web/src/context/useRingContext.hook.js b/net/web/src/context/useRingContext.hook.js index b5f0a073..b2257a6e 100644 --- a/net/web/src/context/useRingContext.hook.js +++ b/net/web/src/context/useRingContext.hook.js @@ -11,9 +11,11 @@ export function useRingContext() { ringing: new Map(), callStatus: null, localStream: null, + localVideo: false, + localAudio: false, remoteStream: null, - video: false, - audio: false, + removeVideo: false, + removeAudio: false, }); const access = useRef(null); @@ -93,15 +95,21 @@ export function useRingContext() { // connect signal socket calling.current = { state: "connecting", callId, contactNode, contactToken, host: false }; - updateState({ callStatus: "connecting" }); + updateState({ callStatus: "connecting", remoteVideo: false, remoteAudio: false }); // form peer connection pc.current = new RTCPeerConnection(); - pc.current.ontrack = (ev) => { //{streams: [stream]}) => { + pc.current.ontrack = (ev) => { if (!stream.current) { stream.current = new MediaStream(); updateState({ remoteStream: stream.current }); } + if (ev.track.kind === 'audio') { + updateState({ remoteAudio: true }); + } + else if (ev.track.kind === 'video') { + updateState({ remoteVideo: true }); + } stream.current.addTrack(ev.track); }; pc.current.onicecandidate = ({candidate}) => { @@ -245,7 +253,7 @@ export function useRingContext() { }, RING); calling.current = { state: "connecting", callId: id, host: true }; - updateState({ callStatus: "connecting" }); + updateState({ callStatus: "connecting", remoteVideo: false, remoteAudio: false }); // form peer connection pc.current = new RTCPeerConnection(); @@ -255,6 +263,12 @@ export function useRingContext() { stream.current = new MediaStream(); updateState({ remoteStream: stream.current }); } + if (ev.track.kind === 'audio') { + updateState({ remoteAudio: true }); + } + else if (ev.track.kind === 'video') { + updateState({ remoteVideo: true }); + } stream.current.addTrack(ev.track); }; pc.current.onicecandidate = ({candidate}) => { @@ -278,7 +292,7 @@ export function useRingContext() { video: false, audio: true, }); - updateState({ video: false, audio: true, localStream: stream }); + updateState({ localVideo: false, localAudio: true, localStream: stream }); for (const track of stream.getTracks()) { if (track.kind === 'audio') { audioTrack.current = track; @@ -371,21 +385,21 @@ export function useRingContext() { else { videoTrack.current.enabled = true; } - updateState({ video: true }); + updateState({ localVideo: true }); }, disableVideo: async () => { if (videoTrack.current) { videoTrack.current.enabled = false; } - updateState({ video: false }); + updateState({ localVideo: false }); }, enableAudio: async () => { audioTrack.current.enabled = true; - updateState({ audio: true }); + updateState({ localAudio: true }); }, disableAudio: async () => { audioTrack.current.enabled = false; - updateState({ audio: false }); + updateState({ loaclAudio: false }); }, } diff --git a/net/web/src/session/Session.jsx b/net/web/src/session/Session.jsx index 84c1693d..8893911f 100644 --- a/net/web/src/session/Session.jsx +++ b/net/web/src/session/Session.jsx @@ -21,8 +21,8 @@ export function Session() { const { state, actions } = useSession(); const [ringing, setRinging] = useState([]); - const [callWidth, setCallWidth] = useState(320); - const [callHeight, setCallHeight] = useState(240); + const [callWidth, setCallWidth] = useState(256); + const [callHeight, setCallHeight] = useState(256); const remote = useRef(); const local = useRef(); @@ -44,10 +44,24 @@ export function Session() { setRinging(incoming); }, [state.ringing]); + const getWidth = () => { + if (state.remoteVideo) { + return callWidth; + } + return 256; + } + + const getHeight = () => { + if (state.remoteVideo) { + return callHeight; + } + return 256; + } + useEffect(() => { if (remote.current) { remote.current.onloadedmetadata = (ev) => { - const { videoWidth, videoHeight } = ev.target || { videoWidth: 320, videoHeight: 240 } + const { videoWidth, videoHeight } = ev.target || { videoWidth: 256, videoHeight: 256 } if ((window.innerWidth * 8) / 10 < videoWidth) { const scaledWidth = window.innerWidth * 8 / 10; const scaledHeight = videoHeight * (scaledWidth / videoWidth) @@ -308,10 +322,13 @@ export function Session() { - + + { !state.remoteVideo && ( + + )} { state.remoteStream && ( -