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>
Reviewed-on: https://chromium-review.googlesource.com/217159
Reviewed-by: Hsinyu Chao <hychao@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>
Reviewed-on: https://chromium-review.googlesource.com/217172
Reviewed-by: Owen Lin <owenlin@chromium.org>
Reviewed-by: Chinyue Chen <chinyue@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>
Reviewed-on: https://chromium-review.googlesource.com/217173
Reviewed-by: Chinyue Chen <chinyue@chromium.org>
CRAS: byte_buffer - Add size helpers.

Add helpers to check the total number of bytes available, and to
return both the pointer to the buffer and the number of bytes that can
be read or written.  This will be used by server side sample rate
conversion.

Change-Id: I1939c85cc5961240b6f6317eda80d81aee82f65b
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217175
Reviewed-by: Owen Lin <owenlin@chromium.org>
Reviewed-by: Chinyue Chen <chinyue@chromium.org>
CRAS: audio_thread - Remove temp variable.

avail_frames was only used once, remove it and use the result of the
function call directly.

Change-Id: I70c0554b4efe60fdd48435c4adda895c6823f968
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217176
Reviewed-by: Chinyue Chen <chinyue@chromium.org>
CRAS: audio_thread - log write_limit from possibly_read

This will be useful to have logged before sample rate conversion on
capture.

Change-Id: I8f3873590b636843632f43dceaf1abff8a2ba506
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217177
Reviewed-by: Chinyue Chen <chinyue@chromium.org>
CRAS: Implement format conversion for the capture path.

Add format conversion for capture samples on the server side.

Change-Id: I056b5ae4362ded6e2be1cebbc4cef8ab3ed80ac0
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217178
CRAS: move timestamp setting to dev_stream

This ins't really related to iodev, move it to devstream.

Change-Id: I8683eeba2b765ca59b5a8370d5927138158ede0b
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217179
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: Account for SRC when reporting delay.

The delay reported by the device need to go through sample rate
conversion to the remote sample rate before being reported to the
client.

Change-Id: I2e9b9d5f8b0dfedf5a14a99bc946053fccecaaa6
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217180
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
Reviewed-by: Chinyue Chen <chinyue@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>
Reviewed-on: https://chromium-review.googlesource.com/217170
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: Only set dev format from the audio thread.

Since the format converter now lives in the audio thread, there is no
need to update the format of the rstream based on the device format.
Now set the device format when adding the first stream in the audio
thread.

BUG=none
TEST=unittest, plus play and record audio with multiple test clients.

Change-Id: I7288529e73a27b78a60c9076442577018e4aa47e
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217174
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_thread - move first_dev helpers.

They are needed earlier in the file by a later commit

BUG=none
TEST=unittests

Change-Id: I37196bddc22c9fcf63cd6e01f61183b51697cc5a
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217490
Reviewed-by: Chinyue Chen <chinyue@chromium.org>
CRAS: audio_thread - pre-fill the odev when opening it.

Filling the odev with one cb_threshold of data will prevent the client
from seeing two quick requests when adding the first stream.

BUG=none
TEST=unittest and play audio on Yoshi.

Change-Id: Id98aaaf18164e684ba07185f9ae11e9264a219c0
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217491
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: util - Add helper to add timespecs.

BUG=none
TEST=none =(

Change-Id: Idf70a1cc038395dca20de385401525b755d9f79f
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217492
Reviewed-by: Owen Lin <owenlin@chromium.org>
baytrail: Fix ucm configuration for external mic

BUG=None
TEST=On clapper, `cras_test_client --dump_server_info` shows a
     0-20 range for Capture Gain when external mic is connected
     (instead of 0-0).

Change-Id: If669bbae6b89da8641c931e2eb948af8445863e3
Reviewed-on: https://chromium-review.googlesource.com/218940
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Commit-Queue: Nicolas Boichat <drinkcat@chromium.org>
Tested-by: Nicolas Boichat <drinkcat@chromium.org>
CRAS: iodev - Set default channel count/layout

When an iodev fails to update channel layout for the format
of the appending stream is using. Fall back to use a default
channel count and matched layout, and let format conversion
do the rest of the work.

This is to fix a bug on ARM and BYT devices, and any USB
headsets don't support configuring channel map. The channel_layout
of the format could be not matched with its channel count, hence
causing record issues.

BUG=chromium:415412
TEST=unittest, youtube.com/my_webcam on Pixel and diasy

Change-Id: Ie99ed6cbd9ae9ccb2ade1b7e8a7f26cc292c6bd3
Reviewed-on: https://chromium-review.googlesource.com/218802
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Commit-Queue: Hsinyu Chao <hychao@chromium.org>
Tested-by: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_thread - set stream draining on error.

If there is an error communicating with a stream, mark is as draining
so it is removed as soon as the audio is used up.

BUG=none
TEST=play audio on Blaze

Change-Id: Ie6d41a7fcdaa74746d9ecd5ee144a2f287b928df
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217532
Reviewed-by: Chinyue Chen <chinyue@chromium.org>
CRAS: Move rstream request to dev_stream.

Move requesting playback samples to a function in dev_stream.  This
removes one more dependency on rstream from audio_thread and will make
a good place to update stream-specific timers in a future change.

Change-Id: I3ccf49b17135ab44050be7cecc132021d2ead2c5
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/217533
CRAS: Keep next callback time per dev_stream.

For each stream, save the time it should next be serviced.  This time
is start + n * callback_number.  Add one callback threshold of sleep
time after every callback.

This time will later be used to better calculate when to make
callbacks to a stream.

Change-Id: I210329b320bdd0b9b885ad1d3ce0d50b166083f0
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: audio_thread - Prefill with more zeros for min_buffer_level > 0

If a device has min_buffer_level set, start with cb_threshold +
min_buffer_level samples.  Without this the device will need to make
more than one consecutive callbacks to get filled to the appropriate
level.

BUG=none
TEST=play audio to BT and USB

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