-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser_service.go
More file actions
566 lines (523 loc) · 15.4 KB
/
Copy pathuser_service.go
File metadata and controls
566 lines (523 loc) · 15.4 KB
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package eyeson
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"strings"
"time"
)
// Timeout provides the maximum number of seconds WaitReady will wait for a
// meeting and user to be ready.
const Timeout int = 180
// Background provides the z-index to represent a background image.
const Background int = -1
// Foreground provides the z-index to represent a foreground image.
const Foreground int = 1
// ImageType provides custom type for specifying image type
type ImageType string
// List of supported image types
const (
JPG ImageType = "jpg"
PNG ImageType = "png"
SVG ImageType = "svg"
WEBP ImageType = "webp"
)
// Layout provides a custom type for specifying layout configuration.
type Layout string
const (
// Auto Automatically sets layouts according to the number of participants
Auto Layout = "auto"
// Custom Maintains manually assigned positions.
Custom Layout = "custom"
)
// UserService provides methods a user can perform.
type UserService struct {
client *Client
Data *RoomResponse
}
// NewUserServiceFromAccessKey Create a new UserService from an access-key.
func NewUserServiceFromAccessKey(accessKey string, options ...ClientOption) (*UserService, error) {
client, err := NewClient("", options...)
if err != nil {
return nil, err
}
u := &UserService{
client: client,
Data: &RoomResponse{
AccessKey: accessKey,
},
}
return u, nil
}
// WaitReady waits until a meeting has successfully been started. It has a
// fixed polling interval of one second. WaitReady responds with an error on
// timeout or any communication problems.
func (u *UserService) WaitReady() error {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(Timeout)*time.Second)
defer cancel()
res := make(chan error)
go func() {
for u.Data.Ready == false {
time.Sleep(1 * time.Second)
if err := u.updateRoomData(); err != nil {
res <- err
break
}
if u.Data.Room.Shutdown {
res <- errors.New("Meeting has been shutdown")
break
}
}
close(res)
}()
for {
select {
case err := <-res:
return err
case <-ctx.Done():
return ctx.Err()
}
}
}
func (u *UserService) updateRoomData() error {
path := "/rooms/" + u.Data.AccessKey
req, err := u.client.NewRequest(http.MethodGet, path, url.Values{})
if err != nil {
return err
}
resp, err := u.client.Do(req, &u.Data)
if err != nil {
return err
}
return validateResponse(resp)
}
// Chat sends a chat message.
func (u *UserService) Chat(content string) error {
data := url.Values{}
data.Set("type", "chat")
data.Set("content", content)
path := "/rooms/" + u.Data.AccessKey + "/messages"
req, err := u.client.NewRequest(http.MethodPost, path, data)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// SendCustomMessage sends a custom message to all participants
// in this meeting.
func (u *UserService) SendCustomMessage(content string) error {
data := url.Values{}
data.Set("type", "custom")
data.Set("content", content)
path := "/rooms/" + u.Data.AccessKey + "/messages"
req, err := u.client.NewRequest(http.MethodPost, path, data)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// StartRecording starts a recording.
func (u *UserService) StartRecording() error {
path := "/rooms/" + u.Data.AccessKey + "/recording"
req, err := u.client.NewRequest(http.MethodPost, path, nil)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// StopRecording stops a recording.
func (u *UserService) StopRecording() error {
path := "/rooms/" + u.Data.AccessKey + "/recording"
req, err := u.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// StartBroadcast starts a broadcast to the given stream url given by a
// streaming service like YouTube, Vimeo, and others.
func (u *UserService) StartBroadcast(streamURL string) error {
data := url.Values{}
data.Set("stream_url", streamURL)
path := "/rooms/" + u.Data.AccessKey + "/broadcasts"
req, err := u.client.NewRequest(http.MethodPost, path, data)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// StopBroadcast stops a broadcast.
func (u *UserService) StopBroadcast() error {
path := "/rooms/" + u.Data.AccessKey + "/broadcasts"
req, err := u.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// LayoutObjectFit defines how content fits within its container in a layout.
type LayoutObjectFit string
const (
// Cover scales the content to cover the entire container, potentially cropping some parts.
Cover LayoutObjectFit = "cover"
// Contain scales the content to fit within the container while maintaining aspect ratio.
Contain LayoutObjectFit = "contain"
// Autofit automatically determines the best fitting method for the content.
Autofit LayoutObjectFit = "auto"
)
// LayoutPos represents the position and dimensions of a participant in a layout.
type LayoutPos struct {
// X is the horizontal position coordinate.
X int
// Y is the vertical position coordinate.
Y int
// Width is the horizontal size of the position.
Width int
// Height is the vertical size of the position.
Height int
// ObjectFit determines how the participant's video fits within the assigned space.
ObjectFit LayoutObjectFit
}
// LayoutMap contains the positions of participants in a custom layout configuration.
type LayoutMap struct {
// Positions is a slice of participant position configurations.
Positions []LayoutPos
}
func (lmap *LayoutMap) toString() string {
serialMaps := []string{}
for _, p := range lmap.Positions {
serialMaps = append(serialMaps, fmt.Sprintf("[%d, %d, %d, %d, \"%s\"]", p.X, p.Y, p.Width, p.Height, p.ObjectFit))
}
return "[" + strings.Join(serialMaps, ",") + "]"
}
// AudioInsertConfig defines the configuration options for audio insertion.
type AudioInsertConfig string
const (
// Enabled indicates that audio insert is shown all the time.
Enabled AudioInsertConfig = "enabled"
// Disabled indicates that audio insert is turned off.
Disabled AudioInsertConfig = "disabled"
// AudioOnly indicates that the insert is only shown if the participant is not shown on the podium.
AudioOnly AudioInsertConfig = "audio_only"
)
// AudioInsertPosition represents the coordinates for positioning an audio insert visual element.
type AudioInsertPosition struct {
// X is the horizontal position coordinate.
X int
// Y is the vertical position coordinate.
Y int
}
// AudioInsert contains configuration for inserting audio into a meeting.
type AudioInsert struct {
// Config specifies whether audio insertion is enabled, disabled, or audio-only.
Config AudioInsertConfig
// Position defines the visual position of the audio insert when enabled.
// May be nil if no position is specified or for audio-only inserts.
Position *AudioInsertPosition
}
// SetLayoutOptions contains options for configuring a meeting layout.
type SetLayoutOptions struct {
// Users is a list of user IDs or empty strings for empty participant positions.
Users []string
// VoiceActivation determines if participants are actively replaced by voice detection.
VoiceActivation bool
// ShowNames determines if participant name overlays are shown. If not specified, defaults
// to true.
ShowNames *bool
// LayoutName specifies an optional name for the layout configuration.
LayoutName string
// LayoutMap contains the custom positions of participants when using custom layout.
LayoutMap *LayoutMap
// AudioInsert contains configuration for audio insertion if required.
AudioInsert *AudioInsert
}
// SetLayout sets a participant podium layout where the layout is either
// "custom" or "auto". The users list is of user-ids or empty strings for empty
// participant positions. The flag voiceActivation replaces participants
// actively by voice detection. The flag showNames show or hides participant
// name overlays.
func (u *UserService) SetLayout(layout Layout, options *SetLayoutOptions) error {
data := url.Values{}
if layout == "custom" {
data.Set("layout", "custom")
} else {
data.Set("layout", "auto")
}
if options != nil {
for _, userID := range options.Users {
data.Add("users[]", userID)
}
if options.VoiceActivation {
data.Set("voice_activation", "true")
} else {
data.Set("voice_activation", "false")
}
if options.ShowNames != nil {
if *options.ShowNames {
data.Set("show_names", "true")
} else {
data.Set("show_names", "false")
}
}
if options.LayoutName != "" {
data.Set("name", options.LayoutName)
}
if options.LayoutMap != nil {
data.Set("map", options.LayoutMap.toString())
}
if options.AudioInsert != nil {
data.Set("audio_insert", string(options.AudioInsert.Config))
if options.AudioInsert.Position != nil {
data.Set("audio_insert_position[x]", fmt.Sprint(options.AudioInsert.Position.X))
data.Set("audio_insert_position[y]", fmt.Sprint(options.AudioInsert.Position.Y))
}
}
}
path := "/rooms/" + u.Data.AccessKey + "/layout"
req, err := u.client.NewRequest(http.MethodPost, path, data)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// LayerOptions provides options for setting a layer.
type LayerOptions struct {
// ID specifies a custom identifier for the layer.
ID string
}
// SetLayer sets a layer image using the given public available URL pointing to
// an image file. The z-index should be set using the constants Foreground or
// Background.
func (u *UserService) SetLayer(imgURL string, zIndex int, options *LayerOptions) error {
data := url.Values{}
data.Set("url", imgURL)
if zIndex == 1 {
data.Set("z-index", "1")
} else {
data.Set("z-index", "-1")
}
if options != nil {
data.Set("id", options.ID)
}
path := "/rooms/" + u.Data.AccessKey + "/layers"
req, err := u.client.NewRequest(http.MethodPost, path, data)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// SetLayerImage sets a layer image providing
// an image file. The z-index should be set using the constants Foreground or
// Background.
func (u *UserService) SetLayerImage(imgData []byte, imageType ImageType, zIndex int,
options *LayerOptions) error {
body := &bytes.Buffer{}
// Create a multipart writer
writer := multipart.NewWriter(body)
fileName := "layer-img."
switch imageType {
case PNG, JPG, SVG, WEBP:
fileName += string(imageType)
default:
return fmt.Errorf("Unsupported image type %s", imageType)
}
part, err := writer.CreateFormFile("file", fileName)
if err != nil {
return err
}
_, err = io.Copy(part, bytes.NewReader(imgData))
if err != nil {
return err
}
if zIndex == 1 {
writer.WriteField("z-index", "1")
} else {
writer.WriteField("z-index", "-1")
}
if options != nil {
writer.WriteField("id", options.ID)
}
writer.Close()
path := "/rooms/" + u.Data.AccessKey + "/layers"
req, err := u.client.NewPlainRequest(http.MethodPost, path, body, writer.FormDataContentType())
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// ClearLayer clears a layer given by the z-index that should be set using
// the constants Foreground or Background.
func (u *UserService) ClearLayer(zIndex int) error {
path := "/rooms/" + u.Data.AccessKey + "/layers/"
if zIndex == 1 {
path += "1"
} else {
path += "-1"
}
req, err := u.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// PlaybackOptions options for starting a playback.
type PlaybackOptions struct {
// ReplacedUserID is the ID of the user to be replaced by the playback.
// If left empty, the playback is shown as a separate participant.
ReplacedUserID string
// PlayID is a custom identifier for the playback.
PlayID string
// Name is a display name for the playback.
Name string
// LoopCount specifies how many times the video should loop.
// Default is 0 (play once).
LoopCount int
// Mute/Unmute video files
Audio bool
}
// StartPlayback starts a playback using the given public available URL to a
// video file. The given user id marks the position of the participant that
// is going to be replaced while the playback is shown. If replacedUserID is left empty
// the playback is shown as a separate participant of the meeting.
func (u *UserService) StartPlayback(playbackURL string, options *PlaybackOptions) error {
data := url.Values{}
data.Set("playback[url]", playbackURL)
if options != nil {
if options.ReplacedUserID != "" {
data.Set("playback[replacement_id]", options.ReplacedUserID)
}
if options.PlayID != "" {
data.Set("playback[play_id]", options.PlayID)
}
if options.Name != "" {
data.Set("playback[name]", options.Name)
}
data.Set("playback[loop_count]", fmt.Sprint(options.LoopCount))
if options.Audio {
data.Set("playback[audio]", "true")
} else {
data.Set("playback[audio]", "false")
}
}
path := "/rooms/" + u.Data.AccessKey + "/playbacks"
req, err := u.client.NewRequest(http.MethodPost, path, data)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// StopPlayback Stops a playback by its playID.
func (u *UserService) StopPlayback(playID string) error {
path := "/rooms/" + u.Data.AccessKey + "/playbacks/" + playID
req, err := u.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// StopMeeting stops a meeting for all participants.
func (u *UserService) StopMeeting() error {
path := "/rooms/" + u.Data.AccessKey
req, err := u.client.NewRequest(http.MethodDelete, path, nil)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// CreateSnapshot creates a new snapshot of the current meeting
func (u *UserService) CreateSnapshot() error {
path := "/rooms/" + u.Data.AccessKey + "/snapshot"
req, err := u.client.NewRequest(http.MethodPost, path, nil)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}
// GetSnapshot Retrieves a snapshot from a running meeting.
func (u *UserService) GetSnapshot(snapshotID string) (*Snapshot, error) {
path := "/rooms/" + u.Data.AccessKey + "/snapshots/" + snapshotID
req, err := u.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, err
}
var snapshot Snapshot
resp, err := u.client.Do(req, &snapshot)
if err != nil {
return nil, err
}
return &snapshot, validateResponse(resp)
}
// LockMeeting locks a meeting, dissalowing new participants from joining
func (u *UserService) LockMeeting() error {
path := "/rooms/" + u.Data.AccessKey + "/lock"
req, err := u.client.NewRequest(http.MethodPost, path, nil)
if err != nil {
return err
}
resp, err := u.client.Do(req, nil)
if err != nil {
return err
}
return validateResponse(resp)
}