Exemple #1
0
        private void ProcessServerSample(IBufferPacket packet)
        {
            // Only process samples when we are in started state
            StspSampleHeader sampleHead;

            // Copy the header object
            sampleHead = StreamConvertor.TakeObject <StspSampleHeader>(packet);
            if (packet.GetLength() < 0)
            {
                ThrowIfError(HResult.E_INVALIDARG);
            }

            if (sampleHead.dwStreamId != _videoStreamId)
            {
                return;
            }

            // Convert packet to MF sample
            IMFSample spSample;

            ThrowIfError(ToMFSample(packet, out spSample));
            SetSampleAttributes(sampleHead, spSample);

            _decoder.ProcessSample(spSample);
        }
Exemple #2
0
        private HResult ToMFSample(IBufferPacket packet, out IMFSample sample)
        {
            sample = null;
            IMFSample spSample;

            var hr = MFExtern.MFCreateSample(out spSample);

            if (MFError.Failed(hr))
            {
                return(hr);
            }

            //Get the media buffer
            IMFMediaBuffer mediaBuffer;

            hr = StreamConvertor.ConverToMediaBuffer(packet, out mediaBuffer);
            if (MFError.Failed(hr))
            {
                return(hr);
            }
            var len = 0;

            mediaBuffer.GetCurrentLength(out len);
            hr = spSample.AddBuffer(mediaBuffer);
            if (MFError.Failed(hr))
            {
                return(hr);
            }

            sample = spSample;
            return(hr);
        }
Exemple #3
0
        private void ProcessServerSample(IBufferPacket packet)
        {
            if (_eSourceState == SourceState.SourceState_Started)
            {
                // Only process samples when we are in started state
                StspSampleHeader sampleHead;

                // Copy the header object
                sampleHead = StreamConvertor.TakeObject <StspSampleHeader>(packet);
                if (packet.GetLength() < 0)
                {
                    ThrowIfError(HResult.E_INVALIDARG);
                }

                MediaStream spStream;
                ThrowIfError(GetStreamById(sampleHead.dwStreamId, out spStream));

                if (spStream.IsActive)
                {
                    // Convert packet to MF sample
                    IMFSample spSample;
                    ThrowIfError(ToMFSample(packet, out spSample));
                    // Forward sample to a proper stream.
                    spStream.ProcessSample(sampleHead, spSample);
                }
            }
            else
            {
                Throw(HResult.MF_E_UNEXPECTED);
            }
        }
Exemple #4
0
        public void TestBytesConvert_StspOperationHeader()
        {
            //result should be:[0,0,0,0,1,0,0,0]
            var result = StreamConvertor.BuildOperationBytes(StspOperation.StspOperation_ClientRequestDescription);

            Assert.IsTrue(BufferHelper.isBytesSame(result, Bytes_ClientRequestDescription));
        }
Exemple #5
0
        private void invokePacketComplete()
        {
            if (null == OnPacketReceived)
            {
                return;
            }

            //Debug.WriteLine($"Buffer complete. buffer length:{_penddingPacket.GetLength()} data length:{_penddingPacket.GetFirstOptionDataLength()}");
            IBufferPacket p;

            lock (_penddingPacketLock)
            {
                p = _penddingPacket.TakeFirstOption();
            }

            var header = StreamConvertor.TakeObject <StspOperationHeader>(p);

            if (header.cbDataSize != p.GetLength())
            {
                throw new InvalidNetworkBufferException();
            }

            try
            {
                OnPacketReceived(header.eOperation, p);
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Exception when invoke PacketComplete event handler:\r{ex.Message} \r {ex.StackTrace}");
            }
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        private void ProcessServerDescription(IBufferPacket data)
        {
            StspDescription desc           = new StspDescription();
            var             dataLen        = data.GetLength();
            int             descSize       = Marshal.SizeOf(typeof(StspDescription));
            int             streamDescSize = Marshal.SizeOf(typeof(StspStreamDescription));

            // Copy description
            desc = StreamConvertor.TakeObject <StspDescription>(data);
            // Size of the packet should match size described in the packet (size of Description structure + size of attribute blob)
            var cbConstantSize = Convert.ToInt32(descSize + (desc.cNumStreams - 1) * streamDescSize);

            // Check if the input parameters are valid. We only support 2 streams.
            if (cbConstantSize < Marshal.SizeOf(desc) || desc.cNumStreams == 0 || desc.cNumStreams > 2 || dataLen < cbConstantSize)
            {
                ThrowIfError(HResult.MF_E_UNSUPPORTED_FORMAT);
            }

            try
            {
                List <StspStreamDescription> streamDescs = new List <StspStreamDescription>(desc.aStreams);

                for (int i = 1; i < desc.cNumStreams; i++)
                {
                    var sd = StreamConvertor.TakeObject <StspStreamDescription>(data);
                    streamDescs.Add(sd);
                }

                int cbAttributeSize = 0;
                for (int i = 0; i < desc.cNumStreams; ++i)
                {
                    cbAttributeSize += streamDescs[i].cbAttributesSize;

                    /* todo: check out of range on cbAttributeSize
                     * if (out of range)
                     * {
                     *  Throw(HResult.MF_E_UNSUPPORTED_FORMAT);
                     * }*/
                }

                // Validate the parameters. Limit the total size of attributes to 64kB.
                if ((dataLen != (cbConstantSize + cbAttributeSize)) || (cbAttributeSize > 0x10000))
                {
                    Throw(HResult.MF_E_UNSUPPORTED_FORMAT);
                }

                //only init for the first video stream.
                foreach (var sd in streamDescs)
                {
                    if (sd.guiMajorType == MFMediaType.Video)
                    {
                        initVideoDesctriptor(sd, data);
                        _decoder.initialize(_videoStreamId, _videoMediaType);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemple #8
0
        private void ProcessServerDescription(IBufferPacket data)
        {
            StspDescription desc           = new StspDescription();
            var             dataLen        = data.GetLength();
            int             descSize       = Marshal.SizeOf(typeof(StspDescription));
            int             streamDescSize = Marshal.SizeOf(typeof(StspStreamDescription));

            // Copy description
            desc = StreamConvertor.TakeObject <StspDescription>(data);
            // Size of the packet should match size described in the packet (size of Description structure + size of attribute blob)
            var cbConstantSize = Convert.ToInt32(descSize + (desc.cNumStreams - 1) * streamDescSize);

            // Check if the input parameters are valid. We only support 2 streams.
            if (cbConstantSize < Marshal.SizeOf(desc) || desc.cNumStreams == 0 || desc.cNumStreams > 2 || dataLen < cbConstantSize)
            {
                ThrowIfError(HResult.MF_E_UNSUPPORTED_FORMAT);
            }

            try
            {
                List <StspStreamDescription> streamDescs = new List <StspStreamDescription>(desc.aStreams);

                for (int i = 1; i < desc.cNumStreams; i++)
                {
                    var sd = StreamConvertor.TakeObject <StspStreamDescription>(data);
                    streamDescs.Add(sd);
                }

                int cbAttributeSize = 0;
                for (int i = 0; i < desc.cNumStreams; ++i)
                {
                    cbAttributeSize += streamDescs[i].cbAttributesSize;

                    /* todo: check out of range on cbAttributeSize
                     * if (out of range)
                     * {
                     *  Throw(HResult.MF_E_UNSUPPORTED_FORMAT);
                     * }*/
                }

                // Validate the parameters. Limit the total size of attributes to 64kB.
                if ((dataLen != (cbConstantSize + cbAttributeSize)) || (cbAttributeSize > 0x10000))
                {
                    Throw(HResult.MF_E_UNSUPPORTED_FORMAT);
                }

                // Create stream for every stream description sent by the server.
                foreach (var sd in streamDescs)
                {
                    MediaStream spStream;
                    ThrowIfError(MediaStream.CreateInstance(sd, data, this, out spStream));
                    _streams.Add(spStream);
                }

                InitPresentationDescription();
                // Everything succeeded we are in stopped state now
                _eSourceState = SourceState.SourceState_Stopped;
                CompleteOpen(HResult.S_OK);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }