我的客户端/服务器出了问题,我从mjpg流媒体(https://github.com/jacksonliam/mjpg-streamer)创建了流媒体gui。每次我尝试使用“设置”按钮更改流分辨率时,它会断开连接并断开套接字。有什么想法有什么不对? 我认为问题在于os.killpg,但我不知道,如何解决这个问题。
现在我应该设置分辨率,当我想要更改它时,我必须断开连接,更改它并再次连接。我需要在流开启期间更改它。
谢谢
服务器:
proc = subprocess.Popen("", shell=True)
proc.wait()
reg_vyraz = "Start(\d+)x(\d+)x(\d+)"
try:
while True:
connection, adress = comms_socket.accept()
print("Connected")
try:
while True:
received = connection.recv(4096).decode('UTF-8')
if proc.poll() is None: # takto zjistujeme, jestli proces bezi nebo je mrtvy, "is None" znamena ze jeste bezi
os.killpg(os.getpgid(proc.pid), signal.SIGINT) # takto proces vrazdime, potom se muze dat jeste proc.wait, aby skutecne skoncil
proc.wait()
if (len(received) == 0):
break;
m = re.match(reg_vyraz, received)
if m:
hodnoty = m.groups()
sirka = hodnoty[0]
vyska = hodnoty[1]
fps = hodnoty[2]
print("s={0}, v={1}, f={2}".format(sirka, vyska, fps))
prikaz = 'mjpg_streamer -o "/usr/local/lib/mjpg-streamer/output_http.so -w /usr/local/share/mjpg-streamer/www" -i "/usr/local/lib/mjpg-streamer/input_raspicam.so -x {0} -y {1} -fps {2} -ex night"'.format(sirka, vyska, fps)
print(prikaz)
proc = subprocess.Popen(prikaz, shell=True)
proc.poll() is None
客户端:
self.stream_socket = QTcpSocket(self)
self.ui.push_connect.clicked.connect(self.OnPushConnect)
self.ui.push_set.clicked.connect(self.OnPushSet)
self.network_manager = QNetworkAccessManager()
self.network_manager.finished.connect(self.OnNetworkManagerFinished)
self.stream_data = b''
@pyqtSlot()
def OnPushConnect(self):
if (self.is_connected):
self.stream_socket.abort()
else:
self.stream_socket.abort()
self.stream_socket.connectToHost(self.ui.edit_addr.text(),
int(self.ui.spin_port_stream.text()))
def SendStreamMessage(self, message):
self.stream_socket.write(bytes(message, "UTF-8"))
@pyqtSlot()
def OnPushSet(self):
width = self.ui.spin_width.value()
height = self.ui.spin_height.value()
fps = self.ui.spin_fps.value()
message = "Start{0}x{1}x{2}".format(width, height, fps)
self.SendStreamMessage(message)
print("Started delay for request")
QTimer.singleShot(2000, self.OnDelayedStreamRequest)
@pyqtSlot()
def OnStreamDataReceived(self):
self.stream_data += bytes(self.stream_reply.readAll())
start = self.stream_data.find(b'\xff\xd8')
end = self.stream_data.find(b'\xff\xd9')
if start != -1 and end != -1:
jpg = self.stream_data[start:end+2]
self.stream_data = self.stream_data[end+2:]
img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
height, width, bpp = img.shape
bytes_per_line = bpp * width
qimg = QImage(img.data, width, height, bytes_per_line, QImage.Format_RGB888)
self.ui.label_stream.setPixmap(QPixmap.fromImage(qimg))