2018-12-12 02:48:05 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2019-01-07 06:19:47 +03:00
|
|
|
helpers.go
|
2018-12-12 02:48:05 +03:00
|
|
|
DESCRIPTION
|
2019-01-07 06:19:47 +03:00
|
|
|
helpers.go provides functionality for editing and reading bytes slices
|
2018-12-14 08:32:47 +03:00
|
|
|
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
|
2019-01-07 06:19:47 +03:00
|
|
|
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 (
|
2018-12-14 06:16:10 +03:00
|
|
|
"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 {
|
2019-01-07 09:43:50 +03:00
|
|
|
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
|
2019-01-07 06:19:47 +03:00
|
|
|
func HasTime(p []byte) bool {
|
2019-01-08 12:34:45 +03:00
|
|
|
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
|
2019-01-07 06:19:47 +03:00
|
|
|
func HasLocation(p []byte) bool {
|
2019-01-08 12:34:45 +03:00
|
|
|
return p[LocationTagIndx] == LocationDescTag
|
2018-12-12 09:32:03 +03:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-01-07 06:19:47 +03:00
|
|
|
if !HasTime(dst) {
|
2019-01-07 06:26:25 +03:00
|
|
|
return errors.New("pmt does not have time descriptor, cannot update")
|
2018-12-11 09:46:26 +03:00
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
ts := TimeBytes(uint64(t))
|
2019-01-07 09:43:50 +03:00
|
|
|
for i := range dst[TimeDataIndx : TimeDataIndx+TimeDataSize] {
|
|
|
|
dst[i+TimeDataIndx] = ts[i]
|
2018-12-11 09:46:26 +03:00
|
|
|
}
|
2019-01-30 08:07:15 +03:00
|
|
|
UpdateCrc(dst[1:])
|
2018-12-11 09:46:26 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-03 10:26:08 +03:00
|
|
|
// SyntaxSecLenFrom takes a byte slice representation of a psi and extracts
|
|
|
|
// it's syntax section length
|
2019-01-29 06:46:33 +03:00
|
|
|
func SyntaxSecLenFrom(p []byte) int {
|
|
|
|
return int(((p[SyntaxSecLenIdx1] & SyntaxSecLenMask1) << 8) | p[SyntaxSecLenIdx2])
|
2019-01-03 10:26:08 +03:00
|
|
|
}
|
|
|
|
|
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
|
2018-12-14 08:36:01 +03:00
|
|
|
func TimeFrom(p []byte) (t uint64, err error) {
|
2019-01-07 06:19:47 +03:00
|
|
|
if !HasTime(p) {
|
2019-01-07 06:26:25 +03:00
|
|
|
return 0, errors.New("pmt does not have a time descriptor")
|
2018-12-11 09:46:26 +03:00
|
|
|
}
|
2019-01-07 09:43:50 +03:00
|
|
|
t = binary.BigEndian.Uint64(p[TimeDataIndx : TimeDataIndx+TimeDataSize])
|
2018-12-11 09:46:26 +03:00
|
|
|
return t, nil
|
|
|
|
}
|
2018-12-12 08:56:59 +03:00
|
|
|
|
2018-12-14 09:16:36 +03:00
|
|
|
// LocationFrom takes a byte slice representation of a psi-pmt and extracts it's
|
2018-12-14 03:45:49 +03:00
|
|
|
// timestamp, returning as a uint64 if it exists, otherwise returning 0 and nil
|
|
|
|
// if it does not exist
|
2018-12-14 08:36:01 +03:00
|
|
|
func LocationFrom(p []byte) (g string, err error) {
|
2019-01-07 06:19:47 +03:00
|
|
|
if !HasLocation(p) {
|
2019-01-07 06:26:25 +03:00
|
|
|
return "", errors.New("pmt does not have location descriptor")
|
2018-12-14 03:45:49 +03:00
|
|
|
}
|
2019-01-07 09:43:50 +03:00
|
|
|
gBytes := p[LocationDataIndx : LocationDataIndx+LocationDataSize]
|
2018-12-14 06:16:10 +03:00
|
|
|
gBytes = bytes.Trim(gBytes, "\x00")
|
2018-12-14 03:45:49 +03:00
|
|
|
g = string(gBytes)
|
|
|
|
return g, nil
|
|
|
|
}
|
|
|
|
|
2018-12-14 08:32:47 +03:00
|
|
|
// 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 {
|
2019-01-07 06:19:47 +03:00
|
|
|
if !HasLocation(d) {
|
2019-01-07 06:26:25 +03:00
|
|
|
return errors.New("pmt does not location descriptor, cannot update")
|
2018-12-12 09:32:03 +03:00
|
|
|
}
|
2019-01-20 12:44:27 +03:00
|
|
|
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
|
|
|
}
|
2019-01-30 08:07:15 +03:00
|
|
|
UpdateCrc(d[1:])
|
2018-12-12 09:32:03 +03:00
|
|
|
return nil
|
|
|
|
}
|
2018-12-13 07:39:23 +03:00
|
|
|
|
2019-01-10 10:01:14 +03:00
|
|
|
func trimTo(d []byte, t byte) []byte {
|
2019-01-07 06:54:54 +03:00
|
|
|
for i, b := range d {
|
|
|
|
if b == t {
|
|
|
|
return d[:i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d
|
2018-12-13 07:39:23 +03:00
|
|
|
}
|
2018-12-27 06:41:23 +03:00
|
|
|
|
|
|
|
// addPadding adds an appropriate amount of padding to a pat or pmt table for
|
2019-05-11 14:44:28 +03:00
|
|
|
// addition to an MPEG-TS packet
|
2019-01-30 08:07:15 +03:00
|
|
|
func AddPadding(d []byte) []byte {
|
2019-01-12 10:21:39 +03:00
|
|
|
t := make([]byte, PacketSize)
|
|
|
|
copy(t, d)
|
|
|
|
padding := t[len(d):]
|
|
|
|
for i := range padding {
|
|
|
|
padding[i] = 0xff
|
2018-12-27 06:41:23 +03:00
|
|
|
}
|
2019-01-13 12:43:35 +03:00
|
|
|
return t
|
2018-12-27 06:41:23 +03:00
|
|
|
}
|