replay.py 1.68 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
Antoine Lima's avatar
Antoine Lima committed
10 11 12 13
from threading import Event
from multiprocessing import Process, Lock

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

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

if onRasp:
	import picamera

21

Antoine Lima's avatar
Antoine Lima committed
22 23 24 25 26
class LockableValue():
	def __init__(self, value):
		self.value = value
		self.mutex = Lock()

27
class Replay():
Antoine Lima's avatar
Antoine Lima committed
28 29 30 31 32
	def __init__(self, side):
		self.recording = LockableValue(False)
		self.replayPath = MainWin.getContent('Replay {}.mp4'.format(side.name))
		self.stopped = Event()

33 34 35 36 37 38 39 40
		if onRasp:
			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.format = Settings['picam.format']
			self.stream = picamera.PiCameraCircularIO(self.cam, seconds=Settings['replay.duration'])
Antoine Lima's avatar
Antoine Lima committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

	def start_recording(self):
		if not onRasp:
			self.stopped.set()
		else:
			self.recording.value = True
			self.stopped.clear()
			self.capture_process = Process(target=self.__capture)
			self.capture_process.start()

	def stop_recording(self):
		self.recording.val = True
		self.stopped.wait(timeout=2.0)
		return self.replayPath

56
	def capture(self, fileToSave):
57 58
		if onRasp:
			self.cam.start_recording(self.stream, self.format)
Antoine Lima's avatar
Antoine Lima committed
59

60
			try:
Antoine Lima's avatar
Antoine Lima committed
61 62 63
				recording = self.recording.val

				while recording:
64
					self.cam.wait_recording(1)
Antoine Lima's avatar
Antoine Lima committed
65
					recording = self.recording.val
66 67
			finally:
				self.cam.stop_recording()
Antoine Lima's avatar
Antoine Lima committed
68 69 70

			self.stopped.set()
			self.stream.copy_to(self.replayPath)
71 72
			self.cam.close()
			self.stream.close()