Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion OnAudioFilterRead don't execute without audio device

Discussion in 'Audio & Video' started by SlienSang, Nov 27, 2023.

  1. SlienSang

    SlienSang

    Joined:
    Apr 15, 2020
    Posts:
    5
    My project uses OnAudioFilterRead to obtain audio data and stream it out. On a normal computer it works fine, but when I move it to a server, problem occurred. The server does not have an audio device. Of course, Unity cannot play sound, but OnAudioFilterRead does not seem to be called back, resulting in the inability to stream audio. In addition to installing the virtual sound card program (which I couldn't install successfully), does Unity have any built-in related features to solve this problem?
     
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    379
    Hello sir,

    I'll be honest, if you're not able to install a virtual audio device, things can get very hard. `OnAudioFilterRead` is ONLY called when an audio device is running, from the audio thread. I'd be curious to understand more clearly what you are trying to achieve... are you...
    • providing audio assets through a server
    • synchronizing multiple game instances on audio playback
    • streaming out the audio of your game
    Good, but different, solutions exist for all these scenarios.

    Streaming audio is really time/performance sensitive, and relying on the main loop of a Unity server (with Update or FixedUpdate calls) is not a reliable course of action in my opinion. The Unity engine is a much more general-purposed framework than what would be needed for this.
     
  3. SlienSang

    SlienSang

    Joined:
    Apr 15, 2020
    Posts:
    5
    Thanks. My project is to stream Unity's audio and video as RTP and play them on other devices. Your answer helped me comfirms that the problem was caused by the server computer not having an audio device. I implemented RTP streaming by integrating FFmpeg as a Unity native plug-in, using Unity's rendering callback and audio callback. Do you mean that this is inappropriate or that Unity is not suitable for implementing such functionality?
     
  4. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    379
    I think your approach is the best in this case! The part I was considering inappropriate was to use OnAudioFilterRead to stream data over the network. As OAFR is bound to the audio thread, and networking operations have uncertain processing times, it's likely a troublesome combination!
     
  5. SlienSang

    SlienSang

    Joined:
    Apr 15, 2020
    Posts:
    5
    I see. Thanks very much.