2019-04-16 10:15:44 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
parse.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
parse.go contains functionality for parsing RTCP packets.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
This is Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
2019-04-21 18:04:03 +03:00
|
|
|
for more details.
|
2019-04-16 10:15:44 +03:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-04-12 11:32:27 +03:00
|
|
|
package rtcp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
2019-04-23 10:10:26 +03:00
|
|
|
// Timestamp describes an NTP timestamp, see https://tools.ietf.org/html/rfc1305
|
|
|
|
type Timestamp struct {
|
2019-04-23 07:55:22 +03:00
|
|
|
Seconds uint32
|
|
|
|
Fraction uint32
|
2019-04-21 18:04:03 +03:00
|
|
|
}
|
|
|
|
|
2019-05-23 07:37:19 +03:00
|
|
|
// ParseTimestamp gets the timestamp from a receiver report and returns it as
|
|
|
|
// a Timestamp as defined above. If the given bytes do not represent a valid
|
|
|
|
// receiver report, an error is returned.
|
2019-04-23 10:10:26 +03:00
|
|
|
func ParseTimestamp(buf []byte) (Timestamp, error) {
|
2019-05-20 11:44:23 +03:00
|
|
|
err := checkPacket(buf)
|
|
|
|
if err != nil {
|
|
|
|
return Timestamp{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return Timestamp{
|
|
|
|
Seconds: binary.BigEndian.Uint32(buf[8:]),
|
|
|
|
Fraction: binary.BigEndian.Uint32(buf[12:]),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseSSRC(buf []byte) (uint32, error) {
|
|
|
|
err := checkPacket(buf)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return binary.BigEndian.Uint32(buf[4:]), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkPacket(buf []byte) error {
|
2019-04-16 06:03:58 +03:00
|
|
|
if len(buf) < 4 {
|
2019-05-20 11:44:23 +03:00
|
|
|
return errors.New("bad RTCP packet, not of sufficient length")
|
2019-04-16 06:03:58 +03:00
|
|
|
}
|
2019-04-16 15:31:38 +03:00
|
|
|
if (buf[0]&0xc0)>>6 != rtcpVer {
|
2019-05-20 11:44:23 +03:00
|
|
|
return errors.New("incompatible RTCP version")
|
2019-04-12 11:32:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if buf[1] != typeSenderReport {
|
2019-05-20 11:44:23 +03:00
|
|
|
return errors.New("RTCP packet is not of sender report type")
|
2019-04-12 11:32:27 +03:00
|
|
|
}
|
2019-05-20 11:44:23 +03:00
|
|
|
return nil
|
2019-04-12 11:32:27 +03:00
|
|
|
}
|