RTSP client doesn´t receive frames/packets from IP camera

Topics: Bug Archive
Apr 1, 2016 at 11:31 AM
Hi,
I want to get video from my IP camera to Visual Studio C#. I can connect to my IP camera, but I can´t recieve any packets or frames. RtspClient_OnConnect() and Sender_OnPlay() work fine, but Client_RtpFrameChanged() doesn´t work. Do you have any advice, what can I try? Thank you very much. (My camera works with rtsp protocol and using h264 format)
    RtspClient rtspClient = null;

    public Form1()
    {
        InitializeComponent();
        string url = "rtsp://172.16.140.119:554/h264";
        InitializeVideoFeed(url);
    }

    void InitializeVideoFeed(string url)
    {
        rtspClient = new RtspClient(url);
        rtspClient.OnConnect += rtspClient_OnConnect;
        rtspClient.Connect();
    }

    private void rtspClient_OnConnect(RtspClient sender, object args)
    {
        if (rtspClient.IsConnected)
        {
            try
            {
                if (!sender.IsPlaying)
                {
                    sender.SocketReadTimeout = sender.SocketWriteTimeout = 3000;
                    sender.OnPlay += sender_OnPlay;
                    sender.StartPlaying();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message.ToString());
                return;
            }
        }
    }

    private void sender_OnPlay(RtspClient sender, object args)
    {
        try
        {
            if (sender.IsPlaying)
            {
                sender.Client.FrameChangedEventsEnabled = true;
                sender.Client.RtpFrameChanged += Client_RtpFrameChanged;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }
    }

    private void Client_RtpFrameChanged(object sender, RtpFrame frame, RtpClient.TransportContext tc, bool final = false)
    {
        label1.Text = "Server is running";

        if (frame.IsComplete)
        {
            using (var receivedFrame = new Media.Rtsp.Server.MediaTypes.RFC2435Media.RFC2435Frame(frame))
            {
                var bitmap = (Bitmap)receivedFrame.ToImage();
                pictureEditMain.Image = bitmap;
                pictureEditMain.Refresh();
            }
        }
    }
Coordinator
Apr 1, 2016 at 12:31 PM
Could be network related, considering trying Tcp rather than Udp.

If you could include a Wireshark capture it would also be helpful.

Let me know and thanks!
Marked as answer by juliusfriedman on 4/1/2016 at 5:31 AM
Apr 1, 2016 at 1:02 PM
Edited Apr 1, 2016 at 1:35 PM
Thank you for helping me.

This is my wireshark. I hope that you think this, because its my first time with this program. http://leteckaposta.cz/536711703 or i have also this http://leteckaposta.cz/660389150

Now I am trying TCP and UDP and it seems doesn´t work.
Coordinator
Apr 2, 2016 at 2:39 PM
No problem, I'll take a look at these on Monday or Tuesday and let you know what I find.
Marked as answer by juliusfriedman on 4/2/2016 at 7:39 AM
Coordinator
Apr 4, 2016 at 12:54 PM
I really need the Wireshark logs, the screen shot isn't going to help.

It's possible the client is sending on a different port than specified in the SETUP, it's hard to say without seeing the entire conversation.

Let me know how I can help you further.
Marked as answer by juliusfriedman on 4/4/2016 at 5:54 AM
Apr 4, 2016 at 9:01 PM
Thank you for help. On Wendsday i will be at school and will send you wireshark capture. And before it I verify port.
Coordinator
Apr 4, 2016 at 9:58 PM
No problem, doesn't have to be a large capture and remember you can filter anything you don't want me to see before you export the capture (if you need to).

Take care!
Marked as answer by juliusfriedman on 4/4/2016 at 2:58 PM
Apr 6, 2016 at 8:08 AM
Edited Apr 6, 2016 at 1:02 PM
Here is my wireshark capture http://leteckaposta.cz/978972550 and this http://leteckaposta.cz/197462952 and for rtcp here http://leteckaposta.cz/368408421 and ICMP here http://leteckaposta.cz/976334896 . I made breakpoints and found, that some packets are coming to the event Client_RtpFrameChanged().
But no text "Server is running" show. In breakpoint of label1 is Text: The function evaluation requieres all threads to run. Do you have any idea why I am inside of this event, but nothing in this method doesn´t work?

(It´s Basler IP camera which uses h264 format and rtsp client. LocalRtcp/LocalRtp: 172.16.140.120:18267, RemoteRtcp/RemoteRtp: 172.16.140.119:554, TCP source port: 554, TCP destination port: 17576.)

Thank you!
Coordinator
Apr 6, 2016 at 1:03 PM
Edited Apr 6, 2016 at 1:04 PM
I haven't looked yet but it seems to be (via your description) that packets are coming but frame change events are not firing? There is either a ssrc issue or another issue such as incorrect payload type, I'll check the capture and see if I can find anything.

It also seems your using the rfc2435 frametype, if you need h264 support you should be using the rfc6184 frametype.

I'll check the capture in a bit and let you know if anything else needs adjustment.
Marked as answer by juliusfriedman on 4/6/2016 at 6:03 AM
Apr 6, 2016 at 1:17 PM
If I have breakpoint in event Client_RtpFrameChanged(), it stopped there, so it means, that event is working. But now, I have in this event only this: label1.Text = "Server is running"; just for testing that everything works fine, but this text doesn´t show in label1 and there is written: The function evaluation requieres all threads to run.. I totally don´t understand this mistake.
Coordinator
Apr 7, 2016 at 6:18 PM
Sorry for the delay, I have been busy with a few other things. I will take a look and when I do I will update this thread.
Marked as answer by juliusfriedman on 4/7/2016 at 11:18 AM
Coordinator
Apr 7, 2016 at 6:29 PM
Edited Apr 7, 2016 at 6:30 PM
I can't use a simply screen shot to do all the investigating I need for this, if you really need help export the entire capture and email it to me or post it somewhere so I can access it.

If your behind NAT you may want to consider allowing nat traversal or setting reuse ports to see if that helps.

I am interested in helping you find out what wrong but I need more than what your giving me to help effectively!
Marked as answer by juliusfriedman on 4/7/2016 at 11:29 AM
Apr 13, 2016 at 10:35 AM
Edited Apr 13, 2016 at 10:36 AM
Thank you for your advices. Now I am receiving packets and can save video from IP camera to my computer by using this code:
    private void Client_RtpFrameChanged(object sender, RtpFrame frame , RtpClient.TransportContext tc = null, bool final = false)
    {
        if (!frame.IsComplete) return;
        RFC6184Media.RFC6184Frame hframe = new RFC6184Media.RFC6184Frame(frame);

        if (bFirst == true)
        {
            hframe.Depacketize();
            byte[] result = hframe.Buffer.ToArray();
            using (var stream = new System.IO.MemoryStream(result.Length))
            {
                hframe.Buffer.CopyTo(stream);
                stream.Position = 0;
                decode_stream(stream);
                bFirst = false;
            }
        }
        hframe.Depacketize();
        decode_stream(hframe.Buffer);
    }
//--------------------------------------------------------------------------------
    public void decode_stream(System.IO.Stream fin)
    {
        using (var fs = new FileStream(string.Format("Data/t.h264"), FileMode.Append)) fin.CopyTo(fs);
        //Bitmap bitmap = new Bitmap(fin);
        //bitmap.Save(string.Format("Data/image.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
    }
But problem is, when I want to save image (jpeg) instead of video (h264), it doesn´t working. Do you have some advice how can I do it? In disccussion I read something about FrameDecoder, but I can´t create it. Thank you! :-)
Coordinator
Apr 13, 2016 at 12:23 PM
Glad you got it working, what was the difficulties before if you don't mind me asking, was it network related?

Decoding h264 isn't provided by this library yet, especially for display in realtime.

I have a bit more I need to do in the way of what I already offer before I start to implement more codecs or decoding.

What OS are you trying to run your application on?

Most already include an h.264 decoder.. what's the goal of your application in the end, just to display the video?

If I can help you in anyway I definitely will.
Marked as answer by juliusfriedman on 4/13/2016 at 5:23 AM