package extension
import (
"encoding/binary"
)
const (
useSRTPHeaderSize = 6
)
type UseSRTP struct {
ProtectionProfiles []SRTPProtectionProfile
MasterKeyIdentifier []byte
}
func (u UseSRTP ) TypeValue () TypeValue {
return UseSRTPTypeValue
}
func (u *UseSRTP ) Marshal () ([]byte , error ) {
out := make ([]byte , useSRTPHeaderSize )
binary .BigEndian .PutUint16 (out , uint16 (u .TypeValue ()))
binary .BigEndian .PutUint16 (
out [2 :],
uint16 (2 +(len (u .ProtectionProfiles )*2 )+ 1 +len (u .MasterKeyIdentifier )),
)
binary .BigEndian .PutUint16 (out [4 :], uint16 (len (u .ProtectionProfiles )*2 ))
for _ , v := range u .ProtectionProfiles {
out = append (out , []byte {0x00 , 0x00 }...)
binary .BigEndian .PutUint16 (out [len (out )-2 :], uint16 (v ))
}
if len (u .MasterKeyIdentifier ) > 255 {
return nil , errMasterKeyIdentifierTooLarge
}
out = append (out , byte (len (u .MasterKeyIdentifier )))
out = append (out , u .MasterKeyIdentifier ...)
return out , nil
}
func (u *UseSRTP ) Unmarshal (data []byte ) error {
if len (data ) <= useSRTPHeaderSize {
return errBufferTooSmall
} else if TypeValue (binary .BigEndian .Uint16 (data )) != u .TypeValue () {
return errInvalidExtensionType
}
profileCount := int (binary .BigEndian .Uint16 (data [4 :]) / 2 )
masterKeyIdentifierIndex := supportedGroupsHeaderSize + (profileCount * 2 )
if masterKeyIdentifierIndex +1 > len (data ) {
return errLengthMismatch
}
for i := 0 ; i < profileCount ; i ++ {
supportedProfile := SRTPProtectionProfile (binary .BigEndian .Uint16 (data [(useSRTPHeaderSize + (i * 2 )):]))
if _ , ok := srtpProtectionProfiles ()[supportedProfile ]; ok {
u .ProtectionProfiles = append (u .ProtectionProfiles , supportedProfile )
}
}
masterKeyIdentifierLen := int (data [masterKeyIdentifierIndex ])
if masterKeyIdentifierIndex +masterKeyIdentifierLen >= len (data ) {
return errLengthMismatch
}
u .MasterKeyIdentifier = append (
[]byte {},
data [masterKeyIdentifierIndex +1 :masterKeyIdentifierIndex +1 +masterKeyIdentifierLen ]...,
)
return nil
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .