You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

60 lines
1.6 KiB

package com.inspect.tcpserver.sip.gb28181;
import java.net.*;
import java.util.concurrent.atomic.AtomicInteger;
public class RtpSender {
private static final int RTP_HEADER_SIZE = 12;
private static final int MTU = 1400;
private final DatagramSocket socket;
private final InetAddress remote;
private final int port;
private final int ssrc;
private final AtomicInteger seq = new AtomicInteger(0);
private int timestamp = 0;
public RtpSender(DatagramSocket socket, InetAddress remote, int port, int ssrc) {
this.socket = socket;
this.remote = remote;
this.port = port;
this.ssrc = ssrc;
}
public void send(byte[] ps, boolean marker) throws Exception {
int offset = 0;
while (offset < ps.length) {
int size = Math.min(MTU, ps.length - offset);
byte[] pkt = new byte[RTP_HEADER_SIZE + size];
pkt[0] = (byte) 0x80;
pkt[1] = (byte) (96 | (marker ? 0x80 : 0x00));
pkt[2] = (byte) (seq.get() >> 8);
pkt[3] = (byte) (seq.getAndIncrement());
timestamp += 3600;
pkt[4] = (byte) (timestamp >> 24);
pkt[5] = (byte) (timestamp >> 16);
pkt[6] = (byte) (timestamp >> 8);
pkt[7] = (byte) (timestamp);
pkt[8] = (byte) (ssrc >> 24);
pkt[9] = (byte) (ssrc >> 16);
pkt[10] = (byte) (ssrc >> 8);
pkt[11] = (byte) (ssrc);
System.arraycopy(ps, offset, pkt, RTP_HEADER_SIZE, size);
socket.send(new DatagramPacket(pkt, pkt.length, remote, port));
offset += size;
}
}
}