-
Notifications
You must be signed in to change notification settings - Fork 119
/
streamersession.go
156 lines (115 loc) · 4 KB
/
streamersession.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package mediaserver
import (
"fmt"
"strings"
"github.com/gofrs/uuid"
"github.com/notedit/sdp"
native "github.com/notedit/media-server-go/wrapper"
)
// StreamerSession represent a rtp session
type StreamerSession struct {
id string
local bool
port int
ip string
incoming *IncomingStreamTrack
outgoing *OutgoingStreamTrack
session native.RTPSessionFacade
onStopListeners []func()
}
// NewStreamerSession new StreamerSession with auto selectd port
func NewStreamerSession(media *sdp.MediaInfo) *StreamerSession {
streamerSession := &StreamerSession{}
var mediaType native.MediaFrameType = 0
if strings.ToLower(media.GetType()) == "video" {
mediaType = 1
}
session := native.NewRTPSessionFacade(mediaType)
streamerSession.id = uuid.Must(uuid.NewV4()).String()
properties := native.NewPropertiesFacade()
if media != nil {
num := 0
for _, codec := range media.GetCodecs() {
item := fmt.Sprintf("codecs.%d", num)
properties.SetPropertyStr(item+".codec", codec.GetCodec())
properties.SetPropertyInt(item+".pt", codec.GetType())
if codec.HasRTX() {
properties.SetPropertyInt(item+".rtx", codec.GetRTX())
}
num = num + 1
}
properties.SetPropertyInt("codecs.length", num)
}
session.Init(properties)
native.DeletePropertiesFacade(properties)
streamerSession.session = session
streamerSession.incoming = NewIncomingStreamTrack(media.GetType(), media.GetType(), native.SessionToReceiver(session), map[string]native.RTPIncomingSourceGroup{"": session.GetIncomingSourceGroup()})
streamerSession.outgoing = newOutgoingStreamTrack(media.GetType(), media.GetType(), native.SessionToSender(session), session.GetOutgoingSourceGroup())
streamerSession.onStopListeners = make([]func(), 0)
return streamerSession
}
// NewStreamerSessionWithLocalPort create streamer session with pre selected port
func NewStreamerSessionWithLocalPort(port int, media *sdp.MediaInfo) *StreamerSession {
streamerSession := &StreamerSession{}
var mediaType native.MediaFrameType = 0
if strings.ToLower(media.GetType()) == "video" {
mediaType = 1
}
session := native.NewRTPSessionFacade(mediaType)
streamerSession.id = uuid.Must(uuid.NewV4()).String()
properties := native.NewPropertiesFacade()
if media != nil {
num := 0
for _, codec := range media.GetCodecs() {
item := fmt.Sprintf("codecs.%d", num)
properties.SetPropertyStr(item+".codec", codec.GetCodec())
properties.SetPropertyInt(item+".pt", codec.GetType())
if codec.HasRTX() {
properties.SetPropertyInt(item+".rtx", codec.GetRTX())
}
num = num + 1
}
properties.SetPropertyInt("codecs.length", num)
}
session.SetLocalPort(port)
session.Init(properties)
native.DeletePropertiesFacade(properties)
streamerSession.session = session
streamerSession.incoming = NewIncomingStreamTrack(media.GetType(), media.GetType(), native.SessionToReceiver(session), map[string]native.RTPIncomingSourceGroup{"": session.GetIncomingSourceGroup()})
streamerSession.outgoing = newOutgoingStreamTrack(media.GetType(), media.GetType(), native.SessionToSender(session), session.GetOutgoingSourceGroup())
streamerSession.onStopListeners = make([]func(), 0)
return streamerSession
}
// GetID get id
func (s *StreamerSession) GetID() string {
return s.id
}
func (s *StreamerSession) GetLocalPort() int {
return s.session.GetLocalPort()
}
func (s *StreamerSession) SetRemotePort(ip string, port int) {
s.session.SetRemotePort(ip, port)
}
// GetIncomingStreamTrack get asso incoming track,
func (s *StreamerSession) GetIncomingStreamTrack() *IncomingStreamTrack {
return s.incoming
}
// GetOutgoingStreamTrack get asso outgoing track,
func (s *StreamerSession) GetOutgoingStreamTrack() *OutgoingStreamTrack {
return s.outgoing
}
// Stop it
func (s *StreamerSession) Stop() {
if s.session == nil {
return
}
if s.incoming != nil {
s.incoming.Stop()
}
if s.outgoing != nil {
s.outgoing.Stop()
}
s.session.End()
native.DeleteRTPSessionFacade(s.session)
s.session = nil
}