Unity-Technologies / com.unity.webrtc

WebRTC package for Unity
Other
739 stars 185 forks source link

[BUG]: unity Webrtc AudioSource noise issue #903

Closed yfangel closed 1 year ago

yfangel commented 1 year ago

Package version

3.0.0-pre.1

Environment

* OS: android
* Unity version: 2019.4

Steps To Reproduce

my code:

1.Obtain local audio from the microphone and transmit it to remote users:

private IEnumerator CaptureAudioStart() { var deviceName = Microphone.devices[0]; var micClip = Microphone.Start(deviceName, true, 1, 48000); // set the latency to “0” samples before the audio starts to play. while (!(Microphone.GetPosition(deviceName) > 0)) { } sourceAudio.clip = micClip; sourceAudio.loop = true; sourceAudio.reverbZoneMix = 0; audioStreamTrack = new AudioStreamTrack(sourceAudio); yield return null; }

2, Obtain audio streams from remote users for real-time playback:

receiveStream = new MediaStream();
 receiveStream.OnAddTrack = e =>
 {
     if (e.Track is AudioStreamTrack track1)
     {
         Debug.LogWarning("audioTrack: " + track1.ToString());
         StartCoroutine(CaptureAudioStart(track1));
         //receiveAudio.SetTrack(track1);
         //receiveAudio.loop = true;
         //receiveAudio.Play();
     }
 };
 pc2.OnTrack = e =>
 {
     receiveStream.AddTrack(e.Track);
     Debug.LogWarning("OnTrack: ");
 };

Current Behavior

Why is there a lot of noise when using a microphone to obtain local audio and then simultaneously playing the remote audio stream?

Expected Behavior

No response

Anything else?

No response

yfangel commented 1 year ago

This problem is really bothering me. Please help me solve it. Thank you! @karasusan

yfangel commented 1 year ago

can you help me?Because my boss is eager for me to solve this problem. @karasusan

karasusan commented 1 year ago

@yfangel First of all, could you try the latest version of our package? You are using the old version.

kannan-xiao4 commented 1 year ago

related issue https://github.com/Unity-Technologies/UnityRenderStreaming/issues/563 https://github.com/Unity-Technologies/com.unity.webrtc/issues/587 https://github.com/Unity-Technologies/com.unity.webrtc/issues/651

memo: WRS-152

yfangel commented 1 year ago

@karasusan I have tried the latest version 3.0.0-pre.4, but it still doesn't work.

As long as the microphone is used for local recording while playing the audio stream from the remote device, there will be a lot of echo. It should be a local recording that also recorded the audio stream from the remote device.

I am a novice developer of Unity. Is there any way to solve this problem of recording while playing? thank you very much!

yfangel commented 1 year ago

related issue Unity-Technologies/UnityRenderStreaming#563 #587 #651

memo: WRS-152

thank you ,I have looked at it, but I don't see a solution

yfangel commented 1 year ago

@karasusan hi,I tried writing the following code in start() ,and it seems that the noise has decreased significantly.

 AudioConfiguration config = AudioSettings.GetConfiguration();
    //"Best Latency",  256  ,   "Good Latency", 512,,  "Best Performance", 1024
    config.dspBufferSize = 1024;
    AudioSettings.Reset(config);
    if (!AudioSettings.Reset(config))
    {
        Debug.LogError("yfange----Failed changing Audio Settings");
    }

I didn't set it up before. I saw it on Google, but I don't know why it's set up like this, what's the purpose?

ZenBre4ker commented 1 year ago

You can also change this line here to make it better for webrtc. while (!(Microphone.GetPosition(deviceName) > 0)) { } to while (!(Microphone.GetPosition(deviceName) > 480)) { }

That will produce a bit more latency, but gives the webrtc more stuff to buffer.

ZenBre4ker commented 1 year ago

I had the same problem and it was reproducible: https://github.com/Unity-Technologies/com.unity.webrtc/issues/839

yfangel commented 1 year ago

You can also change this line here to make it better for webrtc. while (!(Microphone.GetPosition(deviceName) > 0)) { } to while (!(Microphone.GetPosition(deviceName) > 480)) { }

That will produce a bit more latency, but gives the webrtc more stuff to buffer.

Okay, I'll give it a try. Thank you

yfangel commented 1 year ago

@ZenBre4ker by the way,Would it be better to change the duration in the following code from 1 to 10?

var micClip = Microphone.Start(deviceName, true, 1, 48000);

ZenBre4ker commented 1 year ago

@yfangel yes, as I said in https://github.com/Unity-Technologies/com.unity.webrtc/issues/839

yfangel commented 1 year ago

@yfangel yes, as I said in #839

ok,Thank you, buddy.