replay.py 1.8 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 18:34:40 2018

@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
"""

9
from threading import Thread, Event
Antoine Lima's avatar
Antoine Lima committed
10

11
from main import MainWin, OnRasp
12
13
from settings import Settings

14
if OnRasp:
15
16
	import picamera

17
class Replay(Thread):
Antoine Lima's avatar
Antoine Lima committed
18
	def __init__(self, side):
19
		Thread.__init__(self)
Antoine Lima's avatar
Antoine Lima committed
20
		self.replayPath = MainWin.getContent('Replay {}.mp4'.format(side.name))
21
22
23
24
25
		self.shutdown = False
		
		self.start_flag = Event()
		self.stop_flag = Event()
		self.stopped_flag = Event()
Antoine Lima's avatar
Antoine Lima committed
26

27
		if OnRasp:
28
29
30
31
32
33
34
			self.cam = picamera.PiCamera()
			self.cam.resolution = Settings['picam.resolution']
			self.cam.framerate = Settings['picam.fps']
			self.cam.hflip = Settings['picam.hflip']
			self.cam.vflip = Settings['picam.vflip']
			self.stream = picamera.PiCameraCircularIO(self.cam, seconds=Settings['replay.duration'])

Antoine Lima's avatar
Antoine Lima committed
35
	def start_recording(self):
36
		if OnRasp:
37
38
			self.start_flag.set()
			
Antoine Lima's avatar
Antoine Lima committed
39
	def stop_recording(self):
40
		if OnRasp:
41
42
43
			self.stop_flag.set()
			self.stopped_flag.wait()
			
44
            # Clear all control flags
45
46
47
			self.stop_flag.clear()
			self.start_flag.clear()
			self.stopped_flag.clear()
Antoine Lima's avatar
Antoine Lima committed
48

49
		return self.replayPath
Antoine Lima's avatar
Antoine Lima committed
50

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	def stop(self):
		self.start_flag.set()
		self.shutdown = True
	
	def run(self):
		while not self.shutdown:
			self.start_flag.wait()
		
			if not self.shutdown:
				self.cam.start_recording(self.stream, Settings['picam.format'])
				try:
					while not self.stop_flag.is_set():
						self.cam.wait_recording(1)
				
				finally	:
					self.cam.stop_recording()
    
				self.stream.copy_to(self.replayPath)
				self.stream.clear()
70
71

                # Set this flag to tell the calling thread that replay is saved
72
				self.stopped_flag.set()
Antoine Lima's avatar
Antoine Lima committed
73

74
75
		self.cam.close()
		self.stream.close()
76
77
78
	
	@staticmethod
	def isCamAvailable():
79
		return OnRasp # and other checks (ToDo)