AudioState extensions

This patch modifies AudioState to always call InitRecording
before StartRecording(). This makes it possible to do
SetRecording(false) + SetRecording(true), which before this
patch would not actually work if there was sending streams.

The only way was to add/remove streams...via SDP operations, puh :(.

Bonus: We also needed to modifu AndroidAudioDeviceModule
(which is a thin wrapper) so that StopRecording() will
call AudioInput->StopRecording() even when recording is not
enabled.

BUG=b/397376626

Change-Id: I954b5caab11225b544c3e6a78c5dde357d4eedb5
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/378140
Commit-Queue: Jonas Oreland <jonaso@webrtc.org>
Auto-Submit: Jonas Oreland <jonaso@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43946}
This commit is contained in:
Jonas Oreland 2025-02-20 20:39:38 +01:00 committed by WebRTC LUCI CQ
parent 21b5db48da
commit 358c94f5dd
3 changed files with 36 additions and 4 deletions

View File

@ -141,13 +141,16 @@ void AudioState::SetRecording(bool enabled) {
RTC_LOG(LS_INFO) << "SetRecording(" << enabled << ")"; RTC_LOG(LS_INFO) << "SetRecording(" << enabled << ")";
RTC_DCHECK_RUN_ON(&thread_checker_); RTC_DCHECK_RUN_ON(&thread_checker_);
if (recording_enabled_ != enabled) { if (recording_enabled_ != enabled) {
auto* adm = config_.audio_device_module.get();
recording_enabled_ = enabled; recording_enabled_ = enabled;
if (enabled) { if (enabled) {
if (!sending_streams_.empty()) { if (!sending_streams_.empty()) {
config_.audio_device_module->StartRecording(); if (adm->InitRecording() == 0) {
adm->StartRecording();
}
} }
} else { } else {
config_.audio_device_module->StopRecording(); adm->StopRecording();
} }
} }
} }

View File

@ -26,6 +26,7 @@ namespace test {
namespace { namespace {
using ::testing::_; using ::testing::_;
using ::testing::InSequence;
using ::testing::Matcher; using ::testing::Matcher;
using ::testing::NiceMock; using ::testing::NiceMock;
using ::testing::StrictMock; using ::testing::StrictMock;
@ -357,6 +358,36 @@ TEST_P(AudioStateTest,
audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms); audio_buffer, n_samples_out, &elapsed_time_ms, &ntp_time_ms);
} }
TEST_P(AudioStateTest, AlwaysCallInitRecordingBeforeStartRecording) {
ConfigHelper helper(GetParam());
rtc::scoped_refptr<internal::AudioState> audio_state(
rtc::make_ref_counted<internal::AudioState>(helper.config()));
auto* adm = reinterpret_cast<MockAudioDeviceModule*>(
helper.config().audio_device_module.get());
MockAudioSendStream stream;
{
InSequence s;
EXPECT_CALL(*adm, InitRecording());
EXPECT_CALL(*adm, StartRecording());
audio_state->AddSendingStream(&stream, kSampleRate, kNumberOfChannels);
}
EXPECT_CALL(*adm, StopRecording());
audio_state->SetRecording(false);
{
InSequence s;
EXPECT_CALL(*adm, InitRecording());
EXPECT_CALL(*adm, StartRecording());
audio_state->SetRecording(true);
}
EXPECT_CALL(*adm, StopRecording());
audio_state->RemoveSendingStream(&stream);
}
INSTANTIATE_TEST_SUITE_P(AudioStateTest, INSTANTIATE_TEST_SUITE_P(AudioStateTest,
AudioStateTest, AudioStateTest,
Values(ConfigHelper::Params({false, false}), Values(ConfigHelper::Params({false, false}),

View File

@ -298,8 +298,6 @@ class AndroidAudioDeviceModule : public AudioDeviceModule {
RTC_DLOG(LS_INFO) << __FUNCTION__; RTC_DLOG(LS_INFO) << __FUNCTION__;
if (!initialized_) if (!initialized_)
return -1; return -1;
if (!Recording())
return 0;
audio_device_buffer_->StopRecording(); audio_device_buffer_->StopRecording();
int32_t result = input_->StopRecording(); int32_t result = input_->StopRecording();
RTC_DLOG(LS_INFO) << "output: " << result; RTC_DLOG(LS_INFO) << "output: " << result;