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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Streaming/Sending audio files with UNET

Discussion in 'UNet' started by danieltranca, Jan 18, 2016.

  1. danieltranca

    danieltranca

    Joined:
    Jan 9, 2015
    Posts:
    10
    Hi guys. I have a small app where one user can talk to the other. It's pretty much a walkie-talkie but one sided(Meaning only one can speak and the other can hear). I have integrated UNET with this app(I need to do it over the LAN and also I have other info to send through the network). With the other messages(they are simple strings) everything works great, but when I try to send an AudioClip it will be null on the other side. I guess this happens because the AudioClip is not serializable or something similar. So I found SyncListFloat and I add all the audio data to this one(this kinda takes a lot of time which is sort of problematic, I think this happens because the message sending after each Add) but then the Player Prefab(which I use to send data) is no longer added to the scene. If I remove the SyncListFloat variable from the Player script everything works great(except for the audio sending part, obviously).

    I would like to know how can I send and audio files through UNET and if SyncListFloat is the answer how can I add the whole byte array in one go.

    Thank you!
     
  2. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,103
    synclists are buggy at the moment, use a message which has an array of floats and send it in a command and for the channel use UnreliableFragmented. Then it should work.
     
  3. danieltranca

    danieltranca

    Joined:
    Jan 9, 2015
    Posts:
    10
    Can you show me an example please?
     
  4. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,103
    Create a message like this

    Code (CSharp):
    1. class AudioMessage : MessageBase
    2. {
    3. public float[] data;
    4. public short const AudioMsgType = MsgType.highest+1;
    5. }
    Then use Send method in NetworkClient/NetworkServer/NetworkConnection which takes channel number and if you are using NetworkManager add a channel of unreliableFragmented and if you are not using NetworkManager add it by NetworkServer.AddChannel and NetworkClient.AddChannel and use that channel number.

    If you don't use a fragmented channel your data will not be sent due to its size.
     
  5. danieltranca

    danieltranca

    Joined:
    Jan 9, 2015
    Posts:
    10
    So I tried this and no luck. The reason this is not working is that if the message is too large the message is not sent. I could send an array of floats of couple hundred elements but nothing larger than that. Do you have any idea how can I fix that?:)
     
  6. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,103
    Did you use a UnReliableFragmented channel? That can send upto 65536 bytes at a time IIRC

    To do that if using NetworkManager in advanced configuration add a third channel of type UnreliableFragmented, If not In your NetworkCLient instance and NetworkServer add the channel.

    Then use one of the send/SendByChannel overloads which takes channel number and send on that channel.
     
  7. danieltranca

    danieltranca

    Joined:
    Jan 9, 2015
    Posts:
    10
    Ok...did that to...minor improvements...now I can send half second sounds...still not enough though...i need about 6 seconds. If I send messages bigger than 0.5 sec I get this error: UNet Client Data Error: MessageToLong.
     
  8. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,103
    Of course you should send it in chunks of 0.2 seconds or something in an unreliable channel and either play as it comes or send on a reliable channel and play when you got all chunks , if you want file transfer the reliable one is good, if you want realtime audio communication unreliable is good since skipping a single part is not that bad, you can send even in smaller chunks.

    For sending big files streaming APIs should be used and uNet doesn't have a TCP or UDP based streaming channel yet. actually if you want file transfer the best way is to use cloud services or running an http server and client in each application if you don't have a central server. Even HTTP sends stuff in chunks.

    Also in connectionConfig by changing fragment size and other parameters you might be capable of affecting the maximum size which you can send.