How to pass audio from iPhone microphone as buffer

时间:2017-08-05 10:49:25

标签: swift audio buffer avfoundation unsafemutablepointer

I am trying to send audio recorded from the iPhone microphone, to an ip camera. I have an sdk(written in c) in order to communicate with the camera and this is the function i need to pass the data. We are talking of sending realtime audio.

    /*
@Name: FosSdk_SendTalkData.
@Description: Send the data of talk.
@param handle: the handle of current connection information.
@param data: The data need to send.
@param len: Len of data.
@return: Please refer to the enum of FOSCMD_RESULT to get more information.
*/
FOSSDK FOSCMD_RESULT FOSAPI FosSdk_SendTalkData(FOSHANDLE handle, char *data, int len);

This is instead the Swift signature( i am currently using swift )

FosSdk_SendTalkData(handle: UInt32, data: UnsafeMutablePointer<Int8>!, len: Int32)

How do i record the audio from iPhone microphone and pass the audio buffer correctly to sendTalkData? Thanks in advance for any clarification/help.

EDIT

I managed how to get the audio buffer using a AVFoundation and the AVCaptureAudioDataOutputSampleBufferDelegate function, from which i obtain a sample buffer. However, my implementation, does not work. One thing i noticed, is the app crashes when passing length parameter to sendTalkFunction. If for example, i pass Int32(1) as length, the app does not crash but however, it does not have any sense to pass Int32(1).

This snippet of code helped me a lot, but however it s a bit old so i needed to make some edit http://timestocomemobile.com/2014/10/swift-data-to-c-pointer-and-back-again.html

func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, from connection: AVCaptureConnection!) {

    guard let blockBufferRef = CMSampleBufferGetDataBuffer(sampleBuffer) else {return}
    let lengthOfBlock = CMBlockBufferGetDataLength(blockBufferRef)

    guard let data = NSMutableData(length: Int(lengthOfBlock)) else {return}
    CMBlockBufferCopyDataBytes(blockBufferRef, 0, lengthOfBlock, data.mutableBytes)

    // I need to pass the data as UnsafeMutablePointer<Int8> type
    let dataUnsafeMutablePointer = data.mutableBytes.assumingMemoryBound(to: Int8.self)

    FosSdk_SendTalkData(CameraConfigurationManager.mhandle, dataUnsafeMutablePointer, Int32(lengthOfBlock))

}

The AVCaptureSessions are starting correctly and i can see the sampleBuffers are collected every tot milliseconds. I just need to fill function parameters correctly and make these audio buffers be played on the ip camera

0 个答案:

没有答案