-
Notifications
You must be signed in to change notification settings - Fork 10
/
dialog_server.go
175 lines (140 loc) · 3.63 KB
/
dialog_server.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
package sipgox
import (
"context"
"fmt"
"strings"
"github.com/emiago/media"
"github.com/emiago/sipgo"
"github.com/emiago/sipgo/sip"
)
type DialogServerSession struct {
*media.MediaSession
*sipgo.DialogServerSession
waitNotify chan error
// onClose used to cleanup internal logic
onClose func()
}
func (d *DialogServerSession) Close() error {
err := d.DialogServerSession.Close()
if d.MediaSession != nil {
d.MediaSession.Close()
}
if d.onClose != nil {
d.onClose()
}
return err
}
// Hangup is alias for Bye
func (d *DialogServerSession) Hangup(ctx context.Context) error {
return d.Bye(ctx)
}
func (d *DialogServerSession) Bye(ctx context.Context) error {
// defer close(d.done)
// defer d.MediaSession.Close()
return d.DialogServerSession.Bye(ctx)
}
// 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()))
d.waitNotify = make(chan error)
tx, err := d.TransactionRequest(ctx, req)
if err != nil {
return err
}
select {
case <-tx.Done():
return tx.Err()
case res := <-tx.Responses():
if res.StatusCode != sip.StatusAccepted {
return sipgo.ErrDialogResponse{
Res: res,
}
}
case <-ctx.Done():
return tx.Cancel()
}
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) Notify(req *sip.Request) error {
if req.CallID().Value() != d.InviteResponse.CallID().Value() {
return sipgo.ErrDialogDoesNotExists
}
if req.Body() == nil {
return fmt.Errorf("no body in notify")
}
if strings.HasPrefix("SIP/2.0 1", string(req.Body())) {
return nil
}
var e error = nil
if !strings.HasPrefix("SIP/2.0 200", string(req.Body())) {
e = fmt.Errorf("not 200 response")
}
select {
case d.waitNotify <- e:
case <-d.Context().Done():
return d.Context().Err()
}
return nil
}
// func (d *DialogServerSession) MediaStream(s MediaStreamer) error {
// return s.MediaStream(d.MediaSession)
// }
func UACRequestBuild(req *sip.Request, lastReq *sip.Request, lastResp *sip.Response) {
from := lastReq.From()
to := lastReq.To()
callid := lastReq.CallID()
if lastResp != nil {
// To normally gets updated with tag
to = lastResp.To()
}
req.AppendHeader(from)
req.AppendHeader(to)
req.AppendHeader(callid)
if cont := lastReq.GetHeader("Contact"); cont != nil {
req.AppendHeader(cont)
}
// This is not clear
// hdrs := res.GetHeaders("Record-Route")
// for i := len(hdrs) - 1; i >= 0; i-- {
// recordRoute := hdrs[i]
// req.AppendHeader(sip.NewHeader("Route", recordRoute.Value()))
// }
}
func UASRequestBuild(req *sip.Request, lastResp *sip.Response) {
// UAS building request from previous sent response has some work
// From and To must be swapped
// Callid and contact hdr is preserved
// Record-Route hdrs become Route hdrs
//
// rest must be filled by client
from := lastResp.From()
to := lastResp.To()
callid := lastResp.CallID()
newFrom := &sip.FromHeader{
DisplayName: to.DisplayName,
Address: to.Address,
Params: to.Params,
}
newTo := &sip.ToHeader{
DisplayName: from.DisplayName,
Address: from.Address,
Params: from.Params,
}
req.AppendHeader(newFrom)
req.AppendHeader(newTo)
req.AppendHeader(callid)
if cont := lastResp.GetHeader("Contact"); cont != nil {
req.AppendHeader(cont)
}
}