forked from emiago/diago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog_server_session.go
235 lines (188 loc) · 5.51 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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024, Emir Aganovic
package diago
import (
"context"
"fmt"
"sync/atomic"
"time"
"github.com/emiago/diago/media"
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
)
// 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
contactHDR sip.ContactHeader
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 {
// TODO fix this on dialog srv
headers = append(headers, &d.contactHDR)
return d.DialogServerSession.Respond(statusCode, reason, body, headers...)
}
func (d *DialogServerSession) RespondSDP(body []byte) error {
headers := []sip.Header{sip.NewHeader("Content-Type", "application/sdp")}
headers = append(headers, &d.contactHDR)
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.createMediaSession()
if err != nil {
return err
}
rtpSess := media.NewRTPSession(sess)
d.rtpSess = 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.InitMediaSession(
sess,
media.NewRTPPacketReaderSession(rtpSess),
media.NewRTPPacketWriterSession(rtpSess),
)
// 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
}
}
}
}
func (d *DialogServerSession) Hangup(ctx context.Context) error {
return d.Bye(ctx)
}
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 {
// }
}