-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
dialog_server_session.go
334 lines (279 loc) · 7.74 KB
/
dialog_server_session.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024, Emir Aganovic
package diago
import (
"context"
"errors"
"fmt"
"sync/atomic"
"time"
"github.com/emiago/diago/media"
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
"github.com/rs/zerolog/log"
)
// DialogServerSession represents inbound channel
type DialogServerSession struct {
*sipgo.DialogServerSession
// MediaSession *media.MediaSession
DialogMedia
// mu sync.Mutex We will reuse lock from Media
// lastInvite is actual last invite sent by remote REINVITE
// We do not use sipgo as this needs mutex but also keeping original invite
lastInvite *sip.Request
mediaConf MediaConfig
closed atomic.Uint32
}
func (d *DialogServerSession) Id() string {
return d.ID
}
func (d *DialogServerSession) Close() {
if !d.closed.CompareAndSwap(0, 1) {
return
}
d.DialogMedia.Close()
d.DialogServerSession.Close()
}
func (d *DialogServerSession) FromUser() string {
return d.InviteRequest.From().Address.User
}
// User that was dialed
func (d *DialogServerSession) ToUser() string {
return d.InviteRequest.To().Address.User
}
func (d *DialogServerSession) Transport() string {
return d.InviteRequest.Transport()
}
func (d *DialogServerSession) Progress() error {
return d.Respond(sip.StatusTrying, "Trying", nil)
}
func (d *DialogServerSession) Ringing() error {
return d.Respond(sip.StatusRinging, "Ringing", nil)
}
func (d *DialogServerSession) DialogSIP() *sipgo.Dialog {
return &d.Dialog
}
func (d *DialogServerSession) RemoteContact() *sip.ContactHeader {
d.mu.Lock()
defer d.mu.Unlock()
if d.lastInvite != nil {
return d.lastInvite.Contact()
}
return d.InviteRequest.Contact()
}
func (d *DialogServerSession) Respond(statusCode sip.StatusCode, reason string, body []byte, headers ...sip.Header) error {
return d.DialogServerSession.Respond(statusCode, reason, body, headers...)
}
func (d *DialogServerSession) RespondSDP(body []byte) error {
headers := []sip.Header{sip.NewHeader("Content-Type", "application/sdp")}
return d.DialogServerSession.Respond(200, "OK", body, headers...)
}
// Answer creates media session and answers
// NOTE: Not final API
func (d *DialogServerSession) Answer() error {
sess, err := d.createMediaSessionConf(d.mediaConf)
if err != nil {
return err
}
rtpSess := media.NewRTPSession(sess)
return d.answerSession(rtpSess)
}
type AnswerOptions struct {
OnRTPSession func(rtpSess *media.RTPSession)
}
// Experimental
//
// NOTE: API may change
func (d *DialogServerSession) AnswerOptions(opt AnswerOptions) error {
sess, err := d.createMediaSessionConf(d.mediaConf)
if err != nil {
return err
}
rtpSess := media.NewRTPSession(sess)
if opt.OnRTPSession != nil {
opt.OnRTPSession(rtpSess)
}
return d.answerSession(rtpSess)
}
// answerSession. It allows answering with custom RTP Session.
// NOTE: Not final API
func (d *DialogServerSession) answerSession(rtpSess *media.RTPSession) error {
sess := rtpSess.Sess
sdp := d.InviteRequest.Body()
if sdp == nil {
return fmt.Errorf("no sdp present in INVITE")
}
if err := sess.RemoteSDP(sdp); err != nil {
return err
}
if err := d.RespondSDP(sess.LocalSDP()); err != nil {
return err
}
d.mu.Lock()
d.initRTPSessionUnsafe(sess, rtpSess)
// Close RTP session
d.onCloseUnsafe(func() {
if err := rtpSess.Close(); err != nil {
log.Error().Err(err).Msg("Closing session")
}
})
d.mu.Unlock()
// Must be called after media and reader writer is setup
rtpSess.MonitorBackground()
// Wait ACK
// If we do not wait ACK, hanguping call will fail as ACK can be delayed when we are doing Hangup
for {
select {
case <-time.After(10 * time.Second):
return fmt.Errorf("no ACK received")
case state := <-d.StateRead():
if state == sip.DialogStateConfirmed {
return nil
}
if state == sip.DialogStateEnded {
return fmt.Errorf("dialog ended on ack")
}
}
}
}
// AnswerLate does answer with Late offer.
func (d *DialogServerSession) AnswerLate() error {
sess, err := d.createMediaSessionConf(d.mediaConf)
if err != nil {
return err
}
rtpSess := media.NewRTPSession(sess)
localSDP := sess.LocalSDP()
d.mu.Lock()
d.initRTPSessionUnsafe(sess, rtpSess)
// Close RTP session
d.onCloseUnsafe(func() {
if err := rtpSess.Close(); err != nil {
log.Error().Err(err).Msg("Closing session")
}
})
d.mu.Unlock()
if err := d.RespondSDP(localSDP); err != nil {
return err
}
// Wait ACK
// It is expected that on ReadACK SDP will be updated
for {
select {
case <-time.After(10 * time.Second):
return fmt.Errorf("no ACK received")
case state := <-d.StateRead():
if state == sip.DialogStateConfirmed {
// Must be called after media and reader writer is setup
return rtpSess.MonitorBackground()
}
if state == sip.DialogStateEnded {
return fmt.Errorf("dialog ended on ack")
}
}
}
}
func (d *DialogServerSession) ReadAck(req *sip.Request, tx sip.ServerTransaction) error {
// Check do we have some session
err := func() error {
d.mu.Lock()
defer d.mu.Unlock()
sess := d.mediaSession
if sess == nil {
return nil
}
contentType := req.ContentType()
if contentType == nil {
return nil
}
body := req.Body()
if body != nil && contentType.Value() == "application/sdp" {
// This is Late offer response
if err := sess.RemoteSDP(body); err != nil {
return err
}
}
return nil
}()
if err != nil {
e := d.Hangup(d.Context())
return errors.Join(err, e)
}
return d.DialogServerSession.ReadAck(req, tx)
}
func (d *DialogServerSession) Hangup(ctx context.Context) error {
state := d.LoadState()
if state == sip.DialogStateConfirmed {
return d.Bye(ctx)
}
return d.Respond(sip.StatusTemporarilyUnavailable, "Temporarly unavailable", nil)
}
func (d *DialogServerSession) ReInvite(ctx context.Context) error {
sdp := d.mediaSession.LocalSDP()
contact := d.RemoteContact()
req := sip.NewRequest(sip.INVITE, contact.Address)
req.SetBody(sdp)
res, err := d.Do(ctx, req)
if err != nil {
return err
}
if !res.IsSuccess() {
return sipgo.ErrDialogResponse{
Res: res,
}
}
return nil
}
// Refer tries todo refer (blind transfer) on call
func (d *DialogServerSession) Refer(ctx context.Context, referTo sip.Uri) error {
// TODO check state of call
req := sip.NewRequest(sip.REFER, d.InviteRequest.Contact().Address)
// UASRequestBuild(req, d.InviteResponse)
// Invite request tags must be preserved but switched
req.AppendHeader(sip.NewHeader("Refer-to", referTo.String()))
res, err := d.Do(ctx, req)
if err != nil {
return err
}
if res.StatusCode != sip.StatusAccepted {
return sipgo.ErrDialogResponse{
Res: res,
}
}
// d.waitNotify = make(chan error)
return d.Hangup(ctx)
// // There is now implicit subscription
// select {
// case e := <-d.waitNotify:
// return e
// case <-d.Context().Done():
// return d.Context().Err()
// }
}
func (d *DialogServerSession) handleReInvite(req *sip.Request, tx sip.ServerTransaction) {
if err := d.ReadRequest(req, tx); err != nil {
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusBadRequest, err.Error(), nil))
return
}
d.mu.Lock()
defer d.mu.Unlock()
d.lastInvite = req
if err := d.sdpReInviteUnsafe(req.Body()); err != nil {
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusRequestTerminated, err.Error(), nil))
return
}
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusOK, "OK", nil))
}
func (d *DialogServerSession) readSIPInfoDTMF(req *sip.Request, tx sip.ServerTransaction) {
tx.Respond(sip.NewResponseFromRequest(req, sip.StatusNotAcceptable, "Not Acceptable", nil))
// if err := d.ReadRequest(req, tx); err != nil {
// tx.Respond(sip.NewResponseFromRequest(req, sip.StatusBadRequest, "Bad Request", nil))
// return
// }
// Parse this
//Signal=1
// Duration=160
// reader := bytes.NewReader(req.Body())
// for {
// }
}