2018-11-16 12:05:19 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
encoder.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHOR
|
|
|
|
Saxon Nelson-Milton (saxon@ausocean.org)
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
encoder.go is Copyright (C) 2018 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 http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package rtp
|
2018-11-17 10:13:04 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2018-11-17 15:47:08 +03:00
|
|
|
"math/rand"
|
2018-11-17 10:13:04 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-11-17 15:47:08 +03:00
|
|
|
yes = 1
|
|
|
|
no = 0
|
2018-11-20 16:39:42 +03:00
|
|
|
defaultPktType = 33
|
2018-11-17 15:47:08 +03:00
|
|
|
timestampFreq = 90000 // Hz
|
2018-11-20 16:39:42 +03:00
|
|
|
mtsSize = 188
|
|
|
|
bufferSize = 1000
|
2019-02-09 12:01:15 +03:00
|
|
|
sendSize = 7 * 188
|
2018-11-17 10:13:04 +03:00
|
|
|
)
|
|
|
|
|
2018-11-21 12:58:40 +03:00
|
|
|
// Encoder implements io writer and provides functionality to wrap data into
|
|
|
|
// rtp packets
|
2018-11-17 10:13:04 +03:00
|
|
|
type Encoder struct {
|
2018-11-17 15:47:08 +03:00
|
|
|
dst io.Writer
|
|
|
|
ssrc uint32
|
|
|
|
seqNo uint16
|
2018-11-17 10:13:04 +03:00
|
|
|
clock time.Duration
|
|
|
|
frameInterval time.Duration
|
2018-11-20 16:39:42 +03:00
|
|
|
fps int
|
2018-11-21 12:53:02 +03:00
|
|
|
buffer []byte
|
2019-01-02 03:02:48 +03:00
|
|
|
pktSpace [defPktSize]byte
|
2018-11-17 10:13:04 +03:00
|
|
|
}
|
|
|
|
|
2018-11-17 15:47:08 +03:00
|
|
|
// NewEncoder returns a new Encoder type given an io.Writer - the destination
|
|
|
|
// after encoding and the desired fps
|
2018-11-18 10:43:38 +03:00
|
|
|
func NewEncoder(dst io.Writer, fps int) *Encoder {
|
2018-11-17 10:13:04 +03:00
|
|
|
return &Encoder{
|
|
|
|
dst: dst,
|
2018-11-17 15:47:08 +03:00
|
|
|
ssrc: rand.Uint32(),
|
2018-11-18 10:43:38 +03:00
|
|
|
frameInterval: time.Duration(float64(time.Second) / float64(fps)),
|
2018-11-20 16:39:42 +03:00
|
|
|
fps: fps,
|
2018-12-14 12:35:10 +03:00
|
|
|
buffer: make([]byte, 0),
|
2018-11-17 10:13:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 12:58:40 +03:00
|
|
|
// Write provides an interface between a prior encoder and this rtp encoder,
|
|
|
|
// so that multiple layers of packetization can occur.
|
2018-11-21 12:53:02 +03:00
|
|
|
func (e *Encoder) Write(data []byte) (int, error) {
|
|
|
|
e.buffer = append(e.buffer, data...)
|
2019-02-09 12:01:15 +03:00
|
|
|
if len(e.buffer) < sendSize {
|
2019-02-09 05:52:18 +03:00
|
|
|
return len(data), nil
|
2018-11-21 12:53:02 +03:00
|
|
|
}
|
2019-02-09 05:52:18 +03:00
|
|
|
buf := e.buffer
|
|
|
|
for len(buf) != 0 {
|
2019-02-09 12:01:15 +03:00
|
|
|
l := min(sendSize, len(buf))
|
|
|
|
err := e.Encode(buf[:l])
|
|
|
|
if err != nil {
|
|
|
|
return len(data), err
|
|
|
|
}
|
2019-02-09 05:52:18 +03:00
|
|
|
buf = buf[l:]
|
|
|
|
}
|
|
|
|
e.buffer = e.buffer[:0]
|
2018-11-21 12:53:02 +03:00
|
|
|
return len(data), nil
|
|
|
|
}
|
|
|
|
|
2019-02-09 05:52:18 +03:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2018-11-17 15:47:08 +03:00
|
|
|
// Encode takes a nalu unit and encodes it into an rtp packet and
|
|
|
|
// writes to the io.Writer given in NewEncoder
|
2018-11-21 12:53:02 +03:00
|
|
|
func (e *Encoder) Encode(payload []byte) error {
|
|
|
|
pkt := Pkt{
|
|
|
|
V: rtpVer, // version
|
|
|
|
X: no, // header extension
|
|
|
|
CC: no, // CSRC count
|
|
|
|
M: no, // NOTE: need to check if this works (decoders should ignore this)
|
|
|
|
PT: defaultPktType, // 33 for mpegts
|
|
|
|
SN: e.nxtSeqNo(), // sequence number
|
|
|
|
TS: e.nxtTimestamp(), // timestamp
|
|
|
|
SSRC: e.ssrc, // source identifier
|
|
|
|
Payload: payload,
|
|
|
|
Padding: no,
|
2018-11-17 10:13:04 +03:00
|
|
|
}
|
2019-01-02 03:02:48 +03:00
|
|
|
_, err := e.dst.Write(pkt.Bytes(e.pktSpace[:defPktSize]))
|
2018-11-21 12:53:02 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
e.tick()
|
2018-11-17 10:13:04 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// tick advances the clock one frame interval.
|
|
|
|
func (e *Encoder) tick() {
|
|
|
|
e.clock += e.frameInterval
|
|
|
|
}
|
|
|
|
|
2018-11-17 15:47:08 +03:00
|
|
|
// nxtTimestamp gets the next timestamp
|
|
|
|
func (e *Encoder) nxtTimestamp() uint32 {
|
|
|
|
return uint32(e.clock.Seconds() * timestampFreq)
|
2018-11-17 10:13:04 +03:00
|
|
|
}
|
|
|
|
|
2018-11-17 15:47:08 +03:00
|
|
|
// nxtSeqNo gets the next rtp packet sequence number
|
|
|
|
func (e *Encoder) nxtSeqNo() uint16 {
|
2018-11-24 04:44:44 +03:00
|
|
|
e.seqNo++
|
2018-11-17 15:47:08 +03:00
|
|
|
return e.seqNo - 1
|
2018-11-17 10:13:04 +03:00
|
|
|
}
|