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(); }
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; }
/// <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); }
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); }
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(); }