RFC6184 RTSP Server with 111212

Topics: Question
Aug 5, 2015 at 3:24 PM
Edited Aug 5, 2015 at 8:21 PM
I am doing some video processing, creating H.264 NALUs, and trying to use the MMA RTSP server to feed a web client.

Using MMA 110851, the following code mostly works:
       RtspServer server = new RtspServer(IPAddress.Any, 554) {
            Logger = new Media.Rtsp.Server.RtspServerConsoleLogger()
        };

        server.MinimumUdpPort = 50001;

        Media.Rtsp.Server.MediaTypes.RFC6184Media media = new Media.Rtsp.Server.MediaTypes.RFC6184Media(1280, 960, "TestStream", null, false);
        media.RtcpDisabled = true; //FIXME

        server.TryAddMedia(media);
        
        server.Start();

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

        Thread.Sleep(5000);

        int fileCounter = 0;

        while (!quitting) {
          try {

            POC.Utils.StopWatch timer = new POC.Utils.StopWatch();
            timer.Go();

            byte[] frameBytes = File.ReadAllBytes(appDir + "\\h264\\test_" + fileCounter + ".h264.264");
            Media.Rtsp.Server.MediaTypes.RFC6184Media.RFC6184Frame frame = new Media.Rtsp.Server.MediaTypes.RFC6184Media.RFC6184Frame(96);
            
            frame.Packetize(frameBytes, 1442);

            media.AddFrame(frame);
            fileCounter++;

            int processingTime = Convert.ToInt32(timer.Stop());
            if (processingTime <= 31) {
              Thread.Sleep(31 - processingTime);
            }

          } catch (Exception ex) {
            fileCounter++;
          }
RTCP wasn't working well, but otherwise the server was doing just fine. Using WireShark, I could see the RTSP OPTIONS, DESCRIBE, SETUP, PLAY flow, and UDP RTP packets flow out as expected. The stream displays on a WebChimera client.

I saw that you addressed some RTCP changes in MMA 111212, so I switched to the new library. Unfortunately, I can't get any RTP packets to flow after the RTSP PLAY using the new library. Is the server to be configured differently in the new release...should the same code above work?

The Visual Studio debug from the failure is below:
8/5/2015 10:25:13 AM MEDIA: Server Started @ 8/5/2015 2:25:13 PM
8/5/2015 10:25:13 AM MEDIA: Starting Stream: TestStream Id=905587f9-8ebd-4ba2-bfd9-63274f2eea7e
The thread 0x2398 has exited with code 259 (0x103).
The thread 0x186c has exited with code 259 (0x103).
8/5/2015 10:25:28 AM MEDIA: RestartFaultedStreams
8/5/2015 10:25:28 AM MEDIA: DisconnectAndRemoveInactiveSessions
The thread 0x20c4 has exited with code 259 (0x103).
8/5/2015 10:25:58 AM MEDIA: RestartFaultedStreams
8/5/2015 10:25:58 AM MEDIA: DisconnectAndRemoveInactiveSessions
The thread 0x1620 has exited with code 259 (0x103).
8/5/2015 10:26:28 AM MEDIA: RestartFaultedStreams
8/5/2015 10:26:28 AM MEDIA: DisconnectAndRemoveInactiveSessions
The thread 0x12f8 has exited with code 259 (0x103).
The thread 0xa8c has exited with code 0 (0x0).
8/5/2015 10:26:54 AM MEDIA: Adding Client: d284edf0-3363-45df-8090-912256f788f7
8/5/2015 10:26:54 AM MEDIA: Accepted Client: d284edf0-3363-45df-8090-912256f788f7 @ 8/5/2015 2:26:54 PM Added =True
8/5/2015 10:26:54 AM MMA: Received PLAY request
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
8/5/2015 10:26:55 AM MEDIA: Media.Rtp.RtpClient-ddaab864-e598-4459-834c-e8f76a183643@SendRecieve - Begin
8/5/2015 10:26:58 AM MEDIA: RestartFaultedStreams
8/5/2015 10:26:58 AM MEDIA: DisconnectAndRemoveInactiveSessions
The thread 0x11e4 has exited with code 259 (0x103).
8/5/2015 10:27:05 AM MEDIA: Adding Client: ade928e7-78f6-407d-98db-34a8440c28ec
8/5/2015 10:27:05 AM MEDIA: Accepted Client: ade928e7-78f6-407d-98db-34a8440c28ec @ 8/5/2015 2:27:05 PM Added =True
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
The thread 0x23dc has exited with code 259 (0x103).
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
8/5/2015 10:27:05 AM MMA: Received PLAY request
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
8/5/2015 10:27:05 AM MEDIA: Marking Client:d284edf0-3363-45df-8090-912256f788f7 Disconnected
8/5/2015 10:27:05 AM MEDIA: Media.Rtp.RtpClient-bdce8b17-2b82-4387-af85-6b15eae5a9d0@SendRecieve - Begin
The thread 0x196c has exited with code 259 (0x103).
A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
The thread 0x1858 has exited with code 259 (0x103).
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
8/5/2015 10:27:16 AM MEDIA: Marking Client:ade928e7-78f6-407d-98db-34a8440c28ec Disconnected
The thread 0x166c has exited with code 259 (0x103).
The thread 0x2100 has exited with code 259 (0x103).
The thread 0x1104 has exited with code 259 (0x103).
8/5/2015 10:27:28 AM MEDIA: RestartFaultedStreams
8/5/2015 10:27:28 AM MEDIA: DisconnectAndRemoveInactiveSessions
The thread 0x2194 has exited with code 259 (0x103).
8/5/2015 10:27:58 AM MEDIA: RestartFaultedStreams
8/5/2015 10:27:58 AM MEDIA: DisconnectAndRemoveInactiveSessions
The thread 0x13f8 has exited with code 259 (0x103).
8/5/2015 10:28:06 AM TdWeb: Trying add frame 0
8/5/2015 10:28:28 AM MEDIA: RestartFaultedStreams
8/5/2015 10:28:28 AM MEDIA: DisconnectAndRemoveInactiveSessions
8/5/2015 10:28:28 AM MEDIA: Disposing Client: d284edf0-3363-45df-8090-912256f788f7 @ 8/5/2015 2:28:28 PM
8/5/2015 10:28:28 AM MEDIA: Disposing Client: ade928e7-78f6-407d-98db-34a8440c28ec @ 8/5/2015 2:28:28 PM
The thread 0x1554 has exited with code 259 (0x103).
8/5/2015 10:28:58 AM MEDIA: RestartFaultedStreams
8/5/2015 10:28:58 AM MEDIA: DisconnectAndRemoveInactiveSessions
The thread 0x1798 has exited with code 259 (0x103).
8/5/2015 10:29:28 AM MEDIA: RestartFaultedStreams
8/5/2015 10:29:28 AM MEDIA: DisconnectAndRemoveInactiveSessions
The thread 0x151c has exited with code 259 (0x103).
I traced the first mscorlib exception to RtspMessage.cs ProcessPlay Enum.TryParse, but it sorta maybe seems like it's intentional?
//If the mediaType was specified
            if (Enum.TryParse(lastSegment, out mediaType))
            {
I looked through the WireShark capture, and the behavior is what you'd expect. The RTSP exchanges look fine, but then after 10 seconds the UDP gives up and retries with TCP. Either way, no RTP packets ever get out from the server.

Lastly, to ensure that my NALU code and/or packetize wasn't the problem, I reverted everything to point to a RTSP server source ala some of the recent examples in the discussions. This RTSP stream is from a Pelco camera. This code below produced the same results as above, where RTSP looks okay but no RTP packets ever come out.
        // RTSP SOURCE TEST
        var server = new Media.Rtsp.RtspServer(System.Net.IPAddress.Any, 554)
        {
            Logger = new Media.Rtsp.Server.RtspServerConsoleLogger()
        };
        var source = new Media.Rtsp.Server.MediaTypes.RtspSource(
                        "TestStream",
                        "rtsp://172.24.13.18/stream1"
        );
        source.RtcpDisabled = true;
        server.TryAddMedia(source);

        server.Start();
Where did I go wrong?
Aug 5, 2015 at 5:23 PM
Edited Aug 5, 2015 at 5:24 PM
In case it's not obvious, the h264 file stuff is a test harness to be replaced by video processing an incoming decoded stream. Kindly ignore the hokey while file loop!
Coordinator
Aug 7, 2015 at 1:58 PM
Why don't you do something intelligent and use a 'Logger' e.g. the FileLogger to determine what exceptions are being thrown.

You can combine that with Wireshark / Network Logs to determine why a particular exception was thrown without stopping the flow of the program.

In short, the Uri scheme is meant to be a rtsp Uri ending with a Sdp MediaType, for example 'rtsp://1.2.3.7/myStream/video'

Maybe your needing it be case insensitive.... Try the following code as a replacement.

if (Enum.TryParse(lastSegment, true, out mediaType))
Marked as answer by juliusfriedman on 8/7/2015 at 6:59 AM
Aug 7, 2015 at 2:45 PM
Thanks. At this point, I think I've proven that all of the exceptions are red herrings.

I did find what is appearing to be a race condition, where this thread in .Start exits because StreamState != Started when it runs.
            //Make the thread
            m_RtpClient.m_WorkerThread = new System.Threading.Thread(SendPackets);
            m_RtpClient.m_WorkerThread.TrySetApartmentState(System.Threading.ApartmentState.MTA);
            //m_RtpClient.m_WorkerThread.IsBackground = true;
            //m_RtpClient.m_WorkerThread.Priority = System.Threading.ThreadPriority.BelowNormal;
            m_RtpClient.m_WorkerThread.Name = "SourceStream-" + Id;
Since the stream that dequeues packets is gone...no RTP packets come out.

This hack at least appears to get me past the 'no RTP in 111212' problem.
            while (State != StreamState.Started)
            {
                System.Threading.Thread.Sleep(clockRate); // FIXME   ; hack to fix stream up?                 
            }

            while (State == StreamState.Started)
            {
                try
                {
Coordinator
Aug 8, 2015 at 12:34 PM
Edited Aug 8, 2015 at 12:44 PM
red her·ring
noun
1.
a dried smoked herring, which is turned red by the smoke.
2.
something, especially a clue, that is or is intended to be misleading or distracting.
"the book is fast-paced, exciting, and full of red herrings"

..............

Your still using the RFC2435 class and you actually have to ask?

This is why I don't give support, you have no idea what your even doing...

How is that acceptable?

Just fix the issue which is the stream starting... the example purposely does such to exhibit such conditions.

One would easily determine such as you have and fix appropriately.

One of the proper fixex in this case is to override the Start Method

Use the code required which is [also contained in the base class SourceMedia]
         `State = StreamState.Started; 

        m_StartedTimeUtc = DateTime.UtcNow;`
Why would you want to waste as many cycle as possible waiting for something to occur anyway? Use a WaitHandle or something...

Lastly, there are a bunch of changes I have coming but at the rate people package up this code and distribute it I should be very well off...

If you care to have any further discussion I need to know What are you using this project for and Why ?
Marked as answer by juliusfriedman on 8/8/2015 at 6:03 AM