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 (
|
2018-11-20 16:39:42 +03:00
|
|
|
"bitbucket.org/ausocean/av/stream/mts"
|
2018-11-20 17:21:13 +03:00
|
|
|
"errors"
|
2018-11-17 10:13:04 +03:00
|
|
|
"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
|
2018-11-20 17:21:13 +03:00
|
|
|
maxMtsPerRtp = 7
|
2018-11-17 10:13:04 +03:00
|
|
|
)
|
|
|
|
|
2018-11-20 16:39:42 +03:00
|
|
|
type Queue struct {
|
2018-11-20 17:21:13 +03:00
|
|
|
buf [][]byte
|
2018-11-20 16:39:42 +03:00
|
|
|
right uint
|
|
|
|
left uint
|
|
|
|
len uint
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:21:13 +03:00
|
|
|
func NewQueue(maxElements, elementSize uint) (*Queue, error) {
|
|
|
|
if maxElements <= 0 {
|
|
|
|
return nil, errors.New("Max elements of queue must be more than 0")
|
|
|
|
}
|
|
|
|
if elementSize <= 0 {
|
|
|
|
return nil, errors.New("Element size in queue must be more than 0")
|
|
|
|
}
|
|
|
|
buf := make([][]byte, maxElements)
|
|
|
|
for i := range buf {
|
|
|
|
buf[i] = make([]byte, elementSize)
|
|
|
|
}
|
|
|
|
return &Queue{
|
|
|
|
buf: buf,
|
|
|
|
right: 0,
|
|
|
|
left: 0,
|
|
|
|
len: 0,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-11-20 16:39:42 +03:00
|
|
|
func (q *Queue) Write(frame []byte) (int, error) {
|
2018-11-20 17:21:13 +03:00
|
|
|
if q.right > uint(len(q.buf)) {
|
|
|
|
return 0, errors.New("Queue is full, cannot perform write.")
|
|
|
|
}
|
|
|
|
copy(q.buf[q.right], frame)
|
2018-11-20 16:39:42 +03:00
|
|
|
q.right += 1
|
|
|
|
q.len += 1
|
2018-11-20 17:21:13 +03:00
|
|
|
return len(frame), nil
|
2018-11-20 16:39:42 +03:00
|
|
|
}
|
|
|
|
|
2018-11-20 17:21:13 +03:00
|
|
|
func (q *Queue) Read() ([]byte, error) {
|
|
|
|
if q.left >= q.right {
|
|
|
|
return nil, errors.New("Nothing to read from queue.")
|
|
|
|
}
|
2018-11-20 16:39:42 +03:00
|
|
|
q.left += 1
|
|
|
|
q.len -= 1
|
2018-11-20 17:21:13 +03:00
|
|
|
return q.buf[q.left-1], nil
|
2018-11-20 16:39:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Len() uint {
|
|
|
|
return q.len
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queue) Reset() {
|
|
|
|
q.left = 0
|
|
|
|
q.right = 0
|
|
|
|
q.len = 0
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
mtsEncoder *mts.Encoder
|
|
|
|
queue *Queue
|
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-20 16:39:42 +03:00
|
|
|
q := &Queue{}
|
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,
|
|
|
|
mtsEncoder: mts.NewEncoder(q, float64(fps)),
|
|
|
|
queue: q,
|
2018-11-17 10:13:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-17 10:13:04 +03:00
|
|
|
func (e *Encoder) Encode(nalu []byte) error {
|
2018-11-20 16:39:42 +03:00
|
|
|
e.mtsEncoder.Encode(nalu)
|
|
|
|
for e.queue.Len() > 0 {
|
|
|
|
var payload []byte
|
2018-11-20 17:21:13 +03:00
|
|
|
for i := 0; i < maxMtsPerRtp && e.queue.Len() > 0; i++ {
|
|
|
|
data, _ := e.queue.Read()
|
|
|
|
payload = append(payload, data...)
|
2018-11-20 16:39:42 +03:00
|
|
|
}
|
|
|
|
pkt := Pkt{
|
|
|
|
V: rtpVer, // version
|
|
|
|
P: no, // padding
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
_, err := e.dst.Write(pkt.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
e.tick()
|
2018-11-17 10:13:04 +03:00
|
|
|
}
|
2018-11-20 16:39:42 +03:00
|
|
|
e.queue.Reset()
|
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 {
|
|
|
|
e.seqNo += 1
|
|
|
|
return e.seqNo - 1
|
2018-11-17 10:13:04 +03:00
|
|
|
}
|