Improved management of PiCamera start/stop streaming events
This commit is contained in:
parent
b880a02ef6
commit
fb2ff7d1d6
1 changed files with 13 additions and 21 deletions
|
@ -85,8 +85,8 @@ class CameraPiPlugin(CameraPlugin):
|
||||||
self._streaming_thread = None
|
self._streaming_thread = None
|
||||||
self._time_lapse_stop_condition = threading.Condition()
|
self._time_lapse_stop_condition = threading.Condition()
|
||||||
self._recording_stop_condition = threading.Condition()
|
self._recording_stop_condition = threading.Condition()
|
||||||
self._streaming_stop_condition = threading.Condition()
|
|
||||||
self._output = None
|
self._output = None
|
||||||
|
self._can_stream = False
|
||||||
|
|
||||||
# noinspection PyUnresolvedReferences,PyPackageRequirements
|
# noinspection PyUnresolvedReferences,PyPackageRequirements
|
||||||
def _get_camera(self, **opts):
|
def _get_camera(self, **opts):
|
||||||
|
@ -493,24 +493,23 @@ class CameraPiPlugin(CameraPlugin):
|
||||||
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
server_socket.bind(('0.0.0.0', listen_port))
|
server_socket.bind(('0.0.0.0', listen_port))
|
||||||
server_socket.listen(1)
|
server_socket.listen(1)
|
||||||
|
server_socket.settimeout(1)
|
||||||
|
|
||||||
# noinspection PyBroadException
|
# noinspection PyBroadException
|
||||||
def streaming_thread():
|
def streaming_thread():
|
||||||
try:
|
try:
|
||||||
self.logger.info('Starting streaming on port {}'.format(listen_port))
|
self.logger.info('Starting streaming on port {}'.format(listen_port))
|
||||||
should_stop = False
|
|
||||||
|
|
||||||
while not should_stop:
|
while self._can_stream:
|
||||||
sock = None
|
sock = None
|
||||||
stream = None
|
stream = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
server_socket.settimeout(1)
|
|
||||||
sock = server_socket.accept()[0]
|
sock = server_socket.accept()[0]
|
||||||
stream = sock.makefile('wb')
|
stream = sock.makefile('wb')
|
||||||
self.logger.info('Accepted client connection from {}'.format(sock.getpeername()))
|
self.logger.info('Accepted client connection from {}'.format(sock.getpeername()))
|
||||||
except socket.timeout:
|
except socket.timeout:
|
||||||
pass
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if stream:
|
if stream:
|
||||||
|
@ -520,29 +519,24 @@ class CameraPiPlugin(CameraPlugin):
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
self.logger.info('Client closed connection')
|
self.logger.info('Client closed connection')
|
||||||
finally:
|
finally:
|
||||||
if not should_stop:
|
if sock:
|
||||||
self._streaming_stop_condition.acquire()
|
sock.close()
|
||||||
should_stop = self._streaming_stop_condition.wait(timeout=1)
|
finally:
|
||||||
self._streaming_stop_condition.release()
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
server_socket.close()
|
||||||
camera.stop_recording()
|
camera.stop_recording()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sock.close()
|
camera.close()
|
||||||
except:
|
|
||||||
pass
|
|
||||||
finally:
|
|
||||||
try:
|
|
||||||
server_socket.close()
|
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self._streaming_thread = None
|
self._streaming_thread = None
|
||||||
self.logger.info('Stopped camera stream')
|
self.logger.info('Stopped camera stream')
|
||||||
|
|
||||||
|
self._can_stream = True
|
||||||
self._streaming_thread = threading.Thread(target=streaming_thread)
|
self._streaming_thread = threading.Thread(target=streaming_thread)
|
||||||
self._streaming_thread.start()
|
self._streaming_thread.start()
|
||||||
|
|
||||||
|
@ -556,9 +550,7 @@ class CameraPiPlugin(CameraPlugin):
|
||||||
self.logger.info('No recording thread is running')
|
self.logger.info('No recording thread is running')
|
||||||
return
|
return
|
||||||
|
|
||||||
self._streaming_stop_condition.acquire()
|
self._can_stream = False
|
||||||
self._streaming_stop_condition.notify_all()
|
|
||||||
self._streaming_stop_condition.release()
|
|
||||||
|
|
||||||
if self._streaming_thread:
|
if self._streaming_thread:
|
||||||
self._streaming_thread.join()
|
self._streaming_thread.join()
|
||||||
|
|
Loading…
Reference in a new issue