public static bool CreateMediaSession(byte[] mediaData, out MediaSession?session, IntPtr?windowHandle = null, bool isStartUpMediaManager = false) { PresentationDescriptor?pd = null; Topology?topology = null; try { if (isStartUpMediaManager) { MediaManager.Startup(); } var attributes = new MediaAttributes(mediaData.Length); MediaFactory.CreateMediaSession(attributes, out session); var resolver = new SourceResolver(); var byteStream = new ByteStream(mediaData); resolver.CreateObjectFromByteStream(byteStream, null, (int)SourceResolverFlags.ByteStream, null, out var objType, out var videoObject); GetMediaSource(videoObject, out var source); if (source != null) { MediaFactory.CreateTopology(out topology); source.CreatePresentationDescriptor(out pd); var r1 = CreatePlaybackTopology(source, pd, windowHandle.HasValue ? windowHandle.Value : IntPtr.Zero, out topology); if (r1.Success) { session.SetTopology(0, topology); return(true); } else { session = null; topology = null; return(false); } } else { session = null; topology = null; return(false); } } catch (SharpDXException ex) { Debug.Print(ex.ToString()); session = null; return(false); } finally { pd?.Dispose(); topology?.Dispose(); } }
public void CreatePresentationDescriptor(/*IMFPresentationDescriptor*/ out IntPtr ppPresentationDescriptor) { CheckShutdown(); Trace.WriteLine("RtspMediaSource::CreatePresentationDescriptor()"); if (!IsOpened) { throw new SharpDXException(ResultCode.NotInitializeD); } if (_presentationDescriptor == null) { throw new SharpDXException(ResultCode.InvalidStateTransition); } // Clone PresentationDescriptor pd = null; try { _presentationDescriptor.Clone(out pd); ppPresentationDescriptor = pd.Detach(); } finally { pd?.Dispose(); } }
private void Close() { if (IsPlaying || IsPaused) { Stop(); } if (IsOpened) { ChangeState(SourceState.Closed); } _isSampleQueueEmptySubscription?.Dispose(); _isSampleQueueEmptySubscription = null; foreach (var s in _streams) { s?.Dispose(); } _streams = new RtspMediaStream[0]; _presentationDescriptor?.Dispose(); _presentationDescriptor = null; }
public void Load(object customSource) { Close(); AfterClose(); if (Hwnd == IntPtr.Zero) { throw new InvalidOperationException(); } Trace.WriteLine("HwndRenderSession::Load()"); MediaSource source = null; Topology topo = null; PresentationDescriptor pdesc = null; try { // Create MediaSource(check argument) source = ComObject.As <MediaSource>(Marshal.GetIUnknownForObject(customSource)); // GetIUnknownForObject adds reference count // Create MediaSession MediaFactory.CreateMediaSession(null, out _mediaSession); _callback = new MediaSessionCallback(_mediaSession, OnMediaEvent); // Create Topology MediaFactory.CreateTopology(out topo); // Get PresentationDescriptor from MediaSource source.CreatePresentationDescriptor(out pdesc); // Connect each stream for (var i = 0; i < pdesc.StreamDescriptorCount; i++) { RawBool isSelected; using (var sdesc = pdesc.GetStreamDescriptorByIndex(i, out isSelected)) { if (!isSelected) { continue; } Activate renderer = null; TopologyNode srcnode = null; TopologyNode outnode = null; try { // Renderer if (sdesc.MediaTypeHandler.MajorType == MediaTypeGuids.Video) { MediaFactory.CreateVideoRendererActivate(Hwnd, out renderer); } else if (sdesc.MediaTypeHandler.MajorType == MediaTypeGuids.Audio) { MediaFactory.CreateAudioRendererActivate(out renderer); } else { // not supported continue; } // Source Node MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out srcnode); srcnode.Set(TopologyNodeAttributeKeys.Source, source); srcnode.Set(TopologyNodeAttributeKeys.PresentationDescriptor, pdesc); srcnode.Set(TopologyNodeAttributeKeys.StreamDescriptor, sdesc); // Output Node MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outnode); outnode.Object = renderer; // Connect topo.AddNode(srcnode); topo.AddNode(outnode); srcnode.ConnectOutput(0, outnode, 0); } finally { srcnode?.Dispose(); outnode?.Dispose(); renderer?.Dispose(); } } } // Set to session _mediaSession.SetTopology(SessionSetTopologyFlags.None, topo); } catch { Close(); AfterClose(); throw; } finally { pdesc?.Dispose(); topo?.Dispose(); source?.Dispose(); } }