CRAS: HFP: force disconnect for all kinds of read socket errors

There are cases we receive ECONNABORTED read error when
disconnect headset from host. Instead of including more and
more error codes that we need to force disconnect the service
level connection, do that for all read errors and log their
error string.

BUG=chromium:211636
TEST=Connect Jabra EASYCALL headset, and then disconnect
from bluetooth devices UI, see the corresponding SLC is
disconnected and destroyed.

Change-Id: I14a1dcca64decabb7a5e6903e9137fafc4e068a6
Reviewed-on: https://chromium-review.googlesource.com/216093
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: Hsinyu Chao <hychao@chromium.org>
Commit-Queue: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_thread - Handle dev error during drain.

If get_buffer returns an error, propagate that back up so that the
device is closed immediately.  If this error is ignored during
draining, it will stick the audio thread in a tight loop constantly
retrying.  Instead of that, since the device is already draining, if
an error happens abort draining and close the device.

BUG=chrome-os-partner:31818
TEST=play several youtube videos.

Change-Id: I6f2112839f55af3a51574283b5efdbd585bb5549
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/216701
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: fmt_conv - make input buffer const.

The input buffer to convert frames isn't modified.

Change-Id: Ia39737d40d5e11fa2d6a62fbdd4c322f0fcf21c1
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/216778
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: fmt_conv - Add accessors for in and out formats.

These will be useful to get sizes and sample rates easily when doing
SRC on the server side.

Change-Id: If39d2f7f7d810cb9eb91548c35f439aba3b249b6
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/216779
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: fmt_conv - Move creation from client to fmt_conv.

Move the code that configures the format converter from cras_client
into the format conversion core.  This will allow for the server side
to reuse the same set up code when the SRC is added to it.

BUG=none
TEST=unittests, play two different formats of audio.

Change-Id: Ie0277ae66666ba70ae2c2cea7b067b3378f3da3e
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/216828
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: fmt_conv - pass input frames as a pointer.

Pass in a pointer to the number of input frames instead of the number.
This allows the number to be updated with the actual amount of frames
that were read.

BUG=none
TEST=make check

Change-Id: Ife3af9262c22e46ca740d40ea229d50c93948864
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217000
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
Reviewed-by: Chinyue Chen <chinyue@chromium.org>
CRAS: fmt_conv - Allow NULL conv in frame conversion.

If there is no format converter, than the number of converted frames
will be the same as before conversion.  Checking within these
functions will allow other checks to be removed that are present
before each call to them.

Change-Id: I07972146b92c0b9a9708569c3c5e1864cf9132f1
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217001
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_thread - Add stream list for each device.

In later changes this list will be used instead of a thread-wide list
to track attached streams.

Change-Id: I07aed4dea9d6037c0d2ed0f03f3365adbc4d571e
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio_thread - Keep list of attached streams per device.

Instead of keeping a list of attached streams in the audio thread
struct, keep a list per device.  This will allow for different streams
to be routed to different or multiple devices in later commits.

Change-Id: I7d3d8437c96f3496375864575c36dab0b7c45c81
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: mix - Re-order the checks in mix_add_stream.

Re-order the check for mute to be within the while loop.  This will
make mixing streams that require SRC on the server side simpler in a
later change.

Change-Id: I1b2ea5e542cfdec0e562e31cd70bd17276feb745
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio_thread - remove fd from io stream

The fd parameter was just used as a cache.  Remove that and instead
get it from the stream when needed.  This is done so that
cras_io_stream can have per-stream details removed.  Eventually
cras_io_stream will be used for a stream to device mapping, and the
same stream could be held by multiple devices.

BUG=none
TEST=unittest

Change-Id: I9f82d3af07c6f29270d3273105213cd652b1ce10
CRAS: audio_thread - use mix_mute_buffer helper.

Replace meset with cras_mux_mute_buffer.  No functional change.

Change-Id: I288736eb8141295e6ea01b6fb28324d49fc7570d
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio_thread - Factor cras_io_stream to its own file.

Rename cras_io_stream to dev_stream and move it to its own source
file.  In later commits this struct will take on additional
responsibilities keeping sample rate converters and tracking how much
of a stream has been written to each device.

BUG=none
TEST=make check

Change-Id: Ifa19e8f1bb3557ca7c7ff4546ad445729bc3ddfb
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: move stream mixing to dev_stream.

Move the shm interaction and some decision making to dev_stream as
this is a property of the stream.  cras_mix no longer needs to know
about shm and its unit test is now much cleaner.  This paves the way
to add sample rate conversion logic to dev_stream in a later commit.

BUG=none
TEST=make check, play a few simultaneous streams.

Change-Id: I5fbb3c670e86393a0c8d3454e29c4bf587a5015c
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: dev_stream - Add sample rate converter.

Add a sample rate converted to dev stream.  This adds an argument to
the constructor indicating the format of the device the stream will
play on.  If this format doesn't match the stream a sample rate
converter will be created.

A later change will modify audio_thread and rclient to use this
functionality.  For now rclient ensures that the formats match.

BUG=none
TEST=new unittest, the code doesn't run yet.

Change-Id: Id71e593cbed05abc1771e5232c0e181f862053f4
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio_thread - Handle dev_stream with a format converter.

If a dev_stream has a format converter, update the amount of frames
available and use the format converter when rendering samples.

BUG=none
TEST=none, not used yet.

Change-Id: Id1929b1cf8677a57852c4a873c19373bda3303a4
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: rclient - Accept the remote stream format.

Always accept the remote stream format when a stream is attached.  The
format conversion is now done in the audio thread, allow the client to
send whatever it wants.

This change will cause the client never to do format conversion, and
instead rely on the audio thread to convert to the format of the
device.

BUG=none
TEST=unittest, play record audio on blaze.

Change-Id: Ib6261a640cc237f74e92ee37ce555914d047a43e
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio_thread - don't check draining streams

If a stream is draining, don't check if it has replied with more data.

Change-Id: Ia085eb5985ecd802c56b5e1a0f55c3c4a651802f
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio_thread - remove unified timestamp.

Unified streams no longer exist, remove setting a timestamp for them.

Change-Id: Ibc1d8694f0eab27cd1850220626678164cde4a6b
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio_thread - Use helper to check capture frames.

Add a new helper to dev_stream that returns how many frames are
writable in the stream for capture samples.  This will be useful in
later commits as sample rate conversion can make determining this
non-trivial.

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