func (b *Buffer) SetReadDeadline(t time.Time) error {
	b.readDeadline.Set(t)
	return nil
}
SetReadDeadline sets the deadline for the Read operation.
Setting to zero means no deadline.
package packetio
import (
	"errors"
	"io"
	"sync"
	"time"
	"github.com/pion/transport/deadline"
)
var errPacketTooBig = errors.New("packet too big")
type BufferPacketType int
const (
	RTPBufferPacket BufferPacketType = 1
	RTCPBufferPacket BufferPacketType = 2
)
type Buffer struct {
	mutex sync.Mutex
	data       []byte
	head, tail int
	notify chan struct{}
	subs   bool
	closed bool
	count                 int
	limitCount, limitSize int
	readDeadline *deadline.Deadline
}
const (
	minSize    = 2048
	cutoffSize = 128 * 1024
	maxSize    = 4 * 1024 * 1024
)
func NewBuffer() *Buffer {
	return &Buffer{
		notify:       make(chan struct{}),
		readDeadline: deadline.New(),
	}
}
func (b *Buffer) available(size int) bool {
	available := b.head - b.tail
	if available <= 0 {
		available += len(b.data)
	}
	if size+2+1 > available {
		return false
	}
	return true
}
func (b *Buffer) grow() error {
	var newsize int
	if len(b.data) < cutoffSize {
		newsize = 2 * len(b.data)
	} else {
		newsize = 5 * len(b.data) / 4
	}
	if newsize < minSize {
		newsize = minSize
	}
	if (b.limitSize <= 0 || sizeHardlimit) && newsize > maxSize {
		newsize = maxSize
	}
	if b.limitSize > 0 && newsize > b.limitSize+1 {
		newsize = b.limitSize + 1
	}
	if newsize <= len(b.data) {
		return ErrFull
	}
	newdata := make([]byte, newsize)
	var n int
	if b.head <= b.tail {
		n = copy(newdata, b.data[b.head:b.tail])
	} else {
		n = copy(newdata, b.data[b.head:])
		n += copy(newdata[n:], b.data[:b.tail])
	}
	b.head = 0
	b.tail = n
	b.data = newdata
	return nil
}
func (b *Buffer) Write(packet []byte) (int, error) {
	if len(packet) >= 0x10000 {
		return 0, errPacketTooBig
	}
	b.mutex.Lock()
	if b.closed {
		b.mutex.Unlock()
		return 0, io.ErrClosedPipe
	}
	if (b.limitCount > 0 && b.count >= b.limitCount) ||
		(b.limitSize > 0 && b.size()+2+len(packet) > b.limitSize) {
		b.mutex.Unlock()
		return 0, ErrFull
	}
	for !b.available(len(packet)) {
		err := b.grow()
		if err != nil {
			b.mutex.Unlock()
			return 0, err
		}
	}
	var notify chan struct{}
	if b.subs {
		notify = b.notify
		b.notify = make(chan struct{})
		b.subs = false
	}
	b.data[b.tail] = uint8(len(packet) >> 8)
	b.tail++
	if b.tail >= len(b.data) {
		b.tail = 0
	}
	b.data[b.tail] = uint8(len(packet))
	b.tail++
	if b.tail >= len(b.data) {
		b.tail = 0
	}
	n := copy(b.data[b.tail:], packet)
	b.tail += n
	if b.tail >= len(b.data) {
		m := copy(b.data, packet[n:])
		b.tail = m
	}
	b.count++
	b.mutex.Unlock()
	if notify != nil {
		close(notify)
	}
	return len(packet), nil
}
func (b *Buffer) Read(packet []byte) (n int, err error) {
	select {
	case <-b.readDeadline.Done():
		return 0, &netError{ErrTimeout, true, true}
	default:
	}
	for {
		b.mutex.Lock()
		if b.head != b.tail {
			n1 := b.data[b.head]
			b.head++
			if b.head >= len(b.data) {
				b.head = 0
			}
			n2 := b.data[b.head]
			b.head++
			if b.head >= len(b.data) {
				b.head = 0
			}
			count := int((uint16(n1) << 8) | uint16(n2))
			copied := count
			if copied > len(packet) {
				copied = len(packet)
			}
			if b.head+copied < len(b.data) {
				copy(packet, b.data[b.head:b.head+copied])
			} else {
				k := copy(packet, b.data[b.head:])
				copy(packet[k:], b.data[:copied-k])
			}
			b.head += count
			if b.head >= len(b.data) {
				b.head -= len(b.data)
			}
			if b.head == b.tail {
				b.head = 0
				b.tail = 0
			}
			b.count--
			b.mutex.Unlock()
			if copied < count {
				return copied, io.ErrShortBuffer
			}
			return copied, nil
		}
		if b.closed {
			b.mutex.Unlock()
			return 0, io.EOF
		}
		notify := b.notify
		b.subs = true
		b.mutex.Unlock()
		select {
		case <-b.readDeadline.Done():
			return 0, &netError{ErrTimeout, true, true}
		case <-notify:
		}
	}
}
func (b *Buffer) Close() (err error) {
	b.mutex.Lock()
	if b.closed {
		b.mutex.Unlock()
		return nil
	}
	notify := b.notify
	b.closed = true
	b.mutex.Unlock()
	close(notify)
	return nil
}
func (b *Buffer) Count() int {
	b.mutex.Lock()
	defer b.mutex.Unlock()
	return b.count
}
func (b *Buffer) SetLimitCount(limit int) {
	b.mutex.Lock()
	defer b.mutex.Unlock()
	b.limitCount = limit
}
func (b *Buffer) Size() int {
	b.mutex.Lock()
	defer b.mutex.Unlock()
	return b.size()
}
func (b *Buffer) size() int {
	size := b.tail - b.head
	if size < 0 {
		size += len(b.data)
	}
	return size
}
func (b *Buffer) SetLimitSize(limit int) {
	b.mutex.Lock()
	defer b.mutex.Unlock()
	b.limitSize = limit
}