10 Star 39 Fork 19

Gitee 极速下载 / Pion-WebRTC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/pion/webrtc
克隆/下载
goodbye.go 3.82 KB
一键复制 编辑 原始数据 按行查看 历史
Sean DuBois 提交于 2018-09-19 21:59 . RTCP: Add util/GetPadding
package rtcp
import (
"encoding/binary"
"github.com/pions/webrtc/internal/util"
)
// The Goodbye packet indicates that one or more sources are no longer active.
type Goodbye struct {
// The SSRC/CSRC identifiers that are no longer active
Sources []uint32
// Optional text indicating the reason for leaving, e.g., "camera malfunction" or "RTP loop detected"
Reason string
}
// Marshal encodes the Goodbye packet in binary
func (g Goodbye) Marshal() ([]byte, error) {
/*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |V=2|P| SC | PT=BYE=203 | length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | SSRC/CSRC |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* : ... :
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* (opt) | length | reason for leaving ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
rawPacket := make([]byte, len(g.Sources)*ssrcLength)
if len(g.Sources) > countMax {
return nil, errTooManySources
}
for i, s := range g.Sources {
binary.BigEndian.PutUint32(rawPacket[i*ssrcLength:], s)
}
if g.Reason != "" {
reason := []byte(g.Reason)
if len(reason) > sdesMaxOctetCount {
return nil, errReasonTooLong
}
rawPacket = append(rawPacket, uint8(len(reason)))
rawPacket = append(rawPacket, reason...)
// align to 32-bit boundary
rawPacket = append(rawPacket, make([]byte, util.GetPadding(len(rawPacket)))...)
}
h := Header{
Padding: false,
Count: uint8(len(g.Sources)),
Type: TypeGoodbye,
Length: uint16(headerLength + len(rawPacket)),
}
hData, err := h.Marshal()
if err != nil {
return nil, err
}
rawPacket = append(hData, rawPacket...)
return rawPacket, nil
}
// Unmarshal decodes the Goodbye packet from binary
func (g *Goodbye) Unmarshal(rawPacket []byte) error {
/*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |V=2|P| SC | PT=BYE=203 | length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | SSRC/CSRC |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* : ... :
* +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
* (opt) | length | reason for leaving ...
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
var header Header
if err := header.Unmarshal(rawPacket); err != nil {
return err
}
if header.Type != TypeGoodbye {
return errWrongType
}
if util.GetPadding(len(rawPacket)) != 0 {
return errPacketTooShort
}
g.Sources = make([]uint32, header.Count)
reasonOffset := int(headerLength + header.Count*ssrcLength)
if reasonOffset > len(rawPacket) {
return errPacketTooShort
}
for i := 0; i < int(header.Count); i++ {
offset := headerLength + i*ssrcLength
g.Sources[i] = binary.BigEndian.Uint32(rawPacket[offset:])
}
if reasonOffset < len(rawPacket) {
reasonLen := int(rawPacket[reasonOffset])
reasonEnd := reasonOffset + 1 + reasonLen
if reasonEnd > len(rawPacket) {
return errPacketTooShort
}
g.Reason = string(rawPacket[reasonOffset+1 : reasonEnd])
}
return nil
}
1
https://gitee.com/mirrors/Pion-WebRTC.git
git@gitee.com:mirrors/Pion-WebRTC.git
mirrors
Pion-WebRTC
Pion-WebRTC
v1.1.1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891