-
Notifications
You must be signed in to change notification settings - Fork 136
/
video.go
307 lines (262 loc) · 10.5 KB
/
video.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
package gotwilio
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
)
// MediaRegion is the locations of Twilio's
// TURN servers
type MediaRegion string
const (
Australia MediaRegion = "au1"
Brazil MediaRegion = "br1"
Germany MediaRegion = "de1"
Ireland MediaRegion = "ie1"
India MediaRegion = "in1"
Japan MediaRegion = "jp1"
Singapore MediaRegion = "sg1"
USEastCoast MediaRegion = "us1"
USWestCoast MediaRegion = "us2"
)
// VideoStatus is the status of a video room
type VideoStatus string
const (
InProgress VideoStatus = "in-progress"
Failed VideoStatus = "failed"
Completed VideoStatus = "completed"
)
// VideoRoomType is how the participants connect
// to each other, whether peer-to-peer of routed
// through a TURN server.
type VideoRoomType string
const (
PeerToPeer VideoRoomType = "peer-to-peer"
Group VideoRoomType = "group"
GroupSmall VideoRoomType = "group-small"
)
// VideoCodecs are the supported codecs when
// publishing a track to the room.
type VideoCodecs string
const (
VP8 VideoCodecs = "VP8"
H264 VideoCodecs = "H264"
)
// ListVideoResponse is returned when listing rooms
type ListVideoReponse struct {
Rooms []*VideoResponse `json:"rooms"`
Meta struct {
Page int64 `json:"page"`
PageSize int64 `json:"page_size"`
FirstPageUrl string `json:"first_page_url"`
PreviousPageUrl string `json:"previous_page_url"`
NextPageUrl string `json:"next_page_url"`
Url string `json:"url"`
Key string `json:"key"`
} `json:"meta"`
}
// VideoResponse is returned for a single room
type VideoResponse struct {
AccountSid string `json:"account_sid"`
DateCreated time.Time `json:"date_created"`
DateUpdated time.Time `json:"date_updated"`
Duration time.Duration `json:"duration"`
EnableTurn bool `json:"enable_turn"`
EndTime time.Time `json:"end_time"`
MaxParticipants int64 `json:"max_participants"`
MediaRegion MediaRegion `json:"media_region"`
RecordParticipantsOnConnect bool `json:"record_participants_on_connect"`
Sid string `json:"sid"`
Status VideoStatus `json:"status"`
StatusCallback string `json:"status_callback"`
StatusCallbackMethod string `json:"status_callback_method"`
Type VideoRoomType `json:"type"`
UniqueName string `json:"unique_name"`
URL string `json:"url"`
}
type createRoomOptions struct {
EnableTurn bool `json:"EnableTurn"`
MaxParticipants int64 `json:"MaxParticipants"`
MediaRegion MediaRegion `json:"MediaRegion"`
RecordParticipantsOnConnect bool `json:"RecordParticipantsOnConnect"`
StatusCallback string `json:"StatusCallback"`
StatusCallbackMethod string `json:"StatusCallbackMethod"`
Type VideoRoomType `json:"Type"`
UniqueName string `json:"UniqueName"`
VideoCodecs []VideoCodecs `json:"VideoCodecs"`
}
// DefaultVideoRoomOptions are the default options
// for creating a room.
var DefaultVideoRoomOptions = &createRoomOptions{
EnableTurn: true,
MaxParticipants: 10,
MediaRegion: USEastCoast,
RecordParticipantsOnConnect: false,
StatusCallback: "",
StatusCallbackMethod: http.MethodPost,
Type: Group,
UniqueName: "",
VideoCodecs: []VideoCodecs{H264, VP8},
}
// ListVideoRoomOptions are the options to query
// for a list of video rooms.
type ListVideoRoomOptions struct {
DateCreatedAfter time.Time `json:"DateCreatedAfter"`
DateCreatedBefore time.Time `json:"DateCreatedBefore"`
Status VideoStatus `json:"Status"`
UniqueName string `json:"EnableUniqueNameTurn"`
}
// CreateVideoRoom creates a video communication session
// for participants to connect to.
// See https://www.twilio.com/docs/video/api/rooms-resource
// for more information.
func (twilio *Twilio) CreateVideoRoom(options *createRoomOptions) (videoResponse *VideoResponse, exception *Exception, err error) {
return twilio.CreateVideoRoomWithContext(context.Background(), options)
}
func (twilio *Twilio) CreateVideoRoomWithContext(ctx context.Context, options *createRoomOptions) (videoResponse *VideoResponse, exception *Exception, err error) {
twilioUrl := twilio.VideoUrl + "/v1/Rooms"
formValues := createRoomOptionsToFormValues(options)
res, err := twilio.post(ctx, formValues, twilioUrl)
if err != nil {
return videoResponse, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return videoResponse, exception, err
}
if res.StatusCode != http.StatusCreated {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return videoResponse, exception, err
}
videoResponse = new(VideoResponse)
err = json.Unmarshal(responseBody, videoResponse)
return videoResponse, exception, err
}
// DateCreatedAfter time.Time `json:"DateCreatedAfter"`
// DateCreatedBefore time.Time `json:"DateCreatedBefore"`
// Status VideoStatus `json:"Status"`
// UniqueName string `json:"EnableUniqueNameTurn"`
// ListVideoRooms returns a list of all video rooms.
// See https://www.twilio.com/docs/video/api/rooms-resource
// for more information.
func (twilio *Twilio) ListVideoRooms(options *ListVideoRoomOptions) (videoResponse *ListVideoReponse, exception *Exception, err error) {
return twilio.ListVideoRoomsWithContext(context.Background(), options)
}
func (twilio *Twilio) ListVideoRoomsWithContext(ctx context.Context, options *ListVideoRoomOptions) (videoResponse *ListVideoReponse, exception *Exception, err error) {
q := &url.Values{}
if !options.DateCreatedAfter.Equal(time.Time{}) {
q.Set("DateCreatedAfter", options.DateCreatedAfter.Format(time.RFC3339))
}
if !options.DateCreatedBefore.Equal(time.Time{}) {
q.Set("DateCreatedBefore", options.DateCreatedBefore.Format(time.RFC3339))
}
if options.Status != "" {
q.Set("Status", fmt.Sprintf("%s", options.Status))
}
if options.UniqueName != "" {
q.Set("UniqueName", options.UniqueName)
}
twilioUrl := twilio.VideoUrl + "/v1/Rooms?" + q.Encode()
res, err := twilio.get(ctx, twilioUrl)
if err != nil {
return videoResponse, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return videoResponse, exception, err
}
if res.StatusCode != http.StatusOK {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return videoResponse, exception, err
}
videoResponse = new(ListVideoReponse)
err = json.Unmarshal(responseBody, videoResponse)
return videoResponse, exception, err
}
// GetVideoRoom retrievs a single video session
// by name or by Sid.
// See https://www.twilio.com/docs/video/api/rooms-resource
// for more information.
func (twilio *Twilio) GetVideoRoom(nameOrSid string) (videoResponse *VideoResponse, exception *Exception, err error) {
return twilio.GetVideoRoomWithContext(context.Background(), nameOrSid)
}
func (twilio *Twilio) GetVideoRoomWithContext(ctx context.Context, nameOrSid string) (videoResponse *VideoResponse, exception *Exception, err error) {
twilioUrl := twilio.VideoUrl + "/v1/Rooms/" + nameOrSid
res, err := twilio.get(ctx, twilioUrl)
if err != nil {
return videoResponse, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return videoResponse, exception, err
}
if res.StatusCode != http.StatusOK {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return videoResponse, exception, err
}
videoResponse = new(VideoResponse)
err = json.Unmarshal(responseBody, videoResponse)
return videoResponse, exception, err
}
// EndVideoRoom stops a single video session by name
// or by Sid, and disconnects all participants.
// See https://www.twilio.com/docs/video/api/rooms-resource
// for more information.
func (twilio *Twilio) EndVideoRoom(nameOrSid string) (videoResponse *VideoResponse, exception *Exception, err error) {
return twilio.EndVideoRoomWithContext(context.Background(), nameOrSid)
}
func (twilio *Twilio) EndVideoRoomWithContext(ctx context.Context, nameOrSid string) (videoResponse *VideoResponse, exception *Exception, err error) {
twilioUrl := twilio.VideoUrl + "/v1/Rooms/" + nameOrSid
formValues := url.Values{}
formValues.Set("Status", fmt.Sprintf("%s", Completed))
res, err := twilio.post(ctx, formValues, twilioUrl)
if err != nil {
return videoResponse, exception, err
}
defer res.Body.Close()
responseBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return videoResponse, exception, err
}
if res.StatusCode < 200 || res.StatusCode > 299 {
exception = new(Exception)
err = json.Unmarshal(responseBody, exception)
// We aren't checking the error because we don't actually care.
// It's going to be passed to the client either way.
return videoResponse, exception, err
}
videoResponse = new(VideoResponse)
err = json.Unmarshal(responseBody, videoResponse)
return videoResponse, exception, err
}
func createRoomOptionsToFormValues(options *createRoomOptions) url.Values {
formValues := url.Values{}
formValues.Set("EnableTurn", fmt.Sprintf("%t", options.EnableTurn))
formValues.Set("MaxParticipants", fmt.Sprintf("%d", options.MaxParticipants))
formValues.Set("MediaRegion", fmt.Sprintf("%s", options.MediaRegion))
formValues.Set("RecordParticipantsOnConnect", fmt.Sprintf("%t", options.RecordParticipantsOnConnect))
formValues.Set("StatusCallback", options.StatusCallback)
formValues.Set("StatusCallbackMethod", options.StatusCallbackMethod)
formValues.Set("Type", fmt.Sprintf("%s", options.Type))
formValues.Set("UniqueName", options.UniqueName)
formValues.Del("VideoCodecs")
for _, codec := range options.VideoCodecs {
formValues.Add("VideoCodecs", fmt.Sprintf("%v", codec))
}
return formValues
}