mirror of
https://github.com/aler9/gortsplib
synced 2025-10-04 14:52:46 +08:00

* 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
109 lines
2.1 KiB
Go
109 lines
2.1 KiB
Go
package format
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/pion/rtp"
|
|
)
|
|
|
|
// Vorbis is a Vorbis format.
|
|
type Vorbis struct {
|
|
PayloadTyp uint8
|
|
SampleRate int
|
|
ChannelCount int
|
|
Configuration []byte
|
|
}
|
|
|
|
// String implements Format.
|
|
func (t *Vorbis) String() string {
|
|
return "Vorbis"
|
|
}
|
|
|
|
// ClockRate implements Format.
|
|
func (t *Vorbis) ClockRate() int {
|
|
return t.SampleRate
|
|
}
|
|
|
|
// PayloadType implements Format.
|
|
func (t *Vorbis) PayloadType() uint8 {
|
|
return t.PayloadTyp
|
|
}
|
|
|
|
func (t *Vorbis) unmarshal(payloadType uint8, clock string, codec string, rtpmap string, fmtp string) error {
|
|
t.PayloadTyp = payloadType
|
|
|
|
tmp := strings.SplitN(clock, "/", 32)
|
|
if len(tmp) != 2 {
|
|
return fmt.Errorf("invalid clock (%v)", clock)
|
|
}
|
|
|
|
sampleRate, err := strconv.ParseInt(tmp[0], 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t.SampleRate = int(sampleRate)
|
|
|
|
channelCount, err := strconv.ParseInt(tmp[1], 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t.ChannelCount = int(channelCount)
|
|
|
|
if fmtp == "" {
|
|
return fmt.Errorf("fmtp attribute is missing")
|
|
}
|
|
|
|
for _, kv := range strings.Split(fmtp, ";") {
|
|
kv = strings.Trim(kv, " ")
|
|
|
|
if len(kv) == 0 {
|
|
continue
|
|
}
|
|
|
|
tmp := strings.SplitN(kv, "=", 2)
|
|
if len(tmp) != 2 {
|
|
return fmt.Errorf("invalid fmtp (%v)", fmtp)
|
|
}
|
|
|
|
if tmp[0] == "configuration" {
|
|
conf, err := base64.StdEncoding.DecodeString(tmp[1])
|
|
if err != nil {
|
|
return fmt.Errorf("invalid AAC config (%v)", tmp[1])
|
|
}
|
|
|
|
t.Configuration = conf
|
|
}
|
|
}
|
|
|
|
if t.Configuration == nil {
|
|
return fmt.Errorf("config is missing (%v)", fmtp)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Marshal implements Format.
|
|
func (t *Vorbis) Marshal() (string, string) {
|
|
return "VORBIS/" + strconv.FormatInt(int64(t.SampleRate), 10) +
|
|
"/" + strconv.FormatInt(int64(t.ChannelCount), 10),
|
|
"configuration=" + base64.StdEncoding.EncodeToString(t.Configuration)
|
|
}
|
|
|
|
// Clone implements Format.
|
|
func (t *Vorbis) Clone() Format {
|
|
return &Vorbis{
|
|
PayloadTyp: t.PayloadTyp,
|
|
SampleRate: t.SampleRate,
|
|
ChannelCount: t.ChannelCount,
|
|
Configuration: t.Configuration,
|
|
}
|
|
}
|
|
|
|
// PTSEqualsDTS implements Format.
|
|
func (t *Vorbis) PTSEqualsDTS(*rtp.Packet) bool {
|
|
return true
|
|
}
|