/
channel.go
502 lines (404 loc) · 14.4 KB
/
channel.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package native
import (
"errors"
"fmt"
"time"
"github.com/CyCoreSystems/ari/v6"
"github.com/CyCoreSystems/ari/v6/rid"
)
// Channel provides the ARI Channel accessors for the native client
type Channel struct {
client *Client
}
// List lists the current channels and returns the list of channel handles
func (c *Channel) List(filter *ari.Key) (cx []*ari.Key, err error) {
channels := []struct {
ID string `json:"id"`
}{}
if filter == nil {
filter = ari.NewKey(ari.ChannelKey, "")
}
err = c.client.get("/channels", &channels)
for _, i := range channels {
k := c.client.stamp(ari.NewKey(ari.ChannelKey, i.ID))
if filter.Match(k) {
cx = append(cx, k)
}
}
return
}
// Hangup hangs up the given channel using the (optional) reason.
func (c *Channel) Hangup(key *ari.Key, reason string) error {
if key == nil || key.ID == "" {
return errors.New("channel key not supplied")
}
if reason == "" {
reason = "normal"
}
var req string
if reason != "" {
req = fmt.Sprintf("reason=%s", reason)
}
return c.client.del("/channels/"+key.ID, nil, req)
}
// Data retrieves the current state of the channel
func (c *Channel) Data(key *ari.Key) (*ari.ChannelData, error) {
if key == nil || key.ID == "" {
return nil, errors.New("channel key not supplied")
}
data := new(ari.ChannelData)
if err := c.client.get("/channels/"+key.ID, data); err != nil {
return nil, dataGetError(err, "channel", "%v", key.ID)
}
data.Key = c.client.stamp(key)
return data, nil
}
// Get gets the lazy handle for the given channel
func (c *Channel) Get(key *ari.Key) *ari.ChannelHandle {
return ari.NewChannelHandle(c.client.stamp(key), c, nil)
}
// Originate originates a channel and returns the handle.
//
// **Note** that referenceKey is completely optional. It is used for placing
// the new channel onto the correct Asterisk node and for assigning default
// values for communications parameters such as codecs.
func (c *Channel) Originate(referenceKey *ari.Key, req ari.OriginateRequest) (*ari.ChannelHandle, error) {
h, err := c.StageOriginate(referenceKey, req)
if err != nil {
return nil, err
}
return h, h.Exec()
}
// StageOriginate creates a new channel handle with a channel originate request
// staged.
//
// **Note** that referenceKey is completely optional. It is used for placing
// the new channel onto the correct Asterisk node and for assigning default
// values for communications parameters such as codecs.
func (c *Channel) StageOriginate(referenceKey *ari.Key, req ari.OriginateRequest) (*ari.ChannelHandle, error) {
if referenceKey != nil && req.Originator == "" && referenceKey.Kind == ari.ChannelKey {
req.Originator = referenceKey.ID
}
if req.ChannelID == "" {
req.ChannelID = rid.New(rid.Channel)
}
return ari.NewChannelHandle(c.client.stamp(ari.NewKey(ari.ChannelKey, req.ChannelID)), c,
func(ch *ari.ChannelHandle) error {
type response struct {
ID string `json:"id"`
}
var resp response
return c.client.post("/channels", &resp, &req)
},
), nil
}
// Create creates a channel and returns the handle. TODO: expand
// differences between originate and create.
func (c *Channel) Create(key *ari.Key, req ari.ChannelCreateRequest) (*ari.ChannelHandle, error) {
if key != nil && req.Originator == "" && key.Kind == ari.ChannelKey {
req.Originator = key.ID
}
if req.ChannelID == "" {
req.ChannelID = rid.New(rid.Channel)
}
err := c.client.post("/channels/create", nil, &req)
if err != nil {
return nil, err
}
return ari.NewChannelHandle(c.client.stamp(ari.NewKey(ari.ChannelKey, req.ChannelID)), c, nil), nil
}
// Continue tells a channel to process to the given ARI context and extension
func (c *Channel) Continue(key *ari.Key, context, extension string, priority int) (err error) {
req := struct {
Context string `json:"context"`
Extension string `json:"extension"`
Priority int `json:"priority"`
}{
Context: context,
Extension: extension,
Priority: priority,
}
return c.client.post("/channels/"+key.ID+"/continue", nil, &req)
}
// Move moves the channel to another stasis application
func (c *Channel) Move(key *ari.Key, app string, appArgs string) error {
req := struct {
App string `json:"app"`
AppArgs string `json:"appArgs"`
}{
App: app,
AppArgs: appArgs,
}
return c.client.post("/channels/"+key.ID+"/move", nil, &req)
}
// Busy sends the busy status code to the channel (TODO: does this play a busy signal too)
func (c *Channel) Busy(key *ari.Key) error {
return c.Hangup(key, "busy")
}
// Congestion sends the congestion status code to the channel (TODO: does this play a tone?)
func (c *Channel) Congestion(key *ari.Key) error {
return c.Hangup(key, "congestion")
}
// Answer answers a channel, if ringing (TODO: does this return an error if already answered?)
func (c *Channel) Answer(key *ari.Key) error {
return c.client.post("/channels/"+key.ID+"/answer", nil, nil)
}
// Ring causes a channel to start ringing (TODO: does this return an error if already ringing?)
func (c *Channel) Ring(key *ari.Key) error {
return c.client.post("/channels/"+key.ID+"/ring", nil, nil)
}
// StopRing causes a channel to stop ringing (TODO: does this return an error if not ringing?)
func (c *Channel) StopRing(key *ari.Key) error {
return c.client.del("/channels/"+key.ID+"/ring", nil, "")
}
// Hold puts a channel on hold (TODO: does this return an error if already on hold?)
func (c *Channel) Hold(key *ari.Key) error {
return c.client.post("/channels/"+key.ID+"/hold", nil, nil)
}
// StopHold removes a channel from hold (TODO: does this return an error if not on hold)
func (c *Channel) StopHold(key *ari.Key) (err error) {
return c.client.del("/channels/"+key.ID+"/hold", nil, "")
}
// Mute mutes a channel in the given direction (TODO: does this return an error if already muted)
func (c *Channel) Mute(key *ari.Key, dir ari.Direction) error {
if dir == "" {
dir = ari.DirectionBoth
}
req := struct {
Direction ari.Direction `json:"direction,omitempty"`
}{
Direction: dir,
}
return c.client.post("/channels/"+key.ID+"/mute", nil, &req)
}
// Unmute unmutes a channel in the given direction (TODO: does this return an error if unmuted)
func (c *Channel) Unmute(key *ari.Key, dir ari.Direction) (err error) {
if dir == "" {
dir = ari.DirectionBoth
}
req := fmt.Sprintf("direction=%s", dir)
return c.client.del("/channels/"+key.ID+"/mute", nil, req)
}
// SendDTMF sends a string of digits and symbols to the channel
func (c *Channel) SendDTMF(key *ari.Key, dtmf string, opts *ari.DTMFOptions) error {
if opts == nil {
opts = &ari.DTMFOptions{}
}
if opts.Duration < 1 {
opts.Duration = 100 // ARI default, for documenation
}
if opts.Between < 1 {
opts.Between = 100 // ARI default, for documentation
}
req := struct {
Dtmf string `json:"dtmf,omitempty"`
Before int `json:"before,omitempty"`
Between int `json:"between,omitempty"`
Duration int `json:"duration,omitempty"`
After int `json:"after,omitempty"`
}{
Dtmf: dtmf,
Before: int(opts.Before / time.Millisecond),
After: int(opts.After / time.Millisecond),
Duration: int(opts.Duration / time.Millisecond),
Between: int(opts.Between / time.Millisecond),
}
return c.client.post("/channels/"+key.ID+"/dtmf", nil, &req)
}
// MOH plays the given music on hold class to the channel TODO: does this error when already playing MOH?
func (c *Channel) MOH(key *ari.Key, class string) error {
req := struct {
Class string `json:"mohClass,omitempty"`
}{
Class: class,
}
return c.client.post("/channels/"+key.ID+"/moh", nil, &req)
}
// StopMOH stops any music on hold playing on the channel (TODO: does this error when no MOH is playing?)
func (c *Channel) StopMOH(key *ari.Key) error {
return c.client.del("/channels/"+key.ID+"/moh", nil, "")
}
// Silence silences a channel (TODO: does this error when already silences)
func (c *Channel) Silence(key *ari.Key) error {
return c.client.post("/channels/"+key.ID+"/silence", nil, nil)
}
// StopSilence stops the silence on a channel (TODO: does this error when not silenced)
func (c *Channel) StopSilence(key *ari.Key) error {
return c.client.del("/channels/"+key.ID+"/silence", nil, "")
}
// Play plays the given media URI on the channel, using the playbackID as
// the identifier of the created ARI Playback entity
func (c *Channel) Play(key *ari.Key, playbackID string, mediaURI ...string) (*ari.PlaybackHandle, error) {
if playbackID == "" {
playbackID = rid.New(rid.Playback)
}
h, err := c.StagePlay(key, playbackID, mediaURI...)
if err != nil {
return nil, err
}
return h, h.Exec()
}
// StagePlay stages a `Play` operation on the bridge
func (c *Channel) StagePlay(key *ari.Key, playbackID string, mediaURI ...string) (*ari.PlaybackHandle, error) {
if playbackID == "" {
playbackID = rid.New(rid.Playback)
}
resp := make(map[string]interface{})
req := struct {
Media []string `json:"media"`
}{
Media: mediaURI,
}
playbackKey := c.client.stamp(ari.NewKey(ari.PlaybackKey, playbackID))
return ari.NewPlaybackHandle(playbackKey, c.client.Playback(), func(pb *ari.PlaybackHandle) error {
return c.client.post("/channels/"+key.ID+"/play/"+playbackID, &resp, &req)
}), nil
}
// Record records audio on the channel, using the name parameter as the name of the
// created LiveRecording entity.
func (c *Channel) Record(key *ari.Key, name string, opts *ari.RecordingOptions) (*ari.LiveRecordingHandle, error) {
h, err := c.StageRecord(key, name, opts)
if err != nil {
return nil, err
}
return h, h.Exec()
}
// StageRecord stages a `Record` opreation
func (c *Channel) StageRecord(key *ari.Key, name string, opts *ari.RecordingOptions) (*ari.LiveRecordingHandle, error) {
if opts == nil {
opts = &ari.RecordingOptions{}
}
resp := make(map[string]interface{})
req := struct {
Name string `json:"name"`
Format string `json:"format"`
MaxDuration int `json:"maxDurationSeconds"`
MaxSilence int `json:"maxSilenceSeconds"`
IfExists string `json:"ifExists,omitempty"`
Beep bool `json:"beep"`
TerminateOn string `json:"terminateOn,omitempty"`
}{
Name: name,
Format: opts.Format,
MaxDuration: int(opts.MaxDuration / time.Second),
MaxSilence: int(opts.MaxSilence / time.Second),
IfExists: opts.Exists,
Beep: opts.Beep,
TerminateOn: opts.Terminate,
}
recordingKey := c.client.stamp(ari.NewKey(ari.LiveRecordingKey, name))
return ari.NewLiveRecordingHandle(recordingKey, c.client.LiveRecording(), func(h *ari.LiveRecordingHandle) error {
return c.client.post("/channels/"+key.ID+"/record", &resp, &req)
}), nil
}
// Snoop snoops on a channel, using the the given snoopID as the new channel handle ID (TODO: confirm and expand description)
func (c *Channel) Snoop(key *ari.Key, snoopID string, opts *ari.SnoopOptions) (*ari.ChannelHandle, error) {
h, err := c.StageSnoop(key, snoopID, opts)
if err != nil {
return nil, err
}
return h, h.Exec()
}
// StageSnoop creates a new `ChannelHandle` with a `Snoop` operation staged.
func (c *Channel) StageSnoop(key *ari.Key, snoopID string, opts *ari.SnoopOptions) (*ari.ChannelHandle, error) {
if opts == nil {
opts = &ari.SnoopOptions{App: c.client.ApplicationName()}
}
if snoopID == "" {
snoopID = rid.New(rid.Snoop)
}
// Create the snooping channel's key
k := c.client.stamp(ari.NewKey(ari.ChannelKey, snoopID))
return ari.NewChannelHandle(k, c, func(ch *ari.ChannelHandle) error {
return c.client.post("/channels/"+key.ID+"/snoop/"+snoopID, nil, &opts)
}), nil
}
// ExternalMedia implements the ari.Channel interface
func (c *Channel) ExternalMedia(key *ari.Key, opts ari.ExternalMediaOptions) (*ari.ChannelHandle, error) {
h, err := c.StageExternalMedia(key, opts)
if err != nil {
return nil, err
}
return h, h.Exec()
}
// StageExternalMedia implements the ari.Channel interface
func (c *Channel) StageExternalMedia(key *ari.Key, opts ari.ExternalMediaOptions) (*ari.ChannelHandle, error) {
if opts.ChannelID == "" {
opts.ChannelID = rid.New(rid.Channel)
}
if opts.App == "" {
opts.App = c.client.ApplicationName()
}
if opts.ExternalHost == "" {
return nil, errors.New("ExternalHost is mandatory")
}
if opts.Encapsulation == "" {
opts.Encapsulation = "rtp"
}
if opts.Transport == "" {
opts.Transport = "udp"
}
if opts.ConnectionType == "" {
opts.ConnectionType = "client"
}
if opts.Format == "" {
return nil, errors.New("Format is mandatory")
}
if opts.Direction == "" {
opts.Direction = "both"
}
// Create the snooping channel's key
k := c.client.stamp(ari.NewKey(ari.ChannelKey, opts.ChannelID))
return ari.NewChannelHandle(k, c, func(ch *ari.ChannelHandle) error {
return c.client.post("/channels/externalMedia", nil, &opts)
}), nil
}
// Dial dials the given calling channel identifier
func (c *Channel) Dial(key *ari.Key, callingChannelID string, timeout time.Duration) error {
req := struct {
// Caller is the (optional) channel ID of another channel to which media negotiations for the newly-dialed channel will be associated.
Caller string `json:"caller,omitempty"`
// Timeout is the maximum amount of time to allow for the dial to complete.
Timeout int `json:"timeout"`
}{
Caller: callingChannelID,
Timeout: int(timeout.Seconds()),
}
return c.client.post("/channels/"+key.ID+"/dial", nil, &req)
}
// Subscribe creates a new subscription for ARI events related to this channel
func (c *Channel) Subscribe(key *ari.Key, n ...string) ari.Subscription {
return c.client.Bus().Subscribe(key, n...)
}
// GetVariable gets the value of the given variable
func (c *Channel) GetVariable(key *ari.Key, name string) (string, error) {
var m struct {
Value string `json:"value"`
}
err := c.client.get(fmt.Sprintf("/channels/%s/variable?variable=%s", key.ID, name), &m)
return m.Value, err
}
// SetVariable sets the value of the given channel variable
func (c *Channel) SetVariable(key *ari.Key, name, value string) error {
req := struct {
Name string `json:"variable"`
Value string `json:"value,omitempty"`
}{
Name: name,
Value: value,
}
return c.client.post(fmt.Sprintf("/channels/%s/variable", key.ID), nil, &req)
}
// UserEvent - triggers a UserEvent for the given channel
func (c *Channel) UserEvent(key *ari.Key, ue *ari.ChannelUserevent) error {
req := struct {
Application string `json:"application"`
Source string `json:"source"`
Variables interface{} `json:"variables"`
}{
Application: key.App,
Source: "channel:" + key.ID,
Variables: ue.Userevent,
}
return c.client.post(fmt.Sprintf("/events/user/%s", ue.Eventname), nil, &req)
}