Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Echo issue in video call #18

Closed
sukesh-pfx opened this issue Jul 22, 2021 · 3 comments
Closed

Echo issue in video call #18

sukesh-pfx opened this issue Jul 22, 2021 · 3 comments

Comments

@sukesh-pfx
Copy link

Hi, I'm facing a continuous echo issue on a video call. I may be wrong I found that when I toggled the mic on/off it's happening. Please let me know any workaround for this
Using
"agora-rtc-sdk-ng": "^4.6.0",
This is my code
please tell me what I have done wrong .

 `const useVideoCall = (videoClient, screenClient) => {
  const [allUsers, setUsers] = useState([]);
  const [trackStatus, setTrackStatus] = useState({
    video: false,
    audio: false,
  });
  const [localVideoTrack, setLocalVideoTrack] = useState(undefined);
  const [localAudioTrack, setLocalAudioTrack] = useState(undefined);
  const [isLoggedIn, setLoggedIn] = useState(false);
  const [isLoading, setIsLoading] = useState(false);
  const { chatDetails } = useVideoChatDetails();

  const createAudioAndVideoTrack = useCallback(async () => {
    try {
      const {
        videoCall: { uid },
      } = chatDetails;
      setIsLoading(true);
      const microphoneTrack = await AgoraRTC.createMicrophoneAudioTrack({
        AEC: true,
        ANS: true,
      });
      const cameraTrack = await AgoraRTC.createCameraVideoTrack();

      setLocalAudioTrack(microphoneTrack);
      setLocalVideoTrack(cameraTrack);
      setUsers((prevUsers) => [
        ...prevUsers,
        {
          videoTrack: cameraTrack,
          uid: uid,
        },
      ]);
      setTrackStatus({
        video: cameraTrack ? true : false,
        audio: microphoneTrack ? true : false,
      });
      setIsLoading(false);
    } catch (error) {}
  }, [chatDetails]);

  useEffect(() => {
    if (!isLoggedIn) {
      createAudioAndVideoTrack();
    }
  }, [createAudioAndVideoTrack, isLoggedIn]);

  const joinVideoCall = async () => {
    if (!videoClient || !chatDetails || isLoading) return;

    const {
      videoCall: { agoraToken, uid },
    } = chatDetails;

    try {
      await videoClient.join(
        chatDetails?.appId,
        chatDetails?.channelName,
        agoraToken,
        uid
      );
      init();
      videoClient.publish([localAudioTrack, localVideoTrack]);
      setLoggedIn(true);
    } catch (error) {}
  };


  const init = () => {
    videoClient.on("user-published", async (user, mediaType) => {
      await videoClient.subscribe(user, mediaType);
      console.log("subscribe success");
      //   updatePublishStatus(user);
      const remoteVideoTrack = user?.videoTrack || user?._videoTrack;
      const audio = user?.hasAudio;
      const video = user?.hasVideo;
      const id = user?.uid;
      if (mediaType === "video") {
        setUsers((preUser) => {
          return preUser.map((joinedUser) => {
            if (joinedUser?.uid === user?.uid) {
              return {
                ...user,
                videoTrack: remoteVideoTrack,
                video: video,
                audio: audio,
                uid: id,
              };
            }
            return joinedUser;
          });
        });
      }
      if (mediaType === "audio") {
        try {
          user.audioTrack.play();
          setUsers((prevUsers) => {
            return prevUsers.map((User) => {
              if (User.uid === user.uid) {
                return {
                  ...User,
                  videoTrack: remoteVideoTrack,
                  video: video,
                  audio: audio,
                  uid: id,
                };
              }
              return User;
            });
          });
        } catch (error) {}
      }
    });

    videoClient.on("user-unpublished", (user, type) => {
      console.log("unpublished", user, type);
      const remoteVideoTrack = user?.videoTrack || user?._videoTrack;
      const audio = user?.hasAudio;
      const video = user?.hasVideo;
      const id = user?.uid;
      if (type === "audio") {
        try {
          user.audioTrack.stop();
          setUsers((prevUsers) => {
            return prevUsers.map((User) => {
              if (User.uid === user.uid) {
                return {
                  ...User,
                  videoTrack: remoteVideoTrack,
                  video: video,
                  audio: audio,
                  uid: id,
                };
              }
              return User;
            });
          });
        } catch (error) {}
      }
      if (type === "video") {
        setUsers((prevUsers) => {
          return prevUsers.map((User) => {
            if (User.uid === user.uid) {
              return {
                ...User,
                videoTrack: remoteVideoTrack,
                video: video,
                audio: audio,
                uid: id,
              };
            }
            return User;
          });
        });
        
      }
    });

    videoClient.on("user-left", (user) => {
      console.log("leaving", user);
      setUsers((prevUsers) => {
        return prevUsers.filter((User) => User.uid !== user.uid);
      });
    });

    videoClient.on("user-joined", (user) => {
      setUsers((prevUsers) => {
        return [
          ...prevUsers,
          {
            ...user,
            videoTrack: undefined,
            video: false,
            audio: false,
            uid: user?.uid,
          },
        ];
      });
    });
  };

  const toggleAudio = (status) => {
    try {
      localAudioTrack.setEnabled(status);
    } catch (error) {}
  };
  const toggleVideo = (status) => {
    try {
      localVideoTrack.setEnabled(status);
    } catch (error) {}
  };

  return {
    toggleAudio,
    toggleVideo,
    joinVideoCall,
    allUsers,
    isLoggedIn,
  };
};`
@sukesh-pfx
Copy link
Author

Issue fixed
local audio track caused the issue.

@dineshshaw
Copy link

Hii Sukesh,
How did you fix the issue related to localAudioTrack?

@sukesh-pfx
Copy link
Author

Hii Sukesh,
How did you fix the issue related to localAudioTrack?

Hi @dineshshaw

Do not play localAudioTrack

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants