package com.usky.cdi.service.sip.rtp;

/**
 * RTP 包组装
 */
public final class RtpPacketBuilder {

    private RtpPacketBuilder() {
    }

    public static byte[] build(byte[] payload, int payloadType, int sequenceNumber,
                               long timestamp, long ssrc, boolean marker) {
        byte[] packet = new byte[12 + payload.length];
        packet[0] = (byte) 0x80;
        packet[1] = (byte) ((marker ? 0x80 : 0) | (payloadType & 0x7F));
        packet[2] = (byte) (sequenceNumber >> 8);
        packet[3] = (byte) (sequenceNumber & 0xFF);
        packet[4] = (byte) (timestamp >> 24);
        packet[5] = (byte) (timestamp >> 16);
        packet[6] = (byte) (timestamp >> 8);
        packet[7] = (byte) (timestamp);
        packet[8] = (byte) (ssrc >> 24);
        packet[9] = (byte) (ssrc >> 16);
        packet[10] = (byte) (ssrc >> 8);
        packet[11] = (byte) (ssrc);
        System.arraycopy(payload, 0, packet, 12, payload.length);
        return packet;
    }
}
