-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbase.go
147 lines (119 loc) · 3.03 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
package kurento
import (
"fmt"
"log"
"reflect"
"strings"
)
var debug = false
// Debug activate debug information.
func Debug(state bool) {
debug = state
}
// 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{})
// 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{}) {
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)
res := <-elem.connection.Request(req)
if debug {
log.Println("Oncreate response: ", res)
}
if res.Result["value"] != "" {
elem.addChild(m)
//m.setParent(elem)
m.setId(res.Result["value"])
}
}
// 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
}
// 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{}) {
switch v := t.(type) {
case string:
if v != "" {
param[name] = v
}
case int, float64:
if v != 0 {
param[name] = v
}
case bool:
if v {
param[name] = v
}
case IMediaObject, fmt.Stringer:
if v != nil {
val := fmt.Sprintf("%s", v)
if val != "" {
param[name] = val
}
}
}
}