简体   繁体   English

通过 TCP 使用 RTSP 抓取视频

[英]Grabbing video with RTSP over TCP

I want to retrieve some video data from a device with RTSP.我想从带有 RTSP 的设备中检索一些视频数据。

RTSP over UDP works well, but I need it over TCP. RTSP over UDP 运行良好,但我需要它 over TCP。

After issuing the RTSP commands, I receive RTPs but I do not how to handle them here.发出 RTSP 命令后,我收到 RTP,但我不知道如何处理它们。 The payload is as follows: $[channel - 1 byte][length - 2bytes][data]有效载荷如下:$[channel - 1 byte][length - 2bytes][data]

The thing is that I receive such packets and sometimes further packets where channel is 10 or 99 etc.问题是我收到了这样的数据包,有时还会收到更多的数据包,其中通道为 10 或 99 等。

So could anyone please provide some input on how to handle the payload ?那么任何人都可以提供一些关于如何处理有效载荷的意见吗?

You have it all in RFC 2326 "Real Time Streaming Protocol (RTSP)" .您在RFC 2326“实时流协议 (RTSP)”中拥有这一切。 You need "10.12 Embedded (Interleaved) Binary Data":您需要“10.12 嵌入式(交错)二进制数据”:

Stream data such as RTP packets is encapsulated by an ASCII dollar sign (24 hexadecimal), followed by a one-byte channel identifier, followed by the length of the encapsulated binary data as a binary, two-byte integer in network byte order. RTP 数据包等流数据由 ASCII 美元符号(24 位十六进制)封装,后跟一字节通道标识符,后跟封装的二进制数据的长度,以网络字节顺序为二进制、两字节整数。 The stream data follows immediately afterwards, without a CRLF, but including the upper-layer protocol headers.流数据紧随其后,没有 CRLF,但包括上层协议头。 Each $ block contains exactly one upper-layer protocol data unit, eg, one RTP packet.每个$块正好包含一个上层协议数据单元,例如一个RTP包。

There is a small example there as well:还有一个小例子:

 S->C: $\000{2 byte length}{"length" bytes data, w/RTP header}
 S->C: $\000{2 byte length}{"length" bytes data, w/RTP header}
 S->C: $\001{2 byte length}{"length" bytes  RTCP packet}

Getting PPS is IMO straightforward and does not really require parseing the packet.获取 PPS 是 IMO 直接的并且不需要解析数据包。

Your request for SPS , im guessing , will require getting into the packet ( i dont think you need to worry about WS msg 'invalid packet'.我猜您对 SPS 的请求将需要进入数据包(我认为您无需担心 WS msg“无效数据包”。

What about using Type at PT at 0x09 ?在 PT at 0x09 处使用 Type 怎么样?

see here for packet description请参阅此处了解数据包说明

sample implementations of unpacking RTP in the answer here 此处的答案中解包 RTP 的示例实现

try looking here for more info related to RTSP control and SDP over TCP.尝试在 此处查找与 RTSP 控制和 TCP 上的 SDP 相关的更多信息。 If you are getting into inspecting the details of the RTSP session/conversation and the messaging details about control protocol selection for the respective tracks in your video.如果您要检查 RTSP 会话/对话的详细信息以及有关视频中各个轨道的控制协议选择的消息传递详细信息。 If u can get your answer without a diversion into SDP / RTCP then , obviously, thats faster/better.如果你能在不转移到 SDP/RTCP 的情况下得到你的答案,那么显然,那会更快/更好。

this is the packet format for TCP/RTP :这是 TCP/RTP 的数据包格式:

[$ - 1byte][Transport Channel - 1byte][RTP data length - 2bytes][RTP data]

the rest is like upd其余的就像更新

for more info read process raw rtp packets有关更多信息,请阅读process raw rtp 数据包

Just explain something, I'm working for this as well, If you want use RTSP over TCP, please be careful about your socket reading code.解释一下,我也在为此工作,如果您想通过 TCP 使用 RTSP,请注意您的套接字读取代码。 The suitable socket process like below:合适的socket流程如下:

while (socket.connected) {
  char magic = socket.read a char;
  if (magic == '$') {  // is a RTP over TCP packet
     byte channel = socket.read 1 byte;
     unsigned short len = socket.read 2 byte; // len = ((byte1 & 0xFF) << 8) + (byte2 &0xFF);
     int readTotal = 0;
     byte rtpPacket[len];
     while (readTotal < len) {
         // read remaing bytes to rtpPacket from index readTotal
         int r = socket.read(rtpPacket, readTotal, len - readTotal); 
         if (r > 0)
            readTotal += r;
         else    // -1 means socket read error
            break;   
     }
     // now we get full RTP packet, process it!
     call back channel, rtpPacket;
  } else {  // is RTSP protocol response
     string array header;
     while (line = socket.readline() != null) {
        if (line == "") {
           int body_len = Parse header "Content-Length";
           byte body[body_len];
           int readBody = 0;
           while (readBody < body_len) {
              int r = socket.read(body, readBody, body_len - readBody);
              if (r>0)
                readBody += r;
              else
                break;
           }

           // Now we get full header, body of RTSP response, process it
           Callback header, body;   
        } else {
           header.add(line);
        }
     }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM