Stream Hikvision DVR

Topics: Question
Jan 27, 2015 at 5:11 PM

First of all this is great. I could start the stream server and could consume it with VLC client.

I am trying to add a Hikvision DVR camera as source of the stream. My DVR is working on port 554.

The example url in test project seems to be incorrect. I have corrected it though. But even after correcting it to -
"rtsp://xxxx:xxx@x.x.x.x/PSIA/Streaming/channels/101", I am unable to see from VLC. It is giving following error -

Your input can't be opened:
VLC is unable to open the MRL 'rtsp://x.x.x.x/live/Hikvision1'. Check the log for details.

Is it because both hikvision and this server is on port 554??

Have you tested it with Hikvision? Can someone help me in resolving it?

Thanks in Advance..
Jan 27, 2015 at 10:08 PM
Sorry I'm a bit confused.

It works (the server?) and you can consume the hosted stream through vlc. (Quick time and others should also work).

As you can see any compliant source should work.

When you say the encoder is running on port 554, that should only matter if its using the same ip as the application.

I would be most willing to assist you but I need to have an exception and stack trace or a more specific overview of whatever is trying to be achieved to ensure that I give you helpful and correct information.

In short make sure another service is not already using port 554 before attempting to start the server on the same port.

Soon the rtsp server will support multiple endpoints and allow for http tunneled support as well but this doesn't seem related to this feature / question, so just in case I missed something it would be good to have the right information and set the correct expectations as well.
Marked as answer by juliusfriedman on 1/27/2015 at 3:08 PM
Feb 4, 2015 at 5:39 PM
Thanks for the quick response. I am trying to stream live video from Hikvision DVR/Camera. I have added the rtsp source of my cam as per the examples in test project. When I ran the test project there is no exception. But when I tried to view the stream using VLC media player ( My client) as per examples, VLC gives error that MRL is not correct.

I could access another example url from Test with the name "Omega" in test project. It was stream and was accessible through VLC.

But I am unable to do the same for Hikvision. Did you every tried to stream live camera feed and access through any client like vlc?

Need help here!!!
Feb 4, 2015 at 6:00 PM
Yes although there may be issues if there is more than one audio or video item in the session description being aggregated.

Can you include a Wireshark Capture so I can see what is happening?

Feb 4, 2015 at 7:19 PM

After debugging the code, I found that the credentials were not set properly. After adding below streaming started-
sourceHik.RtspClient.AuthenticationScheme = System.Net.AuthenticationSchemes.Basic;

But now when I am trying to access the stream using VLC, it is showing that something is being stremed, but video is not displayed.

Can you guide me how can I stream and record at the same time? I think there is method to record. But I am not sure how to implement? Can you please share a test method to enable streaming and recording?

Thanks in Advance.
Feb 4, 2015 at 11:50 PM
When you say not set properly do you mean in my code or in your code which setup the stream?

Can you include a Wireshark capture?

Feb 7, 2015 at 4:25 PM

I played around with the ode and looked at some other thread:

But when I ran the code I am getting exception like-
"No Context for Channel xxxx"
and "InCompatible Packet xxxx"

I am using live stream from a hikvision camera. Below is my complete code:

System.Net.NetworkCredential cred = new System.Net.NetworkCredential("admin", "12345");
        Media.Rtsp.RtspClient client = new Media.Rtsp.RtspClient("rtsp://x.x.x.x/Streaming/channels/101", RtspClient.ClientProtocolType.Tcp);
        client.AuthenticationScheme = System.Net.AuthenticationSchemes.Basic;
        client.Credential = cred;
        client.OnPlay += (senders, args) =>
            //There is a single intentional duality in the design of the pattern utilized for the RtpClient such that                    
            //client.Client.MaximumRtcpBandwidthPercentage = 25;
            ///It SHOULD also subsequently limit the maximum amount of CPU the client will be able to use

            if (args != null)
                Console.WriteLine("\t*****************Playing `" + args.ToString() + "`");


            //If there is no sdp we have not attached events yet
            if (false == System.IO.File.Exists("current.sdp"))
                //Write the sdp that we are playing
                System.IO.File.WriteAllText("current.sdp", client.SessionDescription.ToString());

            //Attach a logger
            client.Client.Logger = new Media.Common.Loggers.ConsoleLogger();

            //Indicate if LivePlay
            if (client.LivePlay)
                Console.WriteLine("\t*****************Playing from Live Source");
                //Indicate if StartTime is found
                if (client.StartTime.HasValue)
                    Console.WriteLine("\t*****************Media Start Time:" + client.StartTime);


                //Indicate if EndTime is found
                if (client.EndTime.HasValue)
                    Console.WriteLine("\t*****************Media End Time:" + client.EndTime);

            //Show context information
            foreach (Media.Rtp.RtpClient.TransportContext tc in client.Client.GetTransportContexts())
                Console.WriteLine("\t*****************Local Id " + tc.SynchronizationSourceIdentifier + "\t*****************Remote Id " + tc.RemoteSynchronizationSourceIdentifier);


        ///The client has a Client Property which is used to access the RtpClient
        //Performs the Options, Describe, Setup and Play Request

        //Attach events at the frame level
        client.Client.RtpFrameChanged +=
          new Media.Rtp.RtpClient.RtpFrameHandler(Client_RtpFrameChanged);

        //Do something else 
        while (true)

        //Send the Teardown and Goodbye

public bool playStream(Stream fin)
        if (frameDecoder == null)
            frameDecoder = new FrameDecoder(fin);


            //if (bShowCamera)

            var picture = frameDecoder.DecodeFrame();

            var Width = picture.imageWidthWOEdge;
            var Height = picture.imageHeightWOEdge;

            Bitmap bmp = new Bitmap(FrameUtils.imageFromFrameWithoutEdges(picture, Width, Height));
            pictureBox1.Image = bmp;
            //this.Image = bmp;

            return (true);
        catch (EndOfStreamException)
            return false;
void Client_RtpFrameChanged(object sender, Media.Rtp.RtpFrame frame)
        bool bFirst = true;
        Media.Rtp.RtpClient Client = sender as Media.Rtp.RtpClient;
        if (frame.IsComplete)

                var context = ((Media.Rtp.RtpClient)sender).GetContextByPayloadType(frame.PayloadTypeByte);

                if (context == null || context.MediaDescription.MediaType != return;

                using (Media.Rtsp.Server.MediaTypes.RFC6184Media.RFC6184Frame hframe = new Media.Rtsp.Server.MediaTypes.RFC6184Media.RFC6184Frame(frame))
                    if (frameDecoder == null)
                        Media.Sdp.SessionDescriptionLine fmtp = context.MediaDescription.FmtpLine;

                        byte[] sps = null, pps = null;

                        foreach (string p in fmtp.Parts)
                            string trim = p.Trim();
                            if (trim.StartsWith("sprop-parameter-sets=", StringComparison.InvariantCultureIgnoreCase))
                                string[] data = trim.Replace("sprop-parameter-sets=", string.Empty).Split(',');
                                sps = System.Convert.FromBase64String(data[0]);
                                pps = System.Convert.FromBase64String(data[1]);

                        bool hasSps, hasPps, sei, slice, idr;

                        hasSps = hframe.ContainsSPS;
                        hasPps = hframe.ContainsPPS;
                        sei = hframe.ContainsSEI;
                        slice = hframe.ContainsSlice;
                        idr = hframe.ContainsIDR;

                        using (var stream = new System.IO.MemoryStream())
                            if (!hasSps && sps != null)
                                stream.Write(new byte[] { 0x00, 0x00, 0x00, 0x01 }, 0, 4);

                                stream.Write(sps, 0, sps.Length);

                            if (!hasPps && pps != null)
                                stream.Write(new byte[] { 0x00, 0x00, 0x00, 0x01 }, 0, 4);

                                stream.Write(pps, 0, pps.Length);

                            stream.Position = 0;

                            bFirst = false;
                    else hframe.Depacketize();
public void decode_stream(Stream fin)
        using (var fs = new FileStream(string.Format("D:\\temp\\test2.h264"), FileMode.Append))

I am using both decode_stream and playstrem methods for testing purpose.

Can you please help me in sorting out the issue. I see on other thread that people were able to make it working. Seems I am missing something..
Feb 7, 2015 at 4:34 PM
I am confused.

Those aren't exceptions I throw.

Any stream can have an unknown for the first packet received if the server didn't indicate the ssrc in the setup or play response.

What exactly is your question?

Where is the wireshark capture?

It's hard to diagnose without more info and a complete sample of the code in use.

Im fairly sure there are no bugs with my software which cause this.

Make sure you have the latest firmware for the camera.

If you can provide additional information as I requested I can try to look at this again.
Marked as answer by juliusfriedman on 2/7/2015 at 9:34 AM
Feb 7, 2015 at 5:32 PM
I searched and found that those exceptions are in RtpClient class.

I have added the above code snippet on your test project and ran the test. There is no other code. I am not saying that there is any bug in your cde. Since others were able to ran it successfully, I assume it is perfect.
What I am trying to say that I am probably making some mistake.

Can you provide me a working sample for showing live stream from hikvision DVR?

I am going to install wireshark and will share the ourput.
Feb 7, 2015 at 5:52 PM
One more point I am using cscodec and DecodeFrame() method is always throwing EndOfStreamException exception.
Feb 7, 2015 at 5:59 PM
I asked you for a capture because I was trying to ascertain if the data was possibly causing as issue with the encoder.

There are several issues with how HikVision handles requests and responses but not in this library.

That is why I needed the capture.

Where are those exceptions in the class? Post a line number..

There are Logging points not exceptions AFAIK so please provide a stack trace if otherwise.

There are several examples, some are Hikvision some are others what more do you need besides those examples?

The code in the other thread is for csCodec but I do not support it.

If you read the other thread closely you will see exactly how to resolve the issue with EOS by adding the SetStream method I indicated, if you don't post your code I have no way of knowing how to diagnose it.

Besides that, csCodec is not my project and I only indicated how it COULD be modified for easier use. (That information is in the other thread)

If you need anything else please let me know!
Marked as answer by juliusfriedman on 2/7/2015 at 10:59 AM