CRAS: audio_thread - drain based on device buffer size.

The buffer size of the device and the thread may be different, we want
to drain based on the size of the device's buffer.  If the device
buffer is smaller than the thread's, there is no way to fill it all
the way up and this could end in an infinite loop.

BUG=none
TEST=remove last stream while playing to bluetooth device, notice cras
doesn't eat an entire cpu forever.

Change-Id: I62bf3d36b712346764108d7d03f45b40f3ae8e03
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206840
CRAS: audio_thread - Increase audio thread log size to 32k.

This allows more wake cycles to be dumped for debugging.

Change-Id: I75afd1c62e63c162ec5f07760fe276c2f3471393
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206186
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_thread - Log stream add/remove.

Log when streams are added or removed to the audio thread.  This will
make it easier to debug timing issues that occur when a stream is
attached.

BUG=chromium:244615
TEST=check audio log for new events.

Change-Id: I2d45de3c72dd0111f7ec1113b08ae06a56ad6438
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206187
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_thread - Move logging to its own header file.

Move the logging functions to a separate header file so that logging
can be done from outside of audio_thread.c.

BUG=none
TEST=cras_test_client --dump_s

Change-Id: I47dd00bbeb6879ec58ab92c07a2513a9a5e115c2
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206188
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_thread - Remove unused args from iodev callback.

The 'ts' and 'polled' arguments aren't needed.  hfp is the only user,
it totally ignores ts and used polled to check if it should run.  Only
call the callbacks when the fd is ready.

BUG=none
TEST=unittests pass.

Change-Id: Ic9413865fca58a2a635d23bda26826662496e733
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206189
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_thread - Allow disabling callbacks and write fds

Enable audio_thread callbacks to poll an fd for being ready for a
write.  This will allow the a2dp iodev to check when the bluetooth
socket is ready for more data.

BUG=chromium:244615
TEST=unittest only

Change-Id: I6e01aa8ca8dabf2ed0a5801b90b2dd4dbdd400e6
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206250
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: sbc - Return on error.

If there is an error from the sbc codec, propagate it.  If it doesn't
totally fill the buffer, keep going and mark what it wrote as written
instead of bailing out.

Change-Id: I1a14d4d6c674da03accb1c2c011dfb31b5cafa92
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206252
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: a2dp_io - Don't send empty packets.

Don't send a packet just because nothing was processed this time.  If
there isn't data in the buffer there isn't anything to send.

Change-Id: If93a1e2255ef896ed21087331ce99e6d9c7fb1d8
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206253
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: Add a byte_buffer helper.

Circular buffers are used all over the place.  Add a common
implementation of one.  This will be used by a2dp first, it can be
expanded to be used in other places later.

BUG=none
TEST=need unit tests

Change-Id: I3ed63a0b95e208e31b0c0bebd38e1de4e5f2355d
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206615
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: a2dp_io - Use the new byte buffer for the pcm buffer.

The pcm buffer will work as a circular buffer, use the new one.

This change will make separating the timing of bt socket and the
stream's client easier in a later change.

BUG=chromium:244615
TEST=play audio to jambox

Change-Id: Ibdae989b6f3cb10c8803e423bf31618ca1eef2f0
Signed-off-by: Dylan Reid <dgreid@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206254
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: alsa - Update channel map when channel count <= 2

This removes a special check for ch <= 2. Originally we
don't case what the channel layout is when handling mono
or stereo stream. In order to support 3-channel keyboard mic
stream, remove the check so that the channel layout can be
populated by actual hardware capability.

BUG=chromium:370368
TEST=Test on Pixel, select to and capture audio from keyboard
mic by cras_test_client. Add log to verify the updated channel
layout contains only FC(front center).

Change-Id: Ie71ce636abe56e4a750bf285a13d4e08fb442f28
Reviewed-on: https://chromium-review.googlesource.com/204676
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: Hsinyu Chao <hychao@chromium.org>
Commit-Queue: Hsinyu Chao <hychao@chromium.org>
CRAS: iodev: retain channel count/layout when set format

When sets format to iodev, don't set back the channel count
and channel layout to stream. This allows a input device to
serve audio buffer for a subset of channels of a input stream.

This change works together with later CLs to open two mics
for a 3-channel input stream, 2 channels out of which comes
from one mic, and the rest channel from the other mic.

BUG=chromium:370368
TEST=Start two input streams of different channel count/layout
at the same time to verify stream format can now be different
then iodev format.
"cras_test_client --capture_file /tmp/1" and
"cras_test_client --capture_file /tmp/2 --num_channels=3
--channel_layout=0,1,-1,-1,2,-1,-1,-1,-1,-1,-1"

Change-Id: I319644787c279b6d0703d4b14087f1a652716898
Reviewed-on: https://chromium-review.googlesource.com/204677
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Tested-by: Hsinyu Chao <hychao@chromium.org>
Commit-Queue: Hsinyu Chao <hychao@chromium.org>
CRAS: audio area: Move copy function to audio area

Unittest added to cover copy area between mono and
stereo, and copy to keyboard mic 3-channel layout.

BUG=chromium:370368
TEST=unittest

Change-Id: Iee4dba491b6290c1647883c868c71ee0154ce93a
Reviewed-on: https://chromium-review.googlesource.com/204678
Tested-by: Hsinyu Chao <hychao@chromium.org>
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
Commit-Queue: Hsinyu Chao <hychao@chromium.org>
CRAS: test client - Add command to emulate client paused

With the new command 'p' we can use cras_test_client as a
bad client holding the playback request to test how audio
thread handles output client timeout and underrun.

BUG=chromium:391663
TEST=Run 'cras_test_client --playback_file <file>' and hit
'p' to toggle the client pause state.

Change-Id: Ib640370b8e0e02c87bb29d174bb663e5250fcd50
Reviewed-on: https://chromium-review.googlesource.com/206783
Reviewed-by: Owen Lin <owenlin@chromium.org>
Commit-Queue: Hsinyu Chao <hychao@chromium.org>
Tested-by: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_thread - Always adjust output buffer level

Originally the adjust_level() function needed to guard against
affecting unified streams.  To do this the level wasn't adjusted when
both input and output devices were open.  Unified streams are no
longer used.  Remove that check so whenever the min_buffer_level is
set for the output iodev, the hw level is adjusted.  Note that
this issue was hidden by Chrome using a larger stream buffer now. To
trigger the issue, test USB headset on Stumpy, open two streams (1)
capture audio to a file.  (2) playback a file with small buffer size.
Static noise will appear.

Not this may cause odd behavior of unified streams if they are ever
used again.

BUG=chromium:391663
TEST=Manual test on Stumpy using USB headset.
Execute 'cras_test_client --capture_file /tmp/1' to make sure
an input stream is present.
(1) 'cras_test_client --playback_file <raw-file> --block_size 100'
    can hear static noise before this change, verify no noise after
    this fix is applied.
(2) 'cras_test_client --playback_file <raw-file>' and hit 'p' to
    emulate the client is broken. Can hear static noise with or
    without the input stream. This will be fixed in a later change.

Change-Id: I4105bd5eec77196aa9503180f228ba15e8c9115f
Reviewed-on: https://chromium-review.googlesource.com/206784
Reviewed-by: Dylan Reid <dgreid@chromium.org>
Commit-Queue: Hsinyu Chao <hychao@chromium.org>
Tested-by: Hsinyu Chao <hychao@chromium.org>
UCM config: nyan - enable DSP.

Set OutputDspName "speaker_eq" to enable DSP.

BUG=chrome-os-partner:29291
TEST=build on nyan and check dsp.ini is applied.

Change-Id: Ie786baf1e4998631686bd67786a6a8166df7ac4d
Signed-off-by: Cheng-Yi Chiang <cychiang@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/206808
Reviewed-by: Hsinyu Chao <hychao@chromium.org>
CRAS: audio_area_ut - Fix mono/stereo copy tests.

The 'frames' are the number of channel sets, they should always match.

BUG=none
TEST=this is a test.

Change-Id: I1e9246b7453f8bab834aacb37d808ce84d8140c2
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: a2dp_io - Write as much of the buffer as possible.

Loop in flush_data to write as much of the pcm buffer as possible.
This will make de-coupling the pcm and a2dp buffer levels easier.

Change-Id: I3a546a343d390e5defa1988e0b2cff55d94be575
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: a2dp_info - make avdtp_write static.

It isn't used outside the c file.

Change-Id: I5c589c1f0b3895974107b5cbcca2377be90624a1
Signed-off-by: Dylan Reid <dgreid@chromium.org>
CRAS: a2dp_io - Handle ENOSPC error from SBC encoder.

If there isn't any space to encode samples then the SBC codec will
return -ENOSPC.  Handle that explicitly when encoding samples.

BUG=chromium:244625
TEST=play bluetooth audio to jambox.

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