开发者

How to play audio sample buffers from AVCaptureAudioDataOutput

开发者 https://www.devze.com 2023-02-22 21:52 出处:网络
The main goal of the app Im trying to make is a peer-to-peer video streaming. (Sort of like FaceTime using bluetooth/WiFi).

The main goal of the app Im trying to make is a peer-to-peer video streaming. (Sort of like FaceTime using bluetooth/WiFi).

Using AVFoundation, I was able to capture video/audio sample buffers. Then Im sending the video/audo sample buffer data. Now the problem is to process the sample buffer data in the receiving side.

As for the video sample buffer, I was able to 开发者_JS百科get a UIImage from the sample buffer. But for the audio sample buffer, I dont know how to process it so I can play the audio.

So the question is how can I process/play the audio sample buffers?

Right now Im just plotting the waveform, just like in apple's Wavy sample code:

CMSampleBufferRef sampleBuffer;

CMItemCount numSamples = CMSampleBufferGetNumSamples(sampleBuffer);
NSUInteger channelIndex = 0;

CMBlockBufferRef audioBlockBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
size_t audioBlockBufferOffset = (channelIndex * numSamples * sizeof(SInt16));
size_t lengthAtOffset = 0;
size_t totalLength = 0;
SInt16 *samples = NULL;
CMBlockBufferGetDataPointer(audioBlockBuffer, audioBlockBufferOffset, &lengthAtOffset, &totalLength, (char **)(&samples));

int numSamplesToRead = 1;
for (int i = 0; i < numSamplesToRead; i++) {

    SInt16 subSet[numSamples / numSamplesToRead];
    for (int j = 0; j < numSamples / numSamplesToRead; j++)
        subSet[j] = samples[(i * (numSamples / numSamplesToRead)) + j];

    SInt16 audioSample = [Util maxValueInArray:subSet ofSize:(numSamples / numSamplesToRead)];
    double scaledSample = (double) ((audioSample / SINT16_MAX));

    // plot waveform using scaledSample
    [updateUI:scaledSample];
}


To show video you can use (here is : getting of ARGB picture and converting to Qt (nokia qt) QImage you can replace by other image)

place it to delegate class

 - (void)captureOutput:(AVCaptureOutput *)captureOutput
    didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
           fromConnection:(AVCaptureConnection *)connection

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);

CVPixelBufferLockBaseAddress(imageBuffer,0);

SVideoSample sample;

sample.pImage      = (char *)CVPixelBufferGetBaseAddress(imageBuffer);
sample.bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
sample.width       = CVPixelBufferGetWidth(imageBuffer);
sample.height      = CVPixelBufferGetHeight(imageBuffer);

QImage img((unsigned char *)sample.pImage, sample.width, sample.height, sample.bytesPerRow, QImage::Format_ARGB32);

self->m_receiver->eventReceived(img);

CVPixelBufferUnlockBaseAddress(imageBuffer,0);
[pool drain];
0

精彩评论

暂无评论...
验证码 换一张
取 消