-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlib_onvif.go
428 lines (394 loc) · 10.3 KB
/
lib_onvif.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
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
package onvif
import (
"encoding/json"
"github.com/golang/glog"
"net"
"time"
)
var (
mapProfile = make(map[string]string) // key: device XAddr, value: profile of device
mapPtzXAddr = make(map[string]string)
mapMediaXAddr = make(map[string]string)
)
type OnvifData struct {
Error string
Data interface{}
}
func DiscoveryDeviceByIp(ip string, duration int) string {
result := OnvifData{}
// Discover device on interface's network
result.Error = ""
devices, err := discoverDevices(ip, time.Duration(duration)*time.Millisecond)
if err != nil {
result.Error = err.Error()
}
result.Data = devices
str, _ := json.Marshal(result)
return string(str)
}
func DiscoveryDevice(interfaceName string, duration int) string {
result := OnvifData{}
itf, err := net.InterfaceByName(interfaceName) //here your interface
if err != nil {
result.Error = err.Error()
str, _ := json.Marshal(result)
return string(str)
}
item, _ := itf.Addrs()
var ip net.IP
for _, addr := range item {
switch v := addr.(type) {
case *net.IPNet:
if !v.IP.IsLoopback() {
if v.IP.To4() != nil { //Verify if IP is IPV4
ip = v.IP
}
}
}
}
if ip == nil {
result.Error = "cannot get device ip"
str, _ := json.Marshal(result)
return string(str)
}
// Discover device on interface's network
result.Error = ""
devices, err := discoverDevices(ip.String(), time.Duration(duration)*time.Millisecond)
if err != nil {
result.Error = err.Error()
}
result.Data = devices
str, _ := json.Marshal(result)
return string(str)
}
// DiscoveryDevice send a WS-Discovery message and wait for all matching device to respond
func GetMediaInformation(host, username, password string) string {
result := OnvifData{}
profile := CameraProfile{}
// Get media address
od := Device{
XAddr: host,
User: username,
Password: password,
}
sys, err := od.GetInformation()
if err == nil {
profile.Manufacturer = sys.Manufacturer
profile.Model = sys.Model
profile.Serial = sys.SerialNumber
profile.HardwareID = sys.HardwareID
}
caps, err := od.GetCapabilities()
if err != nil {
profile.LastError = "profile.onvif.getcapabilities.error"
glog.Warning("Get capabilities error")
result.Error = "res.error.getcapabilities"
result.Data = profile
str, _ := json.Marshal(result)
return string(str)
}
// Get media profile
odm := Device{
XAddr: caps.Media.XAddr,
User: username,
Password: password,
}
profiles, err := odm.GetProfiles()
// Find highest resolution snapshot
highestHeight := 0
for _, ovfprofile := range profiles {
if ovfprofile.VideoEncoderConfig.Resolution.Height > highestHeight {
highestHeight = ovfprofile.VideoEncoderConfig.Resolution.Height
}
}
if err != nil {
profile.LastError = "profile.onvif.getprofiles.error"
glog.Warning("Get profiles error")
result.Error = "res.error.getprofiles"
result.Data = profile
str, _ := json.Marshal(result)
return string(str)
}
for _, ovfprofile := range profiles {
glog.Infof("Get profile %s", ovfprofile.Token)
// Get streaming uri
uri, err := odm.GetStreamURI(ovfprofile.Token, "RTSP")
if err != nil {
glog.Warning("Get streaming error")
continue
}
// Only get snapshot on highest resolution
var snapshotUri string
if ovfprofile.VideoEncoderConfig.Resolution.Height >= highestHeight {
// Get streaming uri
snapshotUri, err = odm.GetSnapshot(ovfprofile.Token)
}
if err != nil {
profile.LastError = "profile.onvif.getsnapshot.error"
glog.Warningf("Get snapshot for %s error %v", ovfprofile.Token, err)
}
profile.Streams = append(profile.Streams, Stream{
ProfileToken: ovfprofile.Token,
StreamURI: uri.URI,
Resolution: Resolution{
Width: ovfprofile.VideoEncoderConfig.Resolution.Width,
Height: ovfprofile.VideoEncoderConfig.Resolution.Height,
},
SnapshotURI: snapshotUri,
VideoEncToken: ovfprofile.VideoEncoderConfig.Token,
VideoCodec: ovfprofile.VideoEncoderConfig.Encoding,
VideoSourceToken: ovfprofile.VideoSourceConfig.Token,
})
glog.Infof("Get profile %s done", ovfprofile.Token)
}
profile.Authorize = true
if len(profile.Streams) == 0 {
profile.LastError = "profile.onvif.getcapabilities.error"
result.Error = "res.error.getstream"
data, _ := json.Marshal(profile)
result.Data = string(data)
str, _ := json.Marshal(result)
return string(str)
}
result.Error = ""
result.Data = profile
str, _ := json.Marshal(result)
return string(str)
}
func GetXAddress(od Device) (OnvifXAddress, error) {
result := OnvifXAddress{}
caps, err := od.GetCapabilities()
if err != nil || caps.Media.XAddr == "" {
return result, err
}
mapPtzXAddr[od.XAddr] = caps.Ptz.XAddr
mapMediaXAddr[od.XAddr] = caps.Media.XAddr
result.PtzXAddress = caps.Ptz.XAddr
result.MediaXAddress = caps.Media.XAddr
result.EventXAddress = caps.EventsCap.XAddr
return result, nil
}
func PtzStart(host, username, password string, x, y, z float64) string {
result := OnvifData{}
od := Device{
XAddr: host,
User: username,
Password: password,
}
// get ptz XAddr and media XAddr
ptzXAddr := mapPtzXAddr[od.XAddr]
mediaXAddr := mapMediaXAddr[od.XAddr]
if ptzXAddr == "" || mediaXAddr == "" {
glog.Info("Find PTZ And Media Address")
caps, err := GetXAddress(od)
if err != nil {
if CheckAuthorizedError(err.Error()) {
result.Error = "res.error.unauthorized"
} else {
result.Error = "res.error.getptzxaddr"
}
str, _ := json.Marshal(result)
return string(str)
}
ptzXAddr = caps.PtzXAddress
mediaXAddr = caps.MediaXAddress
}
// get profile
profileToken := mapProfile[od.XAddr]
if profileToken == "" {
glog.Info("Find Profile")
// Media device control
odMedia := Device{
XAddr: mediaXAddr,
User: username,
Password: password,
}
profiles, err := odMedia.GetProfiles()
if err != nil {
glog.Info(err)
if CheckAuthorizedError(err.Error()) {
result.Error = "res.error.unauthorized"
} else {
result.Error = "res.error.getprofile"
}
str, _ := json.Marshal(result)
return string(str)
}
mapProfile[od.XAddr] = profiles[0].Token
profileToken = mapProfile[od.XAddr]
}
glog.Info("PTZ XAddr: ", ptzXAddr)
glog.Info("Profile Token: ", profileToken)
// PTZ device control
odPtz := Device{
XAddr: ptzXAddr,
User: username,
Password: password,
}
err := odPtz.ContinuousMove(profileToken, PTZVector{
PanTilt: Vector2D{
X: x,
Y: y,
},
Zoom: Vector1D{
X: z,
},
})
if err != nil {
glog.Warning("PTZ Start Error: ", err.Error())
if CheckAuthorizedError(err.Error()) {
result.Error = "res.error.unauthorized"
} else {
result.Error = "res.error.ptzstart"
}
str, _ := json.Marshal(result)
return string(str)
}
result.Error = ""
str, _ := json.Marshal(result)
return string(str)
}
func PtzStop(host, username, password string) string {
result := OnvifData{}
od := Device{
XAddr: host,
User: username,
Password: password,
}
// get ptz XAddr and media XAddr
ptzXAddr := mapPtzXAddr[od.XAddr]
mediaXAddr := mapMediaXAddr[od.XAddr]
if ptzXAddr == "" || mediaXAddr == "" {
glog.Info("Find PTZ And Media Address")
caps, err := GetXAddress(od)
if err != nil {
if CheckAuthorizedError(err.Error()) {
result.Error = "res.error.unauthorized"
} else {
result.Error = "res.error.getptzxaddr"
}
str, _ := json.Marshal(result)
return string(str)
}
ptzXAddr = caps.PtzXAddress
mediaXAddr = caps.MediaXAddress
}
// get profile
profileToken := mapProfile[od.XAddr]
if profileToken == "" {
glog.Info("Find Profile")
// Media device control
odMedia := Device{
XAddr: mediaXAddr,
User: username,
Password: password,
}
profiles, err := odMedia.GetProfiles()
if err != nil {
glog.Info(err)
if CheckAuthorizedError(err.Error()) {
result.Error = "res.error.unauthorized"
} else {
result.Error = "res.error.getprofile"
}
str, _ := json.Marshal(result)
return string(str)
}
mapProfile[od.XAddr] = profiles[0].Token
profileToken = mapProfile[od.XAddr]
}
glog.Info("PTZ XAddr: ", ptzXAddr)
glog.Info("Profile Token: ", profileToken)
// PTZ device control
odPtz := Device{
XAddr: ptzXAddr,
User: username,
Password: password,
}
err := odPtz.Stop(profileToken)
if err != nil {
glog.Warning("PTZ Stop Error: ", err.Error())
if CheckAuthorizedError(err.Error()) {
result.Error = "res.error.unauthorized"
} else {
result.Error = "res.error.ptzstop"
}
str, _ := json.Marshal(result)
return string(str)
}
result.Error = ""
str, _ := json.Marshal(result)
return string(str)
}
func PtzGoToHome(host, username, password string) string {
result := OnvifData{}
od := Device{
XAddr: host,
User: username,
Password: password,
}
// get ptz XAddr and media XAddr
ptzXAddr := mapPtzXAddr[od.XAddr]
mediaXAddr := mapMediaXAddr[od.XAddr]
if ptzXAddr == "" || mediaXAddr == "" {
glog.Info("Find PTZ And Media Address")
caps, err := GetXAddress(od)
if err != nil {
if CheckAuthorizedError(err.Error()) {
result.Error = "res.error.unauthorized"
} else {
result.Error = "res.error.getptzxaddr"
}
str, _ := json.Marshal(result)
return string(str)
}
ptzXAddr = caps.PtzXAddress
mediaXAddr = caps.MediaXAddress
}
// get profile
profileToken := mapProfile[od.XAddr]
if profileToken == "" {
glog.Info("Find Profile")
// Media device control
odMedia := Device{
XAddr: mediaXAddr,
User: username,
Password: password,
}
profiles, err := odMedia.GetProfiles()
if err != nil {
glog.Info(err)
if CheckAuthorizedError(err.Error()) {
result.Error = "res.error.unauthorized"
} else {
result.Error = "res.error.getprofile"
}
str, _ := json.Marshal(result)
return string(str)
}
mapProfile[od.XAddr] = profiles[0].Token
profileToken = mapProfile[od.XAddr]
}
glog.Info("PTZ XAddr: ", ptzXAddr)
glog.Info("Profile Token: ", profileToken)
// PTZ device control
odPtz := Device{
XAddr: ptzXAddr,
User: username,
Password: password,
}
err := odPtz.GotoHomePosition(profileToken)
if err != nil {
glog.Warning("PTZ Stop Error: ", err.Error())
if CheckAuthorizedError(err.Error()) {
result.Error = "res.error.unauthorized"
} else {
result.Error = "res.error.ptzstop"
}
str, _ := json.Marshal(result)
return string(str)
}
result.Error = ""
str, _ := json.Marshal(result)
return string(str)
}