Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Audio streaming corruption

Discussion in 'Scripting' started by VOX-NeilC, May 31, 2022.

  1. VOX-NeilC

    VOX-NeilC

    Joined:
    Oct 12, 2020
    Posts:
    7
    Hi,
    I'm getting audio corruption when using UnityWebRequestMultimedia.GetAudioClip using an external https:// address to stream audio. This happens in Unity 2020 and 2021 LTS versions.

    I believe it's to do with buffer starvation as waiting for the full clip to download or a larger amount of downloadedBytes will fix my issue but that isn't an ideal solution.

    Is there any way to debug audio streaming or detect buffer starvation to pause / unpause the audio source?
    It looks like you can't create a custom download handler DownloadHandlerAudioClip as it's a sealed class so perhaps there's another way to do this?

    Thanks in advance,
    Neil
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Huh? Is there any OTHER solution? If your network latency can be up to X seconds in any given context, I am not sure how you could successfully stream audio with a buffer shorter than X. But if you know a way... I'm all ears.

    See if there's any buffering settings on the downloading API you're using. I haven't streamed anything with UWR... it may or may not support changing its buffering heuristics, or you might need to make your own custom downloader that defers playing the stream until you have an adequate buffer.
     
  3. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    when you say corruption, do you mean the audio file actually gets corrupted and cannot be played or the audio file starts to lose quality? because a spike in latency can skip samples depending on how you go about things, which sounds like static. or a decrease in bandwidth can possibly be telling your connecting server to decrease the bit-rate or sampling rate which would most likely be the cause of a corrupted file.
     
  4. VOX-NeilC

    VOX-NeilC

    Joined:
    Oct 12, 2020
    Posts:
    7
    Hi, sorry not corruption of the actual file just in the playback. A spike in the latency is an interesting one actually and I wonder if I should put the downloading on a separate thread.

    I can increase the buffer but that could still have issues if bandwidth is low and you still end up with not enough data being downloaded so I was wondering if there was a way with the existing DownloadHandlerAudioClip implementation to detect such things so I can pause / unpause the audio source similar to YouTube for example.

    Perhaps I do need my own download handler but that is going to be tricky with supporting different audio file formats.
     
  5. VOX-NeilC

    VOX-NeilC

    Joined:
    Oct 12, 2020
    Posts:
    7
    But also there's lots of examples doing something like the following:

    Code (CSharp):
    1. using (UnityWebRequest webRequest = UnityWebRequestMultimedia.GetAudioClip(fullPath, type)) {
    2. ((DownloadHandlerAudioClip) webRequest.downloadHandler).streamAudio = true;
    3. yield return webRequest.SendWebRequest();
    4. while (!webRequest.isNetworkError && !webRequest.isHttpError && webRequest.downloadedBytes <= 1000) {
    5.    yield return null;
    6. }
    7. AudioClip clip = DownloadHandlerAudioClip.GetContent(webRequest);
    But the yield return webRequest.SendWebRequest() waits until the whole of the downloaded bytes have been downloaded. Removing the yield makes it work properly but the I get the static / bad audio output.
    That feels like a bug to me.
     
  6. VOX-NeilC

    VOX-NeilC

    Joined:
    Oct 12, 2020
    Posts:
    7
    I'm still wondering if anyone has had any success with actual streaming audio in Unity.

    I've looked into these packages:
    Streaming Management | Unity Render Streaming | 3.1.0-exp.3 (unity3d.com)
    Audio Streaming | WebRTC | 2.0.5-preview (unity3d.com)
    but unfortunately I don't think they are suitable as we are looking for a cross-platform, versatile streaming solution for wav, mp3 and ogg files with compatibility on Android, iOS, UWP, Mac and PC.
    FMOD is an option but as Unity is built with the FMOD api, it feels like there should be solution in Unity.