<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
在 Android
系統的開發過程當中,音訊異常問題通常有如下幾類,無聲,調節不了聲音,爆音,聲音卡頓,聲音效果異常(忽大忽小,低音缺失等)等。
尤其聲音效果這部分問題通常從紀錄檔上資訊量較少,相對難定位根因。想要分析此類問題,便需要對聲音傳輸鏈路有一定的瞭解,能夠在鏈路中對各節點的音訊流進行採集,通過對比分析音訊流的實際效果來縮小問題範圍,找出原因。
網上已經有很多音訊框架圖和相關的大致介紹,這裡就不再贅述,只分享下音訊流的傳輸鏈路,和我們可以重點其中的哪些關鍵節點,來幫助我們快速定位問題。
抓取音訊鏈路當中的音訊資料是分析聲音異常問題的有效方法,通過抓取不同節點的聲音資料,可以幫助我們快速定位問題發生的原因。下面先來看一張安卓官方的音訊系統框架圖:
Audio
音訊資料流整體上經過 APP
,framework
,hal
,kernel driver
四個部分,從應用端發起,不管呼叫 audio
還是 media
介面,最終還是會由 AudioTrack
將資料往下傳,經由 AudioFlinger
啟動 MixThread
或 DirectThread
等,將多個 APP
的聲音混合到一起,將聲音傳輸到 hal
層。
系統會根據音訊流型別 stream
和音訊策略 strategy
來選擇對應的 output
,從而找到對應的 module
,將音訊資料傳輸給 hal
層音訊庫 so
做聲音相關的處理並傳給 audio driver
。
音訊流傳輸路徑圖:
從上述的音訊流流程可以看到,我們首先要確認,當前音訊流是經由哪一個 hal
層庫做處理,是 primary
,usb
還是三方 so
等,然後可以在對應的節點抓取相應的音訊資訊分析。
可以根據自己的需要在音訊流的部分節點埋下相應的 dump
指令,將 pcm 寫入到相應的節點當中。
AudioTrack
:應用寫入音訊流的起點,有 MODE_STATIC
和 MODE_STREAM
模式,通過 write
介面將資料寫入。
此節點寫入的資料是由應用層最原始的音訊資料。
AudioFlinger
:負責啟動執行緒完成各個應用的混音,音訊流聲音調節等工作。裝置同時可能存在多個應用播放聲音,這時便需要將各個應用的聲音混合在一起,並且做音量的調節。
例如在車載場景中,音樂應用播放歌曲和地圖應用語音導航的聲音需要同時存在,便使用到了混音的功能,當導航語音響起時,歌曲聲音有一個明顯的變小,便可以設定音訊流的音量。
audio_hw_hal
:hal
層音訊處理的入口,為 Android
原生邏輯,各廠家需要按照規範實現其中的音訊設定等介面,宣告 HAL_MODULE_INFO_SYM
結構體,實現 legacy_adev_open
方法,承接起連線 framework
和 audio driver
的作用,完成一些音效演演算法等邏輯處理。
AudioStreamOut
:和 audio_hw_hal
一樣,是Android
給廠家提供的通用類,廠家在實現自己的通用庫實現時需要可以按照谷歌規範,然後在相應的音訊處理介面中實現自己的對音訊流做音效,增益等處理。
audio_hw_hal.cpp
程式碼如下,不同廠家這裡的實現略有差異,這裡只擷取部分 AOSP
原始碼。
從音訊流傳輸路徑圖可以看到,如何找到是哪一個音訊 so
處理聲音也是至關重要的。我們知道,系統對於應用層曝光的其實只有通道型別。
舉個例子:當用戶打電話時,可以使用通話通道 STREAM_VOICE_CALL
,當用戶播放視訊時,可以使用媒體通道 STREAM_MUSIC
,當傳送通知時,可以使用 STREAM_NOTIFICATION
。
那傳入這些通道的聲音資料,又是怎麼最終流向到具體的硬體輸出裝置呢?
以媒體通道為例,當應用層將音訊資料往 MUSIC
通道寫入時,系統便會根據 StreamType
來生成相應的
audio_attributes_t(.usage = AUDIO_USAGE_MEDIA, .content_type = AUDIO_CONTENT_TYPE_MUSIC})
再通過 audio_attributes_t
來獲取對應的 ProductStrategy
(STRATEGY_MEDIA
),最後在拿到對應的 outputDevice
。
Android
原生邏輯outputDevice
的選擇在 Engine.cpp
上,會具體根據當前裝置是否有接藍芽,耳機等外設,按照優先順序來選擇相應的外設裝置作為輸出,可能是耳機 (AUDIO_DEVICE_OUT_WIRED_HEADSET
),聽筒(AUDIO_DEVICE_OUT_EARPIECE
),喇叭(AUDIO_DEVICE_OUT_SPEAKER
)等。
具體可以看 getDeviceForStrategyInt
方法。
/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #define LOG_TAG "APM::AudioPolicyEngine" //#define LOG_NDEBUG 0 //#define VERY_VERBOSE_LOGGING #ifdef VERY_VERBOSE_LOGGING #define ALOGVV ALOGV #else #define ALOGVV(a...) do { } while(0) #endif #include "Engine.h" #include <AudioPolicyManagerObserver.h> #include <AudioPort.h> #include <IOProfile.h> #include <policy.h> #include <utils/String8.h> #include <utils/Log.h> namespace android { namespace audio_policy { Engine::Engine() : mManagerInterface(this), mPhoneState(AUDIO_MODE_NORMAL), mApmObserver(NULL) { for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) { mForceUse[i] = AUDIO_POLICY_FORCE_NONE; } } Engine::~Engine() { } void Engine::setObserver(AudioPolicyManagerObserver *observer) { ALOG_ASSERT(observer != NULL, "Invalid Audio Policy Manager observer"); mApmObserver = observer; } status_t Engine::initCheck() { return (mApmObserver != NULL) ? NO_ERROR : NO_INIT; } status_t Engine::setPhoneState(audio_mode_t state) { ALOGV("setPhoneState() state %d", state); if (state < 0 || state >= AUDIO_MODE_CNT) { ALOGW("setPhoneState() invalid state %d", state); return BAD_VALUE; } if (state == mPhoneState ) { ALOGW("setPhoneState() setting same state %d", state); return BAD_VALUE; } // store previous phone state for management of sonification strategy below int oldState = mPhoneState; mPhoneState = state; if (!is_state_in_call(oldState) && is_state_in_call(state)) { ALOGV(" Entering call in setPhoneState()"); mApmObserver->getVolumeCurves().switchVolumeCurve(AUDIO_STREAM_VOICE_CALL, AUDIO_STREAM_DTMF); } else if (is_state_in_call(oldState) && !is_state_in_call(state)) { ALOGV(" Exiting call in setPhoneState()"); mApmObserver->getVolumeCurves().restoreOriginVolumeCurve(AUDIO_STREAM_DTMF); } return NO_ERROR; } status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config) { switch(usage) { case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) { ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); return BAD_VALUE; } mForceUse[usage] = config; break; case AUDIO_POLICY_FORCE_FOR_MEDIA: if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && config != AUDIO_POLICY_FORCE_ANALOG_DOCK && config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) { ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); return BAD_VALUE; } mForceUse[usage] = config; break; case AUDIO_POLICY_FORCE_FOR_RECORD: if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && config != AUDIO_POLICY_FORCE_NONE) { ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); return BAD_VALUE; } mForceUse[usage] = config; break; case AUDIO_POLICY_FORCE_FOR_DOCK: if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && config != AUDIO_POLICY_FORCE_ANALOG_DOCK && config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); } mForceUse[usage] = config; break; case AUDIO_POLICY_FORCE_FOR_SYSTEM: if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); } mForceUse[usage] = config; break; case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config); } mForceUse[usage] = config; break; case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND: if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER && config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS && config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config); return BAD_VALUE; } mForceUse[usage] = config; break; case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING: if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) { ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config); return BAD_VALUE; } mForceUse[usage] = config; break; default: ALOGW("setForceUse() invalid usage %d", usage); break; // TODO return BAD_VALUE? } return NO_ERROR; } routing_strategy Engine::getStrategyForStream(audio_stream_type_t stream) { // stream to strategy mapping switch (stream) { case AUDIO_STREAM_VOICE_CALL: case AUDIO_STREAM_BLUETOOTH_SCO: return STRATEGY_PHONE; case AUDIO_STREAM_RING: case AUDIO_STREAM_ALARM: return STRATEGY_SONIFICATION; case AUDIO_STREAM_NOTIFICATION: return STRATEGY_SONIFICATION_RESPECTFUL; case AUDIO_STREAM_DTMF: return STRATEGY_DTMF; default: ALOGE("unknown stream type %d", stream); case AUDIO_STREAM_SYSTEM: // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs // while key clicks are played produces a poor result case AUDIO_STREAM_MUSIC: return STRATEGY_MEDIA; case AUDIO_STREAM_ENFORCED_AUDIBLE: return STRATEGY_ENFORCED_AUDIBLE; case AUDIO_STREAM_TTS: return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; case AUDIO_STREAM_ACCESSIBILITY: return STRATEGY_ACCESSIBILITY; case AUDIO_STREAM_REROUTING: return STRATEGY_REROUTING; } } routing_strategy Engine::getStrategyForUsage(audio_usage_t usage) { // usage to strategy mapping switch (usage) { case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: return STRATEGY_ACCESSIBILITY; case AUDIO_USAGE_MEDIA: case AUDIO_USAGE_GAME: case AUDIO_USAGE_ASSISTANT: case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: case AUDIO_USAGE_ASSISTANCE_SONIFICATION: return STRATEGY_MEDIA; case AUDIO_USAGE_VOICE_COMMUNICATION: return STRATEGY_PHONE; case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: return STRATEGY_DTMF; case AUDIO_USAGE_ALARM: case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: return STRATEGY_SONIFICATION; case AUDIO_USAGE_NOTIFICATION: case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: case AUDIO_USAGE_NOTIFICATION_EVENT: return STRATEGY_SONIFICATION_RESPECTFUL; case AUDIO_USAGE_UNKNOWN: default: return STRATEGY_MEDIA; } } audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy) const { DeviceVector availableOutputDevices = mApmObserver->getAvailableOutputDevices(); DeviceVector availableInputDevices = mApmObserver->getAvailableInputDevices(); const SwAudioOutputCollection &outputs = mApmObserver->getOutputs(); return getDeviceForStrategyInt(strategy, availableOutputDevices, availableInputDevices, outputs, (uint32_t)AUDIO_DEVICE_NONE); } audio_devices_t Engine::getDeviceForStrategyInt(routing_strategy strategy, DeviceVector availableOutputDevices, DeviceVector availableInputDevices, const SwAudioOutputCollection &outputs, uint32_t outputDeviceTypesToIgnore) const { uint32_t device = AUDIO_DEVICE_NONE; uint32_t availableOutputDevicesType = availableOutputDevices.types() & ~outputDeviceTypesToIgnore; switch (strategy) { case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; break; case STRATEGY_SONIFICATION_RESPECTFUL: if (isInCall() || outputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL)) { device = getDeviceForStrategyInt( STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs, outputDeviceTypesToIgnore); } else { bool media_active_locally = outputs.isStreamActiveLocally( AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY) || outputs.isStreamActiveLocally( AUDIO_STREAM_ACCESSIBILITY, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY); // routing is same as media without the "remote" device device = getDeviceForStrategyInt(STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs, AUDIO_DEVICE_OUT_REMOTE_SUBMIX | outputDeviceTypesToIgnore); // if no media is playing on the device, check for mandatory use of "safe" speaker // when media would have played on speaker, and the safe speaker path is available if (!media_active_locally && (device & AUDIO_DEVICE_OUT_SPEAKER) && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE; device &= ~AUDIO_DEVICE_OUT_SPEAKER; } } break; case STRATEGY_DTMF: if (!isInCall()) { // when off call, DTMF strategy follows the same rules as MEDIA strategy device = getDeviceForStrategyInt( STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs, outputDeviceTypesToIgnore); break; } // when in call, DTMF and PHONE strategies follow the same rules // FALL THROUGH case STRATEGY_PHONE: // Force use of only devices on primary output if: // - in call AND // - cannot route from voice call RX OR // - audio HAL version is < 3.0 and TX device is on the primary HW module if (getPhoneState() == AUDIO_MODE_IN_CALL) { audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); audio_devices_t availPrimaryInputDevices = availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle()); // TODO: getPrimaryOutput return only devices from first module in // audio_policy_configuration.xml, hearing aid is not there, but it's // a primary device // FIXME: this is not the right way of solving this problem audio_devices_t availPrimaryOutputDevices = (primaryOutput->supportedDevices() | AUDIO_DEVICE_OUT_HEARING_AID) & availableOutputDevices.types(); if (((availableInputDevices.types() & AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) || (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) { availableOutputDevicesType = availPrimaryOutputDevices; } } // for phone strategy, we first consider the forced use and then the available devices by // order of priority switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { case AUDIO_POLICY_FORCE_BT_SCO: if (!isInCall() || strategy != STRATEGY_DTMF) { device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; if (device) break; } device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; if (device) break; // if SCO device is requested but no SCO device is available, fall back to default case // FALL THROUGH default: // FORCE_NONE device = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID; if (device) break; // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP if (!isInCall() && (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && outputs.isA2dpSupported()) { device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; if (device) break; } device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; if (device) break; if (!isInCall()) { device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; if (device) break; } device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE; break; case AUDIO_POLICY_FORCE_SPEAKER: // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to // A2DP speaker when forcing to speaker output if (!isInCall() && (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && outputs.isA2dpSupported()) { device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; if (device) break; } if (!isInCall()) { device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; if (device) break; device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; if (device) break; } device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; break; } break; case STRATEGY_SONIFICATION: // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by // handleIncallSonification(). if (isInCall() || outputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL)) { device = getDeviceForStrategyInt( STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs, outputDeviceTypesToIgnore); break; } // FALL THROUGH case STRATEGY_ENFORCED_AUDIBLE: // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION // except: // - when in call where it doesn't default to STRATEGY_PHONE behavior // - in countries where not enforced in which case it follows STRATEGY_MEDIA if ((strategy == STRATEGY_SONIFICATION) || (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; } // if SCO headset is connected and we are told to use it, play ringtone over // speaker and BT SCO if ((availableOutputDevicesType & AUDIO_DEVICE_OUT_ALL_SCO) != 0) { uint32_t device2 = AUDIO_DEVICE_NONE; device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; } // Use ONLY Bluetooth SCO output when ringing in vibration mode if (!((mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) && (strategy == STRATEGY_ENFORCED_AUDIBLE))) { if (mForceUse[AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING] == AUDIO_POLICY_FORCE_BT_SCO) { if (device2 != AUDIO_DEVICE_NONE) { device = device2; break; } } } // Use both Bluetooth SCO and phone default output when ringing in normal mode if (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) { if ((strategy == STRATEGY_SONIFICATION) && (device & AUDIO_DEVICE_OUT_SPEAKER) && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE; device &= ~AUDIO_DEVICE_OUT_SPEAKER; } if (device2 != AUDIO_DEVICE_NONE) { device |= device2; break; } } } // The second device used for sonification is the same as the device used by media strategy // FALL THROUGH case STRATEGY_ACCESSIBILITY: if (strategy == STRATEGY_ACCESSIBILITY) { // do not route accessibility prompts to a digital output currently configured with a // compressed format as they would likely not be mixed and dropped. for (size_t i = 0; i < outputs.size(); i++) { sp<AudioOutputDescriptor> desc = outputs.valueAt(i); audio_devices_t devices = desc->device() & (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC); if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) && devices != AUDIO_DEVICE_NONE) { availableOutputDevicesType = availableOutputDevices.types() & ~devices; } } availableOutputDevices = availableOutputDevices.getDevicesFromType(availableOutputDevicesType); if (outputs.isStreamActive(AUDIO_STREAM_RING) || outputs.isStreamActive(AUDIO_STREAM_ALARM)) { return getDeviceForStrategyInt( STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs, outputDeviceTypesToIgnore); } if (isInCall()) { return getDeviceForStrategyInt( STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs, outputDeviceTypesToIgnore); } } // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA // FALL THROUGH // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now case STRATEGY_REROUTING: case STRATEGY_MEDIA: { uint32_t device2 = AUDIO_DEVICE_NONE; if (strategy != STRATEGY_SONIFICATION) { // no sonification on remote submix (e.g. WFD) if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) { device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; } } if (isInCall() && (strategy == STRATEGY_MEDIA)) { device = getDeviceForStrategyInt( STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs, outputDeviceTypesToIgnore); break; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID; } if ((device2 == AUDIO_DEVICE_NONE) && (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && outputs.isA2dpSupported()) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; } } if ((device2 == AUDIO_DEVICE_NONE) && (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; } if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { // no sonification on aux digital (e.g. HDMI) device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL; } if ((device2 == AUDIO_DEVICE_NONE) && (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; } if (device2 == AUDIO_DEVICE_NONE) { device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER; } int device3 = AUDIO_DEVICE_NONE; if (strategy == STRATEGY_MEDIA) { // ARC, SPDIF and AUX_LINE can co-exist with others. device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC; device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF); device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE); } device2 |= device3; // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise device |= device2; // If hdmi system audio mode is on, remove speaker out of output list. if ((strategy == STRATEGY_MEDIA) && (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] == AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { device &= ~AUDIO_DEVICE_OUT_SPEAKER; } // for STRATEGY_SONIFICATION: // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead if ((strategy == STRATEGY_SONIFICATION) && (device & AUDIO_DEVICE_OUT_SPEAKER) && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) { device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE; device &= ~AUDIO_DEVICE_OUT_SPEAKER; } } break; default: ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); break; } if (device == AUDIO_DEVICE_NONE) { ALOGV("getDeviceForStrategy() no device found for strategy %d", strategy); device = mApmObserver->getDefaultOutputDevice()->type(); ALOGE_IF(device == AUDIO_DEVICE_NONE, "getDeviceForStrategy() no default device defined"); } ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); return device; } audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const { const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices(); const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices(); const SwAudioOutputCollection &outputs = mApmObserver->getOutputs(); audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; uint32_t device = AUDIO_DEVICE_NONE; // when a call is active, force device selection to match source VOICE_COMMUNICATION // for most other input sources to avoid rerouting call TX audio if (isInCall()) { switch (inputSource) { case AUDIO_SOURCE_DEFAULT: case AUDIO_SOURCE_MIC: case AUDIO_SOURCE_VOICE_RECOGNITION: case AUDIO_SOURCE_UNPROCESSED: case AUDIO_SOURCE_HOTWORD: case AUDIO_SOURCE_CAMCORDER: inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION; break; default: break; } } switch (inputSource) { case AUDIO_SOURCE_VOICE_UPLINK: if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { device = AUDIO_DEVICE_IN_VOICE_CALL; break; } break; case AUDIO_SOURCE_DEFAULT: case AUDIO_SOURCE_MIC: if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) && (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) { device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { device = AUDIO_DEVICE_IN_WIRED_HEADSET; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { device = AUDIO_DEVICE_IN_USB_HEADSET; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { device = AUDIO_DEVICE_IN_USB_DEVICE; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { device = AUDIO_DEVICE_IN_BUILTIN_MIC; } break; case AUDIO_SOURCE_VOICE_COMMUNICATION: // Allow only use of devices on primary input if in call and HAL does not support routing // to voice call path. if ((getPhoneState() == AUDIO_MODE_IN_CALL) && (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) { sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput(); availableDeviceTypes = availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle()) & ~AUDIO_DEVICE_BIT_IN; } switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { case AUDIO_POLICY_FORCE_BT_SCO: // if SCO device is requested but no SCO device is available, fall back to default case if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; break; } // FALL THROUGH default: // FORCE_NONE if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { device = AUDIO_DEVICE_IN_WIRED_HEADSET; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { device = AUDIO_DEVICE_IN_USB_HEADSET; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { device = AUDIO_DEVICE_IN_USB_DEVICE; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { device = AUDIO_DEVICE_IN_BUILTIN_MIC; } break; case AUDIO_POLICY_FORCE_SPEAKER: if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { device = AUDIO_DEVICE_IN_BACK_MIC; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { device = AUDIO_DEVICE_IN_BUILTIN_MIC; } break; } break; case AUDIO_SOURCE_VOICE_RECOGNITION: case AUDIO_SOURCE_UNPROCESSED: case AUDIO_SOURCE_HOTWORD: if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO && availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { device = AUDIO_DEVICE_IN_WIRED_HEADSET; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) { device = AUDIO_DEVICE_IN_USB_HEADSET; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { device = AUDIO_DEVICE_IN_USB_DEVICE; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { device = AUDIO_DEVICE_IN_BUILTIN_MIC; } break; case AUDIO_SOURCE_CAMCORDER: if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { device = AUDIO_DEVICE_IN_BACK_MIC; } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { device = AUDIO_DEVICE_IN_BUILTIN_MIC; } break; case AUDIO_SOURCE_VOICE_DOWNLINK: case AUDIO_SOURCE_VOICE_CALL: if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { device = AUDIO_DEVICE_IN_VOICE_CALL; } break; case AUDIO_SOURCE_REMOTE_SUBMIX: if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; } break; case AUDIO_SOURCE_FM_TUNER: if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) { device = AUDIO_DEVICE_IN_FM_TUNER; } break; default: ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); break; } if (device == AUDIO_DEVICE_NONE) { ALOGV("getDeviceForInputSource() no device found for source %d", inputSource); if (availableDeviceTypes & AUDIO_DEVICE_IN_STUB) { device = AUDIO_DEVICE_IN_STUB; } ALOGE_IF(device == AUDIO_DEVICE_NONE, "getDeviceForInputSource() no default device defined"); } ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); return device; } template <> AudioPolicyManagerInterface *Engine::queryInterface() { return &mManagerInterface; } } // namespace audio_policy } // namespace android
通過以上分析,我們知道了音訊會流向哪個輸出裝置,那麼下一個問題來了,是由誰負責傳輸和對音訊資料做最後的處理呢?
這裡就需要看音訊裝置的策略檔案,還是以媒體通道為例,假設裝置沒有接任何外接裝置,選擇的 outputDevice
是 AUDIO_DEVICE_OUT_SPEAKER
。
接下來就要看哪個 output
so
支援 AUDIO_DEVICE_OUT_SPEAKER
,符合度最高的 output
so
將會負責資料傳輸,最終經由 tinyalsa
寫入到 pcm
節點中。
不同的 Android
版本在組態檔上會有些許差異,可能放置在 *audio_policy_configuration.xml
中,有些在 audio_policy.conf
中。
有使用者反饋使用優酷視訊播放視訊時,概率性出現聲音忽大忽小的問題,一旦出現就是在播放指定音訊時是必現的。接下來聯絡使用者幫提供裝置的紀錄檔資訊和操作步驟,按照使用者操作來複現問題,通過 demo
還原使用者環境引數便能復現。 首先分析確認發現在這個過程中聲音音量均無變化,所以初步懷疑可能是和音訊流資料出現異常有關。在上圖中數位有標識的5個點中分別抓取音訊,使用 Audacity
匯入音訊檔來進行分析,發現位置4的音訊正常,而位置5的音訊出現了聲音異常的現象。具體見下圖:
所以便可以確認在音訊資料經過 hal 層音效處理前是正常等,經過音效處理後,在某些特殊的聲音資料下,音效庫縮小了聲音的幅值,從而導致聲音的異常。
為了實錘是音效庫 so 導致的問題,通過關閉音效庫的功能,最終發現聲音忽大忽小的問題消失了。
從以上嘗試的結果綜合分析可以確認,是音效 so 庫對通道聲音進行處理時影響到了原有聲音的功能。通過修改 so
庫最終來解決這個問題。
有使用者反饋說是開啟應用A播放視訊正常,然後直接返回到 home 目錄,應用A後臺播放時會出現斷音的現象。
聲音卡頓,錄音掉幀類的現象在聲音問題中非常常見。從現象上來看,就是使用者切換到後臺時沒有暫停播放,視訊在後臺播放時出現。老規矩,我們先分析相關 log
,通過紀錄檔分析發現,當問題出現時,紀錄檔上頻繁列印 get null buffer
的資訊。
所以懷疑是否是音訊資料丟失導致的。dump
音訊資料抓取到系統混音後輸出到輸出裝置的原始音訊,可以幫助實錘上層系統傳下來的資料是否正常。於是發現位置3的音訊如下:
從抓取到的音訊可以看到,在後臺異常播放時,在該時間段內會出現明顯音訊丟幀的現象。接下來要看看是在哪一塊出現了丟幀。進一步分析 audioTrack
傳下來的資料,出現了丟失掉一部分音訊的現象,時長相當於原音訊的一半。
基於此,為了實錘是應用A傳下來的資料就有缺失,從紀錄檔資訊跟蹤,決定在 audioTrack 上加紀錄檔資訊來看,發現當切換到後臺時,audioTrack 每次還是寫 4096 個 byte
,但是寫資料的頻率降低了一半。
正常:28.600-27.546=1.054 44次 間隔 1.054/43= 0.0245秒/次。
異常:40.839-39.804=1.035 22次 間隔 1.035/21= 0.0493秒/次。
考慮到這一塊是否是和後臺程序的優先順序相關。當程序降低時導致了寫資料的執行緒能夠拿到的CPU資源變小,出現了斷音的問題。通過和其他型號的平板對比發現,各廠家 Android 10 的平板大部分均有此問題,而 android7 和 android 8 的平板就沒有這個問題。基於以上情況,更加懷疑是和 android 的特性相關,可能是新的 android 平板針對後臺執行緒優先順序做了處理,目的也很明確,就是限制後臺應用的活躍程度,來保證裝置效能。
此時進一步分析最終發現是和 TimerSlackHigh
的引數相關。
system/core/libprocessgroup/profiles/task_profiles.json:
{ "Name": "TimerSlackHigh", "Actions": [ { "Name": "SetTimerSlack", "Params": { "Slack": "40000000" } } ] }, { "Name": "TimerSlackNormal", "Actions": [ { "Name": "SetTimerSlack", "Params": { "Slack": "50000" } } ] },
該引數影響了應用後臺執行緒 sleep/wait 之間所消耗的時間。可以看到,當應用從前臺切換到後臺時,這個時間從50 微秒上調到 40 毫秒。從而導致寫入音訊資料量大大減少。通過修改引數可以解決,但是提高後臺執行緒的活躍度,很可能影響到整體效能,因此不作處理。最終像使用者解釋,切換後臺時可以手動停止播放視訊,同時反饋給應用,由應用規範應用流程,起後臺程序來做單獨處理。
按照以上案例,我們來總結下當聲音出現異常時一些快速定位偵錯的手段:
track
音量調為0,此時調節系統音量時不會有聲音的。需要使用者點選該應用內的一個靜音按鈕才有聲音,這時候就會在位置1抓到一串無聲的音訊,這種在安卓版本表現是一致的。但是也有可能是像案例2一樣和後臺優先順序有關,導致只在較高的版本上出現問題。output so
來處理的,可能是音效庫處理資料等操作導致聲音異常。以上就是Android audio音訊流資料異常問題解決分析的詳細內容,更多關於Android audio音訊流資料異常的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45