util.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package rtcp
  2. // getPadding Returns the padding required to make the length a multiple of 4
  3. func getPadding(packetLen int) int {
  4. if packetLen%4 == 0 {
  5. return 0
  6. }
  7. return 4 - (packetLen % 4)
  8. }
  9. // setNBitsOfUint16 will truncate the value to size, left-shift to startIndex position and set
  10. func setNBitsOfUint16(src, size, startIndex, val uint16) (uint16, error) {
  11. if startIndex+size > 16 {
  12. return 0, errInvalidSizeOrStartIndex
  13. }
  14. // truncate val to size bits
  15. val &= (1 << size) - 1
  16. return src | (val << (16 - size - startIndex)), nil
  17. }
  18. // appendBit32 will left-shift and append n bits of val
  19. func appendNBitsToUint32(src, n, val uint32) uint32 {
  20. return (src << n) | (val & (0xFFFFFFFF >> (32 - n)))
  21. }
  22. // getNBit get n bits from 1 byte, begin with a position
  23. func getNBitsFromByte(b byte, begin, n uint16) uint16 {
  24. endShift := 8 - (begin + n)
  25. mask := (0xFF >> begin) & uint8(0xFF<<endShift)
  26. return uint16(b&mask) >> endShift
  27. }
  28. // get24BitFromBytes get 24bits from `[3]byte` slice
  29. func get24BitsFromBytes(b []byte) uint32 {
  30. return uint32(b[0])<<16 + uint32(b[1])<<8 + uint32(b[2])
  31. }