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.
 
 

84 lines
2.5 KiB

package com.inspect.tcpserver.sip.gb28181;
import java.net.*;
import java.util.concurrent.atomic.AtomicInteger;
public class RtpSenderEx {
private final DatagramSocket socket;
private final InetSocketAddress target;
private final int ssrc;
private final int payloadType;
private final AtomicInteger seq = new AtomicInteger(0);
public RtpSenderEx(String ip, int port, int ssrc, int pt) throws Exception {
this.socket = new DatagramSocket();
this.target = new InetSocketAddress(ip, port);
this.ssrc = ssrc;
this.payloadType = pt;
}
// public void send(byte[] ps, long ts90k) {
// int offset = 0;
// while (offset < ps.length) {
// int len = Math.min(1400, ps.length - offset);
// byte[] rtp = buildRtp(ps, offset, len, ts90k);
// socketSend(rtp);
// offset += len;
// }
// }
//
// private byte[] buildRtp(byte[] ps, int off, int len, long ts) {
// byte[] pkt = new byte[12 + len];
// pkt[0] = (byte) 0x80;
// pkt[1] = (byte) payloadType;
// pkt[2] = (byte) (seq.getAndIncrement() >> 8);
// pkt[3] = (byte) seq.get();
// pkt[4] = (byte) (ts >> 24);
// pkt[5] = (byte) (ts >> 16);
// pkt[6] = (byte) (ts >> 8);
// pkt[7] = (byte) ts;
// pkt[8] = (byte) (ssrc >> 24);
// pkt[9] = (byte) (ssrc >> 16);
// pkt[10] = (byte) (ssrc >> 8);
// pkt[11] = (byte) ssrc;
//
// System.arraycopy(ps, off, pkt, 12, len);
// return pkt;
// }
public void sendOne(byte[] ps, long ts90k) {
byte[] rtp = buildRtp(ps, ts90k, true);
socketSend(rtp);
}
private byte[] buildRtp(byte[] payload, long ts, boolean marker) {
byte[] pkt = new byte[12 + payload.length];
pkt[0] = (byte) 0x80;
pkt[1] = (byte) (payloadType | (marker ? 0x80 : 0));
pkt[2] = (byte) (seq.get() >> 8);
pkt[3] = (byte) seq.getAndIncrement();
pkt[4] = (byte) (ts >> 24);
pkt[5] = (byte) (ts >> 16);
pkt[6] = (byte) (ts >> 8);
pkt[7] = (byte) ts;
pkt[8] = (byte) (ssrc >> 24);
pkt[9] = (byte) (ssrc >> 16);
pkt[10] = (byte) (ssrc >> 8);
pkt[11] = (byte) ssrc;
System.arraycopy(payload, 0, pkt, 12, payload.length);
return pkt;
}
private void socketSend(byte[] pkt) {
try {
socket.send(new DatagramPacket(pkt, pkt.length, target));
} catch (Exception ignored) {}
}
public void close() {
socket.close();
}
}