av/stream/mts/psi/helpers.go

138 lines
4.1 KiB
Go
Raw Normal View History

2018-12-12 02:48:05 +03:00
/*
NAME
helpers.go
2018-12-12 02:48:05 +03:00
DESCRIPTION
helpers.go provides functionality for editing and reading bytes slices
directly in order to insert/read timestamp and location data in psi.
2018-12-12 02:48:05 +03:00
AUTHOR
Saxon Milton <saxon@ausocean.org>
LICENSE
helpers.go is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
2018-12-12 02:48:05 +03:00
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
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
2018-12-11 09:46:26 +03:00
package psi
import (
"bytes"
2018-12-11 09:46:26 +03:00
"encoding/binary"
"errors"
)
2018-12-14 09:16:36 +03:00
// TimeBytes takes a timestamp as an uint64 and converts to an 8 byte slice -
2018-12-12 15:47:28 +03:00
// allows for updating of timestamp in pmt time descriptor.
2018-12-14 08:52:48 +03:00
func TimeBytes(t uint64) []byte {
var s [TimeDataSize]byte
2018-12-12 15:47:28 +03:00
binary.BigEndian.PutUint64(s[:], t)
2018-12-14 08:52:48 +03:00
return s[:]
2018-12-11 09:46:26 +03:00
}
2018-12-14 09:16:36 +03:00
// HasTime takes a psi as a byte slice and checks to see if it has a time descriptor
2018-12-12 15:47:28 +03:00
// - if so return nil, otherwise return error
func HasTime(p []byte) bool {
return p[TimeTagIndx] == TimeDescTag
2018-12-11 09:46:26 +03:00
}
2018-12-14 09:16:36 +03:00
// HasLocation takes a psi as a byte slice and checks to see if it has a location descriptor
2018-12-12 15:47:28 +03:00
// - if so return nil, otherwise return error
func HasLocation(p []byte) bool {
return p[LocationTagIndx] == LocationDescTag
}
2018-12-12 15:47:28 +03:00
// UpdateTime takes the byte slice representation of a psi-pmt as well as a time
// as an integer and attempts to update the time descriptor in the pmt with the
// given time if the time descriptor exists, otherwise an error is returned
2018-12-14 08:51:56 +03:00
func UpdateTime(dst []byte, t uint64) error {
if !HasTime(dst) {
return errors.New("pmt does not have time descriptor, cannot update")
2018-12-11 09:46:26 +03:00
}
ts := TimeBytes(uint64(t))
for i := range dst[TimeDataIndx : TimeDataIndx+TimeDataSize] {
dst[i+TimeDataIndx] = ts[i]
2018-12-11 09:46:26 +03:00
}
updateCrc(dst[1:])
2018-12-11 09:46:26 +03:00
return nil
}
// SyntaxSecLenFrom takes a byte slice representation of a psi and extracts
// it's syntax section length
func SyntaxSecLenFrom(p []byte) int {
return int(((p[SyntaxSecLenIdx1] & SyntaxSecLenMask1) << 8) | p[SyntaxSecLenIdx2])
}
2018-12-14 09:16:36 +03:00
// TimeFrom takes a byte slice representation of a psi-pmt and extracts it's
2018-12-12 15:47:28 +03:00
// timestamp, returning as a uint64 if it exists, otherwise returning 0 and nil
// if it does not exist
func TimeFrom(p []byte) (t uint64, err error) {
if !HasTime(p) {
return 0, errors.New("pmt does not have a time descriptor")
2018-12-11 09:46:26 +03:00
}
t = binary.BigEndian.Uint64(p[TimeDataIndx : TimeDataIndx+TimeDataSize])
2018-12-11 09:46:26 +03:00
return t, nil
}
2018-12-14 09:16:36 +03:00
// LocationFrom takes a byte slice representation of a psi-pmt and extracts it's
// timestamp, returning as a uint64 if it exists, otherwise returning 0 and nil
// if it does not exist
func LocationFrom(p []byte) (g string, err error) {
if !HasLocation(p) {
return "", errors.New("pmt does not have location descriptor")
}
gBytes := p[LocationDataIndx : LocationDataIndx+LocationDataSize]
gBytes = bytes.Trim(gBytes, "\x00")
g = string(gBytes)
return g, nil
}
// UpdateLocation takes a byte slice representation of a psi-pmt containing a location
// descriptor and attempts to update the location data value with the passed string.
// If the psi does not contain a location descriptor, and error is returned.
func UpdateLocation(d []byte, s string) error {
if !HasLocation(d) {
return errors.New("pmt does not location descriptor, cannot update")
}
loc := d[LocationDataIndx : LocationDataIndx+LocationDataSize]
n := copy(loc, s)
loc = loc[n:]
for i := range loc {
loc[i] = 0
2019-01-18 05:51:35 +03:00
}
updateCrc(d[1:])
return nil
}
2019-01-10 10:01:14 +03:00
func trimTo(d []byte, t byte) []byte {
for i, b := range d {
if b == t {
return d[:i]
}
}
return d
}
// addPadding adds an appropriate amount of padding to a pat or pmt table for
// addition to an mpegts packet
func addPadding(d []byte) []byte {
t := make([]byte, PacketSize)
copy(t, d)
padding := t[len(d):]
for i := range padding {
padding[i] = 0xff
}
return t
}