package rtp
import (
"encoding/binary"
"time"
)
const (
absCaptureTimeExtensionSize = 8
absCaptureTimeExtendedExtensionSize = 16
)
type AbsCaptureTimeExtension struct {
Timestamp uint64
EstimatedCaptureClockOffset *int64
}
func (t AbsCaptureTimeExtension ) Marshal () ([]byte , error ) {
if t .EstimatedCaptureClockOffset != nil {
buf := make ([]byte , 16 )
binary .BigEndian .PutUint64 (buf [0 :8 ], t .Timestamp )
binary .BigEndian .PutUint64 (buf [8 :16 ], uint64 (*t .EstimatedCaptureClockOffset ))
return buf , nil
}
buf := make ([]byte , 8 )
binary .BigEndian .PutUint64 (buf [0 :8 ], t .Timestamp )
return buf , nil
}
func (t *AbsCaptureTimeExtension ) Unmarshal (rawData []byte ) error {
if len (rawData ) < absCaptureTimeExtensionSize {
return errTooSmall
}
t .Timestamp = binary .BigEndian .Uint64 (rawData [0 :8 ])
if len (rawData ) >= absCaptureTimeExtendedExtensionSize {
offset := int64 (binary .BigEndian .Uint64 (rawData [8 :16 ]))
t .EstimatedCaptureClockOffset = &offset
}
return nil
}
func (t AbsCaptureTimeExtension ) CaptureTime () time .Time {
return toTime (t .Timestamp )
}
func (t AbsCaptureTimeExtension ) EstimatedCaptureClockOffsetDuration () *time .Duration {
if t .EstimatedCaptureClockOffset == nil {
return nil
}
offset := *t .EstimatedCaptureClockOffset
negative := false
if offset < 0 {
offset = -offset
negative = true
}
duration := time .Duration (offset /(1 <<32 ))*time .Second + time .Duration ((offset &0xFFFFFFFF )*1e9 /(1 <<32 ))*time .Nanosecond
if negative {
duration = -duration
}
return &duration
}
func NewAbsCaptureTimeExtension (captureTime time .Time ) *AbsCaptureTimeExtension {
return &AbsCaptureTimeExtension {
Timestamp : toNtpTime (captureTime ),
}
}
func NewAbsCaptureTimeExtensionWithCaptureClockOffset (
captureTime time .Time ,
captureClockOffset time .Duration ,
) *AbsCaptureTimeExtension {
ns := captureClockOffset .Nanoseconds ()
negative := false
if ns < 0 {
ns = -ns
negative = true
}
lsb := (ns / 1e9 ) & 0xFFFFFFFF
msb := (((ns % 1e9 ) * (1 << 32 )) / 1e9 ) & 0xFFFFFFFF
offset := (lsb << 32 ) | msb
if negative {
offset = -offset
}
return &AbsCaptureTimeExtension {
Timestamp : toNtpTime (captureTime ),
EstimatedCaptureClockOffset : &offset ,
}
}
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 .