forked from notedit/janus-go
/
types.go
335 lines (278 loc) · 7.37 KB
/
types.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
// Msg Types
//
// All messages received from the gateway are first decoded to the BaseMsg
// type. The BaseMsg type extracts the following JSON from the message:
// {
// "janus": <Type>,
// "transaction": <ID>,
// "session_id": <Session>,
// "sender": <Handle>
// }
// The Type field is inspected to determine which concrete type
// to decode the message to, while the other fields (ID/Session/Handle) are
// inspected to determine where the message should be delivered. Messages
// with an ID field defined are considered responses to previous requests, and
// will be passed directly to requester. Messages without an ID field are
// considered unsolicited events from the gateway and are expected to have
// both Session and Handle fields defined. They will be passed to the Events
// channel of the related Handle and can be read from there.
package janus
import (
"encoding/json"
"fmt"
"strings"
"time"
)
var msgtypes = map[string]func() interface{}{
"error": func() interface{} { return &ErrorMsg{} },
"success": func() interface{} { return &SuccessMsg{} },
"detached": func() interface{} { return &DetachedMsg{} },
"server_info": func() interface{} { return &InfoMsg{} },
"ack": func() interface{} { return &AckMsg{} },
"event": func() interface{} { return &EventMsg{} },
"webrtcup": func() interface{} { return &WebRTCUpMsg{} },
"media": func() interface{} { return &MediaMsg{} },
"hangup": func() interface{} { return &HangupMsg{} },
"slowlink": func() interface{} { return &SlowLinkMsg{} },
"timeout": func() interface{} { return &TimeoutMsg{} },
}
type BaseMsg struct {
Type string `json:"janus"`
ID string `json:"transaction"`
Session uint64 `json:"session_id"`
Handle uint64 `json:"sender"`
PluginData PluginData `json:"plugindata"`
}
type ErrorMsg struct {
Err ErrorData `json:"error"`
}
type ErrorData struct {
Code int
Reason string
}
func (err *ErrorMsg) Error() string {
return err.Err.Reason
}
type SuccessMsg struct {
Data SuccessData
PluginData PluginData
Session uint64 `json:"session_id"`
Handle uint64 `json:"sender"`
}
type SuccessData struct {
ID uint64
}
type DetachedMsg struct{}
type InfoMsg struct {
Name string
Version int
VersionString string `json:"version_string"`
Author string
DataChannels bool `json:"data_channels"`
IPv6 bool `json:"ipv6"`
LocalIP string `json:"local-ip"`
IceTCP bool `json:"ice-tcp"`
Transports map[string]PluginInfo
Plugins map[string]PluginInfo
}
type PluginInfo struct {
Name string
Author string
Description string
Version int
VersionString string `json:"version_string"`
}
type AckMsg struct{}
type EventMsg struct {
Plugindata PluginData
Jsep map[string]interface{}
Session uint64 `json:"session_id"`
Handle uint64 `json:"sender"`
}
type PluginData struct {
Plugin string
Data map[string]interface{}
}
type WebRTCUpMsg struct {
Session uint64 `json:"session_id"`
Handle uint64 `json:"sender"`
}
type TimeoutMsg struct {
Session uint64 `json:"session_id"`
}
type SlowLinkMsg struct {
Uplink bool
Lost int64
}
type MediaMsg struct {
Type string
Receiving bool
}
type HangupMsg struct {
Reason string
Session uint64 `json:"session_id"`
Handle uint64 `json:"sender"`
}
// Event handler types
type BaseEvent struct {
Emitter string
Type int
Subtype int
Timestamp int64
Session uint64 `json:"session_id"`
Handle uint64 `json:"handle_id"`
OpaqueID string `json:"opaque_id"`
Event map[string]interface{}
}
type SessionEventBody struct {
Name string
Transport map[string]interface{}
}
type SessionEvent struct {
BaseEvent
Event SessionEventBody `json:"event"`
}
type HandleEventBody struct {
Name string
Plugin string
}
type HandleEvent struct {
BaseEvent
Event HandleEventBody `json:"event"`
}
type ExternalEventBody struct {
Schema string
Data map[string]interface{}
}
type ExternalEvent struct {
BaseEvent
Event ExternalEventBody `json:"event"`
}
type JSEPInfo struct {
Type string
SDP string
}
type JSEPEventBody struct {
Owner string
JSEP JSEPInfo
}
type JSEPEvent struct {
BaseEvent
Event JSEPEventBody `json:"event"`
}
type WebRTCEvent struct {
BaseEvent
}
type MediaEvent struct {
BaseEvent
}
type PluginEventBody struct {
Plugin string
Data map[string]interface{}
}
type PluginEvent struct {
BaseEvent
Event PluginEventBody `json:"event"`
}
type TransportEventBody struct {
Transport string
ID string
Data map[string]interface{}
}
type TransportEvent struct {
BaseEvent
Event TransportEventBody `json:"event"`
}
type CoreEvent struct {
BaseEvent
}
var eventTypes = map[int]func() interface{}{
1: func() interface{} { return &SessionEvent{} },
2: func() interface{} { return &HandleEvent{} },
4: func() interface{} { return &ExternalEvent{} },
8: func() interface{} { return &JSEPEvent{} },
16: func() interface{} { return &WebRTCEvent{} },
32: func() interface{} { return &MediaEvent{} },
64: func() interface{} { return &PluginEvent{} },
128: func() interface{} { return &TransportEvent{} },
256: func() interface{} { return &CoreEvent{} },
}
// plugin specific types
type TextroomPostMsg struct {
Textroom string
Room int // this has changed to string in janus API
From string
Date DateTime
Text string
Whisper bool
}
// Janus date time
type DateTime struct {
time.Time
}
const dtFormat = "2006-01-02T15:04:05Z0700" // almost like RFC3339 but zone has no : (man strftime)
func (dt *DateTime) UnmarshalJSON(b []byte) (err error) {
s := strings.Trim(string(b), "\"")
if s == "null" {
dt.Time = time.Time{}
return
}
dt.Time, err = time.Parse(dtFormat, s)
return
}
func (dt DateTime) MarshalJSON() ([]byte, error) {
if dt.Time.UnixNano() == nilTime {
return []byte("null"), nil
}
return []byte(fmt.Sprintf("\"%s\"", dt.Time.Format(dtFormat))), nil
}
var nilTime = (time.Time{}).UnixNano()
func (dt *DateTime) IsSet() bool {
return dt.UnixNano() != nilTime
}
func ParseMessage(data []byte) (*BaseMsg, interface{}, error) {
var base BaseMsg
if err := json.Unmarshal(data, &base); err != nil {
return nil, nil, fmt.Errorf("json.Unmarshal: %w", err)
}
typeFunc, ok := msgtypes[base.Type]
if !ok {
return &base, nil, fmt.Errorf("unknown message type received: %s", base.Type)
}
msg := typeFunc()
if err := json.Unmarshal(data, &msg); err != nil {
return &base, nil, fmt.Errorf("json.Unmarshal %s : %w", base.Type, err)
}
return &base, msg, nil
}
func ParseEvent(data []byte) (interface{}, error) {
var base BaseEvent
if err := json.Unmarshal(data, &base); err != nil {
return nil, fmt.Errorf("json.Unmarshal: %w", err)
}
switch base.Type {
case 16:
return &WebRTCEvent{base}, nil
case 32:
return &MediaEvent{base}, nil
case 256:
return &CoreEvent{base}, nil
default:
typeFunc, ok := eventTypes[base.Type]
if !ok {
return nil, fmt.Errorf("unknown event type received: %d", base.Type)
}
msg := typeFunc()
if err := json.Unmarshal(data, &msg); err != nil {
return nil, fmt.Errorf("json.Unmarshal %d : %w", base.Type, err)
}
return msg, nil
}
}
func ParseTextroomMessage(data []byte) (*TextroomPostMsg, error) {
var msg *TextroomPostMsg
if err := json.Unmarshal(data, &msg); err != nil {
return nil, fmt.Errorf("json.Unmarshal: %w", err)
}
return msg, nil
}