forked from bluenviron/gortsplib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracks.go
92 lines (76 loc) · 1.93 KB
/
tracks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gortsplib
import (
"fmt"
"strconv"
"strings"
psdp "github.com/pion/sdp/v3"
"github.com/Coimbra1984/gortsplib/pkg/sdp"
)
// Tracks is a list of tracks.
type Tracks []Track
// ReadTracks decodes tracks from the SDP format.
func ReadTracks(byts []byte, skipGenericTracksWithoutClockRate bool) (Tracks, error) {
var sd sdp.SessionDescription
err := sd.Unmarshal(byts)
if err != nil {
return nil, err
}
var tracks Tracks //nolint:prealloc
for i, md := range sd.MediaDescriptions {
t, err := newTrackFromMediaDescription(md)
if err != nil {
if skipGenericTracksWithoutClockRate &&
strings.HasPrefix(err.Error(), "unable to get clock rate") {
continue
}
return nil, fmt.Errorf("unable to parse track %d: %s", i+1, err)
}
tracks = append(tracks, t)
}
if len(tracks) == 0 {
return nil, fmt.Errorf("no valid tracks found")
}
return tracks, nil
}
func (ts Tracks) clone() Tracks {
ret := make(Tracks, len(ts))
for i, track := range ts {
ret[i] = track.clone()
}
return ret
}
func (ts Tracks) setControls() {
for i, t := range ts {
t.SetControl("trackID=" + strconv.FormatInt(int64(i), 10))
}
}
// Write encodes tracks in the SDP format.
func (ts Tracks) Write(multicast bool) []byte {
address := "0.0.0.0"
if multicast {
address = "224.1.0.0"
}
sout := &sdp.SessionDescription{
SessionName: psdp.SessionName("Stream"),
Origin: psdp.Origin{
Username: "-",
NetworkType: "IN",
AddressType: "IP4",
UnicastAddress: "127.0.0.1",
},
// required by Darwin Streaming Server
ConnectionInformation: &psdp.ConnectionInformation{
NetworkType: "IN",
AddressType: "IP4",
Address: &psdp.Address{Address: address},
},
TimeDescriptions: []psdp.TimeDescription{
{Timing: psdp.Timing{0, 0}}, //nolint:govet
},
}
for _, track := range ts {
sout.MediaDescriptions = append(sout.MediaDescriptions, track.MediaDescription())
}
byts, _ := sout.Marshal()
return byts
}