iOS - 同时发送和接收数据

时间:2017-02-07 19:58:17

标签: ios swift multithreading audio stream

感谢您抽出宝贵时间阅读我的问题。

基本上我使用MultipeerConnectivity连接到另一台设备,我想在接收数据时将数据流式传输到另一台设备。

我的问题是其中一个设备最终只发送数据,而另一个设备最终只收到它。

这是我的发送方法。此方法是来自AVCaptureAudioDataOutputSampleBufferDelegate的委托方法,因此会自动调用它。

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {
    //-------------------------------------------------------
    // Writing to output stream

    var blockBuffer: CMBlockBuffer?
    var audioBufferList: AudioBufferList = AudioBufferList.init()

    CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampleBuffer, nil, &audioBufferList, MemoryLayout<AudioBufferList>.size, nil, nil, kCMSampleBufferFlag_AudioBufferList_Assure16ByteAlignment, &blockBuffer)
    let buffers = UnsafeMutableAudioBufferListPointer(&audioBufferList)

    for buffer in buffers {
        let u8ptr = buffer.mData!.assumingMemoryBound(to: UInt8.self)
        let output = outputStream!.write(u8ptr, maxLength: Int(buffer.mDataByteSize))

        if (output == -1) {
            let error = outputStream?.streamError
            print("\(#file) > \(#function) > Error on outputStream: \(error!.localizedDescription)")
        }
        else {
            print("\(#file) > \(#function) > Data sent")
        }
    }
}

这是我的接收方法(我确实处理其他流事件,它们在这里无关紧要)。你可以忽略所有音频相关的东西,它现在不能工作,但它不是我的主要问题。此方法是OutputStream的委托,因此当数据从流中可用时,它也会自动调用。

func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
    case Stream.Event.hasBytesAvailable:
        print("\(#file) > \(#function) > New data has arrived")
        readFromStream()
}


func readFromStream() {
    while (inputStream!.hasBytesAvailable) {
        var buffer = [UInt8](repeating: 0, count: 4096)
        let length = inputStream!.read(&buffer, maxLength: buffer.count)

        if (length > 0) {

            if (audioEngine!.isRunning) {
                print("\(#file) > \(#function) > audioEngine is running")
                audioEngine!.stop()
                audioEngine!.reset()
            }
            else {
                print("\(#file) > \(#function) > audioEngine is NOT running")
            }

            print("\(#file) > \(#function) > \(length) bytes read")

            let audioBuffer = bytesToAudioBuffer(buffer)
            let mainMixer = audioEngine!.mainMixerNode

            audioEngine!.connect(audioPlayer!, to: mainMixer, format: audioBuffer.format)
            audioPlayer!.scheduleBuffer(audioBuffer, completionHandler: nil)

            do {
                try audioEngine!.start()
            }
            catch let error as NSError {
                print("\(#file) > \(#function) > error: \(error.localizedDescription)")
            }
            audioPlayer!.play()
        }
    }
}

从这两种方法中,您可以看到我有数据进入时和数据发送时的打印语句。在其中一个设备上,我最终一直在读取数据,从不发送任何数据,而在另一个设备上,我最终不断发送数据,从不接收任何数据。

我想这是因为我没有使用线程..但我从来没有使用iOS上的线程。如果是这种情况,任何人都可以指导我如何这样做吗?

如果您需要更多信息,请告诉我们。

1 个答案:

答案 0 :(得分:0)

最终我找到了一种使用线程的可行方法:

让streamReceiverQueue = DispatchQueue(标签:“newQueue”,qos:DispatchQoS.userInteractive)

streamReceiverQueue.sync {
    // code here
}

streamReceiverQueue.async {
    // code here
}