| 1 | package cz.vutbr.feec.packets.rtcp; |
| 2 | |
| 3 | import cz.vutbr.feec.packets.IPacket; |
| 4 | import cz.vutbr.feec.packets.PacketGenerateException; |
| 5 | import cz.vutbr.feec.packets.PacketParseException; |
| 6 | import cz.vutbr.feec.packets.PacketUtils; |
| 7 | |
| 8 | public class AppPacket implements IPacket { |
| 9 | |
| 10 | private int version = 2; |
| 11 | private int padding; |
| 12 | private int subtype; |
| 13 | private int payloadtype = RTCPConstants.RTCP_APP; |
| 14 | private int length; |
| 15 | private long SSRC_CSRC; |
| 16 | private long nameASCII; |
| 17 | private byte[] appData; |
| 18 | private int appDataLength; |
| 19 | |
| 20 | public int getVersion() { |
| 21 | return version; |
| 22 | } |
| 23 | |
| 24 | public void setVersion(int version) { |
| 25 | this.version = version; |
| 26 | } |
| 27 | |
| 28 | public int getPadding() { |
| 29 | return padding; |
| 30 | } |
| 31 | |
| 32 | public void setPadding(int padding) { |
| 33 | this.padding = padding; |
| 34 | } |
| 35 | |
| 36 | public int getSubtype() { |
| 37 | return subtype; |
| 38 | } |
| 39 | |
| 40 | public void setSubtype(int subtype) { |
| 41 | this.subtype = subtype; |
| 42 | } |
| 43 | |
| 44 | public int getPayloadtype() { |
| 45 | return payloadtype; |
| 46 | } |
| 47 | |
| 48 | public void setPayloadtype(int payloadtype) { |
| 49 | this.payloadtype = payloadtype; |
| 50 | } |
| 51 | |
| 52 | public int getLength() { |
| 53 | return length; |
| 54 | } |
| 55 | |
| 56 | public void setLength(int length) { |
| 57 | this.length = length; |
| 58 | } |
| 59 | |
| 60 | public long getSSRC_CSRC() { |
| 61 | return SSRC_CSRC; |
| 62 | } |
| 63 | |
| 64 | public void setSSRC_CSRC(long ssrc_csrc) { |
| 65 | SSRC_CSRC = ssrc_csrc; |
| 66 | } |
| 67 | |
| 68 | public long getNameASCII() { |
| 69 | return nameASCII; |
| 70 | } |
| 71 | |
| 72 | public void setNameASCII(long nameASCII) { |
| 73 | this.nameASCII = nameASCII; |
| 74 | } |
| 75 | |
| 76 | public byte[] getAppData() { |
| 77 | return appData; |
| 78 | } |
| 79 | |
| 80 | public void setAppData(byte[] appData, int appDataLength) { |
| 81 | this.appData = new byte[appDataLength]; |
| 82 | this.appData = appData; |
| 83 | } |
| 84 | |
| 85 | public int getAppDataLength() { |
| 86 | return appDataLength; |
| 87 | } |
| 88 | |
| 89 | public void setAppDataLength(int appDataLength) { |
| 90 | this.appDataLength = appDataLength; |
| 91 | } |
| 92 | |
| 93 | public int parseData(byte[] buffer, int offset, int dataLenghtinBufffer) { |
| 94 | int i; |
| 95 | this.appData = new byte[dataLenghtinBufffer]; |
| 96 | for (i = offset; i < dataLenghtinBufffer + offset; i++) { |
| 97 | this.appData[i - offset] = buffer[i]; |
| 98 | } |
| 99 | this.appDataLength = i - offset; |
| 100 | return appDataLength; |
| 101 | } |
| 102 | |
| 103 | public int generate(byte[] buffer, int offset) |
| 104 | throws PacketGenerateException { |
| 105 | |
| 106 | assert PacketUtils.checkSize(this.getVersion(), 2); |
| 107 | assert PacketUtils.checkSize(this.getPadding(), 1); |
| 108 | assert PacketUtils.checkSize(this.getSubtype(), 5); |
| 109 | assert PacketUtils.checkSize(this.getPayloadtype(), 8); |
| 110 | assert PacketUtils.checkSize(this.getLength(), 16); |
| 111 | assert PacketUtils.checkSize(this.getSSRC_CSRC(), 32); |
| 112 | assert PacketUtils.checkSize(this.getNameASCII(), 32); |
| 113 | |
| 114 | byte V_P_ST = (byte) ((this.getVersion() << 6) & 0xC0 |
| 115 | | (this.getPadding() << 5) & 0x20 | this.getSubtype() & 0x1F); |
| 116 | buffer[offset] = V_P_ST; |
| 117 | |
| 118 | PacketUtils.setOctet1(buffer, offset + 1, this.getPayloadtype()); |
| 119 | PacketUtils.setOctet2(buffer, offset + 2, this.getLength()); |
| 120 | PacketUtils.setOctet4(buffer, offset + 4, this.getSSRC_CSRC()); |
| 121 | PacketUtils.setOctet4(buffer, offset + 8, this.getNameASCII()); |
| 122 | |
| 123 | int length = this.getAppDataLength(); |
| 124 | for (int i = 0; i < length; i++) { |
| 125 | buffer[offset + 12 + i] = this.getAppData()[i]; |
| 126 | } |
| 127 | |
| 128 | return 12 + (length / 4); |
| 129 | } |
| 130 | |
| 131 | public int parse(byte[] buffer, int offset, int length) |
| 132 | throws PacketParseException { |
| 133 | |
| 134 | int first = PacketUtils.getOctet1(buffer, offset); |
| 135 | this.setVersion((first & 0xC0) >> 6); |
| 136 | this.setPadding((first & 0x20) >> 5); |
| 137 | this.setSubtype(first & 0x1F); |
| 138 | |
| 139 | this.setPayloadtype(PacketUtils.getOctet1(buffer, offset + 1)); |
| 140 | this.setLength(PacketUtils.getOctet2(buffer, offset + 2)); |
| 141 | this.setSSRC_CSRC(PacketUtils.getOctet4(buffer, offset + 4)); |
| 142 | this.setNameASCII(PacketUtils.getOctet4(buffer, offset + 8)); |
| 143 | |
| 144 | int len = 12; |
| 145 | len += this.parseData(buffer, offset+12, length-12); |
| 146 | |
| 147 | return len; |
| 148 | } |
| 149 | |
| 150 | } |