forked from deepch/go-onvif
-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.go
160 lines (139 loc) · 5.84 KB
/
media.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
package onvif
var mediaXMLNs = []string{
`xmlns:trt="http://www.onvif.org/ver10/media/wsdl"`,
`xmlns:tt="http://www.onvif.org/ver10/schema"`,
`xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl"`,
}
// GetProfiles fetch available media profiles of ONVIF camera
func (device Device) GetProfiles() ([]MediaProfile, error) {
// Create SOAP
soap := SOAP{
Body: "<trt:GetProfiles/>",
XMLNs: mediaXMLNs,
}
// Send SOAP request
response, err := soap.SendRequest(device.XAddr)
if err != nil {
return []MediaProfile{}, err
}
// Get and parse list of profile to interface
ifaceProfiles, err := response.ValuesForPath("Envelope.Body.GetProfilesResponse.Profiles")
if err != nil {
return []MediaProfile{}, err
}
// Create initial result
result := []MediaProfile{}
// Parse each available profile
for _, ifaceProfile := range ifaceProfiles {
if mapProfile, ok := ifaceProfile.(map[string]interface{}); ok {
// Parse name and token
profile := MediaProfile{}
profile.Name = interfaceToString(mapProfile["Name"])
profile.Token = interfaceToString(mapProfile["-token"])
// Parse video source configuration
videoSource := MediaSourceConfig{}
if mapVideoSource, ok := mapProfile["VideoSourceConfiguration"].(map[string]interface{}); ok {
videoSource.Name = interfaceToString(mapVideoSource["Name"])
videoSource.Token = interfaceToString(mapVideoSource["-token"])
videoSource.SourceToken = interfaceToString(mapVideoSource["SourceToken"])
// Parse video bounds
bounds := MediaBounds{}
if mapVideoBounds, ok := mapVideoSource["Bounds"].(map[string]interface{}); ok {
bounds.Height = interfaceToInt(mapVideoBounds["-height"])
bounds.Width = interfaceToInt(mapVideoBounds["-width"])
}
videoSource.Bounds = bounds
}
profile.VideoSourceConfig = videoSource
// Parse video encoder configuration
videoEncoder := VideoEncoderConfig{}
if mapVideoEncoder, ok := mapProfile["VideoEncoderConfiguration"].(map[string]interface{}); ok {
videoEncoder.Name = interfaceToString(mapVideoEncoder["Name"])
videoEncoder.Token = interfaceToString(mapVideoEncoder["-token"])
videoEncoder.Encoding = interfaceToString(mapVideoEncoder["Encoding"])
videoEncoder.Quality = interfaceToInt(mapVideoEncoder["Quality"])
videoEncoder.SessionTimeout = interfaceToString(mapVideoEncoder["SessionTimeout"])
// Parse video rate control
rateControl := VideoRateControl{}
if mapVideoRate, ok := mapVideoEncoder["RateControl"].(map[string]interface{}); ok {
rateControl.BitrateLimit = interfaceToInt(mapVideoRate["BitrateLimit"])
rateControl.EncodingInterval = interfaceToInt(mapVideoRate["EncodingInterval"])
rateControl.FrameRateLimit = interfaceToInt(mapVideoRate["FrameRateLimit"])
}
videoEncoder.RateControl = rateControl
// Parse video resolution
resolution := MediaBounds{}
if mapVideoRes, ok := mapVideoEncoder["Resolution"].(map[string]interface{}); ok {
resolution.Height = interfaceToInt(mapVideoRes["Height"])
resolution.Width = interfaceToInt(mapVideoRes["Width"])
}
videoEncoder.Resolution = resolution
}
profile.VideoEncoderConfig = videoEncoder
// Parse audio source configuration
audioSource := MediaSourceConfig{}
if mapAudioSource, ok := mapProfile["AudioSourceConfiguration"].(map[string]interface{}); ok {
audioSource.Name = interfaceToString(mapAudioSource["Name"])
audioSource.Token = interfaceToString(mapAudioSource["-token"])
audioSource.SourceToken = interfaceToString(mapAudioSource["SourceToken"])
}
profile.AudioSourceConfig = audioSource
// Parse audio encoder configuration
audioEncoder := AudioEncoderConfig{}
if mapAudioEncoder, ok := mapProfile["AudioEncoderConfiguration"].(map[string]interface{}); ok {
audioEncoder.Name = interfaceToString(mapAudioEncoder["Name"])
audioEncoder.Token = interfaceToString(mapAudioEncoder["-token"])
audioEncoder.Encoding = interfaceToString(mapAudioEncoder["Encoding"])
audioEncoder.Bitrate = interfaceToInt(mapAudioEncoder["Bitrate"])
audioEncoder.SampleRate = interfaceToInt(mapAudioEncoder["SampleRate"])
audioEncoder.SessionTimeout = interfaceToString(mapAudioEncoder["SessionTimeout"])
}
profile.AudioEncoderConfig = audioEncoder
// Parse PTZ configuration
ptzConfig := PTZConfig{}
if mapPTZ, ok := mapProfile["PTZConfiguration"].(map[string]interface{}); ok {
ptzConfig.Name = interfaceToString(mapPTZ["Name"])
ptzConfig.Token = interfaceToString(mapPTZ["-token"])
ptzConfig.NodeToken = interfaceToString(mapPTZ["NodeToken"])
}
profile.PTZConfig = ptzConfig
// Push profile to result
result = append(result, profile)
}
}
return result, nil
}
// GetStreamURI fetch stream URI of a media profile.
// Possible protocol is UDP, HTTP or RTSP
func (device Device) GetStreamURI(profileToken, protocol string) (MediaURI, error) {
// Create SOAP
soap := SOAP{
XMLNs: mediaXMLNs,
Body: `<trt:GetStreamUri>
<trt:StreamSetup>
<tt:Stream>RTP-Unicast</tt:Stream>
<tt:Transport><tt:Protocol>` + protocol + `</tt:Protocol></tt:Transport>
</trt:StreamSetup>
<trt:ProfileToken>` + profileToken + `</trt:ProfileToken>
</trt:GetStreamUri>`,
}
// Send SOAP request
response, err := soap.SendRequest(device.XAddr)
if err != nil {
return MediaURI{}, err
}
// Parse response to interface
ifaceURI, err := response.ValueForPath("Envelope.Body.GetStreamUriResponse.MediaUri")
if err != nil {
return MediaURI{}, err
}
// Parse interface to struct
streamURI := MediaURI{}
if mapURI, ok := ifaceURI.(map[string]interface{}); ok {
streamURI.URI = interfaceToString(mapURI["Uri"])
streamURI.Timeout = interfaceToString(mapURI["Timeout"])
streamURI.InvalidAfterConnect = interfaceToBool(mapURI["InvalidAfterConnect"])
streamURI.InvalidAfterReboot = interfaceToBool(mapURI["InvalidAfterReboot"])
}
return streamURI, nil
}