Skip to content

Commit 57f9c82

Browse files
baili0411Chromeos LUCI
authored and
Chromeos LUCI
committed
cras_iodev: Add processing argument to fill zeros
In certain cases where we need to fill zeros in the main thread, we don't want audio processing. Filling zeros from the main thread is needed to prevent underrun from devices without start ops. Add an argument `processing` to cras_iodev_fill_odev_zeros to choose to skip the audio processing. BUG=b:340138001 TEST=tast run $DUT audio.CrasMultiChannelCapture.\* audio.CrasAEC.\* Change-Id: I4c51d944ce03aeab8c1f270ea9b59e55f80ca9d0 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/5541353 Commit-Queue: Baili Deng <[email protected]> Reviewed-by: Li-Yu Yu <[email protected]> Tested-by: [email protected] <[email protected]> Reviewed-by: Yu-Hsuan Hsu <[email protected]>
1 parent 9d32a79 commit 57f9c82

18 files changed

+56
-39
lines changed

cras/src/server/audio_thread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static int audio_thread_read_command(struct audio_thread* thread,
272272
// Builds an initial buffer to avoid an underrun. Adds min_level of latency.
273273
static void fill_odevs_zeros_min_level(struct cras_iodev* odev) {
274274
int rc;
275-
rc = cras_iodev_fill_odev_zeros(odev, odev->min_buffer_level);
275+
rc = cras_iodev_fill_odev_zeros(odev, odev->min_buffer_level, false);
276276
if (rc < 0) {
277277
syslog(LOG_WARNING, "failed to fill odev with %u length of zeros:%d",
278278
odev->min_buffer_level, rc);

cras/src/server/cras_a2dp_iodev.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ static int fill_zeros_to_target_level(struct cras_iodev* iodev,
151151
unsigned int local_queued_frames = bt_local_queued_frames(iodev);
152152

153153
if (local_queued_frames < target_level) {
154-
return cras_iodev_fill_odev_zeros(iodev,
155-
target_level - local_queued_frames);
154+
return cras_iodev_fill_odev_zeros(iodev, target_level - local_queued_frames,
155+
true);
156156
}
157157
return 0;
158158
}
@@ -185,7 +185,7 @@ static int output_underrun(struct cras_iodev* iodev) {
185185
return 0;
186186
}
187187

188-
return cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level);
188+
return cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level, true);
189189
}
190190

191191
/*

cras/src/server/cras_alsa_io.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,7 +1711,7 @@ static int adjust_appl_ptr_samples_remaining(struct cras_iodev* odev) {
17111711

17121712
// Fill zeros to make sure there are enough zero samples in device buffer.
17131713
if (offset > real_hw_level) {
1714-
rc = cras_iodev_fill_odev_zeros(odev, offset - real_hw_level);
1714+
rc = cras_iodev_fill_odev_zeros(odev, offset - real_hw_level, true);
17151715
if (rc < 0) {
17161716
return rc;
17171717
}
@@ -1781,7 +1781,7 @@ static int possibly_enter_free_run(struct cras_iodev* odev) {
17811781
fr_to_write = MIN(cras_time_to_frames(&no_stream_fill_zeros_duration,
17821782
odev->format->frame_rate),
17831783
odev->buffer_size - real_hw_level);
1784-
rc = cras_iodev_fill_odev_zeros(odev, fr_to_write);
1784+
rc = cras_iodev_fill_odev_zeros(odev, fr_to_write, true);
17851785
if (rc < 0) {
17861786
return rc;
17871787
}

cras/src/server/cras_alsa_usb_io.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ static int usb_adjust_appl_ptr_samples_remaining(struct cras_iodev* odev) {
12721272

12731273
// Fill zeros to make sure there are enough zero samples in device buffer.
12741274
if (offset > real_hw_level) {
1275-
rc = cras_iodev_fill_odev_zeros(odev, offset - real_hw_level);
1275+
rc = cras_iodev_fill_odev_zeros(odev, offset - real_hw_level, true);
12761276
if (rc < 0) {
12771277
return rc;
12781278
}
@@ -1342,7 +1342,7 @@ static int usb_possibly_enter_free_run(struct cras_iodev* odev) {
13421342
fr_to_write = MIN(cras_time_to_frames(&no_stream_fill_zeros_duration,
13431343
odev->format->frame_rate),
13441344
odev->buffer_size - real_hw_level);
1345-
rc = cras_iodev_fill_odev_zeros(odev, fr_to_write);
1345+
rc = cras_iodev_fill_odev_zeros(odev, fr_to_write, true);
13461346
if (rc < 0) {
13471347
return rc;
13481348
}

cras/src/server/cras_fl_pcm_iodev.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ static int fill_zeros_to_target_level(struct cras_iodev* iodev,
159159
unsigned int local_queued_frames = bt_local_queued_frames(iodev);
160160

161161
if (local_queued_frames < target_level) {
162-
return cras_iodev_fill_odev_zeros(iodev,
163-
target_level - local_queued_frames);
162+
return cras_iodev_fill_odev_zeros(iodev, target_level - local_queued_frames,
163+
true);
164164
}
165165
return 0;
166166
}
@@ -250,7 +250,7 @@ static int hfp_output_underrun(struct cras_iodev* iodev) {
250250
}
251251

252252
// Handle it the same way as cras_iodev_output_underrun().
253-
return cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level);
253+
return cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level, true);
254254
}
255255

256256
static int hfp_no_stream(struct cras_iodev* iodev, int enable) {
@@ -343,7 +343,7 @@ static int a2dp_configure_dev(struct cras_iodev* iodev) {
343343

344344
/* Send one block of silence to Floss as jitter buffer to handle the
345345
* variation in packet scheduling caused by clock drift and state-polling. */
346-
cras_iodev_fill_odev_zeros(iodev, a2dpio->write_block);
346+
cras_iodev_fill_odev_zeros(iodev, a2dpio->write_block, true);
347347
init_level = a2dpio->write_block * cras_get_format_bytes(iodev->format);
348348

349349
buf = buf_read_pointer_size(a2dpio->pcm_buf, &to_send);

cras/src/server/cras_hfp_iodev.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static int frames_queued(const struct cras_iodev* iodev,
146146

147147
static int output_underrun(struct cras_iodev* iodev) {
148148
// Handle it the same way as cras_iodev_output_underrun().
149-
return cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level);
149+
return cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level, true);
150150
}
151151

152152
/* Handles cras sr bt enabling and disabling cases.

cras/src/server/cras_iodev.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ static int default_no_stream_playback(struct cras_iodev* odev) {
121121
fr_to_write = cras_iodev_buffer_avail(odev, hw_level);
122122
if (hw_level <= target_hw_level) {
123123
fr_to_write = MIN(target_hw_level - hw_level, fr_to_write);
124-
rc = cras_iodev_fill_odev_zeros(odev, fr_to_write);
124+
rc = cras_iodev_fill_odev_zeros(odev, fr_to_write, true);
125125
if (rc < 0) {
126126
return rc;
127127
}
@@ -306,7 +306,7 @@ static int cras_iodev_output_event_sample_ready(struct cras_iodev* odev) {
306306
* stream, fill 1 min_cb_level of zeros first and fill sample
307307
* from stream later.
308308
* Starts the device here to finish state transition. */
309-
rc = cras_iodev_fill_odev_zeros(odev, odev->min_cb_level);
309+
rc = cras_iodev_fill_odev_zeros(odev, odev->min_cb_level, true);
310310
if (rc < 0) {
311311
syslog(LOG_WARNING, "Failed to fill zeros to device %s before start",
312312
odev->info.name);
@@ -1218,7 +1218,7 @@ int cras_iodev_open(struct cras_iodev* iodev,
12181218
} else {
12191219
iodev->state = CRAS_IODEV_STATE_NO_STREAM_RUN;
12201220

1221-
rc = cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level);
1221+
rc = cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level, false);
12221222
if (rc < 0) {
12231223
syslog(LOG_WARNING, "Failed to fill zeros to device %s while open",
12241224
iodev->info.name);
@@ -1600,7 +1600,9 @@ int cras_iodev_buffer_avail(struct cras_iodev* iodev, unsigned hw_level) {
16001600
return iodev->buffer_size - iodev->min_buffer_level - hw_level;
16011601
}
16021602

1603-
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames) {
1603+
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev,
1604+
unsigned int frames,
1605+
bool processing) {
16041606
struct cras_audio_area* area = NULL;
16051607
unsigned int frame_bytes, frames_writable;
16061608
int rc = 0, filled_frames = 0;
@@ -1650,7 +1652,11 @@ int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames) {
16501652
}
16511653
// Update offsets to mark existing valid data as written.
16521654
cras_iodev_all_streams_written(odev, frames_writable);
1653-
rc = cras_iodev_put_output_buffer(odev, buf, frames_writable, NULL, NULL);
1655+
if (processing) {
1656+
rc = cras_iodev_put_output_buffer(odev, buf, frames_writable, NULL, NULL);
1657+
} else {
1658+
rc = odev->put_buffer(odev, frames_writable);
1659+
}
16541660
if (rc < 0) {
16551661
syslog(LOG_WARNING, "Put output buffer failed: %d", rc);
16561662
return rc;
@@ -1676,7 +1682,7 @@ int cras_iodev_output_underrun(struct cras_iodev* odev,
16761682
if (odev->output_underrun) {
16771683
return odev->output_underrun(odev);
16781684
} else {
1679-
rc = cras_iodev_fill_odev_zeros(odev, odev->min_cb_level);
1685+
rc = cras_iodev_fill_odev_zeros(odev, odev->min_cb_level, true);
16801686
if (rc > 0) {
16811687
cras_iodev_update_underrun_duration(odev, rc);
16821688
}

cras/src/server/cras_iodev.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,11 +807,13 @@ void cras_iodev_set_dsp_offload_disallow_by_aec_ref(struct cras_iodev* iodev,
807807
* Args:
808808
* odev - The device.
809809
* frames - The number of frames of zero samples to put into the device.
810+
* processing - Whether to handle audio processing or not.
810811
* Returns:
811812
* Number of frames filled with zeros, negative errno if failed.
812813
*/
813-
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames);
814-
814+
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev,
815+
unsigned int frames,
816+
bool processing);
815817
/*
816818
* The default implementation of frames_to_play_in_sleep ops, used when an
817819
* iodev doesn't have its own logic.

cras/src/server/cras_lea_iodev.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include "cras/src/server/audio_thread_log.h"
2020
#include "cras/src/server/cras_audio_area.h"
2121
#include "cras/src/server/cras_audio_thread_monitor.h"
22-
#include "cras/src/server/cras_lea_manager.h"
2322
#include "cras/src/server/cras_iodev.h"
2423
#include "cras/src/server/cras_iodev_list.h"
24+
#include "cras/src/server/cras_lea_manager.h"
2525
#include "cras/src/server/cras_utf8.h"
2626
#include "cras_types.h"
2727
#include "cras_util.h"
@@ -130,7 +130,7 @@ static int output_underrun(struct cras_iodev* iodev) {
130130
}
131131

132132
// Handle it the same way as cras_iodev_output_underrun().
133-
return cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level);
133+
return cras_iodev_fill_odev_zeros(iodev, iodev->min_cb_level, true);
134134
}
135135

136136
static int no_stream(struct cras_iodev* iodev, int enable) {

cras/src/server/cras_stream_apm.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -936,12 +936,7 @@ int cras_stream_apm_destroy(struct cras_stream_apm* stream) {
936936
static int process_reverse(struct float_buffer* fbuf,
937937
unsigned int frame_rate,
938938
const struct cras_iodev* echo_ref) {
939-
// TODO(b/340138001): Switch back to checked_audio_ctx() after we stop
940-
// calling this function in the main thread.
941-
struct cras_audio_ctx* actx = get_audio_ctx_or_null();
942-
if (!actx) {
943-
return 0;
944-
}
939+
struct cras_audio_ctx* actx = checked_audio_ctx();
945940

946941
struct active_apm* active;
947942
int ret;

cras/src/tests/a2dp_iodev_unittest.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,9 @@ void cras_iodev_init_audio_area(struct cras_iodev* iodev) {
907907

908908
void cras_iodev_free_audio_area(struct cras_iodev* iodev) {}
909909

910-
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames) {
910+
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev,
911+
unsigned int frames,
912+
bool processing) {
911913
struct cras_audio_area* area;
912914
cras_iodev_fill_odev_zeros_called++;
913915
cras_iodev_fill_odev_zeros_frames = frames;

cras/src/tests/alsa_io_unittest.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3204,7 +3204,9 @@ int cras_iodev_buffer_avail(struct cras_iodev* iodev, unsigned hw_level) {
32043204
return cras_iodev_buffer_avail_ret;
32053205
}
32063206

3207-
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames) {
3207+
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev,
3208+
unsigned int frames,
3209+
bool processing) {
32083210
cras_iodev_fill_odev_zeros_called++;
32093211
cras_iodev_fill_odev_zeros_frames = frames;
32103212
return (int)frames;

cras/src/tests/audio_thread_unittest.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1325,7 +1325,9 @@ int cras_iodev_buffer_avail(struct cras_iodev* iodev, unsigned hw_level) {
13251325
return iodev->buffer_size - iodev->frames_queued(iodev, &tstamp);
13261326
}
13271327

1328-
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames) {
1328+
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev,
1329+
unsigned int frames,
1330+
bool processing) {
13291331
cras_iodev_fill_odev_zeros_frames = frames;
13301332
return (int)frames;
13311333
}

cras/src/tests/cras_alsa_usb_io_unittest.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,9 @@ int cras_iodev_buffer_avail(struct cras_iodev* iodev, unsigned hw_level) {
15991599
return cras_iodev_buffer_avail_ret;
16001600
}
16011601

1602-
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames) {
1602+
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev,
1603+
unsigned int frames,
1604+
bool processing) {
16031605
cras_iodev_fill_odev_zeros_called++;
16041606
cras_iodev_fill_odev_zeros_frames = frames;
16051607
return (int)frames;

cras/src/tests/fl_pcm_iodev_unittest.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,9 @@ void cras_audio_area_config_buf_pointers(struct cras_audio_area* area,
768768
mock_audio_area->channels[0].buf = base_buffer;
769769
}
770770

771-
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames) {
771+
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev,
772+
unsigned int frames,
773+
bool processing) {
772774
return (int)frames;
773775
}
774776

cras/src/tests/hfp_iodev_unittest.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ void cras_iodev_free_resources(struct cras_iodev* iodev) {
528528
cras_iodev_free_resources_called++;
529529
}
530530

531-
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames) {
531+
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev,
532+
unsigned int frames,
533+
bool processing) {
532534
return (int)frames;
533535
}
534536

cras/src/tests/iodev_unittest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,11 +1683,11 @@ TEST(IoDev, FillZeros) {
16831683
iodev.put_buffer = put_buffer;
16841684

16851685
iodev.direction = CRAS_STREAM_INPUT;
1686-
rc = cras_iodev_fill_odev_zeros(&iodev, frames);
1686+
rc = cras_iodev_fill_odev_zeros(&iodev, frames, true);
16871687
EXPECT_EQ(-EINVAL, rc);
16881688

16891689
iodev.direction = CRAS_STREAM_OUTPUT;
1690-
rc = cras_iodev_fill_odev_zeros(&iodev, frames);
1690+
rc = cras_iodev_fill_odev_zeros(&iodev, frames, true);
16911691

16921692
EXPECT_EQ(frames, rc);
16931693
EXPECT_EQ(frames, put_buffer_nframes);

cras/src/tests/lea_iodev_unittest.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include "cras/src/server/audio_thread.h"
1111
#include "cras/src/server/audio_thread_log.h"
1212
#include "cras/src/server/cras_audio_area.h"
13-
#include "cras/src/server/cras_lea_manager.h"
1413
#include "cras/src/server/cras_iodev.h"
1514
#include "cras/src/server/cras_iodev_list.h"
15+
#include "cras/src/server/cras_lea_manager.h"
1616
#include "cras/src/tests/test_util.hh"
1717
#include "third_party/utlist/utlist.h"
1818

@@ -505,7 +505,9 @@ void cras_audio_area_config_buf_pointers(struct cras_audio_area* area,
505505
mock_audio_area->channels[0].buf = base_buffer;
506506
}
507507

508-
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev, unsigned int frames) {
508+
int cras_iodev_fill_odev_zeros(struct cras_iodev* odev,
509+
unsigned int frames,
510+
bool processing) {
509511
return (int)frames;
510512
}
511513

0 commit comments

Comments
 (0)