-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdjiplayer.py
99 lines (79 loc) · 2.78 KB
/
djiplayer.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from lib import USBDev
from lib import FIFOWriter
import time
import os
# import tkinter as tk
import subprocess
import shlex
StartPlayer = True
StatusInterval = 0.5
headset = USBDev.USBDev()
headset.StartDevCheckThread()
output = FIFOWriter.FIFOWriter()
output.Open()
# print("Writing to FIFO at " + output.FIFO)
# A bit of cheese. Raspberry Pi OS:
PlayCmd = "/usr/bin/ffplay -fs -i " + output.FIFO + " -analyzeduration 1 -probesize 32 -sync ext"
WrapPlayCmd = ["/usr/bin/lxterminal", "-e", PlayCmd]
PlayerProc = None
# State globals:
BytesWritten = 0
LastStatus = 0
StatusData = dict()
StatusData['Msg'] = "Starting Up"
###############################################################################
def PrintStatus(update):
"""Update the program status"""
global LastStatus
now = time.time()
if (now - LastStatus) < StatusInterval:
return
print(update['Msg'] + " ", end='\r')
LastStatus = now
###############################################################################
def StartPlayer():
global PlayCmd, WrapPlayCmd, PlayerProc
if PlayerProc:
# PlayerProc.terminate()
PlayerProc.kill()
subprocess.run(["/usr/bin/killall", "ffplay"])
# PlayerProc = subprocess.Popen(WrapPlayCmd)
PlayerProc = subprocess.Popen(shlex.split(PlayCmd),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
stdin=subprocess.DEVNULL
)
###############################################################################
while True:
PrintStatus(StatusData)
if not PlayerProc:
StartPlayer()
if headset.DevicePresent == False:
# print("Waiting on headset...")
StatusData['Msg'] = "Waiting on headset..."
# If we are waiting on a headset and data has been written, everything needs to be reset:
if BytesWritten:
BytesWritten = 0
output.Reset()
StartPlayer() # Really a restart, since we're unlinking the FIFO
# No sense being speedy on the loop when we are waiting on a human
time.sleep(.5)
continue
packet = headset.RecvData()
if packet == None:
# print("RecvData() returned None: {}".format(headset.LastError.strerror))
StatusData['Msg'] = "Device State: " + headset.DeviceStatus
continue
out = output.Write(packet)
if out:
BytesWritten += out
StatusData['Total'] = BytesWritten
else:
# print("Pausing output, no readers...")
StatusData['Msg'] = "Output buffer full, pausing output..."
time.sleep(.5)
continue
# print("In write loop, error: " + str(output.LastError) + ", total bytes written: " + str(BytesWritten))
StatusData['Msg'] = "Device State: " + headset.DeviceStatus
# time.sleep(.5)
# exit()