예제 #1
0
        public void GetRtspMessageRequest(string requestLine, RtspRequest.RequestType requestType)
        {
            RtspMessage oneMessage = RtspMessage.GetRtspMessage(requestLine);

            Assert.IsInstanceOfType(typeof(RtspRequest), oneMessage);

            RtspRequest oneRequest = oneMessage as RtspRequest;

            Assert.AreEqual(requestType, oneRequest.RequestTyped);
        }
예제 #2
0
        public void GetRtspMessageResponse(string requestLine, int returnCode, string returnMessage)
        {
            RtspMessage oneMessage = RtspMessage.GetRtspMessage(requestLine);

            Assert.IsInstanceOfType(typeof(RtspResponse), oneMessage);

            RtspResponse oneResponse = oneMessage as RtspResponse;

            Assert.AreEqual(returnCode, oneResponse.ReturnCode);
            Assert.AreEqual(returnMessage, oneResponse.ReturnMessage);
        }
예제 #3
0
        /// <summary>
        /// Reads one message.
        /// </summary>
        /// <param name="commandStream">The Rtsp stream.</param>
        /// <returns>Message readen</returns>
        public RtspChunk ReadOneMessage(Stream commandStream)
        {
            if (commandStream == null)
            {
                throw new ArgumentNullException("commandStream");
            }
            Contract.EndContractBlock();

            ReadingState currentReadingState = ReadingState.NewCommand;
            // current decode message , create a fake new to permit compile.
            RtspChunk currentMessage = null;

            int         size       = 0;
            int         byteReaden = 0;
            List <byte> buffer     = new List <byte>(256);
            string      oneLine    = String.Empty;

            while (currentReadingState != ReadingState.End)
            {
                // if the system is not reading binary data.
                if (currentReadingState != ReadingState.Data && currentReadingState != ReadingState.MoreInterleavedData)
                {
                    oneLine = String.Empty;
                    bool needMoreChar = true;
                    // I do not know to make readline blocking
                    while (needMoreChar)
                    {
                        int currentByte = commandStream.ReadByte();

                        switch (currentByte)
                        {
                        case -1:
                            // the read is blocking, so if we got -1 it is because the client close;
                            currentReadingState = ReadingState.End;
                            needMoreChar        = false;
                            break;

                        case '\n':
                            oneLine = ASCIIEncoding.UTF8.GetString(buffer.ToArray());
                            buffer.Clear();
                            needMoreChar = false;
                            break;

                        case '\r':
                            // simply ignore this
                            break;

                        case '$':     // if first caracter of packet is $ it is an interleaved data packet
                            if (currentReadingState == ReadingState.NewCommand && buffer.Count == 0)
                            {
                                currentReadingState = ReadingState.InterleavedData;
                                needMoreChar        = false;
                            }
                            else
                            {
                                goto default;
                            }
                            break;

                        default:
                            buffer.Add((byte)currentByte);
                            break;
                        }
                    }
                }

                switch (currentReadingState)
                {
                case ReadingState.NewCommand:
                    currentMessage      = RtspMessage.GetRtspMessage(oneLine);
                    currentReadingState = ReadingState.Headers;
                    break;

                case ReadingState.Headers:
                    string line = oneLine;
                    if (string.IsNullOrEmpty(line))
                    {
                        currentReadingState = ReadingState.Data;
                        ((RtspMessage)currentMessage).InitialiseDataFromContentLength();
                    }
                    else
                    {
                        ((RtspMessage)currentMessage).AddHeader(line);
                    }
                    break;

                case ReadingState.Data:
                    if (currentMessage.Data.Length > 0)
                    {
                        // Read the remaning data
                        byteReaden += commandStream.Read(currentMessage.Data, byteReaden,
                                                         currentMessage.Data.Length - byteReaden);
                        //_logger.Debug(CultureInfo.InvariantCulture, "Readen {0} byte of data", byteReaden);
                    }
                    // if we haven't read all go there again else go to end.
                    if (byteReaden >= currentMessage.Data.Length)
                    {
                        currentReadingState = ReadingState.End;
                    }
                    break;

                case ReadingState.InterleavedData:
                    currentMessage = new RtspData();
                    ((RtspData)currentMessage).Channel = commandStream.ReadByte();
                    size = (commandStream.ReadByte() << 8) + commandStream.ReadByte();
                    currentMessage.Data = new byte[size];
                    currentReadingState = ReadingState.MoreInterleavedData;
                    break;

                case ReadingState.MoreInterleavedData:
                    // apparently non blocking
                    byteReaden += commandStream.Read(currentMessage.Data, byteReaden, size - byteReaden);
                    if (byteReaden < size)
                    {
                        currentReadingState = ReadingState.MoreInterleavedData;
                    }
                    else
                    {
                        currentReadingState = ReadingState.End;
                    }
                    break;

                default:
                    break;
                }
            }
            if (currentMessage != null)
            {
                currentMessage.SourcePort = this;
            }
            return(currentMessage);
        }