alsa_utils: Check for correct record device id

Currently we assume device id is zero and use plughw:0 for record.
However, it is not true for oak, which is on device 1.
We should get device id before doing record by alsa utils.

BUG=none
TEST=Run audio_Microphone

Change-Id: I295e81bd6982813ac3d7e129cf60c3b3d310d107
Signed-off-by: Koro Chen <koro.chen@mediatek.com>
Reviewed-on: https://chromium-review.googlesource.com/294049
Reviewed-by: Chinyue Chen <chinyue@chromium.org>
diff --git a/client/cros/audio/alsa_utils.py b/client/cros/audio/alsa_utils.py
index 0acdfbe..79d4676 100644
--- a/client/cros/audio/alsa_utils.py
+++ b/client/cros/audio/alsa_utils.py
@@ -12,6 +12,7 @@
 APLAY_PATH = '/usr/bin/aplay'
 AMIXER_PATH = '/usr/bin/amixer'
 CARD_NUM_RE = re.compile('(\d+) \[.*\]:.*')
+DEV_NUM_RE = re.compile('.* \[.*\], device (\d+):.*')
 CONTROL_NAME_RE = re.compile("name='(.*)'")
 SCONTROL_NAME_RE = re.compile("Simple mixer control '(.*)'")
 
@@ -150,7 +151,22 @@
     card_id = get_first_soundcard_with_control(cname='Mic Jack', scname='Mic')
     if card_id is None:
         return None
-    return 'plughw:%d' % card_id
+
+    # Get first device id of this card.
+    cmd = ARECORD_PATH + ' -l'
+    p = cmd_utils.popen(shlex.split(cmd), stdout=cmd_utils.PIPE)
+    output, _ = p.communicate()
+    if p.wait() != 0:
+        raise RuntimeError('arecord -l command failed')
+
+    dev_id = 0
+    for line in output.splitlines():
+        if 'card %d:' % card_id in line:
+            match = DEV_NUM_RE.search(line)
+            if match:
+                dev_id = int(match.group(1))
+                break
+    return 'plughw:%d,%d' % (card_id, dev_id)
 
 
 def _get_sysdefault(cmd):