-
Notifications
You must be signed in to change notification settings - Fork 180
/
medias.go
84 lines (71 loc) · 1.86 KB
/
medias.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
package media
import (
"fmt"
"github.com/google/uuid"
psdp "github.com/pion/sdp/v3"
"github.com/aler9/gortsplib/v2/pkg/sdp"
)
// Medias is a list of media streams.
type Medias []*Media
// Unmarshal decodes medias from the SDP format.
func (ms *Medias) Unmarshal(mds []*psdp.MediaDescription) error {
*ms = make(Medias, len(mds))
for i, md := range mds {
var m Media
err := m.unmarshal(md)
if err != nil {
return fmt.Errorf("media %d is invalid: %v", i+1, err)
}
(*ms)[i] = &m
}
return nil
}
// Marshal encodes the medias in SDP format.
func (ms Medias) Marshal(multicast bool) *sdp.SessionDescription {
var address string
if multicast {
address = "224.1.0.0"
} else {
address = "0.0.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{StartTime: 0, StopTime: 0}},
},
MediaDescriptions: make([]*psdp.MediaDescription, len(ms)),
}
for i, media := range ms {
sout.MediaDescriptions[i] = media.Marshal()
}
return sout
}
// SetControls sets the control attribute of all medias in the list.
func (ms Medias) SetControls() {
for _, media := range ms {
media.Control = "mediaUUID=" + uuid.New().String()
}
}
// FindFormat finds a certain format among all the formats in all the medias.
// If the format is found, it is inserted into forma, and its media is returned.
func (ms Medias) FindFormat(forma interface{}) *Media {
for _, media := range ms {
ok := media.FindFormat(forma)
if ok {
return media
}
}
return nil
}