예제 #1
0
        private MediaSource CreateMediaSource(string sURL)
        {
            SourceResolver sourceResolver = new SourceResolver();
            ComObject      comObject;

            comObject = sourceResolver.CreateObjectFromURL(sURL, SourceResolverFlags.MediaSource | SourceResolverFlags.ContentDoesNotHaveToMatchExtensionOrMimeType);
            return(comObject.QueryInterface <MediaSource>());
        }
예제 #2
0
        private void PlatformInitialize()
        {
            if (Topology != null)
            {
                return;
            }

            MediaManagerState.CheckStartup();

            MediaFactory.CreateTopology(out _topology);

            SharpDX.MediaFoundation.MediaSource mediaSource;
            {
                SourceResolver resolver = new SourceResolver();

                ObjectType otype;
                ComObject  source = resolver.CreateObjectFromURL(FileName, SourceResolverFlags.MediaSource, null, out otype);
                mediaSource = source.QueryInterface <SharpDX.MediaFoundation.MediaSource>();
                resolver.Dispose();
                source.Dispose();
            }

            PresentationDescriptor presDesc;

            mediaSource.CreatePresentationDescriptor(out presDesc);

            for (var i = 0; i < presDesc.StreamDescriptorCount; i++)
            {
                SharpDX.Mathematics.Interop.RawBool selected;
                StreamDescriptor desc;
                presDesc.GetStreamDescriptorByIndex(i, out selected, out desc);

                if (selected)
                {
                    TopologyNode sourceNode;
                    MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out sourceNode);

                    sourceNode.Set(TopologyNodeAttributeKeys.Source, mediaSource);
                    sourceNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, presDesc);
                    sourceNode.Set(TopologyNodeAttributeKeys.StreamDescriptor, desc);

                    TopologyNode outputNode;
                    MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outputNode);

                    var majorType = desc.MediaTypeHandler.MajorType;
                    if (majorType == MediaTypeGuids.Video)
                    {
                        Activate activate;

                        SampleGrabber = new VideoSampleGrabber();

                        _mediaType = new MediaType();

                        _mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);

                        // Specify that we want the data to come in as RGB32.
                        _mediaType.Set(MediaTypeAttributeKeys.Subtype, new Guid("00000016-0000-0010-8000-00AA00389B71"));

                        MediaFactory.CreateSampleGrabberSinkActivate(_mediaType, SampleGrabber, out activate);
                        outputNode.Object = activate;
                    }

                    if (majorType == MediaTypeGuids.Audio)
                    {
                        Activate activate;
                        MediaFactory.CreateAudioRendererActivate(out activate);

                        outputNode.Object = activate;
                    }

                    _topology.AddNode(sourceNode);
                    _topology.AddNode(outputNode);
                    sourceNode.ConnectOutput(0, outputNode, 0);

                    sourceNode.Dispose();
                    outputNode.Dispose();
                }

                desc.Dispose();
            }

            presDesc.Dispose();
            mediaSource.Dispose();
        }
예제 #3
0
        public void Setup(string fileName, Direct3DDeviceManager devMan = null)
        {
            logger.Debug("VideoFileSource::Setup()");

            //
            using (var sourceResolver = new SourceResolver())
            {
                var unkObj = sourceResolver.CreateObjectFromURL(fileName, SourceResolverFlags.MediaSource);

                var guid = typeof(MediaSource).GUID;
                unkObj.QueryInterface(ref guid, out var pUnk);

                mediaSource = new MediaSource(pUnk);
            }


            using (var mediaAttributes = new MediaAttributes(IntPtr.Zero))
            {
                MediaFactory.CreateAttributes(mediaAttributes, 5);
                //mediaAttributes.Set(SourceReaderAttributeKeys.EnableVideoProcessing, 1);

                if (devMan != null)
                {
                    //mediaAttributes.Set(SourceReaderAttributeKeys.DisableDxva, 0);
                    mediaAttributes.Set(SourceReaderAttributeKeys.D3DManager, devMan);
                }
                //mediaAttributes.Set(CodecApiPropertyKeys.AVLowLatencyMode, false);
                sourceReader = new SourceReader(mediaSource, mediaAttributes);
            }


            var charact = mediaSource.Characteristics;

            Console.WriteLine(MfTool.LogEnumFlags((MediaSourceCharacteristics)charact));


            Console.WriteLine("------------------CurrentMediaType-------------------");
            int videoStreamIndex = (int)SourceReaderIndex.FirstVideoStream;

            using (var currentMediaType = sourceReader.GetCurrentMediaType(videoStreamIndex))
            {
                Console.WriteLine(MfTool.LogMediaType(currentMediaType));

                var frameSize = currentMediaType.Get(MediaTypeAttributeKeys.FrameSize);
                var frameRate = currentMediaType.Get(MediaTypeAttributeKeys.FrameRate);

                OutputMediaType = new MediaType();

                OutputMediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);
                OutputMediaType.Set(MediaTypeAttributeKeys.Subtype, VideoFormatGuids.NV12);                // VideoFormatGuids.Yv12);
                OutputMediaType.Set(MediaTypeAttributeKeys.FrameSize, frameSize);
                OutputMediaType.Set(MediaTypeAttributeKeys.FrameRate, frameRate);

                OutputMediaType.Set(MediaTypeAttributeKeys.InterlaceMode, (int)VideoInterlaceMode.Progressive);
                OutputMediaType.Set(MediaTypeAttributeKeys.AllSamplesIndependent, 1);

                sourceReader.SetCurrentMediaType(videoStreamIndex, OutputMediaType);

                Console.WriteLine("------------------NEW MediaType-------------------");
                Console.WriteLine(MfTool.LogMediaType(OutputMediaType));
            }
        }
        public override void PlayFile(string filename)
        {
            //Load the file
            MediaSource mediaSource;

            {
                var        resolver = new SourceResolver();
                ObjectType otype;
                var        source = new ComObject(resolver.CreateObjectFromURL(filename, SourceResolverFlags.MediaSource, null, out otype));
                try
                {
                    // Sometimes throws HRESULT: [0x80004002], Module: [General], ApiCode: [E_NOINTERFACE/No such interface supported], Message: No such interface supported. Bug?
                    mediaSource = source.QueryInterface <MediaSource>();
                }
                catch (SharpDXException)
                {
                    mediaSource = null;
                    FLLog.Error("VideoPlayerWMF", "QueryInterface failed on Media Foundation");
                }
                resolver.Dispose();
                source.Dispose();
            }
            if (mediaSource is null)
            {
                return;
            }

            PresentationDescriptor presDesc;

            mediaSource.CreatePresentationDescriptor(out presDesc);

            for (int i = 0; i < presDesc.StreamDescriptorCount; i++)
            {
                SharpDX.Mathematics.Interop.RawBool selected;
                StreamDescriptor desc;
                presDesc.GetStreamDescriptorByIndex(i, out selected, out desc);
                if (selected)
                {
                    TopologyNode sourceNode;
                    MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out sourceNode);

                    sourceNode.Set(TopologyNodeAttributeKeys.Source, mediaSource);
                    sourceNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, presDesc);
                    sourceNode.Set(TopologyNodeAttributeKeys.StreamDescriptor, desc);

                    TopologyNode outputNode;
                    MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outputNode);

                    var majorType = desc.MediaTypeHandler.MajorType;
                    if (majorType == MediaTypeGuids.Video)
                    {
                        Activate activate;

                        videoSampler = new MFSamples();
                        //retrieve size of video
                        long sz = desc.MediaTypeHandler.CurrentMediaType.Get <long>(new Guid("{1652c33d-d6b2-4012-b834-72030849a37d}"));
                        int  height = (int)(sz & uint.MaxValue), width = (int)(sz >> 32);
                        _texture = new Texture2D(width, height, false, SurfaceFormat.Color);
                        mt       = new MediaType();

                        mt.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);

                        // Specify that we want the data to come in as RGB32.
                        mt.Set(MediaTypeAttributeKeys.Subtype, new Guid("00000016-0000-0010-8000-00AA00389B71"));
                        GetMethods();
                        MFCreateSampleGrabberSinkActivate(mt, videoSampler, out activate);
                        outputNode.Object = activate;
                    }

                    if (majorType == MediaTypeGuids.Audio)
                    {
                        Activate activate;
                        MediaFactory.CreateAudioRendererActivate(out activate);

                        outputNode.Object = activate;
                    }

                    topology.AddNode(sourceNode);
                    topology.AddNode(outputNode);
                    sourceNode.ConnectOutput(0, outputNode, 0);

                    sourceNode.Dispose();
                    outputNode.Dispose();
                }
                desc.Dispose();
            }

            presDesc.Dispose();
            mediaSource.Dispose();
            //Play the file
            cb = new MFCallback(this, session);
            session.BeginGetEvent(cb, null);
            session.SetTopology(SessionSetTopologyFlags.Immediate, topology);
            // Get the clock
            clock = session.Clock.QueryInterface <PresentationClock>();

            // Start playing.
            Playing = true;
        }
예제 #5
0
        private void PlatformInitialize()
        {
            if (Topology != null)
            {
                return;
            }

            //MediaManagerState.CheckStartup();

            MediaFactory.CreateTopology(out _topology);

            SharpDX.MediaFoundation.MediaSource mediaSource;
            {
                SourceResolver resolver = new SourceResolver();

                ObjectType otype;
                ComObject  source = resolver.CreateObjectFromURL(FileName, SourceResolverFlags.MediaSource, null, out otype);
                mediaSource = source.QueryInterface <SharpDX.MediaFoundation.MediaSource>();


                resolver.Dispose();
                source.Dispose();
            }


            PresentationDescriptor presDesc;

            mediaSource.CreatePresentationDescriptor(out presDesc);

            for (var i = 0; i < presDesc.StreamDescriptorCount; i++)
            {
                RawBool          selected = false;
                StreamDescriptor desc;
                presDesc.GetStreamDescriptorByIndex(i, out selected, out desc);

                if (selected)
                {
                    TopologyNode sourceNode;
                    MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out sourceNode);

                    sourceNode.Set(TopologyNodeAttributeKeys.Source, mediaSource);
                    sourceNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, presDesc);
                    sourceNode.Set(TopologyNodeAttributeKeys.StreamDescriptor, desc);


                    TopologyNode outputNode;
                    MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outputNode);

                    var majorType = desc.MediaTypeHandler.MajorType;

                    if (majorType == MediaTypeGuids.Video)
                    {
                        Activate activate;

                        SampleGrabber = new VideoSampleGrabber();

                        _mediaType = new MediaType();

                        _mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);

                        // Specify that we want the data to come in as RGB32.
                        _mediaType.Set(MediaTypeAttributeKeys.Subtype, new Guid("00000016-0000-0010-8000-00AA00389B71"));

                        MediaFactory.CreateSampleGrabberSinkActivate(_mediaType, SampleGrabber, out activate);
                        outputNode.Object = activate;


                        long frameSize = desc.MediaTypeHandler.CurrentMediaType.Get <long>(MediaTypeAttributeKeys.FrameSize);

                        Width  = (int)(frameSize >> 32);
                        Height = (int)(frameSize & 0x0000FFFF);
                    }

                    if (majorType == MediaTypeGuids.Audio)
                    {
                        Activate activate;
                        MediaFactory.CreateAudioRendererActivate(out activate);

                        outputNode.Object = activate;
                    }

                    _topology.AddNode(sourceNode);
                    _topology.AddNode(outputNode);
                    sourceNode.ConnectOutput(0, outputNode, 0);


                    Duration = new TimeSpan(presDesc.Get <long>(PresentationDescriptionAttributeKeys.Duration));


                    sourceNode.Dispose();
                    outputNode.Dispose();
                }

                desc.Dispose();
            }

            presDesc.Dispose();
            mediaSource.Dispose();


            VideoFrame = new Texture2D(Game.Instance.GraphicsDevice, Width, Height, ColorFormat.Bgra8, false);
        }
예제 #6
0
파일: Video.cs 프로젝트: ttou73/IronStar
        /// <summary>
        ///
        /// </summary>
        private void PlatformInitialize(byte[] bytes, Stream stream, string url)
        {
            if (Topology != null)
            {
                return;
            }

            MediaFactory.CreateTopology(out _topology);

            SharpDX.MediaFoundation.MediaSource mediaSource;
            {
                SourceResolver resolver = new SourceResolver();

                ObjectType otype;
                ComObject  source = null;

                if (url != null)
                {
                    source = resolver.CreateObjectFromURL(url, SourceResolverFlags.MediaSource, null, out otype);
                }

                if (stream != null)
                {
                    var bs = new ByteStream(stream);
                    source = resolver.CreateObjectFromStream(bs, null, SourceResolverFlags.MediaSource, null, out otype);
                }

                if (bytes != null)
                {
                    var bs = new ByteStream(bytes);
                    source = resolver.CreateObjectFromStream(bs, null, SourceResolverFlags.MediaSource | SourceResolverFlags.ContentDoesNotHaveToMatchExtensionOrMimeType, null, out otype);
                }

                if (source == null)
                {
                    throw new ArgumentException("'stream' and 'url' are null!");
                }

                mediaSource = source.QueryInterface <SharpDX.MediaFoundation.MediaSource>();


                resolver.Dispose();
                source.Dispose();
            }


            PresentationDescriptor presDesc;

            mediaSource.CreatePresentationDescriptor(out presDesc);

            for (var i = 0; i < presDesc.StreamDescriptorCount; i++)
            {
                RawBool          selected = false;
                StreamDescriptor desc;
                presDesc.GetStreamDescriptorByIndex(i, out selected, out desc);

                if (selected)
                {
                    TopologyNode sourceNode;
                    MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out sourceNode);

                    sourceNode.Set(TopologyNodeAttributeKeys.Source, mediaSource);
                    sourceNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, presDesc);
                    sourceNode.Set(TopologyNodeAttributeKeys.StreamDescriptor, desc);


                    TopologyNode outputNode;
                    MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outputNode);

                    var majorType = desc.MediaTypeHandler.MajorType;

                    if (majorType == MediaTypeGuids.Video)
                    {
                        Activate activate;

                        sampleGrabber = new VideoSampleGrabber();

                        _mediaType = new MediaType();

                        _mediaType.Set(MediaTypeAttributeKeys.MajorType, MediaTypeGuids.Video);

                        // Specify that we want the data to come in as RGB32.
                        _mediaType.Set(MediaTypeAttributeKeys.Subtype, new Guid("00000016-0000-0010-8000-00AA00389B71"));

                        MediaFactory.CreateSampleGrabberSinkActivate(_mediaType, SampleGrabber, out activate);
                        outputNode.Object = activate;


                        long frameSize = desc.MediaTypeHandler.CurrentMediaType.Get <long>(MediaTypeAttributeKeys.FrameSize);

                        Width  = (int)(frameSize >> 32);
                        Height = (int)(frameSize & 0x0000FFFF);
                    }

                    if (majorType == MediaTypeGuids.Audio)
                    {
                        Activate activate;
                        MediaFactory.CreateAudioRendererActivate(out activate);

                        outputNode.Object = activate;
                    }

                    _topology.AddNode(sourceNode);
                    _topology.AddNode(outputNode);
                    sourceNode.ConnectOutput(0, outputNode, 0);


                    Duration = new TimeSpan(presDesc.Get <long>(PresentationDescriptionAttributeKeys.Duration));


                    sourceNode.Dispose();
                    outputNode.Dispose();
                }

                desc.Dispose();
            }

            presDesc.Dispose();
            mediaSource.Dispose();


            videoFrame = new DynamicTexture(Game.Instance.RenderSystem, Width, Height, typeof(ColorBGRA), false, false);
        }
예제 #7
0
        private void PlatformInitialize(string fileName)
        {
            if (_topology != null)
            {
                return;
            }

            MediaManagerState.CheckStartup();

            MediaFactory.CreateTopology(out _topology);

            SharpDX.MediaFoundation.MediaSource mediaSource;
            {
                SourceResolver resolver = new SourceResolver();

                ComObject source = resolver.CreateObjectFromURL(FilePath, SourceResolverFlags.MediaSource);
                mediaSource = source.QueryInterface <SharpDX.MediaFoundation.MediaSource>();
                resolver.Dispose();
                source.Dispose();
            }

            mediaSource.CreatePresentationDescriptor(out PresentationDescriptor presDesc);

            for (var i = 0; i < presDesc.StreamDescriptorCount; i++)
            {
                presDesc.GetStreamDescriptorByIndex(i, out SharpDX.Mathematics.Interop.RawBool selected, out StreamDescriptor desc);

                if (selected)
                {
                    MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out TopologyNode sourceNode);

                    sourceNode.Set(TopologyNodeAttributeKeys.Source, mediaSource);
                    sourceNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, presDesc);
                    sourceNode.Set(TopologyNodeAttributeKeys.StreamDescriptor, desc);

                    MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out TopologyNode outputNode);

                    var typeHandler = desc.MediaTypeHandler;
                    var majorType   = typeHandler.MajorType;
                    if (majorType != MediaTypeGuids.Audio)
                    {
                        throw new NotSupportedException("The song contains video data!");
                    }

                    MediaFactory.CreateAudioRendererActivate(out Activate activate);
                    outputNode.Object = activate;

                    _topology.AddNode(sourceNode);
                    _topology.AddNode(outputNode);
                    sourceNode.ConnectOutput(0, outputNode, 0);

                    sourceNode.Dispose();
                    outputNode.Dispose();
                    typeHandler.Dispose();
                    activate.Dispose();
                }

                desc.Dispose();
            }

            presDesc.Dispose();
            mediaSource.Dispose();
        }