alsa_util: Add helper functions to get sysdefault device.

Helper functions to get sysdefault from aplay/arecord -L output.

BUG=chromium:419048
TEST=Tested on panther.

Change-Id: Ie46e5cc0a182e52a13dfd3e5e6364dc96f94ac26
Reviewed-on: https://chromium-review.googlesource.com/299041
Commit-Ready: Chinyue Chen <chinyue@chromium.org>
Tested-by: Chinyue Chen <chinyue@chromium.org>
Reviewed-by: Cheng-Yi Chiang <cychiang@chromium.org>
diff --git a/client/cros/audio/alsa_utils.py b/client/cros/audio/alsa_utils.py
index cdb25dc..0acdfbe 100644
--- a/client/cros/audio/alsa_utils.py
+++ b/client/cros/audio/alsa_utils.py
@@ -153,6 +153,30 @@
     return 'plughw:%d' % card_id
 
 
+def _get_sysdefault(cmd):
+    p = cmd_utils.popen(shlex.split(cmd), stdout=cmd_utils.PIPE)
+    output, _ = p.communicate()
+    if p.wait() != 0:
+        raise RuntimeError('%s failed' % cmd)
+
+    for line in output.splitlines():
+        if 'sysdefault' in line:
+            return line
+    return None
+
+
+def get_sysdefault_playback_device():
+    '''Gets the sysdefault device from aplay -L output.'''
+
+    return _get_sysdefault(APLAY_PATH + ' -L')
+
+
+def get_sysdefault_record_device():
+    '''Gets the sysdefault device from arecord -L output.'''
+
+    return _get_sysdefault(ARECORD_PATH + ' -L')
+
+
 def playback(*args, **kwargs):
     '''A helper funciton to execute playback_cmd.