/// <summary> /// Take the object buffer from the packet and convert object buffer to struct entity. /// </summary> /// <param name="packet">The buffer where struct bytes are sasved.</param> public static T TakeObject <T>(IBufferPacket packet) { //todo: convert return type as HResult. var size = Marshal.SizeOf(typeof(T)); var buffer = packet.TakeBuffer(size); return(BytesToStruct <T>(buffer, size)); }
private void Initialize(StspStreamDescription pStreamDescription, IBufferPacket attributesBuffer) { //Create the media event queue. ThrowIfError(MFExtern.MFCreateEventQueue(out _spEventQueue)); IMFMediaType mediaType; IMFStreamDescriptor spSD; IMFMediaTypeHandler spMediaTypeHandler; _isVideo = (pStreamDescription.guiMajorType == MFMediaType.Video); //Create a media type object. ThrowIfError(MFExtern.MFCreateMediaType(out mediaType)); if (attributesBuffer.GetLength() < pStreamDescription.cbAttributesSize || pStreamDescription.cbAttributesSize == 0) { //Invalid stream description Throw(HResult.MF_E_UNSUPPORTED_FORMAT); } //Prepare buffer where we will copy attributes to, then initialize media type's attributes var pAttributes = Marshal.AllocHGlobal(pStreamDescription.cbAttributesSize); try { Marshal.Copy(attributesBuffer.TakeBuffer(pStreamDescription.cbAttributesSize), 0, pAttributes, pStreamDescription.cbAttributesSize); ThrowIfError(MFExtern.MFInitAttributesFromBlob(mediaType, pAttributes, pStreamDescription.cbAttributesSize)); } finally { Marshal.FreeHGlobal(pAttributes); } Validation.ValidateInputMediaType(pStreamDescription.guiMajorType, pStreamDescription.guiSubType, mediaType); ThrowIfError(mediaType.SetGUID(MF_MT_MAJOR_TYPE, pStreamDescription.guiMajorType)); ThrowIfError(mediaType.SetGUID(MF_MT_SUBTYPE, pStreamDescription.guiSubType)); //Now we can create MF stream descriptor. ThrowIfError(MFExtern.MFCreateStreamDescriptor(pStreamDescription.dwStreamId, 1, new IMFMediaType[] { mediaType }, out spSD)); ThrowIfError(spSD.GetMediaTypeHandler(out spMediaTypeHandler)); //Set current media type ThrowIfError(spMediaTypeHandler.SetCurrentMediaType(mediaType)); _spStreamDescriptor = spSD; _id = pStreamDescription.dwStreamId; //State of the stream is started. _eSourceState = SourceState.SourceState_Stopped; }
public static HResult ConverToMediaBuffer(IBufferPacket packet, out IMFMediaBuffer mediaBuffer) { mediaBuffer = null; var dataLength = packet.GetLength(); if (packet == null || dataLength == 0) { return(HResult.E_INVALIDARG); } IMFMediaBuffer spMediaBuffer; HResult hr = MFExtern.MFCreateMemoryBuffer(dataLength, out spMediaBuffer); if (MFError.Failed(hr)) { return(hr); } IntPtr pBuffer; int cbMaxLength; int cbCurrentLength; //todo: call lock2d on a 2d buffer because the lock2d is more efficient. /* * if (MFError.Succeeded(Marshal.intp spMediaBuffer.QueryInterface(IID_PPV_ARGS(&_sp2DBuffer)))) * { * LONG lPitch; * hr = _sp2DBuffer.Lock2D(&_pBuffer, &lPitch); * } * else * { * hr = pMediaBuffer->Lock(&_pBuffer, &cbMaxLength, &cbCurrentLength); * }*/ hr = spMediaBuffer.Lock(out pBuffer, out cbMaxLength, out cbCurrentLength); if (MFError.Failed(hr)) { return(hr); } var buffer = packet.TakeBuffer(dataLength); Marshal.Copy(buffer, 0, pBuffer, buffer.Length); spMediaBuffer.SetCurrentLength(buffer.Length); spMediaBuffer.Unlock(); mediaBuffer = spMediaBuffer; buffer = null; return(hr); }
private void initVideoDesctriptor(StspStreamDescription pStreamDescription, IBufferPacket attributesBuffer) { IMFMediaType mediaType; IMFStreamDescriptor spSD; IMFMediaTypeHandler spMediaTypeHandler; //Create a media type object. ThrowIfError(MFExtern.MFCreateMediaType(out mediaType)); if (attributesBuffer.GetLength() < pStreamDescription.cbAttributesSize || pStreamDescription.cbAttributesSize == 0) { //Invalid stream description Throw(HResult.MF_E_UNSUPPORTED_FORMAT); } //Prepare buffer where we will copy attributes to, then initialize media type's attributes var pAttributes = Marshal.AllocHGlobal(pStreamDescription.cbAttributesSize); try { Marshal.Copy(attributesBuffer.TakeBuffer(pStreamDescription.cbAttributesSize), 0, pAttributes, pStreamDescription.cbAttributesSize); ThrowIfError(MFExtern.MFInitAttributesFromBlob(mediaType, pAttributes, pStreamDescription.cbAttributesSize)); } finally { Marshal.FreeHGlobal(pAttributes); } ThrowIfError(mediaType.SetGUID(MF_MT_MAJOR_TYPE, pStreamDescription.guiMajorType)); ThrowIfError(mediaType.SetGUID(MF_MT_SUBTYPE, pStreamDescription.guiSubType)); //Now we can create MF stream descriptor. ThrowIfError(MFExtern.MFCreateStreamDescriptor(pStreamDescription.dwStreamId, 1, new IMFMediaType[] { mediaType }, out spSD)); ThrowIfError(spSD.GetMediaTypeHandler(out spMediaTypeHandler)); //Set current media type ThrowIfError(spMediaTypeHandler.SetCurrentMediaType(mediaType)); _videoMediaType = mediaType; _videoStreamDescriptor = spSD; _videoStreamId = pStreamDescription.dwStreamId; ThrowIfError(MFExtern.MFGetAttributeSize(mediaType, MFAttributesClsid.MF_MT_FRAME_SIZE, out _videoWitdh, out _videoHeight)); ThrowIfError(MFExtern.MFGetAttributeRatio(mediaType, MFAttributesClsid.MF_MT_PIXEL_ASPECT_RATIO, out _videoRatioN, out _videoRatioD)); _drawDevice.InitializeSetVideoSize(_videoWitdh, _videoHeight, new MFRatio(_videoRatioN, _videoRatioD)); }
private void ProcessServerFormatChange(IBufferPacket packet) { IntPtr ptr; try { if (_eSourceState != SourceState.SourceState_Started) { Throw(HResult.MF_E_UNEXPECTED); } int cbTotalLen = packet.GetLength(); if (cbTotalLen <= 0) { Throw(HResult.E_INVALIDARG); } // Minimum size of the operation payload is size of Description structure if (cbTotalLen < Marshal.SizeOf(typeof(StspStreamDescription))) { ThrowIfError(HResult.MF_E_UNSUPPORTED_FORMAT); } //todo: add try or use enhanced method to judge the HResult received from TakeObject(...) StspStreamDescription streamDesc = StreamConvertor.TakeObject <StspStreamDescription>(packet); if (cbTotalLen != Marshal.SizeOf(typeof(StspStreamDescription)) + streamDesc.cbAttributesSize || streamDesc.cbAttributesSize == 0) { ThrowIfError(HResult.MF_E_UNSUPPORTED_FORMAT); } // Prepare buffer where we will copy attributes to ptr = Marshal.AllocHGlobal(streamDesc.cbAttributesSize); var data = packet.TakeBuffer(streamDesc.cbAttributesSize); Marshal.Copy(data, 0, ptr, streamDesc.cbAttributesSize); IMFMediaType spMediaType; // Create a media type object. ThrowIfError(MFExtern.MFCreateMediaType(out spMediaType)); // Initialize media type's attributes ThrowIfError(MFExtern.MFInitAttributesFromBlob(spMediaType, ptr, streamDesc.cbAttributesSize)); } catch (Exception ex) { throw ex; } Marshal.Release(ptr); }
public void TestTakeBuffer() { clearPacket(); packet.AddBuffer(data1); packet.AddBuffer(data2); packet.AddBuffer(data3); packet.AddBuffer(data4); Assert.AreEqual(data1.Length + data2.Length + data3.Length + data4.Length, packet.GetLength()); Assert.IsTrue(BufferHelper.isBytesSame(data1, packet.TakeBuffer(data1.Length))); Assert.AreEqual(data2.Length + data3.Length + data4.Length, packet.GetLength()); Assert.IsTrue(BufferHelper.isBytesSame(data2, packet.TakeBuffer(data2.Length))); Assert.AreEqual(data3.Length + data4.Length, packet.GetLength()); Assert.IsTrue(BufferHelper.isBytesSame(data3, packet.TakeBuffer(data3.Length))); Assert.AreEqual(data4.Length, packet.GetLength()); Assert.IsTrue(BufferHelper.isBytesSame(data4, packet.TakeBuffer(data4.Length))); Assert.AreEqual(0, packet.GetLength()); }