【问题标题】:NSData or bytes from CMSampleBufferRef来自 CMSampleBufferRef 的 NSData 或字节
【发布时间】:2013-09-15 11:36:23
【问题描述】:

您好,我需要通过网络发送CMSampleBufferRef。然后客户端通过音频队列服务播放CMSampleBufferRef。我看过一些关于堆栈溢出的例子,但大多数只是发送缓冲区。但随后一些信息丢失了。我发现[AVAssetReaderOutput copyNextSampleBuffer] 返回了对opaqueCMSampleBuffer 结构的引用。我知道如何获取opaqueCMSampleBuffer 的内存地址,但是如何将地址的内容复制到数组中以便通过网络发送?或者有没有更优雅的方法通过网络发送CMSampleBuffer。或者我什至可以以某种方式访问​​opaqueCMSampleBuffer

感谢您的时间和帮助

【问题讨论】:

    标签: objective-c avfoundation audioqueueservices cmsamplebufferref


    【解决方案1】:

    以下是从 CMSampleBufferRef 创建 NSData 对象的方法:

    1. 在接口(.h)文件中,将样本缓冲区引用添加为属性,同时转换为对象:

      @property (nonatomic, strong) __attribute__((NSObject)) CMSampleBufferRef sampleBuffer;

    2. 在实现文件(.m)中:

      CMSampleBufferRef sampleBuffer = (CMSampleBufferRef)[(AVAssetReaderTrackOutput *)[assetReader 输出][0] copyNextSampleBuffer];

      NSPurgeableData *sampleBufferData = (NSPurgeableData *)[self imageToBuffer:sampleBuffer];

    要从 NSData 对象访问样本缓冲区,只需使用强制转换:

    (CMSampleBufferRef)sampleBufferData;
    

    这是另一种方式:

    - (NSData *) imageToBuffer:(CMSampleBufferRef)source {
        CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(source);
        CVPixelBufferLockBaseAddress(imageBuffer,0);
    
        size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
        size_t width = CVPixelBufferGetWidth(imageBuffer);
        size_t height = CVPixelBufferGetHeight(imageBuffer);
        void *src_buff = CVPixelBufferGetBaseAddress(imageBuffer);
    
        NSData *data = [NSData dataWithBytes:src_buff length:bytesPerRow * height];
    
        CVPixelBufferUnlockBaseAddress(imageBuffer, 0);
        return data;
    }
    

    【讨论】:

    • 您能否分享代码 sn-p 以将 AVCaptureAudioDataOutputCMSampleBufferRef 转换为 NSData
    猜你喜欢
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多