Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2022, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | // #define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "AudioFlinger::MelReporter" |
| 20 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 21 | #include "AudioFlinger.h" |
| 22 | |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 23 | #include <android/media/ISoundDoseCallback.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 24 | #include <audio_utils/power.h> |
| 25 | #include <utils/Log.h> |
| 26 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 27 | namespace android { |
| 28 | |
| 29 | bool AudioFlinger::MelReporter::shouldComputeMelForDeviceType(audio_devices_t device) { |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 30 | if (mSoundDoseManager.computeCsdOnAllDevices()) { |
| 31 | return true; |
| 32 | } |
| 33 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 34 | switch (device) { |
| 35 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: |
| 36 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: |
| 37 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: |
| 38 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: |
| 39 | case AUDIO_DEVICE_OUT_HEARING_AID: |
| 40 | case AUDIO_DEVICE_OUT_USB_HEADSET: |
| 41 | case AUDIO_DEVICE_OUT_BLE_HEADSET: |
| 42 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: |
| 43 | return true; |
| 44 | default: |
| 45 | return false; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void AudioFlinger::MelReporter::onCreateAudioPatch(audio_patch_handle_t handle, |
| 50 | const PatchPanel::Patch& patch) { |
| 51 | ALOGV("%s: handle %d mHalHandle %d device sink %08x", |
| 52 | __func__, handle, patch.mHalHandle, |
| 53 | patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0); |
| 54 | if (patch.mAudioPatch.num_sources == 0 |
| 55 | || patch.mAudioPatch.sources[0].type != AUDIO_PORT_TYPE_MIX) { |
| 56 | ALOGW("%s: patch does not contain any mix sources", __func__); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | audio_io_handle_t streamHandle = patch.mAudioPatch.sources[0].ext.mix.handle; |
| 61 | ActiveMelPatch newPatch; |
| 62 | newPatch.streamHandle = streamHandle; |
| 63 | for (int i = 0; i < patch.mAudioPatch.num_sinks; ++ i) { |
| 64 | if (patch.mAudioPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE |
| 65 | && shouldComputeMelForDeviceType(patch.mAudioPatch.sinks[i].ext.device.type)) { |
| 66 | audio_port_handle_t deviceId = patch.mAudioPatch.sinks[i].id; |
| 67 | newPatch.deviceHandles.push_back(deviceId); |
| 68 | |
| 69 | // Start the MEL calculation in the PlaybackThread |
| 70 | std::lock_guard _lAf(mAudioFlinger.mLock); |
| 71 | auto thread = mAudioFlinger.checkPlaybackThread_l(streamHandle); |
| 72 | if (thread != nullptr) { |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 73 | thread->startMelComputation(mSoundDoseManager.getOrCreateProcessorForDevice( |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 74 | deviceId, |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 75 | newPatch.streamHandle, |
| 76 | thread->mSampleRate, |
| 77 | thread->mChannelCount, |
| 78 | thread->mFormat)); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | std::lock_guard _l(mLock); |
| 84 | mActiveMelPatches[patch.mAudioPatch.id] = newPatch; |
| 85 | } |
| 86 | |
| 87 | void AudioFlinger::MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) { |
| 88 | ALOGV("%s", __func__); |
| 89 | |
| 90 | ActiveMelPatch melPatch; |
| 91 | { |
| 92 | std::lock_guard _l(mLock); |
| 93 | |
| 94 | auto patchIt = mActiveMelPatches.find(handle); |
| 95 | if (patchIt == mActiveMelPatches.end()) { |
| 96 | ALOGW( |
| 97 | "%s patch does not contain any mix sources with active MEL calculation", |
| 98 | __func__); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | melPatch = patchIt->second; |
| 103 | mActiveMelPatches.erase(patchIt); |
| 104 | } |
| 105 | |
| 106 | // Stop MEL calculation for the PlaybackThread |
| 107 | std::lock_guard _lAf(mAudioFlinger.mLock); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 108 | mSoundDoseManager.removeStreamProcessor(melPatch.streamHandle); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 109 | auto thread = mAudioFlinger.checkPlaybackThread_l(melPatch.streamHandle); |
| 110 | if (thread != nullptr) { |
| 111 | thread->stopMelComputation(); |
| 112 | } |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 115 | sp<media::ISoundDose> AudioFlinger::MelReporter::getSoundDoseInterface( |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 116 | const sp<media::ISoundDoseCallback>& callback) { |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 117 | // no need to lock since getSoundDoseInterface is synchronized |
| 118 | return mSoundDoseManager.getSoundDoseInterface(callback); |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 121 | std::string AudioFlinger::MelReporter::dump() { |
| 122 | std::lock_guard _l(mLock); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 123 | std::string output("\nSound Dose:\n"); |
| 124 | output.append(mSoundDoseManager.dump()); |
Vlad Popa | b042ee6 | 2022-10-20 18:05:00 +0200 | [diff] [blame] | 125 | return output; |
| 126 | } |
| 127 | |
| 128 | } // namespace android |