Files
gortsplib/pkg/rtpcodecs/rtplpcm/decoder.go
Alessandro Ros a1396206b5 convert Tracks into Medias and Formats (#155)
* split tracks from medias

* move tracks into dedicated package

* move media into dedicated package

* edit Medias.Marshal() in order to return SDP

* add medias.Find() and simplify examples

* improve coverage

* fix rebase errors

* replace TrackIDs with MediaIDs

* implement media-specific and track-specific callbacks for reading RTCP and RTP packets

* rename publish into record, read into play

* add v2 tag

* rename tracks into formats
2022-12-11 22:03:22 +01:00

38 lines
810 B
Go

package rtplpcm
import (
"fmt"
"time"
"github.com/pion/rtp"
"github.com/aler9/gortsplib/v2/pkg/rtptimedec"
)
// Decoder is a RTP/LPCM decoder.
type Decoder struct {
BitDepth int
SampleRate int
ChannelCount int
timeDecoder *rtptimedec.Decoder
sampleSize int
}
// Init initializes the decoder.
func (d *Decoder) Init() {
d.timeDecoder = rtptimedec.New(d.SampleRate)
d.sampleSize = d.BitDepth * d.ChannelCount / 8
}
// Decode decodes audio samples from a RTP packet.
// It returns audio samples and PTS of the first sample.
func (d *Decoder) Decode(pkt *rtp.Packet) ([]byte, time.Duration, error) {
plen := len(pkt.Payload)
if (plen % d.sampleSize) != 0 {
return nil, 0, fmt.Errorf("received payload of wrong size")
}
return pkt.Payload, d.timeDecoder.Decode(pkt.Timestamp), nil
}