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; }
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 static HResult CreateInstance(StspStreamDescription pStreamDescription, IBufferPacket pAttributesBuffer, NetworkSource pSource, out MediaStream ppStream) { ppStream = null; HResult hr = HResult.S_OK; try { MediaStream stream = new MediaStream(pSource); if (null == stream) { MFError.ThrowExceptionForHR(HResult.E_OUTOFMEMORY); } stream.Initialize(pStreamDescription, pAttributesBuffer); ppStream = stream; } catch (Exception ex) { hr = (HResult)ex.HResult; } return(hr); }