제 질문을 읽어 주셔서 감사합니다.iOS - 데이터를 동시에 보내고받는 중
기본적으로 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에서는 스레드로 작업 한 적이 없습니다. 이 경우, 어떻게 그렇게 할 수 있는지 안내해 줄 수 있습니까?
더 자세한 정보가 필요하면 알려주십시오.
할 streamReceiverQueue = DispatchQueue (레이블 : "newQueue"의 QoS : DispatchQoS.userInteractive)
streamReceiverQueue.sync {
// code here
}
streamReceiverQueue.async {
// code here
}