Make audiovideo_CRASFormatConversion not so flaky.

Fix the following issues:

1. Use fixed but different sine tones in the two audio streams at different
   sampling rate
2. Set output node volume level to 100

TEST=Run the test on stout and parrot.
BUG=chromium:307378

Change-Id: I36df3522f8b116f142d5147ffe2c71cbf399332d
Reviewed-on: https://chromium-review.googlesource.com/177430
Reviewed-by: Owen Lin <owenlin@chromium.org>
Tested-by: Owen Lin <owenlin@chromium.org>
Commit-Queue: Owen Lin <owenlin@chromium.org>
diff --git a/client/cros/audio/cras_utils.py b/client/cros/audio/cras_utils.py
new file mode 100644
index 0000000..92279e6
--- /dev/null
+++ b/client/cros/audio/cras_utils.py
@@ -0,0 +1,97 @@
+# Copyright (c) 2013 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import logging
+import re
+
+from autotest_lib.client.bin import utils
+from autotest_lib.client.cros.audio import audio_helper
+
+_CRAS_TEST_CLIENT = '/usr/bin/cras_test_client'
+_RE_SELECTED_OUTPUT_NODE = re.compile('Selected Output Node: (.*)')
+_RE_SELECTED_INPUT_NODE = re.compile('Selected Input Node: (.*)')
+
+def playback(*args, **kargs):
+    """A helper function to execute the playback_cmd."""
+    audio_helper.execute(playback_cmd(*args, **kargs))
+
+def capture(*args, **kargs):
+    """A helper function to execute the capture_cmd."""
+    audio_helper.execute(capture_cmd(*args, **kargs))
+
+def playback_cmd(playback_file, buffer_frames=None, duration=None, rate=44100):
+    """Gets a command to playback a file with given settings.
+
+    @param playback_file: the name of the file to play. '-' indicates to
+                          playback raw audio from the stdin.
+    @param buffer_frames: total number of frames to buffer.
+    @param duration: seconds to playback
+    @param rate: the sampling rate
+    """
+    args = [_CRAS_TEST_CLIENT]
+    args += ['--playback_file', playback_file]
+    if buffer_frames is not None:
+        args += ['--buffer_frames', str(buffer_frames)]
+    if duration is not None:
+        args += ['--duration', str(duration)]
+    args += ['--rate', str(rate)]
+    return args
+
+def capture_cmd(capture_file, buffer_frames=None, duration=10, rate=44100):
+    """Gets a command to capture the audio into the file with given settings.
+
+    @param capture_file: the name of file the audio to be stored in.
+    @param buffer_frames: total number of frames to bufffer.
+    @param duration: seconds to record.
+    @param rate: the sampling rate.
+    """
+    args = [_CRAS_TEST_CLIENT]
+    args += ['--capture_file', capture_file]
+    if buffer_frames is not None:
+        args += ['--buffer_frames', str(buffer_frames)]
+    args += ['--duration', str(duration)]
+    args += ['--rate', str(rate)]
+    return args
+
+def set_system_volume(volume):
+    """Set the system volume.
+
+    @param volume: the system output vlume to be set(0 - 100).
+    """
+    args = [_CRAS_TEST_CLIENT]
+    args += ['--volume', str(volume)]
+    audio_helper.execute(args)
+
+def set_node_volume(node_id, volume):
+    """Set the volume of the given output node.
+
+    @param node_id: the id of the output node to be set the volume.
+    @param volume: the volume to be set(0-100).
+    """
+    args = [_CRAS_TEST_CLIENT]
+    args += ['--set_node_volume', '%s:%d' % (node_id, volume)]
+    audio_helper.execute(args)
+
+def set_capture_gain(gain):
+    """Set the system capture gain.
+    @param gain the capture gain in db*100 (100 = 1dB)
+    """
+    args = [_CRAS_TEST_CLIENT]
+    args += ['--capture_gain', str(gain)]
+    audio_helper.execute(args)
+
+def dump_server_info():
+    """Gets the CRAS's server information."""
+    return utils.system_output('%s --dump_server_info' % _CRAS_TEST_CLIENT)
+
+def get_selected_nodes():
+    """Returns the pair of active output node and input node."""
+    server_info = dump_server_info()
+    output_match = _RE_SELECTED_OUTPUT_NODE.search(server_info)
+    input_match = _RE_SELECTED_INPUT_NODE.search(server_info)
+    if not output_match or not input_match:
+        logging.error(server_info)
+        raise RuntimeError('No match for the pattern')
+
+    return (output_match.group(1).strip(), input_match.group(1).strip())