Esempio n. 1
0
        /// <summary>
        /// Creates the RTP source.
        /// </summary>
        /// <param name="fileSink">The file sink.</param>
        /// <param name="serverAddress">The server address.</param>
        /// <param name="serverRtpPort">The server RTP port.</param>
        /// <param name="serverRtcpPort">The server RTCP port.</param>
        /// <param name="clientRtpPort">The client RTP port.</param>
        /// <param name="clientRtcpPort">The client RTCP port.</param>
        /// <returns>Succeeded or failed.</returns>
        public bool CreateRtpSource(FileSink fileSink, string serverAddress,
                                    int serverRtpPort, int serverRtcpPort,
                                    int clientRtpPort, int clientRtcpPort)
        {
            // Creates rtp socket and rtcp socket:
            ClientSocketBase clientRtpSocket  = null;
            ClientSocketBase clientRtcpSocket = null;
            Socket           socket           = null;

            socket = Utils.CreateUdpSocket(clientRtpPort);
            if (socket == null)
            {
                return(false);
            }

            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(serverAddress), serverRtpPort);

            clientRtpSocket = new ClientSocketUdp(socket, (EndPoint)iep);

            socket = Utils.CreateUdpSocket(clientRtcpPort);
            if (socket != null)
            {
                iep = new IPEndPoint(IPAddress.Parse(serverAddress), serverRtcpPort);
                clientRtcpSocket = new ClientSocketUdp(socket, (EndPoint)iep);
            }

            Source = new RtpSource(fileSink, clientRtpSocket);
            if (Source == null)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the output file.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <param name="fileSize">Size of the file.</param>
        /// <returns>The FileSink object.</returns>
        private FileSink CreateOutputFile(string filePath, long fileSize)
        {
            FileSink fileSink = null;

            fileSink = new FileSink(filePath, fileSize);
            fileSink.UpdateAccessRange += new EventHandler <TFilePermissionEventArgs>(this.OnUpdateAccessRange);

            return(fileSink);
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RtpSource"/> class.
        /// </summary>
        /// <param name="fileSink">The file sink.</param>
        /// <param name="clientRtpSocket">The client RTP socket.</param>
        public RtpSource(FileSink fileSink, ClientSocketBase clientRtpSocket)
        {
            this.fileSink        = fileSink;
            this.clientRtpSocket = clientRtpSocket;

            this.clientRtpSocket.DatagramReceived += new EventHandler <TSocketEventArgs>(this.OnDatagramReceived);

            rtpPacket = new RtpPacket();

            preferredFrameSize = 1024;
            internalPacketSize = preferredFrameSize + 16;
        }
Esempio n. 4
0
        /// <summary>
        /// Opens the stream.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="filePath">The file path.</param>
        /// <returns>Succeeded or failed.</returns>
        public bool OpenStream(string url, string filePath)
        {
            if (socket == null || !socket.Connected)
            {
                return(false);
            }

            // Sets the request url:
            requestUrl = url;

            // Sends "OPTIONS" command and then gets the response:
            bool result = SendOptionsCmd();

            if (!result)
            {
                CloseStream();
                return(false);
            }

            // Sends "DESCRIBE" command and then gets the SDP description:
            string sdpDescription = SendDescribeCmd();

            if (string.IsNullOrEmpty(sdpDescription))
            {
                Utils.OutputMessage(false, MsgLevel.Error, "RtspClient -- OpenStream", "Sdp description is null or empty.");
                CloseStream();
                return(false);
            }

            // Creates a media session object from the SDP description which
            // we have just received from the server:
            mediaSession = new MediaSession(sdpDescription);

            // Then, resolves the SDP description and initializes all basic
            // information:
            result = mediaSession.ResolveSdpDescription();
            if (!result)
            {
                Utils.OutputMessage(false, MsgLevel.Error, "RtspClient -- OpenStream", "Resolve sdp description failed.");
                CloseStream();
                return(false);
            }

            long fileSize = mediaSession.FileSize;

            Duration = mediaSession.PlayEndTime - mediaSession.PlayStartTime;

            // And then, creates the output file to write the data:
            FileSink fileSink = CreateOutputFile(filePath, fileSize);

            if (fileSink == null)
            {
                Utils.OutputMessage(false, MsgLevel.Error, "RtspClient -- OpenStream", "Create output file failed.");
                CloseStream();
                return(false);
            }

            // After that, sends the "SETUP" command and setups the stream:
            result = SendSetupCmd();
            if (!result)
            {
                Utils.OutputMessage(false, MsgLevel.Error, "RtspClient -- OpenStream", "Send setup command failed.");
                CloseStream();
                return(false);
            }

            result = mediaSession.CreateRtpSource(fileSink, serverAddress,
                                                  serverRtpPort, serverRtcpPort, clientRtpPort, clientRtcpPort);
            if (!result)
            {
                Utils.OutputMessage(false, MsgLevel.Error, "RtspClient -- OpenStream", "Create the rtp source failed.");
                CloseStream();
                return(false);
            }

            // Start the receiving work in asynchronous mode:
            mediaSession.Source.StartPlaying();

            // Finally, sends the "PLAY" command and starts playing stream:
            result = PlayStream();
            if (!result)
            {
                Utils.OutputMessage(false, MsgLevel.Error, "RtspClient -- OpenStream", "Play the stream failed.");
                CloseStream();
                return(false);
            }

            return(true);
        }