About dividing Data type data into small pieces to fit in datagram size

I am trying to send and receive an audio data(saved by .caf extension device) using Network.framework.

This is my plan.

  1. convert file into Data type using below code:
guard let data = try? Data(contentsOf: recordedDocumentURL) else {
            print("recorded file to data conversion failed in touchUpCallButton Method")
            return
        }
  1. send Data type data using NWConnection.send, UDP.
  2. receive Data type data using NWConnection.receiveMessage
  3. convert received Data type data into AVAudioFile type data and play it using AVAudioEngine

Right now my problem is that size of data converted from audio file is too big to fit in maximumDatagramSize to send.

So in my understanding, I need to split Data type into many small bytes of data and send it one by one.

But in this case, I need to collect received data to make complete audio file again, so received device can play complete audio file.

And.. I'm stuck at this step.

I can't find right solution to divide Data type into small pieces to send by datagram using UDP.

What I have in my mind is to use 'subdata(in: Range<Data.Index>) -> Data' function and 'append(Data)' function to divide and sum up data.

Is this right approach to solve my problem?

Little advice would be very appreciated!

Answered by DTS Engineer in 714412022

What I am trying to do is to build a Walkie-Talkie app.

Real-time audio is one reason to use UDP. However, you can’t just break a file up into chunks, send the chunks over UDP, and reassemble them at the other end. Well, you can, but by doing so you lose any benefit you get from using UDP in the first place.

You have a choice here:

  • If you want the audio to go across in real time, use UDP and split up your data so that each chunk is playable. The receiver can then play the data as it arrives. If a packet is lost, the user will hear a glitch but such is the nature of real time audio.

  • If you want to eliminate such glitches, send the data over a TCP connection. The downside here is that you lose real time playback. If a packet is lost, TCP will retransmit under the covers and you won’t be able to play any audio until the retransmit is complete.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Why are you using UDP for this?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

What I am trying to do is to build a Walkie-Talkie app.

Real-time audio is one reason to use UDP. However, you can’t just break a file up into chunks, send the chunks over UDP, and reassemble them at the other end. Well, you can, but by doing so you lose any benefit you get from using UDP in the first place.

You have a choice here:

  • If you want the audio to go across in real time, use UDP and split up your data so that each chunk is playable. The receiver can then play the data as it arrives. If a packet is lost, the user will hear a glitch but such is the nature of real time audio.

  • If you want to eliminate such glitches, send the data over a TCP connection. The downside here is that you lose real time playback. If a packet is lost, TCP will retransmit under the covers and you won’t be able to play any audio until the retransmit is complete.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hey @EricKwon, was wondering if you ever figured this out. I've managed to break it up into chunks of Data, but am getting errors when I receive it and convert it back to audio. I'm wondering if something like NWProtocolFramerImplementation has to be introduced. Any advice?

About dividing Data type data into small pieces to fit in datagram size
 
 
Q