コード例 #1
0
        private static Result CreatePlaybackTopology(MediaSource source, PresentationDescriptor descriptor, IntPtr hVideoWnd, out Topology?topology)
        {
            Topology?_topology = null;

            try
            {
                MediaFactory.CreateTopology(out _topology);
                int  index  = 0;
                bool status = true;
                do
                {
                    status = AddBranchToPartialTopology(_topology, source, descriptor, index, hVideoWnd);
                    index++;
                } while (status);
                topology = _topology;
                return(Result.Ok);
            }
            catch (SharpDXException ex)
            {
                topology = null;
                return(ex.ResultCode);
            }
            finally
            {
                _topology?.Dispose();
            }
        }
コード例 #2
0
 public RtspMediaSource()
 {
     _eventGenerator                 = new MediaEventGeneratorImpl();
     _stateSubject                   = new BehaviorSubject <SourceState>(SourceState.Closed);
     _streams                        = new RtspMediaStream[0];
     _presentationDescriptor         = null;
     _isSampleQueueEmptySubscription = null;
     _bufferingStateLock             = new SpinLock();
     _bufferingStartedSent           = false;
 }
コード例 #3
0
        private TopologyNode CreateSourceStreamNode(PresentationDescriptor presentationDescriptor, StreamDescriptor streamDescriptor)
        {
            TopologyNode inputNode;

            MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out inputNode);
            inputNode.Set(TopologyNodeAttributeKeys.Source.Guid, mediaSource);
            inputNode.Set(TopologyNodeAttributeKeys.PresentationDescriptor.Guid, presentationDescriptor);
            inputNode.Set(TopologyNodeAttributeKeys.StreamDescriptor.Guid, streamDescriptor);

            return(inputNode);
        }
コード例 #4
0
        private void AddBranchToPartialTopology(PresentationDescriptor presentationDescriptor, int iStream)
        {
            SharpDX.Bool     isSelected;
            StreamDescriptor streamDescriptor;

            presentationDescriptor.GetStreamDescriptorByIndex(iStream, out isSelected, out streamDescriptor);
            if (isSelected)
            {
                TopologyNode sourceNode = CreateSourceStreamNode(presentationDescriptor, streamDescriptor);
                //var resource = videoTexture.QueryInterface<SharpDX.DXGI.Resource>();
                //var sharedTex = _device.OpenSharedResource<Texture2D>(resource.SharedHandle)
                TopologyNode outputNode = CreateOutputNode(streamDescriptor, videoHwnd);

                topology.AddNode(sourceNode);
                topology.AddNode(outputNode);
                sourceNode.ConnectOutput(0, outputNode, 0);
            }
        }
コード例 #5
0
        private static void CreateMediaSinkActivate(PresentationDescriptor pd, StreamDescriptor sd, int streamIndex, IntPtr hVideoWnd, out Activate?activate)
        {
            var handler   = sd.MediaTypeHandler;
            var majorType = handler.MajorType;

            if (majorType == MediaTypeGuids.Audio)
            {
                MediaFactory.CreateAudioRendererActivate(out activate);
            }
            else if (majorType == MediaTypeGuids.Video)
            {
                MediaFactory.CreateVideoRendererActivate(hVideoWnd, out activate);
            }
            else
            {
                pd.DeselectStream(streamIndex);
                activate = null;
            }
        }
コード例 #6
0
        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();
            }
        }
コード例 #7
0
        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;
        }
コード例 #8
0
        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();
            }
        }
コード例 #9
0
        private static bool AddBranchToPartialTopology(Topology topology, MediaSource source, PresentationDescriptor descriptor, int streamIndex, IntPtr hVideoWnd)
        {
            StreamDescriptor?streamDescriptor = null;
            Activate?        activate = null;
            TopologyNode?    sourceNode = null, outputNode = null;

            try
            {
                streamDescriptor = descriptor.GetStreamDescriptorByIndex(streamIndex, out var isSelected);
                if (isSelected)
                {
                    CreateMediaSinkActivate(descriptor, streamDescriptor, streamIndex, hVideoWnd, out activate);
                    if (activate != null)
                    {
                        var sourceNodePtr = Marshal.AllocHGlobal(Marshal.SizeOf(source));
                        MediaFactory.CreateTopologyNode(TopologyType.SourceStreamNode, out sourceNode);
                        sourceNode.Object = source;
                        topology.AddNode(sourceNode);
                        MediaFactory.CreateTopologyNode(TopologyType.OutputNode, out outputNode);
                        outputNode.Object = activate;
                        topology.AddNode(outputNode);
                        sourceNode.ConnectOutput(0, outputNode, 0);
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (SharpDXException ex)
            {
                Debug.Print(ex.ToString());
                return(false);
            }
            finally
            {
                streamDescriptor?.Dispose();
                activate?.Dispose();
                sourceNode?.Dispose();
                outputNode?.Dispose();
            }
        }