forked from metal3d/kurento-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase.go
246 lines (205 loc) · 5.44 KB
/
base.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
package kurento
import (
"errors"
"fmt"
"log"
"reflect"
"strings"
)
var debug = false
// Debug activate debug information.
func SetDebug(state bool) {
debug = state
}
type SubscriptionHandler interface {
Handle(event Response)
}
// IMadiaElement implements some basic methods as getConstructorParams or Create().
type IMediaObject interface {
// Return the constructor parameters
getConstructorParams(IMediaObject, map[string]interface{}) map[string]interface{}
// Each media object should be able to create another object
// Those options are sent to getConstructorParams
Create(IMediaObject, map[string]interface{}) error
// Add a subscription to event of "type"
Subscribe(eventType string, handler SubscriptionHandler) error
// remove a subscription to event of
Unsubscribe(eventType string, subscriptionId string) error
// Release the underlying resources in kurento
Release() error
// Set ID of the element
setId(string)
//Implement Stringer
String() string
setParent(IMediaObject)
addChild(IMediaObject)
setConnection(*Connection)
}
// Create object "m" with given "options"
func (elem *MediaObject) Create(m IMediaObject, options map[string]interface{}) error {
req := elem.getCreateRequest()
constparams := m.getConstructorParams(elem, options)
// TODO params["sessionId"]
req["params"] = map[string]interface{}{
"type": getMediaElementType(m),
"constructorParams": constparams,
}
if debug {
log.Printf("request to be sent: %+v\n", req)
}
m.setConnection(elem.connection)
responses, err := elem.connection.Request(req)
if err != nil {
return err
}
var res Response
select {
case res = <-responses:
case <-elem.connection.closeSig:
return ErrConnectionClosing
}
if debug {
log.Printf("Oncreate response: %+v\n", string(res.Result.Value))
log.Println(len(res.Result.Value))
if len(res.Result.Value) != 0 {
log.Println(string(res.Result.Value))
}
}
if len(res.Result.Value) != 0 {
elem.addChild(m)
//m.setParent(elem)
m.setId(trimQuotes(string(res.Result.Value)))
}
return res.Error
}
// Starts to send data to the endpoint `MediaSource`
func (elem *MediaObject) GetGstreamerDot() (string, error) {
req := elem.getInvokeRequest()
req["params"] = map[string]interface{}{
"operation": "getElementGstreamerDot",
"object": elem.Id,
}
// Call server and wait response
responses, err := elem.connection.Request(req)
if err != nil {
return "", err
}
var response Response
select {
case response = <-responses:
// Returns error or nil
if response.Error != nil {
return "", errors.New(fmt.Sprintf("[%d] %s %s", response.Error.Code, response.Error.Message, response.Error.Data))
}
case <-elem.connection.closeSig:
return "", ErrConnectionClosing
}
dot, err := response.Result.Value.MarshalJSON()
if err != nil {
return "", err
}
return string(dot), nil
}
// Create an object in memory that represents a remote object without creating it
func HydrateMediaObject(id string, parent IMediaObject, c *Connection, elem IMediaObject) error {
elem.setConnection(c)
elem.setId(id)
if parent != nil {
parent.addChild(elem)
}
return nil
}
// Implement setConnection that allows element to handle connection
func (elem *MediaObject) setConnection(c *Connection) {
elem.connection = c
}
// Set parent of current element
// BUG(recursion) a recursion happends while testing, I must find why
func (elem *MediaObject) setParent(m IMediaObject) {
elem.Parent = m
}
// Append child to the element
func (elem *MediaObject) addChild(m IMediaObject) {
elem.Childs = append(elem.Childs, m)
}
// setId set object id from a KMS response
func (m *MediaObject) setId(id string) {
m.Id = id
}
// Build a prepared create request
func (m *MediaObject) getCreateRequest() map[string]interface{} {
return map[string]interface{}{
"jsonrpc": "2.0",
"method": "create",
"params": make(map[string]interface{}),
}
}
// Build a prepared invoke request
func (m *MediaObject) getInvokeRequest() map[string]interface{} {
req := m.getCreateRequest()
req["method"] = "invoke"
return req
}
func (m *MediaObject) getSubscribeRequest() map[string]interface{} {
req := m.getCreateRequest()
req["method"] = "subscribe"
return req
}
func (m *MediaObject) getReleaseRequest() map[string]interface{} {
req := m.getCreateRequest()
req["method"] = "release"
return req
}
// String implements fmt.Stringer interface, return ID
func (m *MediaObject) String() string {
return m.Id
}
// Return name of the object
func getMediaElementType(i interface{}) string {
n := reflect.TypeOf(i).String()
p := strings.Split(n, ".")
return p[len(p)-1]
}
func mergeOptions(a, b map[string]interface{}) {
for key, val := range b {
a[key] = val
}
}
func setIfNotEmpty(param map[string]interface{}, name string, t interface{}) {
log.Println("in Set if not empty")
log.Println(t)
switch v := t.(type) {
case string:
if v != "" {
log.Println("Set Map Type String")
param[name] = v
}
case int, float64:
if v != 0 {
log.Println("Set Map Type num")
param[name] = v
}
case bool:
if v {
log.Println("Set Map Type bool")
param[name] = v
}
case IMediaObject, fmt.Stringer:
if v != nil {
val := fmt.Sprintf("%s", v)
if val != "" {
log.Println("Set Map Type Stringer")
param[name] = val
}
}
case IceCandidate:
val := fmt.Sprintf("%s", v)
if val != "" {
log.Println("Set Map Type Candidate")
param[name] = v
}
default:
log.Println("Couldn't set map type")
log.Println(v)
}
}