Android audio音頻流數(shù)據(jù)異常問(wèn)題解決分析
一、背景
在 Android
系統(tǒng)的開(kāi)發(fā)過(guò)程當(dāng)中,音頻異常問(wèn)題通常有如下幾類,無(wú)聲,調(diào)節(jié)不了聲音,爆音,聲音卡頓,聲音效果異常(忽大忽小,低音缺失等)等。
尤其聲音效果這部分問(wèn)題通常從日志上信息量較少,相對(duì)難定位根因。想要分析此類問(wèn)題,便需要對(duì)聲音傳輸鏈路有一定的了解,能夠在鏈路中對(duì)各節(jié)點(diǎn)的音頻流進(jìn)行采集,通過(guò)對(duì)比分析音頻流的實(shí)際效果來(lái)縮小問(wèn)題范圍,找出原因。
網(wǎng)上已經(jīng)有很多音頻框架圖和相關(guān)的大致介紹,這里就不再贅述,只分享下音頻流的傳輸鏈路,和我們可以重點(diǎn)其中的哪些關(guān)鍵節(jié)點(diǎn),來(lái)幫助我們快速定位問(wèn)題。
二、Android Audio 音頻系統(tǒng)
1. 音頻鏈路
抓取音頻鏈路當(dāng)中的音頻數(shù)據(jù)是分析聲音異常問(wèn)題的有效方法,通過(guò)抓取不同節(jié)點(diǎn)的聲音數(shù)據(jù),可以幫助我們快速定位問(wèn)題發(fā)生的原因。下面先來(lái)看一張安卓官方的音頻系統(tǒng)框架圖:
Audio
音頻數(shù)據(jù)流整體上經(jīng)過(guò) APP
,framework
,hal
,kernel driver
四個(gè)部分,從應(yīng)用端發(fā)起,不管調(diào)用 audio
還是 media
接口,最終還是會(huì)由 AudioTrack
將數(shù)據(jù)往下傳,經(jīng)由 AudioFlinger
啟動(dòng) MixThread
或 DirectThread
等,將多個(gè) APP
的聲音混合到一起,將聲音傳輸?shù)?hal
層。
系統(tǒng)會(huì)根據(jù)音頻流類型 stream
和音頻策略 strategy
來(lái)選擇對(duì)應(yīng)的 output
,從而找到對(duì)應(yīng)的 module
,將音頻數(shù)據(jù)傳輸給 hal
層音頻庫(kù) so
做聲音相關(guān)的處理并傳給 audio driver
。
音頻流傳輸路徑圖:
從上述的音頻流流程可以看到,我們首先要確認(rèn),當(dāng)前音頻流是經(jīng)由哪一個(gè) hal
層庫(kù)做處理,是 primary
,usb
還是三方 so
等,然后可以在對(duì)應(yīng)的節(jié)點(diǎn)抓取相應(yīng)的音頻信息分析。
可以根據(jù)自己的需要在音頻流的部分節(jié)點(diǎn)埋下相應(yīng)的 dump
指令,將 pcm 寫(xiě)入到相應(yīng)的節(jié)點(diǎn)當(dāng)中。
2. 音頻鏈路關(guān)鍵節(jié)點(diǎn):
AudioTrack
:應(yīng)用寫(xiě)入音頻流的起點(diǎn),有 MODE_STATIC
和 MODE_STREAM
模式,通過(guò) write
接口將數(shù)據(jù)寫(xiě)入。
此節(jié)點(diǎn)寫(xiě)入的數(shù)據(jù)是由應(yīng)用層最原始的音頻數(shù)據(jù)。
AudioFlinger
:負(fù)責(zé)啟動(dòng)線程完成各個(gè)應(yīng)用的混音,音頻流聲音調(diào)節(jié)等工作。設(shè)備同時(shí)可能存在多個(gè)應(yīng)用播放聲音,這時(shí)便需要將各個(gè)應(yīng)用的聲音混合在一起,并且做音量的調(diào)節(jié)。
例如在車載場(chǎng)景中,音樂(lè)應(yīng)用播放歌曲和地圖應(yīng)用語(yǔ)音導(dǎo)航的聲音需要同時(shí)存在,便使用到了混音的功能,當(dāng)導(dǎo)航語(yǔ)音響起時(shí),歌曲聲音有一個(gè)明顯的變小,便可以設(shè)置音頻流的音量。
audio_hw_hal
:hal
層音頻處理的入口,為 Android
原生邏輯,各廠家需要按照規(guī)范實(shí)現(xiàn)其中的音頻設(shè)置等接口,聲明 HAL_MODULE_INFO_SYM
結(jié)構(gòu)體,實(shí)現(xiàn) legacy_adev_open
方法,承接起連接 framework
和 audio driver
的作用,完成一些音效算法等邏輯處理。
AudioStreamOut
:和 audio_hw_hal
一樣,是Android
給廠家提供的通用類,廠家在實(shí)現(xiàn)自己的通用庫(kù)實(shí)現(xiàn)時(shí)需要可以按照谷歌規(guī)范,然后在相應(yīng)的音頻處理接口中實(shí)現(xiàn)自己的對(duì)音頻流做音效,增益等處理。
audio_hw_hal.cpp
代碼如下,不同廠家這里的實(shí)現(xiàn)略有差異,這里只截取部分 AOSP
源碼。
3. 音頻庫(kù)的選擇
從音頻流傳輸路徑圖可以看到,如何找到是哪一個(gè)音頻 so
處理聲音也是至關(guān)重要的。我們知道,系統(tǒng)對(duì)于應(yīng)用層曝光的其實(shí)只有通道類型。
舉個(gè)例子:當(dāng)用戶打電話時(shí),可以使用通話通道 STREAM_VOICE_CALL
,當(dāng)用戶播放視頻時(shí),可以使用媒體通道 STREAM_MUSIC
,當(dāng)發(fā)送通知時(shí),可以使用 STREAM_NOTIFICATION
。
那傳入這些通道的聲音數(shù)據(jù),又是怎么最終流向到具體的硬件輸出設(shè)備呢?
以媒體通道為例,當(dāng)應(yīng)用層將音頻數(shù)據(jù)往 MUSIC
通道寫(xiě)入時(shí),系統(tǒng)便會(huì)根據(jù) StreamType
來(lái)生成相應(yīng)的
audio_attributes_t(.usage = AUDIO_USAGE_MEDIA, .content_type = AUDIO_CONTENT_TYPE_MUSIC})
再通過(guò) audio_attributes_t
來(lái)獲取對(duì)應(yīng)的 ProductStrategy
(STRATEGY_MEDIA
),最后在拿到對(duì)應(yīng)的 outputDevice
。
Android
原生邏輯outputDevice
的選擇在 Engine.cpp
上,會(huì)具體根據(jù)當(dāng)前設(shè)備是否有接藍(lán)牙,耳機(jī)等外設(shè),按照優(yōu)先級(jí)來(lái)選擇相應(yīng)的外設(shè)設(shè)備作為輸出,可能是耳機(jī) (AUDIO_DEVICE_OUT_WIRED_HEADSET
),聽(tīng)筒(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
通過(guò)以上分析,我們知道了音頻會(huì)流向哪個(gè)輸出設(shè)備,那么下一個(gè)問(wèn)題來(lái)了,是由誰(shuí)負(fù)責(zé)傳輸和對(duì)音頻數(shù)據(jù)做最后的處理呢?
這里就需要看音頻設(shè)備的策略文件,還是以媒體通道為例,假設(shè)設(shè)備沒(méi)有接任何外接設(shè)備,選擇的 outputDevice
是 AUDIO_DEVICE_OUT_SPEAKER
。
接下來(lái)就要看哪個(gè) output
so
支持 AUDIO_DEVICE_OUT_SPEAKER
,符合度最高的 output
so
將會(huì)負(fù)責(zé)數(shù)據(jù)傳輸,最終經(jīng)由 tinyalsa
寫(xiě)入到 pcm
節(jié)點(diǎn)中。
不同的 Android
版本在配置文件上會(huì)有些許差異,可能放置在 *audio_policy_configuration.xml
中,有些在 audio_policy.conf
中。
三、案例分析
1. 聲音忽大忽小問(wèn)題
具體分析
有用戶反饋使用優(yōu)酷視頻播放視頻時(shí),概率性出現(xiàn)聲音忽大忽小的問(wèn)題,一旦出現(xiàn)就是在播放指定音頻時(shí)是必現(xiàn)的。接下來(lái)聯(lián)系用戶幫提供設(shè)備的日志信息和操作步驟,按照用戶操作來(lái)復(fù)現(xiàn)問(wèn)題,通過(guò) demo
還原用戶環(huán)境參數(shù)便能復(fù)現(xiàn)。 首先分析確認(rèn)發(fā)現(xiàn)在這個(gè)過(guò)程中聲音音量均無(wú)變化,所以初步懷疑可能是和音頻流數(shù)據(jù)出現(xiàn)異常有關(guān)。在上圖中數(shù)字有標(biāo)識(shí)的5個(gè)點(diǎn)中分別抓取音頻,使用 Audacity
導(dǎo)入音頻文件來(lái)進(jìn)行分析,發(fā)現(xiàn)位置4的音頻正常,而位置5的音頻出現(xiàn)了聲音異常的現(xiàn)象。具體見(jiàn)下圖:
所以便可以確認(rèn)在音頻數(shù)據(jù)經(jīng)過(guò) hal 層音效處理前是正常等,經(jīng)過(guò)音效處理后,在某些特殊的聲音數(shù)據(jù)下,音效庫(kù)縮小了聲音的幅值,從而導(dǎo)致聲音的異常。
為了實(shí)錘是音效庫(kù) so 導(dǎo)致的問(wèn)題,通過(guò)關(guān)閉音效庫(kù)的功能,最終發(fā)現(xiàn)聲音忽大忽小的問(wèn)題消失了。
從以上嘗試的結(jié)果綜合分析可以確認(rèn),是音效 so 庫(kù)對(duì)通道聲音進(jìn)行處理時(shí)影響到了原有聲音的功能。通過(guò)修改 so
庫(kù)最終來(lái)解決這個(gè)問(wèn)題。
2. 應(yīng)用卡頓問(wèn)題
有用戶反饋說(shuō)是打開(kāi)應(yīng)用A播放視頻正常,然后直接返回到 home 目錄,應(yīng)用A后臺(tái)播放時(shí)會(huì)出現(xiàn)斷音的現(xiàn)象。
具體分析
聲音卡頓,錄音掉幀類的現(xiàn)象在聲音問(wèn)題中非常常見(jiàn)。從現(xiàn)象上來(lái)看,就是用戶切換到后臺(tái)時(shí)沒(méi)有暫停播放,視頻在后臺(tái)播放時(shí)出現(xiàn)。老規(guī)矩,我們先分析相關(guān) log
,通過(guò)日志分析發(fā)現(xiàn),當(dāng)問(wèn)題出現(xiàn)時(shí),日志上頻繁打印 get null buffer
的信息。
所以懷疑是否是音頻數(shù)據(jù)丟失導(dǎo)致的。dump
音頻數(shù)據(jù)抓取到系統(tǒng)混音后輸出到輸出設(shè)備的原始音頻,可以幫助實(shí)錘上層系統(tǒng)傳下來(lái)的數(shù)據(jù)是否正常。于是發(fā)現(xiàn)位置3的音頻如下:
從抓取到的音頻可以看到,在后臺(tái)異常播放時(shí),在該時(shí)間段內(nèi)會(huì)出現(xiàn)明顯音頻丟幀的現(xiàn)象。接下來(lái)要看看是在哪一塊出現(xiàn)了丟幀。進(jìn)一步分析 audioTrack
傳下來(lái)的數(shù)據(jù),出現(xiàn)了丟失掉一部分音頻的現(xiàn)象,時(shí)長(zhǎng)相當(dāng)于原音頻的一半。
基于此,為了實(shí)錘是應(yīng)用A傳下來(lái)的數(shù)據(jù)就有缺失,從日志信息跟蹤,決定在 audioTrack 上加日志信息來(lái)看,發(fā)現(xiàn)當(dāng)切換到后臺(tái)時(shí),audioTrack 每次還是寫(xiě) 4096 個(gè) byte
,但是寫(xiě)數(shù)據(jù)的頻率降低了一半。
正常:28.600-27.546=1.054 44次 間隔 1.054/43= 0.0245秒/次。
異常:40.839-39.804=1.035 22次 間隔 1.035/21= 0.0493秒/次。
考慮到這一塊是否是和后臺(tái)進(jìn)程的優(yōu)先級(jí)相關(guān)。當(dāng)進(jìn)程降低時(shí)導(dǎo)致了寫(xiě)數(shù)據(jù)的線程能夠拿到的CPU資源變小,出現(xiàn)了斷音的問(wèn)題。通過(guò)和其他型號(hào)的平板對(duì)比發(fā)現(xiàn),各廠家 Android 10 的平板大部分均有此問(wèn)題,而 android7 和 android 8 的平板就沒(méi)有這個(gè)問(wèn)題?;谝陨锨闆r,更加懷疑是和 android 的特性相關(guān),可能是新的 android 平板針對(duì)后臺(tái)線程優(yōu)先級(jí)做了處理,目的也很明確,就是限制后臺(tái)應(yīng)用的活躍程度,來(lái)保證設(shè)備性能。
此時(shí)進(jìn)一步分析最終發(fā)現(xiàn)是和 TimerSlackHigh
的參數(shù)相關(guān)。
system/core/libprocessgroup/profiles/task_profiles.json:
{ "Name": "TimerSlackHigh", "Actions": [ { "Name": "SetTimerSlack", "Params": { "Slack": "40000000" } } ] }, { "Name": "TimerSlackNormal", "Actions": [ { "Name": "SetTimerSlack", "Params": { "Slack": "50000" } } ] },
該參數(shù)影響了應(yīng)用后臺(tái)線程 sleep/wait 之間所消耗的時(shí)間??梢钥吹?,當(dāng)應(yīng)用從前臺(tái)切換到后臺(tái)時(shí),這個(gè)時(shí)間從50 微秒上調(diào)到 40 毫秒。從而導(dǎo)致寫(xiě)入音頻數(shù)據(jù)量大大減少。通過(guò)修改參數(shù)可以解決,但是提高后臺(tái)線程的活躍度,很可能影響到整體性能,因此不作處理。最終像用戶解釋,切換后臺(tái)時(shí)可以手動(dòng)停止播放視頻,同時(shí)反饋給應(yīng)用,由應(yīng)用規(guī)范應(yīng)用流程,起后臺(tái)進(jìn)程來(lái)做單獨(dú)處理。
四、總結(jié)
按照以上案例,我們來(lái)總結(jié)下當(dāng)聲音出現(xiàn)異常時(shí)一些快速定位調(diào)試的手段:
- 抓取位置1的音頻數(shù)據(jù),如果該數(shù)據(jù)異常。代表從應(yīng)用端傳遞下來(lái)的數(shù)據(jù)即為異常,大部分情況下為應(yīng)用問(wèn)題。曾經(jīng)遇到一個(gè)是應(yīng)用默認(rèn)會(huì)將
track
音量調(diào)為0,此時(shí)調(diào)節(jié)系統(tǒng)音量時(shí)不會(huì)有聲音的。需要用戶點(diǎn)擊該應(yīng)用內(nèi)的一個(gè)靜音按鈕才有聲音,這時(shí)候就會(huì)在位置1抓到一串無(wú)聲的音頻,這種在安卓版本表現(xiàn)是一致的。但是也有可能是像案例2一樣和后臺(tái)優(yōu)先級(jí)有關(guān),導(dǎo)致只在較高的版本上出現(xiàn)問(wèn)題。 - 抓取位置3的音頻數(shù)據(jù),若此音頻流經(jīng)過(guò)各播放線程時(shí)出現(xiàn)問(wèn)題,則可能是系統(tǒng) mix, direct 邏輯出現(xiàn)問(wèn)題,原生邏輯通常不會(huì)有問(wèn)題,有可能是客制化修改引發(fā)的。
- 抓取位置5的音頻數(shù)據(jù),此部分邏輯是由
output so
來(lái)處理的,可能是音效庫(kù)處理數(shù)據(jù)等操作導(dǎo)致聲音異常。
以上就是Android audio音頻流數(shù)據(jù)異常問(wèn)題解決分析的詳細(xì)內(nèi)容,更多關(guān)于Android audio音頻流數(shù)據(jù)異常的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android實(shí)現(xiàn)列表時(shí)間軸
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)列表時(shí)間軸效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05android TextView不用ScrollViewe也可以滾動(dòng)的方法
這篇文章主要介紹了android TextView不用ScrollViewe也可以滾動(dòng)的方法,很簡(jiǎn)單實(shí)用的代碼,大家參考使用吧2013-11-11Android使用Kotlin API實(shí)踐WorkManager
這篇文章主要介紹了Android使用Kotlin API實(shí)踐WorkManager的步驟,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-04-04Android編程實(shí)現(xiàn)畫(huà)板功能的方法總結(jié)【附源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)畫(huà)板功能的方法,結(jié)合實(shí)例形式總結(jié)分析了Android基于自定義View與Canvas類實(shí)現(xiàn)畫(huà)板功能的具體操作步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-02-02Android多渠道打包時(shí)獲取當(dāng)前渠道的方法
這篇文章主要介紹了Android多渠道打包時(shí)獲取當(dāng)前渠道的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01