replay.py 1.71 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
import os
10
from threading import Thread, Event
Antoine Lima's avatar
Antoine Lima committed
11 12

from main import MainWin
13 14 15 16 17 18 19
from settings import Settings

onRasp = os.uname()[1] == 'raspberrypi'

if onRasp:
	import picamera

20
class Replay(Thread):
Antoine Lima's avatar
Antoine Lima committed
21
	def __init__(self, side):
22
		Thread.__init__(self)
Antoine Lima's avatar
Antoine Lima committed
23
		self.replayPath = MainWin.getContent('Replay {}.mp4'.format(side.name))
24 25 26 27 28 29 30 31 32 33 34
		self.shutdown = False
		
		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'])
		self.start_flag = Event()
		self.stop_flag = Event()
		self.stopped_flag = Event()
Antoine Lima's avatar
Antoine Lima committed
35 36

	def start_recording(self):
37 38 39
		if onRasp:
			self.start_flag.set()
			
Antoine Lima's avatar
Antoine Lima committed
40
	def stop_recording(self):
41
		if onRasp:
42 43 44 45 46 47
			self.stop_flag.set()
			self.stopped_flag.wait()
			
			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 70 71 72 73 74 75
	def stop(self):
		self.start_flag.set()
		self.shutdown = True
	
	def run(self):
		while not self.shutdown:
			print('1')
			self.start_flag.wait()
		
			print('2')
			if not self.shutdown:
				print('3')
				self.cam.start_recording(self.stream, Settings['picam.format'])
				try:
					while not self.stop_flag.is_set():
						self.cam.wait_recording(1)
						print('4')
				
				finally	:
					self.cam.stop_recording()
    
				self.stream.copy_to(self.replayPath)
				self.stream.clear()
				self.stopped_flag.set()
				print('5')
Antoine Lima's avatar
Antoine Lima committed
76

77 78 79
		self.cam.close()
		self.stream.close()
		print('6')