9

I have a problem to send audio by CocoaAsyncSocket via UDP in swift.

First of all I run below code to start listening 4444 UDP port.

vlc --demux=rawaud --rawaud-channels=1 --rawaud-samplerate=48000 udp://@:4444

after that I run my application in iPad2 and press connect.

import UIKit
import CocoaAsyncSocket
import AVFoundation
class ViewController: UIViewController , GCDAsyncUdpSocketDelegate {

var avAudioEngine : AVAudioEngine?

@IBAction func btnAction(sender: UIButton) {

    avAudioEngine = AVAudioEngine()
    let input = avAudioEngine?.inputNode

    let socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
    do {
        try socket.bindToPort(4445)
        try socket.connectToHost("192.168.0.137",onPort : 4444)
        try socket.beginReceiving()

        input?.installTapOnBus(0, bufferSize: 2048, format: input?.inputFormatForBus(0), block: { (buffer : AVAudioPCMBuffer, timeE: AVAudioTime) -> Void in
            socket.sendData(self.toNSData(buffer), withTimeout: 0, tag: 0)
        })

        avAudioEngine?.prepare()
        try avAudioEngine?.start()

        // with nc -l -u -p 4444 and uncomment below block I can get "someText"
       //socket.sendData("someText\n".dataUsingEncoding(NSUTF8StringEncoding), withTimeout: 0, tag: 0)
       // socket.close()

    }

    catch{
        print("err")
    }

}

    //socket.sendData just accepts NSData, So I think that we must convert it to NSData!

func toNSData(PCMBuffer: AVAudioPCMBuffer) -> NSData {
    let channelCount = 1  // given PCMBuffer channel count is 1
    let channels = UnsafeBufferPointer(start: PCMBuffer.floatChannelData, count: channelCount)
    let ch0Data = NSData(bytes: channels[0], length:Int(PCMBuffer.frameCapacity * PCMBuffer.format.streamDescription.memory.mBytesPerFrame))
    return ch0Data
}

Any Idea?

Anand Suthar
  • 3,678
  • 2
  • 30
  • 52
  • Can you show your self.toNSData(buffer) method? does that buffer have 1 channel or 2 channel audio? – Josh May 31 '16 at 10:25
  • I wrote it before, just scroll down :) – Ali SadeghipourKorabaslo May 31 '16 at 13:52
  • @AliKörabbaslu did you solve this? I'm working on a similiar solution and I'm wondering if I should use this framework or not. – nullforlife Oct 11 '16 at 13:35
  • @nullforlife unfortunately didn't work. I'm writing my own framework right now, after finished I will gut in github. – Ali SadeghipourKorabaslo Oct 11 '16 at 13:59
  • @AliKörabbaslu I see. Nice too hear you are working with your own framework though. Are you digging into the CFNetwork and CFSocket APIs? – nullforlife Oct 11 '16 at 14:15
  • I dont know but of couse if necessary I will use. – Ali SadeghipourKorabaslo Oct 12 '16 at 06:15
  • I guess we need some more information to be able to help you. What happens? Is the closure you pass to installTapOnBus being called? Do you receive any audio data if you run `nc`? – Sven May 28 '17 at 17:28
  • @Sven my app is connected to telnet on Mac and I am trying to send raw audio data to telnet over wifi, but telnet did't receive any data. If I hard code send string inside 'installTapOnBus' it will work as expected but with audio data nothing works. – Anand Suthar May 30 '17 at 04:19
  • I used the framework upnpx via CocoaUPnP: https://github.com/arcam/CocoaUPnP. It is not currently Swift but I used with my Swift 3 application and it works brilliant. If I can get any time and the need is there, I may work on Porting this over to Swift. Just not – Matthew Cawley May 31 '17 at 22:22

0 Answers0