CRAS: iodev - factor out common audio thread funcs.

Pull the code for running the audio thread out of alsa_io/empty_iodev.
It can be reused by more types of iodevs, such as bluetooth.  This
will also enable pairing input and output iodevs to support unified,
synchronous, IO.

empty_iodev - Remove code that was duplicated from alsa_io and use the
common code refactored into audio_thread.

Mostly this just moves code around, while the patch is bug, there
aren't many functional changes.

BUG=chromium-os:35272
TEST=updated alsa_io_unittest and added audio_thread_unittest
run cras select empty iodev with the test client and play/record audio.

Change-Id: I7765cd33695c06689ad753708f7a0006d420cc37
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: thread ut - add constant for extra sleep time.

Add a static const that defines the amount of extra frames that should
be added to the sleep time when capturing audio.  This time is added
to insure that enough frames are ready in hardware when the system
wakes up.

BUG=chromum-os:35272
TEST=make check

Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - Don't set iodev format twice.

The iodev format must be set before it is opened, so don't call it again
when opening the device.

BUG=chromium-os:35272
TEST=alsa_io_unittest

Change-Id: I287cc7805f4fa11acfa0125715ebbbcc42d7b4d2
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_io - Apply settings only when open.

Check if the device is open before applying the settings.  If not open
they will be applied in init_device_settings when the device is
opened.  Check if the device is open directly instead of assuming it
is open if there are streams attached.

BUG=chromium-os:35272
TEST=alsa_io_unittest

Change-Id: I8a36c0c64265ce741cf0af5d1be923deca6a2720
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: rclient - Add helper to close client fd.

The unit test was calling close(0).  Instead add a helper that can be
stubbed to close the fd.  This new helper mirrors the helper used to
open the device.

Change-Id: I52f656b173423e0dc5c65ed7aeb12f8e84537961
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: iodev - Create thread outside of iodev.

Instead of having the iodev create its own thread, create the thread and
pass it to the iodev.  This will allow for iodevs to share the same
thread, enabling pairing input and output thread for unified IO.

This moves a bunch of code from alsa_io/iodev into audio thread as the
functionality will be common to all iodevs, not just alsa.

BUG=chromium-os:35272
TEST=updated unittest, plus, GTalk and YouTube on device.
Signed-off-by: Dylan Reid <dgreid@chromium.org>

Change-Id: I90bcd53243c8564922d0e716ed5f3e176aff566e
CRAS: rstream: Track thread instead of iodev.

Continuing to prepare to have an input and output device for each
stream, don't track which device is attached to the stream, just track
the thread.  If the device is needed, it is contained in the thread.

BUG=chromium-os:35272
TEST=make check; record and playback audio with the test client.

Change-Id: I5377e93a8ddb787277f6b51b59ea7f52ab3762c5
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: thread UT - Don't rely on thread creation.

The audio thread unit test was relying on the running thread.  This
isn't what needs to be tested and can lead to flakiness, instead, call
the thread functions directly from the test to check their output.

BUG=none
TEST=update unit tests.

Change-Id: I2d5be42fdcad7bfc6279659f2ba06cdf15e8988d
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio thread - Track input and output device.

Instead of each thread having a single device pointer, keep a separate
pointer for the input and output devices.  This will allow for a future
change to generate a combined input and output thread.

BUG=chromium-os:35272
TEST=updagted unit tests, playback and record audio with the test
client.

Change-Id: I0500d06cc9c08cee597f9a7d6fe66bfa4fc9c788
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio thread - user helper to calculate sleep time.

Using the helper will help to combine the read and write functions in
a future commit.

BUG=chromium-os:35272
TEST=make check

Change-Id: I802938282de1216caf73c8737c1b9cb957f222b3
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio thread - Make audio callbacks more similar.

Make the calculation of how long to sleep more similar for read and
write callbacks.  This is done to make them easier to merge into a
common callback.  The goal is to get read and write as similar as
possible before combining them.

BUG=chromium-os:35272
TEST=make check

Change-Id: I63cae3763df3105f8b222d77e05ec81fd3f6231a
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio thread - Check dev running.

The input path wasn't checking if the device was running in the case
where samples stopped being captured, add that and a UT to verify.

BUG=chromium-os:35272
TEST=updated audio_thread_unittest

Change-Id: I370a20b82715ec498d76e70d8ff43943b99e77b5
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio thread - remove extra local.

Don't need the extra local var.  Not having it makes read and write
more similar.

BUG=chromium-os:35272
TEST=make check

Change-Id: Ib20c4565009ca3373779668dbf46ecf947364329
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio thread - Make local function static.

These functions used to be used outside of this file. Since they are
no longer make them static.

BUG=chromium-os:35272
TEST=make check

Change-Id: I9288cccc869a6d9213f75f3371b451f7cfded8bb
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio thread - Combine common parts of read and write audio.

Create a unified io function that can handle both input and output
streams.  A follow on commit will add the ability for a single stream
to have both an input and an output component.

BUG=chromium-os:35272
TEST=play and record with test client in addition to unit tests.

Signed-off-by: Dylan Reid <dgreid@chromium.org>

Change-Id: I866892002896b8bad0c38bb290dd7053d25ce65d
CRAS: audio thread - Set iodev from thread.

Instead of setting it from rclient.  It was too difficult to determine
when to clear it from rclient, do that from the thread as well.

BUG=chromium-os:35272
TEST=make check

Change-Id: Ib21ce3517ee30f9899fd314d2449f13e4d38939d
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: rstream UT - Add input creation test.

BUG=none
TEST=this is a test.

Change-Id: I307df7c1ee0b0d9b7884ca16d8c3b0391ffeeb5e
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: rstream - separate shm init.

To allow for a future commit to add input and output shm sections to a
stream.

BUG=chromium-os:35272
TEST=make check

Change-Id: If1845cf3501e83a15523e6e46426ae8732a74a14
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: rstream - Store input and output shm pointer.

Allow a stream to have both input and output shm areas.  Only one of
the two is used until a following commit enables unified input/output
streams.

BUG=chromium-os:35272
TEST=updated unittests and play audio tiwh test client.

Change-Id: I00f4b3090767f4f0c0d286a6b649b2719ea28ce1
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: alsa_jack - Replace asserts with checks.

Instead of asserting, fail to initialize the jack list.  No need to
assert if an index is out of range.

BUG=none
TEST=updated alsa_jack_unittest

Change-Id: If95a87dae3a469057d2e726406132992f88511bc
Signed-off-by: Dylan Reid <dgreid@chromium.org>