Exemplo n.º 1
0
        /// <summary>
        /// Sends the Rtcp Goodbye and detaches all sources
        /// </summary>
        public override void Dispose()
        {
            if (IsDisposed) return;

            base.Dispose();

            RemoveAllAttachmentsAndClearPlaying();

            //Mark as disconnected
            IsDisconnected = true;

            //Disconnect the RtpClient so it's not hanging around wasting resources for nothing
            if (m_RtpClient != null)
            {
                try
                {
                    m_RtpClient.InterleavedData -= m_RtpClient_InterleavedData;

                    m_RtpClient.Dispose();

                    m_RtpClient = null;
                }
                catch { }
            }

            if (m_Buffer != null)
            {
                try
                {
                    m_Buffer.Dispose();

                    m_Buffer = null;
                }
                catch { }
            }

            if (m_RtspSocket != null)
            {
                try
                {
                    if (false == LeaveOpen) m_RtspSocket.Dispose();

                    m_RtspSocket = null;
                }
                catch { }
            }

            if (LastRequest != null)
            {
                try
                {
                    LastRequest.Dispose();

                    LastRequest = null;
                }
                catch { }
            }

            if (LastResponse != null)
            {
                try
                {
                    LastResponse.Dispose();

                    LastResponse = null;
                }
                catch { }
            }

            m_Server = m_Contained = null;
        }
Exemplo n.º 2
0
        static void TestRtspServer()
        {
            string ipAddress = GetLocalIPAddress();

            try
            {
                using (Media.Rtsp.RtspServer server = new Media.Rtsp.RtspServer(IPAddress.Parse(ipAddress), 554)
                {
                    Logger = new Media.Rtsp.Server.RtspServerConsoleLogger(),
                    ClientSessionLogger = new Media.Rtsp.Server.RtspServerDebugLogger()
                })
                {
                    RFC2435Media sourceScreenCapture = new RFC2435Media("screenCapture", null, false, 1920, 1080, false);

                    server.TryAddMedia(sourceScreenCapture);

                    Thread captureThread = new Thread(new ParameterizedThreadStart((o) =>
                    {
                        Start:

                        using (var bmpScreenShot = new System.Drawing.Bitmap(
                                   Screen.PrimaryScreen.Bounds.Width,
                                   Screen.PrimaryScreen.Bounds.Height,
                                   System.Drawing.Imaging.PixelFormat.Format32bppArgb))
                        {
                            using (var gfxScreenShot = Graphics.FromImage(bmpScreenShot))
                            {
                                while (server.IsRunning)
                                {
                                    try
                                    {
                                        // Take the screenshot from the upper left corner to the right bottom corner.
                                        gfxScreenShot.CopyFromScreen(
                                            Screen.PrimaryScreen.Bounds.X,
                                            Screen.PrimaryScreen.Bounds.Y,
                                            0, 0,
                                            Screen.PrimaryScreen.Bounds.Size,
                                            System.Drawing.CopyPixelOperation.SourceCopy);

                                        sourceScreenCapture.Packetize(bmpScreenShot);

                                        if (!Thread.Yield())
                                        {
                                            Thread.Sleep(9);
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        server.Logger.LogException(ex);

                                        gfxScreenShot.Dispose();

                                        bmpScreenShot.Dispose();

                                        if (server != null && server.IsRunning)
                                        {
                                            goto Start;
                                        }
                                    }
                                }

                                int exit = 1;

                                if (exit == 1)
                                {
                                    return;
                                }
                            }
                        }
                    }));

                    server.Start();

                    while (!server.IsRunning)
                    {
                        Thread.Sleep(0);
                    }

                    if (false.Equals(object.ReferenceEquals(captureThread, null)))
                    {
                        //captureThread.Priority = System.Threading.ThreadPriority.BelowNormal;

                        captureThread.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 3
0
        public ClientSession(RtspServer server, Socket rtspSocket, Common.MemorySegment buffer = null)
        {
            Id = Guid.NewGuid();

            //The RtspSession ID should be set here to prevent this session from accessing another session,
            //The only problem with this is that is how the nature of TCP may work also... e.g. the client may open and close connections at will in between requests.
            //This means any TCP connection can technially just as in UDP access another session so long as the SessionID is known.
            //Agents will attempt to check the EndPoint however if the packet was forged [and successfully transmitted] then the session is obtained through that mechanism...
            //TCP provides a `stronger` protection against this type of attack (forging) by default where as UDP does not and most large entities are their own provider and thus...

            m_Server = server;

            //Assign the socket and remote endPoint, IPPacketInformation provides thus for UDP
            RtspSocket = rtspSocket;

            if (m_RtspSocket == null) return;

            //m_RtspSocket.Blocking = false;

            //m_RtspSocket.ExclusiveAddressUse = true;

            //Configure TCP Sockets
            if (m_RtspSocket.ProtocolType == ProtocolType.Tcp)
            {
                m_RtspSocket.NoDelay = true;

                m_RtspSocket.SendBufferSize = 0;

                m_RtspSocket.ReceiveBufferSize = 0;

                Media.Common.Extensions.Socket.SocketExtensions.DisableLinger(m_RtspSocket);

                //Use expedited data as defined in RFC-1222. This option can be set only once; after it is set, it cannot be turned off.
                m_RtspSocket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.Expedited, true);
            }

            if (m_RtspSocket.AddressFamily == AddressFamily.InterNetwork) m_RtspSocket.DontFragment = true;

            //m_RtspSocket.SendTimeout = m_RtspSocket.ReceiveTimeout = (int)(m_Server.RtspClientInactivityTimeout.TotalMilliseconds / 3);

            m_RtspSocket.SendTimeout = m_RtspSocket.ReceiveTimeout = (int)RtspClient.DefaultConnectionTime.TotalMilliseconds;

            //Create a buffer using the size of the largest message possible without a Content-Length header.
            //This helps to ensure that partial messages are not recieved by the server from a client if possible (should eventually allow much smaller)
            if (buffer == null)
                m_Buffer = new Common.MemorySegment(RtspMessage.MaximumLength);
            else
                m_Buffer = buffer;

            //Start receiving data
            StartReceive();
        }