Click here to Skip to main content
15,885,868 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am working on capture images by calling RTSP service.

For calling RTSP service I have created one sample application (as a client) and in that I have created socket object and call RTSP service listen method.

I am also able to receive byte[] using UDP listener, but I am not able to use this byte[] value neither for create image nor for create video and it throwing error like "invalid parameter". So, How I can create image/video using this byte[] ?

For reference here I have attached image and in that you can see received byte[]
2017-07-11_1531[^]

What I have tried:

--Code for calling RTSP service -----
C#
IPEndPoint ip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 322);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
server.Connect(ip);
var msg ="PLAY rtsp://{cameraIP}/axis-media/media.amp?resolution=352x240&compression=80 RTSP/1.0\r\nRange: npt=0-\r\nCSeq: 5\r\nUser-Agent: Lavf57.62.100\r\nSession: 100\r\n\r\n")";
server.Send(msg);

--Code for receive UDP packets sent by RTSP service -----
C#
var myEndPoint = (EndPoint)endPoint;
var udpListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
udpListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
udpListener.Bind(myEndPoint);

var buffer = new byte[65536];
var size = udpListener.ReceiveFrom(buffer, ref myEndPoint);
Array.Resize(ref buffer, size);

//code for save image

MemoryStream ms = new MemoryStream(buffer);
var image = Image.FromStream(ms);
var path = string.Format(@"d:\MyImage\camImage-temp.jpeg");
image.Save(path);
Posted
Updated 11-Jul-17 0:30am
v2

1 solution

Why are you sending TCP and receiving UDP packets??? Use TCP alsp on the receiver side.

You should use the Image.Save() with a parameter to enforce jpeg.
C#
image.Save(path, ImageFormat.Jpeg);

Else check for other problems, like the transmitted data and its size.
 
Share this answer
 
Comments
Member 13117475 11-Jul-17 6:42am    
1) My RTSP service sending packets using UDP protocol since I have used UDP and if I am using TCP protocol then application throwing error like "forcely closed by remote endpoint"

2) Data length i.e. I am receiving is not static but maximum byte[] size I receiving is 95.

-- sample code written for sending packets --

ms.WriteByte(0x80); // RTCP header
ms.WriteByte(0xc8); // packet type = 200 = sender report
ms.WriteByte(0);
ms.WriteByte(6); // length in words - 1
ms.Write(ssrc, 0, 4); // SSRC of sender
ms.Write(zero, 0, 4); // NTP timestamp, whole seconds
ms.Write(zero, 0, 4); // NTP timestamp, microseconds
ms.Write(rtp_ts, 0, 4); // RTP timestamp
ms.Write(zero, 0, 4); // sender's packet count
ms.Write(zero, 0, 4); // sender's octet count
int len = 6 + cname.Length + 1;
ms.WriteByte(0x80); // RTCP header
ms.WriteByte(0xca); // packet type = 202 = sdes
ms.WriteByte(0);
ms.WriteByte((byte)((len + 3) / 4)); // length in words - 1
ms.Write(ssrc, 0, 4); // SSRC of sender
ms.WriteByte(1);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900