using System;
using System.Runtime.InteropServices;
namespace NAudio.Dmo
{
///
/// Media Object InPlace
///
public class MediaObjectInPlace : IDisposable
{
private IMediaObjectInPlace mediaObjectInPlace;
///
/// Creates a new Media Object InPlace
///
/// Media Object InPlace COM Interface
internal MediaObjectInPlace(IMediaObjectInPlace mediaObjectInPlace)
{
this.mediaObjectInPlace = mediaObjectInPlace;
}
///
/// Processes a block of data.
/// The application supplies a pointer to a block of input data. The DMO processes the data in place.
///
/// Size of the data, in bytes.
/// offset into buffer
/// In/Out Data Buffer
/// Start time of the data.
/// DmoInplaceProcessFlags
/// Return value when Process is executed with IMediaObjectInPlace
public DmoInPlaceProcessReturn Process(int size, int offset, byte[] data, long timeStart, DmoInPlaceProcessFlags inPlaceFlag)
{
var pointer = Marshal.AllocHGlobal(size);
Marshal.Copy(data, offset, pointer, size);
var result = mediaObjectInPlace.Process(size, pointer, timeStart, inPlaceFlag);
Marshal.ThrowExceptionForHR(result);
Marshal.Copy(pointer, data, offset, size);
Marshal.FreeHGlobal(pointer);
return (DmoInPlaceProcessReturn) result;
}
///
/// Creates a copy of the DMO in its current state.
///
/// Copyed MediaObjectInPlace
public MediaObjectInPlace Clone()
{
Marshal.ThrowExceptionForHR(this.mediaObjectInPlace.Clone(out var cloneObj));
return new MediaObjectInPlace(cloneObj);
}
///
/// Retrieves the latency introduced by this DMO.
///
/// The latency, in 100-nanosecond units
public long GetLatency()
{
Marshal.ThrowExceptionForHR(this.mediaObjectInPlace.GetLatency(out var latencyTime));
return latencyTime;
}
///
/// Get Media Object
///
/// Media Object
public MediaObject GetMediaObject()
{
return new MediaObject((IMediaObject) mediaObjectInPlace);
}
///
/// Dispose code
///
public void Dispose()
{
if (mediaObjectInPlace != null)
{
Marshal.ReleaseComObject(mediaObjectInPlace);
mediaObjectInPlace = null;
}
}
}
}