public void CanLockAndUnlockStore() { Guid guid = Guid.NewGuid(); double val; using (var instance = new MFAttributes(0)) using (var waitHandle = new AutoResetEvent(false)) { instance.LockStore(); try { instance.SetDouble(Guid.NewGuid(), 10); ThreadPool.QueueUserWorkItem(o => { waitHandle.Set(); instance.SetDouble(guid, 500); waitHandle.Set(); }); waitHandle.WaitOne(); //wait for the threadpool the queue the user item Thread.Sleep(2000); //give the threadpool some additional time to call instance.SetDouble Assert.IsFalse(instance.TryGet(guid, out val), "Second thread could set the attribute while instance was locked."); } finally { instance.UnlockStore(); } waitHandle.WaitOne(); } }
public static IntPtr CreateSinkWriterFromMFByteStreamNative(MFByteStream byteStream, MFAttributes attributes) { IntPtr p; int result = NativeMethods.ExternMFCreateSinkWriterFromURL(null, byteStream.BasePtr, attributes.BasePtr, out p); MediaFoundationException.Try(result, "Interops", "MFCreateSinkWriterFromURL"); return p; }
public void CanCreateEmptyMFAttributes() { using (var instance = new MFAttributes(0)) { Assert.IsNotNull(instance); Assert.AreNotSame(IntPtr.Zero, instance.BasePtr); } }
public void CanSetAndGetValues() { var random = new Random(); using (var instance = new MFAttributes(0)) { Guid guid = Guid.NewGuid(); instance.SetDouble(guid, 10d); Assert.AreEqual(10d, instance.GetDouble(guid)); guid = Guid.NewGuid(); instance.SetGuid(guid, guid); Assert.AreEqual(guid, instance.GetGuid(guid)); guid = Guid.NewGuid(); instance.SetString(guid, "Hello World"); Assert.AreEqual("Hello World", instance.GetString(guid)); guid = Guid.NewGuid(); instance.SetUINT32(guid, 10); Assert.AreEqual(10, instance.GetUINT32(guid)); guid = Guid.NewGuid(); instance.SetUINT64(guid, 10L); Assert.AreEqual(10L, instance.GetUINT64(guid)); guid = Guid.NewGuid(); var value = new PropertyVariant() { DataType = VarEnum.VT_UI4, UIntValue = 10 }; instance.SetItem(guid, value); Assert.AreEqual(value, instance.GetItem(guid)); guid = Guid.NewGuid(); byte[] inputBlob = new byte[100]; random.NextBytes(inputBlob); instance.Set(guid, inputBlob); CollectionAssert.AreEqual(inputBlob, instance.Get <byte[]>(guid)); guid = Guid.NewGuid(); TestStruct testStructValue = new TestStruct() { Field0 = Guid.NewGuid(), Field1 = random.Next(), Field2 = random.NextDouble() * random.Next(), }; instance.Set(guid, testStructValue); Assert.AreEqual(testStructValue, instance.Get <TestStruct>(guid)); } }
public void CanDeleteItem() { double val; using (var instance = new MFAttributes(0)) { Guid guid = Guid.NewGuid(); instance.SetDouble(guid, 100); Assert.AreEqual(100, instance.GetDouble(guid)); instance.DeleteItem(guid); Assert.IsFalse(instance.TryGet(guid, out val), "Item got not deleted."); } }
public void CanGetItemByIndex() { using (var instance = new MFAttributes(0)) { var guid = Guid.NewGuid(); instance.SetDouble(guid, 10); Assert.AreEqual(10d, instance[0].GetValue()); Assert.AreEqual(10d, instance[guid]); using (var instance0 = new MFAttributes(0)) { instance.CopyAllItems(instance0); Assert.AreEqual(10d, instance[0].GetValue()); Assert.AreEqual(10d, instance[guid]); } } }
public void CanDeleteAllItems() { const int count = 20; double val; Random random = new Random(); using (var instance = new MFAttributes(0)) { for (int i = 0; i < count; i++) { Guid guid = Guid.NewGuid(); val = random.NextDouble() * (i + 1); instance.SetDouble(guid, val); Assert.AreEqual(val, instance.GetDouble(guid)); } Assert.AreEqual(count, instance.Count); instance.DeleteAllItems(); Assert.AreEqual(0, instance.Count); } }
/// <summary> /// Sets and initializes the targetstream for the encoding process. /// </summary> /// <param name="stream">Stream which should be used as the targetstream.</param> /// <param name="inputMediaType">Mediatype of the raw input data to encode.</param> /// <param name="targetMediaType">Mediatype of the encoded data.</param> /// <param name="containerType">Container type which should be used.</param> protected void SetTargetStream(Stream stream, MFMediaType inputMediaType, MFMediaType targetMediaType, Guid containerType) { MFAttributes attributes = null; try { _targetBaseStream = new ComStream(stream); _targetStream = MediaFoundationCore.IStreamToByteStream(_targetBaseStream); attributes = new MFAttributes(2); attributes.SetUINT32(MediaFoundationAttributes.MF_READWRITE_ENABLE_HARDWARE_TRANSFORMS, 1); attributes.SetGuid(MediaFoundationAttributes.MF_TRANSCODE_CONTAINERTYPE, containerType); _sinkWriter = new MFSinkWriter(_targetStream, attributes); _streamIndex = _sinkWriter.AddStream(targetMediaType); _sinkWriter.SetInputMediaType(_streamIndex, inputMediaType, null); _targetMediaType = targetMediaType; _sourceBytesPerSecond = inputMediaType.AverageBytesPerSecond; //initialize the sinkwriter _sinkWriter.BeginWriting(); } catch (Exception) { if (_sinkWriter != null) { _sinkWriter.Dispose(); _sinkWriter = null; } if (_targetStream != null) { _targetStream.Dispose(); _targetStream = null; } throw; } finally { if (attributes != null) attributes.Dispose(); } }
/// <summary> /// Sets the input format for a stream on the sink writer. /// </summary> /// <param name="encodingParameters">Optional</param> /// <returns>HRESULT</returns> public unsafe int SetInputMediaTypeNative(int streamIndex, MFMediaType inputMediaType, MFAttributes encodingParameters) { void* imt = (void*)(inputMediaType == null ? IntPtr.Zero : inputMediaType.BasePtr); void* ep = (void*)(encodingParameters == null ? IntPtr.Zero : encodingParameters.BasePtr); return InteropCalls.CalliMethodPtr(_basePtr, streamIndex, imt, ep, ((void**)(*(void**)_basePtr))[4]); }
/// <summary> /// Sets the input format for a stream on the sink writer. /// </summary> /// <param name="encodingParameters">Optional</param> public void SetInputMediaType(int streamIndex, MFMediaType inputMediaType, MFAttributes encodingParameters) { MediaFoundationException.Try(SetInputMediaTypeNative(streamIndex, inputMediaType, encodingParameters), c, "SetInputMediaType"); }
/// <summary> /// Initializes a new instance of the <see cref="MFSinkWriter"/> class with a underlying <paramref name="byteStream"/>. /// </summary> /// <param name="byteStream">The underlying <see cref="MFByteStream"/> to use.</param> /// <param name="attributes">Attributes to configure the <see cref="MFSinkWriter"/>. For more information, see <see href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd389284(v=vs.85).aspx"/>. Use null/nothing as the default value.</param> public MFSinkWriter(MFByteStream byteStream, MFAttributes attributes = null) : this(MediaFoundationCore.CreateSinkWriterFromMFByteStreamNative(byteStream, attributes)) { }