2021-10-25 09:44:04 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
import subprocess
|
|
|
|
|
import time
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
from cereal import log, car
|
|
|
|
|
import cereal.messaging as messaging
|
|
|
|
|
from selfdrive.test.helpers import phone_only, with_processes
|
|
|
|
|
# TODO: rewrite for unittest
|
|
|
|
|
from common.realtime import DT_CTRL
|
2022-06-11 16:38:24 -07:00
|
|
|
from system.hardware import HARDWARE
|
2021-10-25 09:44:04 -07:00
|
|
|
|
|
|
|
|
AudibleAlert = car.CarControl.HUDControl.AudibleAlert
|
|
|
|
|
|
|
|
|
|
SOUNDS = {
|
|
|
|
|
# sound: total writes
|
|
|
|
|
AudibleAlert.none: 0,
|
2021-12-07 13:45:49 -08:00
|
|
|
AudibleAlert.engage: 184,
|
|
|
|
|
AudibleAlert.disengage: 186,
|
|
|
|
|
AudibleAlert.refuse: 194,
|
|
|
|
|
AudibleAlert.prompt: 184,
|
|
|
|
|
AudibleAlert.promptRepeat: 487,
|
|
|
|
|
AudibleAlert.promptDistracted: 508,
|
|
|
|
|
AudibleAlert.warningSoft: 471,
|
|
|
|
|
AudibleAlert.warningImmediate: 470,
|
2021-10-25 09:44:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def get_total_writes():
|
|
|
|
|
audio_flinger = subprocess.check_output('dumpsys media.audio_flinger', shell=True, encoding='utf-8').strip()
|
|
|
|
|
write_lines = [l for l in audio_flinger.split('\n') if l.strip().startswith('Total writes')]
|
2021-12-04 07:57:19 +01:00
|
|
|
return sum(int(l.split(':')[1]) for l in write_lines)
|
2021-10-25 09:44:04 -07:00
|
|
|
|
|
|
|
|
class TestSoundd(unittest.TestCase):
|
|
|
|
|
def test_sound_card_init(self):
|
|
|
|
|
assert HARDWARE.get_sound_card_online()
|
|
|
|
|
|
|
|
|
|
@phone_only
|
|
|
|
|
@with_processes(['soundd'])
|
|
|
|
|
def test_alert_sounds(self):
|
2021-11-12 15:04:57 -08:00
|
|
|
pm = messaging.PubMaster(['deviceState', 'controlsState'])
|
2021-10-25 09:44:04 -07:00
|
|
|
|
|
|
|
|
# make sure they're all defined
|
2021-12-02 00:28:25 -08:00
|
|
|
alert_sounds = {v: k for k, v in car.CarControl.HUDControl.AudibleAlert.schema.enumerants.items()}
|
2021-10-25 09:44:04 -07:00
|
|
|
diff = set(SOUNDS.keys()).symmetric_difference(alert_sounds.keys())
|
|
|
|
|
assert len(diff) == 0, f"not all sounds defined in test: {diff}"
|
|
|
|
|
|
|
|
|
|
# wait for procs to init
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
for sound, expected_writes in SOUNDS.items():
|
|
|
|
|
print(f"testing {alert_sounds[sound]}")
|
|
|
|
|
start_writes = get_total_writes()
|
|
|
|
|
|
2021-12-02 19:55:19 -08:00
|
|
|
for i in range(int(10 / DT_CTRL)):
|
2021-11-12 15:04:57 -08:00
|
|
|
msg = messaging.new_message('deviceState')
|
|
|
|
|
msg.deviceState.started = True
|
|
|
|
|
pm.send('deviceState', msg)
|
|
|
|
|
|
2021-10-25 09:44:04 -07:00
|
|
|
msg = messaging.new_message('controlsState')
|
2021-12-02 19:55:19 -08:00
|
|
|
if i < int(6 / DT_CTRL):
|
|
|
|
|
msg.controlsState.alertSound = sound
|
|
|
|
|
msg.controlsState.alertType = str(sound)
|
|
|
|
|
msg.controlsState.alertText1 = "Testing Sounds"
|
|
|
|
|
msg.controlsState.alertText2 = f"playing {alert_sounds[sound]}"
|
|
|
|
|
msg.controlsState.alertSize = log.ControlsState.AlertSize.mid
|
2021-10-25 09:44:04 -07:00
|
|
|
pm.send('controlsState', msg)
|
|
|
|
|
time.sleep(DT_CTRL)
|
|
|
|
|
|
2021-12-05 13:33:12 -08:00
|
|
|
tolerance = expected_writes / 8
|
2021-10-25 09:44:04 -07:00
|
|
|
actual_writes = get_total_writes() - start_writes
|
2021-12-05 13:33:12 -08:00
|
|
|
print(f" expected {expected_writes} writes, got {actual_writes}")
|
2021-10-25 09:44:04 -07:00
|
|
|
assert abs(expected_writes - actual_writes) <= tolerance, f"{alert_sounds[sound]}: expected {expected_writes} writes, got {actual_writes}"
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|