Monotouch - CMSampleBufferGetSampleTimingInfoArray

时间:2013-06-04 15:11:54

标签: c# ios xamarin.ios xamarin

我目前正在开发一个用monotouch编写的概念验证应用程序,用于录制视频。我需要调整CMSampleBuffer的时间,我找到了一个代码,但它是用Objective-C编写的。我试着翻译下面的代码,但我找不到任何映射到这个函数 - CMSampleBufferGetSampleTimingInfoArray ,我们如何在monotouch中调用这个函数?以下是调整时间的完整代码,但在Objective-C中:

- (CMSampleBufferRef) adjustTime:(CMSampleBufferRef) sample by:(CMTime) offset
{
    CMItemCount count;
    CMSampleBufferGetSampleTimingInfoArray(sample, 0, nil, &count);
    CMSampleTimingInfo* pInfo = malloc(sizeof(CMSampleTimingInfo) * count);
    CMSampleBufferGetSampleTimingInfoArray(sample, count, pInfo, &count);
    for (CMItemCount i = 0; i < count; i++)
    {
        pInfo[i].decodeTimeStamp = CMTimeSubtract(pInfo[i].decodeTimeStamp, offset);
        pInfo[i].presentationTimeStamp = CMTimeSubtract(pInfo[i].presentationTimeStamp, offset);
    }
    CMSampleBufferRef sout;
    CMSampleBufferCreateCopyWithNewTiming(nil, sample, count, pInfo, &sout);
    free(pInfo);
    return sout;
}

1 个答案:

答案 0 :(得分:1)

这是未经测试的,但我相信它应该让你走上正轨:

[DllImport(Constants.CoreMediaLibrary)]
int CMSampleBufferGetSampleTimingInfoArray (
    IntPtr sbuf,
    int timingArrayEntries,
    CMSampleTimingInfo [] timingArrayOut,
    out int timingArrayEntriesNeededOut
);

[DllImport(Constants.CoreMediaLibrary)]
int CMSampleBufferCreateCopyWithNewTiming (
    IntPtr allocator,
    IntPtr originalSBuf,
    int numSampleTimingEntries,
    CMSampleTimingInfo []sampleTimingArray,
    out IntPtr sBufCopyOut
);

CMSampleBuffer AdjustTime (CMSampleBuffer sample, CMTime offset)
{
    int count;
    CMSampleBufferGetSampleTimingInfoArray (sample.Handle, 0, null, out count);
    CMSampleTimingInfo [] pInfo = new CMSampleTimingInfo [count];
    CMSampleBufferGetSampleTimingInfoArray (sample.Handle, count, pInfo, out count);
    for (int i = 0; i < count; i++) {
        pInfo [i].DecodeTimeStamp = CMTime.Subtract (pInfo [i].DecodeTimeStamp, offset);
        pInfo [i].PresentationTimeStamp = CMTime.Subtract (pInfo [i].PresentationTimeStamp, offset);
    }
    IntPtr sout;
    CMSampleBufferCreateCopyWithTiming (IntPtr.Zero, sample, count, pInfo, out sout);
    // Unfortunately the constructor we need is internal, but that can be fixed
    // for a future version of Xamarin.iOS.
    return (CMSampleBuffer) typeof (CMSampleBuffer).GetConstructor (BindingFlags.Instance | BindingFlags.NonPublic, null, new Type [] { typeof (IntPtr), typeof (bool) }, null).Invoke (new object [] { sout, true });
}

<强>更新

我已经在Xamarin.iOS中实现了一些必需的API,所以在将来的版本中你将能够做到这一点:

CMSampleBuffer AdjustTime (CMSampleBuffer sample, CMTime offset)
{
    var info = sample.GetSampleTimingInfo ();
    for (int i = 0; i < info.Length; i++) {
        info [i].DecodeTimeStamp = CMTime.Subtract (info [i].DecodeTimeStamp, offset);
        info [i].PresentationTimeStamp = CMTime.Subtract (info [i].PresentationTimeStamp, offset);
    }
    return CMSampleBuffer.CreateWithNewTiming (sample, info)
}