2019-02-15 06:19:37 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
discontinuity.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
discontinuity.go provides functionality for detecting discontinuities in
|
|
|
|
mpegts and accounting for using the discontinuity indicator in the adaptation
|
|
|
|
field.
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
discontinuity.go is Copyright (C) 2017-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
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with revid in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses).
|
|
|
|
*/
|
|
|
|
|
|
|
|
package mts
|
|
|
|
|
|
|
|
import "github.com/Comcast/gots/packet"
|
|
|
|
|
|
|
|
// discontinuityRepairer provides function to detect discontinuities in mpegts
|
|
|
|
// and set the discontinuity indicator as appropriate.
|
|
|
|
type DiscontinuityRepairer struct {
|
|
|
|
expCC uint8
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:54:07 +03:00
|
|
|
// NewDiscontinuityRepairer returns a pointer to a new discontinuityRepairer.
|
2019-02-15 06:19:37 +03:00
|
|
|
func NewDiscontinuityRepairer() *DiscontinuityRepairer {
|
|
|
|
return &DiscontinuityRepairer{expCC: 16}
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:54:07 +03:00
|
|
|
// Failed is to be called in the case of a failed send. This will decrement the
|
|
|
|
// expectedCC so that it aligns with the failed chunks cc.
|
2019-02-15 06:19:37 +03:00
|
|
|
func (dr *DiscontinuityRepairer) Failed() {
|
|
|
|
dr.decExpectedCC()
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:54:07 +03:00
|
|
|
// Repair takes a clip of mpegts and checks that the first packet, which should
|
|
|
|
// be a PAT, contains a cc that is expected, otherwise the discontinuity indicator
|
|
|
|
// is set to true.
|
2019-02-15 06:19:37 +03:00
|
|
|
func (dr *DiscontinuityRepairer) Repair(d []byte) error {
|
|
|
|
var pkt [PacketSize]byte
|
|
|
|
copy(pkt[:], d[:PacketSize])
|
|
|
|
p := (*packet.Packet)(&pkt)
|
|
|
|
if p.PID() != PatPid {
|
|
|
|
panic("Clip to repair must have PAT first")
|
|
|
|
}
|
|
|
|
cc := p.ContinuityCounter()
|
2019-02-16 06:56:51 +03:00
|
|
|
expect, exists := dr.expectedCC(3)
|
2019-02-15 06:19:37 +03:00
|
|
|
dr.incExpectedCC()
|
|
|
|
if !exists {
|
|
|
|
dr.setExpectedCC(uint8(cc))
|
|
|
|
} else if cc != int(expect) {
|
|
|
|
if packet.ContainsAdaptationField(p) {
|
|
|
|
(*packet.AdaptationField)(p).SetDiscontinuity(true)
|
|
|
|
} else {
|
|
|
|
err := addAdaptationField(&pkt, DiscontinuityIndicator(true))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dr.setExpectedCC(uint8(cc))
|
|
|
|
copy(d[:PacketSize], pkt[:])
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:54:07 +03:00
|
|
|
// expectedCC returns the expected cc. If the cc hasn't been used yet, then 16
|
|
|
|
// and false is returned.
|
2019-02-15 18:10:35 +03:00
|
|
|
func (dr *DiscontinuityRepairer) expectedCC(pid int) (byte, bool) {
|
2019-02-15 06:19:37 +03:00
|
|
|
if dr.expCC == 16 {
|
|
|
|
return 16, false
|
|
|
|
}
|
|
|
|
return dr.expCC, true
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:54:07 +03:00
|
|
|
// incExpectedCC increments the expected cc.
|
2019-02-15 06:19:37 +03:00
|
|
|
func (dr *DiscontinuityRepairer) incExpectedCC() {
|
|
|
|
dr.expCC = (dr.expCC + 1) & 0xf
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:54:07 +03:00
|
|
|
// decExpectedCC decrements the expected cc.
|
2019-02-15 06:19:37 +03:00
|
|
|
func (dr *DiscontinuityRepairer) decExpectedCC() {
|
|
|
|
dr.expCC = (dr.expCC - 1) & 0xf
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:54:07 +03:00
|
|
|
// setExpectedCC sets the expected cc.
|
2019-02-15 06:19:37 +03:00
|
|
|
func (dr *DiscontinuityRepairer) setExpectedCC(cc uint8) {
|
|
|
|
dr.expCC = cc
|
|
|
|
}
|