-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.py
251 lines (219 loc) · 8.43 KB
/
service.py
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
# coding=UTF-8
import requests
import os
from datetime import datetime
from PySide2 import QtCore
import socket
class SignalSystem(QtCore.QObject):
log = QtCore.Signal(object)
connect_state = QtCore.Signal(bool)
class InstaCamera(object):
'''
Insta360 Camera
'''
def __init__(self):
self._fingerprint = ''
self._status = ''
self.connected = False
self.post_dic = {}
self.post_dic['headers'] = {}
self.post_dic['headers']['content-type'] = 'application/json'
self.rtmp_server = None
self.camera_adress = None
self.signals = SignalSystem()
def inital_service(self, parm_dict):
self.rtmp_server = parm_dict['rtsp_server']
self.camera_adress = parm_dict['camera_ip']
self.command_api = "%s%s" % (parm_dict['camera_ip'], ":20000/osc/commands/execute")
self.state_api = "%s%s" % (parm_dict['camera_ip'], ":20000/osc/state")
self.file_api = "%s%s" % (parm_dict['camera_ip'], ":8000")
self.preview_api = "%s%s" % (parm_dict['camera_ip'], ":1935/live/preview")
return self.rtmp_server, self.camera_adress
def get_current_time(self):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
return current_time
def get_state(self):
return self._status
def ping_camera(self):
results = False
ip = self.camera_adress[7:]
response = os.system("ping -c 1 " + ip)
if response == 0:
self.signals.log.emit("get ip and connecting....")
results = True
else:
self.signals.log.emit("Ping Camera Adress Fail")
results = False
return results
def connect_camera(self, parm_dict):
self.inital_service(parm_dict)
self.post_dic['json'] = {
"name": "camera._connect",
"parameters":
{
"hw_time": "MMDDhhmm[[CC]YY][.ss]",
"time_zone": "GMT+08:00/GMT-08:00"
}
}
if self.ping_camera():
try:
response = requests.post(self.command_api, **self.post_dic)
if response.status_code == 200:
self.connected = True
results = response.json()['results']
self._fingerprint = results['Fingerprint']
self._status = results['last_info']['state']
self.post_dic['headers']['Fingerprint'] = self._fingerprint
self.connected = True
self.signals.log.emit(response.json())
else:
self.connected = False
except Exception as e:
self.connected = False
self.signals.log.emit(e)
return self.connected
def camera_state(self, parm_dict):
self.inital_service(parm_dict)
response = requests.post(self.state_api, **self.post_dic)
self.signals.log.emit(response.json()['state'])
def start_preview(self, parm_dict):
results = False
if self.connected:
self.post_dic['json'] = {
"name": "camera._startPreview",
"parameters": {
"origin": {
"mime": "h264",
"width": 1920,
"height": 1440,
"framerate": 30,
"bitrate": 20480
},
"stiching": {
"mode": "pano",
"mime": "h264",
"width": 3840,
"height": 1920,
"framerate": 30,
"bitrate": 10240
},
"audio": {
"mime":'aac',
"sampleFormat":'s16',
"channelLayout":'stereo',
"samplerate":48000,
"bitrate":128
}
},
"stabilization": True
}
try:
response = requests.post(self.command_api, **self.post_dic)
if response.status_code == 200:
self.signals.log.emit(response.json())
self.signals.log.emit("previe url: rtsp://%s/live/preview" % (self.camera_adress[7:]) )
results = True
else:
self.signals.log.emit(response.json())
results = False
except Exception as e:
self.connected = False
self.signals.log.emit(e)
return results
def stop_preview(self):
if self.connected:
self.post_dic['json'] = {"name": "camera._stopPreview"}
try:
response = requests.post(self.command_api, **self.post_dic)
if response.status_code == 200:
self.signals.log.emit(response.json())
results = True
else:
self.signals.log.emit(response.json())
results = False
except Exception as e:
self.connected = False
self.signals.log.emit(e)
return results
def get_option(self):
self.post_dic['json'] = {
"name": "camera._getOptions",
"parameters": {
"property": "origin"
}
}
response = requests.post(self.command_api, **self.post_dic)
return response
def start_live(self, parm_dict):
liveUrl = "%s/live" % (self.rtmp_server)
if self.connected:
self.post_dic['json'] = {
"name": "camera._startLive",
"parameters": {
"origin": {
"mime": "h265",
"width": 3840,
"height": 2160, #2880 for max size,2160 for stitching
"framerate": 30,
"bitrate": 20480,
"liveUrl": liveUrl,
"saveOrigin": False
},
# "stiching": {
# "mode": "pano",
# "mime": "h264",
# "width": 3840, # 3840*1920 for normal, 7680*3840 for max
# "height": 1920,
# "framerate": 30,
# "bitrate": 10240,
# "_liveUrl": "rtsp://192.168.1.111:8554/live/stitch"
# },
"audio": {
"mime":'aac',
"sampleFormat":'s16',
"channelLayout":'stereo',
"samplerate":48000,
"bitrate":64
},
"autoConnect":{
"enable": True,
"interval": 5,
"count": 3
}
},
"stabilization": False,
"mode": "origin live"
}
try:
response = requests.post(self.command_api, **self.post_dic)
if response.status_code == 200:
self.signals.log.emit(response.json())
results = True
else:
self.signals.log.emit(response.json())
results = False
except Exception as e:
self.connected = False
self.signals.log.emit(e)
return results
def stop_live(self):
if self.connected:
self.post_dic['json'] = {"name": "camera._stopLive"}
try:
response = requests.post(self.command_api, **self.post_dic)
if response.status_code == 200:
self.signals.log.emit(response.json())
results = True
else:
self.signals.log.emit(response.json())
results = False
except Exception as e:
self.connected = False
self.signals.log.emit(e)
return results
def get_image_param(self):
self.post_dic['json'] = {"name": "camera._getImageParam"}
response = requests.post(self.command_api, **self.post_dic)
self._status = response
return response