mirror of https://bitbucket.org/ausocean/av.git
rtp: finished encoder file - wrote encode function, wrote timestamp function and sequence number function - need to test
This commit is contained in:
parent
ddf25e1fbe
commit
5cc35a77a5
|
@ -29,51 +29,52 @@ package rtp
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Time related constants.
|
|
||||||
const (
|
const (
|
||||||
rtpVer = 2
|
yes = 1
|
||||||
yes = 1
|
no = 0
|
||||||
no = 0
|
defaultPktType = 1
|
||||||
ccCount = 0
|
timestampFreq = 90000 // Hz
|
||||||
// pcrFreq is the base Program Clock Reference frequency.
|
|
||||||
timestampFreq = 90000 // Hz
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Encoder struct {
|
type Encoder struct {
|
||||||
dst io.Writer
|
dst io.Writer
|
||||||
|
ssrc uint32
|
||||||
|
seqNo uint16
|
||||||
clock time.Duration
|
clock time.Duration
|
||||||
frameInterval time.Duration
|
frameInterval time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewEncoder returns an Encoder with the specified frame rate.
|
// NewEncoder returns a new Encoder type given an io.Writer - the destination
|
||||||
|
// after encoding and the desired fps
|
||||||
func NewEncoder(dst io.Writer, fps float64) *Encoder {
|
func NewEncoder(dst io.Writer, fps float64) *Encoder {
|
||||||
return &Encoder{
|
return &Encoder{
|
||||||
dst: dst,
|
dst: dst,
|
||||||
|
ssrc: rand.Uint32(),
|
||||||
frameInterval: time.Duration(float64(time.Second) / fps),
|
frameInterval: time.Duration(float64(time.Second) / fps),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Encode takes a nalu unit and encodes it into an rtp packet and
|
||||||
|
// writes to the io.Writer given in NewEncoder
|
||||||
func (e *Encoder) Encode(nalu []byte) error {
|
func (e *Encoder) Encode(nalu []byte) error {
|
||||||
pkt := Pkt{
|
pkt := Pkt{
|
||||||
Pkt{
|
V: rtpVer, // version
|
||||||
V: rtpVer, // version
|
P: no, // padding
|
||||||
P: no, // padding
|
X: no, // header extension
|
||||||
X: no, // header extension
|
CC: no, // CSRC count
|
||||||
CC: ccCount,
|
M: no, // NOTE: need to check if this works (decoders should ignore this)
|
||||||
M: no, // NOTE: need to check if this works (decoders should ignore this)
|
PT: defaultPktType, // NOTE: 1-23 according to rtp-h264 specs (don't think we need this)
|
||||||
PT: 6,
|
SN: e.nxtSeqNo(), // sequence number
|
||||||
SN: 167,
|
TS: e.nxtTimestamp(), // timestamp
|
||||||
TS: 160,
|
SSRC: e.ssrc, // source identifier
|
||||||
SSRC: 10,
|
Payload: nalu,
|
||||||
Payload: []byte{0x00, 0x01, 0x07, 0xf0, 0x56, 0x37, 0x0a, 0x0f},
|
Padding: no,
|
||||||
Padding: 0,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
// Write rtp packet to
|
|
||||||
_, err := e.dst.Write(pkt.Bytes())
|
_, err := e.dst.Write(pkt.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -89,15 +90,13 @@ func (e *Encoder) tick() {
|
||||||
e.clock += e.frameInterval
|
e.clock += e.frameInterval
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: alter and make this work for rtp
|
// nxtTimestamp gets the next timestamp
|
||||||
func (e *Encoder) pts() uint64 {
|
func (e *Encoder) nxtTimestamp() uint32 {
|
||||||
return uint64((e.clock + e.ptsOffset).Seconds() * pcrFreq)
|
return uint32(e.clock.Seconds() * timestampFreq)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: alter and apply this to rtp for sequence number
|
// nxtSeqNo gets the next rtp packet sequence number
|
||||||
func (e *Encoder) ccFor(pid int) byte {
|
func (e *Encoder) nxtSeqNo() uint16 {
|
||||||
cc := e.continuity[pid]
|
e.seqNo += 1
|
||||||
const continuityCounterMask = 0xf
|
return e.seqNo - 1
|
||||||
e.continuity[pid] = (cc + 1) & continuityCounterMask
|
|
||||||
return cc
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,9 +41,9 @@ type Pkt struct {
|
||||||
CC byte // CSRC count
|
CC byte // CSRC count
|
||||||
M byte // Marker bit
|
M byte // Marker bit
|
||||||
PT byte // Packet type
|
PT byte // Packet type
|
||||||
SN int16 // Synch number
|
SN uint16 // Synch number
|
||||||
TS int32 // Timestamp
|
TS uint32 // Timestamp
|
||||||
SSRC int32 // Synchronisation source identifier
|
SSRC uint32 // Synchronisation source identifier
|
||||||
Payload []byte // H264 Payload data
|
Payload []byte // H264 Payload data
|
||||||
Padding int // No of bytes of padding
|
Padding int // No of bytes of padding
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue