From 4cb69cb75918b99d3bc2ecbd1be5ced330fd4997 Mon Sep 17 00:00:00 2001 From: chenhao Date: Fri, 10 Jan 2025 15:01:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E9=9F=B3=E9=A2=91=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E5=88=87=E6=8D=A2=EF=BC=88=E5=A6=82=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=88=96=E6=96=AD=E5=BC=80=E8=93=9D=E7=89=99=E8=80=B3=E6=9C=BA?= =?UTF-8?q?=EF=BC=89=E7=9B=B8=E5=85=B3=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenhao --- modules/audio_device/ohos/ohaudio_player.cc | 7 ++++--- modules/audio_device/ohos/ohaudio_player.h | 5 +++-- modules/audio_device/ohos/ohaudio_player_wrapper.cc | 11 ++++++++++- modules/audio_device/ohos/ohaudio_recorder_wrapper.cc | 3 +++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/modules/audio_device/ohos/ohaudio_player.cc b/modules/audio_device/ohos/ohaudio_player.cc index eb65622be4..a87dc49cb4 100755 --- a/modules/audio_device/ohos/ohaudio_player.cc +++ b/modules/audio_device/ohos/ohaudio_player.cc @@ -30,7 +30,6 @@ OHAudioPlayer::OHAudioPlayer(const AudioParameters& audio_parameters) ohaudio_(audio_parameters, AUDIOSTREAM_TYPE_RENDERER, this) { RTC_LOG(LS_INFO) << "ctor"; - thread_checker_ohaudio_.Detach(); } OHAudioPlayer::~OHAudioPlayer() @@ -111,7 +110,6 @@ int OHAudioPlayer::StopPlayout() RTC_LOG(LS_ERROR) << "StopPlayout failed"; return -1; } - thread_checker_ohaudio_.Detach(); initialized_ = false; playing_ = false; return 0; @@ -149,7 +147,10 @@ int32_t OHAudioPlayer::OnErrorCallback(OH_AudioStream_Result error) int32_t OHAudioPlayer::OnDataCallback(void* audio_data, int32_t num_frames) { static const float half_sec = 0.5; - RTC_DCHECK_RUN_ON(&thread_checker_ohaudio_); + // Some tests shows that this callback can sometimes be called on different + // threads (after output device changed for example), so a thread checker + // here may cause problem, and use race checker instead. + RTC_DCHECK_RUNS_SERIALIZED(&race_checker_ohaudio_); if (first_data_callback_) { RTC_LOG(LS_INFO) << "--- First output data callback: device id=" << ohaudio_.device_id(); diff --git a/modules/audio_device/ohos/ohaudio_player.h b/modules/audio_device/ohos/ohaudio_player.h index 52345a20c5..7a3fb77648 100755 --- a/modules/audio_device/ohos/ohaudio_player.h +++ b/modules/audio_device/ohos/ohaudio_player.h @@ -27,6 +27,7 @@ #include "modules/audio_device/audio_device_buffer.h" #include "modules/audio_device/include/audio_device_defines.h" #include "rtc_base/thread_annotations.h" +#include "rtc_base/race_checker.h" #include "modules/audio_device/ohos/ohaudio_player_wrapper.h" #include "modules/audio_device/ohos/audio_device_module.h" @@ -66,7 +67,7 @@ private: int GetPlayoutUnderrunCount() override { return 0; } void HandleStreamDisconnected(); SequenceChecker main_thread_checker_; - SequenceChecker thread_checker_ohaudio_; + rtc::RaceChecker race_checker_ohaudio_; TaskQueueBase* main_thread_; OHAudioPlayerWrapper ohaudio_; std::unique_ptr fine_audio_buffer_; @@ -77,7 +78,7 @@ private: bool initialized_ RTC_GUARDED_BY(main_thread_checker_) = false; bool playing_ RTC_GUARDED_BY(main_thread_checker_) = false; - double latency_millis_ RTC_GUARDED_BY(thread_checker_ohaudio_) = 0; + double latency_millis_ RTC_GUARDED_BY(race_checker_ohaudio_) = 0; static const int default_channels = 2; }; } // namespace webrtc diff --git a/modules/audio_device/ohos/ohaudio_player_wrapper.cc b/modules/audio_device/ohos/ohaudio_player_wrapper.cc index 7614f9dfaa..467e89b334 100755 --- a/modules/audio_device/ohos/ohaudio_player_wrapper.cc +++ b/modules/audio_device/ohos/ohaudio_player_wrapper.cc @@ -112,7 +112,12 @@ static int32_t ErrorCallback(OH_AudioRenderer *stream, void* user_data, OH_Audio static int32_t DataCallback(OH_AudioRenderer *stream, void* user_data, void* audio_data, int32_t bufferLen) { RTC_DCHECK(user_data); - RTC_DCHECK(audio_data); + // Some tests shows that this callback can sometimes be called with nullptr + // 'audio_data' and zero 'bufferLen' (after output device changed for example). + if (!audio_data || bufferLen == 0) { + RTC_LOG(LS_WARNING) << "Invalid data buffer"; + return 0; + } RTC_LOG(LS_INFO) << "bufferLen=" << bufferLen; OHAudioPlayerWrapper* ohaudio_wrapper = reinterpret_cast(user_data); RTC_DCHECK(ohaudio_wrapper->observer()); @@ -336,6 +341,10 @@ void OHAudioPlayerWrapper::SetStreamConfiguration(OH_AudioStreamBuilder* builder RTC_LOG(LS_INFO) << "SetStreamConfiguration"; RTC_DCHECK(builder); RTC_DCHECK(thread_checker_.IsCurrent()); + // Set renderer info to AUDIOSTREAM_USAGE_VOICE_COMMUNICATION seems more reasonable, or set to + // AUDIOSTREAM_USAGE_VIDEO_COMMUNICATION if you prefer to use the speaker as default output device. + // See also OH_AudioRenderer_SetDefaultOutputDevice. + OH_AudioStreamBuilder_SetRendererInfo(builder, AUDIOSTREAM_USAGE_VOICE_COMMUNICATION); OH_AudioStreamBuilder_SetSamplingRate(builder, audio_parameters().sample_rate()); OH_AudioStreamBuilder_SetChannelCount(builder, audio_parameters().channels()); OH_AudioStreamBuilder_SetSampleFormat(builder, AUDIOSTREAM_SAMPLE_S16LE); diff --git a/modules/audio_device/ohos/ohaudio_recorder_wrapper.cc b/modules/audio_device/ohos/ohaudio_recorder_wrapper.cc index 41393ae5b4..123c48aab0 100755 --- a/modules/audio_device/ohos/ohaudio_recorder_wrapper.cc +++ b/modules/audio_device/ohos/ohaudio_recorder_wrapper.cc @@ -322,6 +322,9 @@ void OHAudioRecorderWrapper::SetStreamConfiguration(OH_AudioStreamBuilder* build RTC_LOG(LS_INFO) << "SetStreamConfiguration"; RTC_DCHECK(builder); RTC_DCHECK(thread_checker_.IsCurrent()); + // Use default capturer info (which is AUDIOSTREAM_SOURCE_TYPE_MIC) seems not work well after input device changed, + // such as from mic to bluetooth earphone, so use AUDIOSTREAM_SOURCE_TYPE_VOICE_COMMUNICATION instead. + OH_AudioStreamBuilder_SetCapturerInfo(builder, AUDIOSTREAM_SOURCE_TYPE_VOICE_COMMUNICATION); OH_AudioStreamBuilder_SetSamplingRate(builder, audio_parameters().sample_rate()); OH_AudioStreamBuilder_SetChannelCount(builder, audio_parameters().channels()); OH_AudioStreamBuilder_SetSampleFormat(builder, AUDIOSTREAM_SAMPLE_S16LE); -- Gitee