| 1 | package cz.vutbr.feec.packets.rtcp; |
| 2 | |
| 3 | import java.util.Vector; |
| 4 | |
| 5 | import cz.vutbr.feec.packets.IPacket; |
| 6 | import cz.vutbr.feec.packets.PacketGenerateException; |
| 7 | import cz.vutbr.feec.packets.PacketParseException; |
| 8 | import cz.vutbr.feec.packets.PacketUtils; |
| 9 | import cz.vutbr.feec.session.rtprtcp.internal.RTCPConstants; |
| 10 | |
| 11 | import org.apache.commons.lang.builder.ToStringBuilder; |
| 12 | import org.apache.commons.lang.builder.EqualsBuilder; |
| 13 | import org.apache.commons.lang.builder.HashCodeBuilder; |
| 14 | |
| 15 | /** |
| 16 | * The Class SDESPacket. |
| 17 | */ |
| 18 | public class SDESPacket implements IPacket { |
| 19 | |
| 20 | private int version = RTCPConstants.VERSION; |
| 21 | |
| 22 | private int padding = RTCPConstants.PADDING; |
| 23 | |
| 24 | private int payloadtype = RTCPConstants.RTCP_SDES; |
| 25 | |
| 26 | private int length; |
| 27 | |
| 28 | private Vector<SDESChunk> chunks = new Vector<SDESChunk>(); |
| 29 | |
| 30 | public void addChunk(SDESChunk block) { |
| 31 | chunks.add(block); |
| 32 | } |
| 33 | |
| 34 | public SDESChunk getChunkBlock(int index) { |
| 35 | return chunks.get(index); |
| 36 | } |
| 37 | |
| 38 | public int numberofChunks() { |
| 39 | return chunks.size(); |
| 40 | } |
| 41 | |
| 42 | public int generate(byte[] buffer, int offset) |
| 43 | throws PacketGenerateException { |
| 44 | |
| 45 | assert PacketUtils.checkSize(this.getVersion(), 2); |
| 46 | assert PacketUtils.checkSize(this.getPadding(), 1); |
| 47 | assert PacketUtils.checkSize(this.getChunkCount(), 5); |
| 48 | assert PacketUtils.checkSize(this.getPayloadtype(), 8); |
| 49 | assert PacketUtils.checkSize(this.getLength(), 16); |
| 50 | |
| 51 | byte V_P_SC = (byte) ((this.getVersion() << 6) & 0xC0 |
| 52 | | (this.getPadding() << 5) & 0x20 | (this.getChunkCount() & 0x1F)); |
| 53 | buffer[offset] = V_P_SC; |
| 54 | |
| 55 | PacketUtils.setOctet1(buffer, offset + 1, this.getPayloadtype()); |
| 56 | PacketUtils.setOctet2(buffer, offset + 2, this.getLength()); |
| 57 | |
| 58 | int len = 4; |
| 59 | for (SDESChunk chk: chunks) { |
| 60 | len += chk.generate(buffer, offset+len); |
| 61 | } |
| 62 | return len; |
| 63 | } |
| 64 | |
| 65 | public int parse(byte[] buffer, int offset, int length) |
| 66 | throws PacketParseException { |
| 67 | int first = PacketUtils.getOctet1(buffer, offset); |
| 68 | this.setVersion((first & 0xC0) >> 6); |
| 69 | this.setPadding((first & 0x20) >> 5); |
| 70 | int parsedSourceCount = (first & 0x1F); |
| 71 | this.setPayloadtype(PacketUtils.getOctet1(buffer, offset + 1)); |
| 72 | this.setLength(PacketUtils.getOctet2(buffer, offset + 2)); |
| 73 | |
| 74 | int len = 4; |
| 75 | chunks.clear(); |
| 76 | for (int i = 0; i < parsedSourceCount; i++) { |
| 77 | SDESChunk block = new SDESChunk(); |
| 78 | len += block.parse(buffer, offset+len, length); |
| 79 | this.addChunk(block); |
| 80 | } |
| 81 | |
| 82 | return len; |
| 83 | } |
| 84 | |
| 85 | public int getVersion() { |
| 86 | return version; |
| 87 | } |
| 88 | |
| 89 | public void setVersion(int version) { |
| 90 | this.version = version; |
| 91 | } |
| 92 | |
| 93 | public int getPadding() { |
| 94 | return padding; |
| 95 | } |
| 96 | |
| 97 | public void setPadding(int padding) { |
| 98 | this.padding = padding; |
| 99 | } |
| 100 | |
| 101 | public int getChunkCount() { |
| 102 | return chunks.size(); |
| 103 | } |
| 104 | |
| 105 | public int getPayloadtype() { |
| 106 | return payloadtype; |
| 107 | } |
| 108 | |
| 109 | public void setPayloadtype(int payloadtype) { |
| 110 | this.payloadtype = payloadtype; |
| 111 | } |
| 112 | |
| 113 | public int getLength() { |
| 114 | return length; |
| 115 | } |
| 116 | |
| 117 | public void setLength(int length) { |
| 118 | this.length = length; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @see java.lang.Object#toString() |
| 123 | */ |
| 124 | public String toString() { |
| 125 | return new ToStringBuilder(this) |
| 126 | .append("chunks", this.chunks) |
| 127 | .append("version", this.version) |
| 128 | .append("padding", this.padding).append("length", this.length) |
| 129 | .append("payloadtype", this.payloadtype).append("sourceCount", |
| 130 | this.getChunkCount()).toString(); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @see java.lang.Object#equals(Object) |
| 135 | */ |
| 136 | public boolean equals(Object object) { |
| 137 | if (!(object instanceof SDESPacket)) { |
| 138 | return false; |
| 139 | } |
| 140 | SDESPacket rhs = (SDESPacket) object; |
| 141 | return new EqualsBuilder().append( |
| 142 | this.chunks, rhs.chunks).append(this.length, rhs.length) |
| 143 | .append(this.padding, rhs.padding).append(this.payloadtype, |
| 144 | rhs.payloadtype).append(this.version, rhs.version) |
| 145 | .isEquals(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @see java.lang.Object#hashCode() |
| 150 | */ |
| 151 | public int hashCode() { |
| 152 | return new HashCodeBuilder(55139609, 99832983).append(this.chunks).append(this.length) |
| 153 | .append(this.padding).append(this.payloadtype).append( |
| 154 | this.version).toHashCode(); |
| 155 | } |
| 156 | } |