-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinetworkvideostream.py
60 lines (51 loc) · 1.79 KB
/
pinetworkvideostream.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
"""
Project Needle
Auburn University
Senior Design | Fall 2019
Team Members:
Andrea Walker, Rich Surgenor, Jackson Solley, Jacob Hagewood,
Justin Sutherland, Laura Grace Ayers.
"""
import socket
import struct
import time
import threading
import picamera
import io
IP = "192.168.0.1"
# TODO: auto try to reconnect if disconnected from server...
class ImageStreamer(threading.Thread):
def __init__(self, connection, client_socket, pool, connection_lock, pool_lock):
super(ImageStreamer, self).__init__()
self.stream = io.BytesIO()
self.event = threading.Event()
self.terminated = False
self.start()
self.connection = connection
self.client_socket = client_socket
self.pool = pool
self.connection_lock = connection_lock
self.pool_lock = pool_lock
self.id = None
self.dead = False
def run(self):
# This method runs in a background thread
while not self.terminated:
# Wait for the image to be written to the stream
if self.event.wait(1):
try:
with self.connection_lock:
# < = little endian, L = unsigned int
self.connection.write(struct.pack('<L', self.stream.tell()))
self.connection.flush()
self.stream.seek(0)
self.connection.write(self.stream.read())
except Exception as e:
print("caught exception!!")
self.dead = True
finally:
self.stream.seek(0)
self.stream.truncate()
self.event.clear()
with self.pool_lock:
self.pool.append(self)