blob: 6c783eca8fa3d91dd105ba51b986a0fb3773050f [file] [log] [blame]
Vlad Popab042ee62022-10-20 18:05:00 +02001/*
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 Popab042ee62022-10-20 18:05:00 +020021#include "AudioFlinger.h"
22
Vlad Popa63f047e2022-11-05 14:09:19 +010023#include <android/media/ISoundDoseCallback.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020024#include <audio_utils/power.h>
Vlad Popa1d5f0d52022-12-18 12:21:26 +010025#include <android/binder_manager.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020026#include <utils/Log.h>
27
Vlad Popa1d5f0d52022-12-18 12:21:26 +010028using aidl::android::hardware::audio::core::ISoundDose;
29using aidl::android::hardware::audio::sounddose::ISoundDoseFactory;
30
Vlad Popab042ee62022-10-20 18:05:00 +020031namespace android {
32
Vlad Popa1d5f0d52022-12-18 12:21:26 +010033constexpr std::string_view kSoundDoseInterfaceModule = "/default";
34
35bool AudioFlinger::MelReporter::activateHalSoundDoseComputation(const std::string& module) {
Vlad Popa3d6d39d2022-12-21 18:59:16 +010036 if (mSoundDoseManager->forceUseFrameworkMel()) {
37 ALOGD("%s: Forcing use of internal MEL computation.", __func__);
38 activateInternalSoundDoseComputation();
39 return false;
40 }
41
Vlad Popa1d5f0d52022-12-18 12:21:26 +010042 if (mSoundDoseFactory == nullptr) {
Vlad Popa3d6d39d2022-12-21 18:59:16 +010043 ALOGW("%s: sound dose HAL reporting not available", __func__);
Vlad Popa1d5f0d52022-12-18 12:21:26 +010044 activateInternalSoundDoseComputation();
45 return false;
46 }
47
48 std::shared_ptr<ISoundDose> soundDoseInterface;
49 auto result = mSoundDoseFactory->getSoundDose(module, &soundDoseInterface);
50 if (!result.isOk()) {
Vlad Popa3d6d39d2022-12-21 18:59:16 +010051 ALOGW("%s: HAL cannot provide sound dose interface for module %s",
Vlad Popa1d5f0d52022-12-18 12:21:26 +010052 __func__, module.c_str());
53 activateInternalSoundDoseComputation();
54 return false;
55 }
56
57 if (!mSoundDoseManager->setHalSoundDoseInterface(soundDoseInterface)) {
Vlad Popa3d6d39d2022-12-21 18:59:16 +010058 ALOGW("%s: cannot activate HAL MEL reporting for module %s", __func__, module.c_str());
Vlad Popa1d5f0d52022-12-18 12:21:26 +010059 activateInternalSoundDoseComputation();
60 return false;
61 }
62
Vlad Popa1d5f0d52022-12-18 12:21:26 +010063 stopInternalMelComputation();
64 return true;
65}
66
67void AudioFlinger::MelReporter::activateInternalSoundDoseComputation() {
Vlad Popa3d6d39d2022-12-21 18:59:16 +010068 {
69 std::lock_guard _l(mLock);
70 if (!mUseHalSoundDoseInterface) {
71 // no need to start internal MEL on active patches
72 return;
73 }
74 mUseHalSoundDoseInterface = false;
75 }
Vlad Popa1d5f0d52022-12-18 12:21:26 +010076
Vlad Popa3d6d39d2022-12-21 18:59:16 +010077 mSoundDoseManager->setHalSoundDoseInterface(nullptr);
Vlad Popa1d5f0d52022-12-18 12:21:26 +010078}
79
80void AudioFlinger::MelReporter::onFirstRef() {
81 mAudioFlinger.mPatchCommandThread->addListener(this);
82
83 std::string interface =
84 std::string(ISoundDoseFactory::descriptor) + kSoundDoseInterfaceModule.data();
85 AIBinder* binder = AServiceManager_checkService(interface.c_str());
86 if (binder == nullptr) {
87 ALOGW("%s service %s doesn't exist", __func__, interface.c_str());
88 return;
89 }
90
91 mSoundDoseFactory = ISoundDoseFactory::fromBinder(ndk::SpAIBinder(binder));
92}
93
Vlad Popab042ee62022-10-20 18:05:00 +020094bool AudioFlinger::MelReporter::shouldComputeMelForDeviceType(audio_devices_t device) {
Vlad Popa3d6d39d2022-12-21 18:59:16 +010095 if (mSoundDoseManager->forceComputeCsdOnAllDevices()) {
Vlad Popa91930462022-12-20 22:42:48 +010096 return true;
97 }
98
Vlad Popab042ee62022-10-20 18:05:00 +020099 switch (device) {
100 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
101 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
102 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
103 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
104 case AUDIO_DEVICE_OUT_HEARING_AID:
105 case AUDIO_DEVICE_OUT_USB_HEADSET:
106 case AUDIO_DEVICE_OUT_BLE_HEADSET:
107 case AUDIO_DEVICE_OUT_BLE_BROADCAST:
108 return true;
109 default:
110 return false;
111 }
112}
113
114void AudioFlinger::MelReporter::onCreateAudioPatch(audio_patch_handle_t handle,
115 const PatchPanel::Patch& patch) {
116 ALOGV("%s: handle %d mHalHandle %d device sink %08x",
117 __func__, handle, patch.mHalHandle,
118 patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0);
119 if (patch.mAudioPatch.num_sources == 0
120 || patch.mAudioPatch.sources[0].type != AUDIO_PORT_TYPE_MIX) {
121 ALOGW("%s: patch does not contain any mix sources", __func__);
122 return;
123 }
124
125 audio_io_handle_t streamHandle = patch.mAudioPatch.sources[0].ext.mix.handle;
126 ActiveMelPatch newPatch;
127 newPatch.streamHandle = streamHandle;
128 for (int i = 0; i < patch.mAudioPatch.num_sinks; ++ i) {
129 if (patch.mAudioPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE
130 && shouldComputeMelForDeviceType(patch.mAudioPatch.sinks[i].ext.device.type)) {
131 audio_port_handle_t deviceId = patch.mAudioPatch.sinks[i].id;
132 newPatch.deviceHandles.push_back(deviceId);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100133 AudioDeviceTypeAddr adt{patch.mAudioPatch.sinks[i].ext.device.type,
134 patch.mAudioPatch.sinks[i].ext.device.address};
135 mSoundDoseManager->mapAddressToDeviceId(adt, deviceId);
Vlad Popab042ee62022-10-20 18:05:00 +0200136
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100137 bool useHalSoundDoseInterface = !mSoundDoseManager->forceUseFrameworkMel();
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100138 {
139 std::lock_guard _l(mLock);
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100140 useHalSoundDoseInterface &= mUseHalSoundDoseInterface;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100141 }
142 if (!useHalSoundDoseInterface) {
143 startMelComputationForNewPatch(streamHandle, deviceId);
Vlad Popab042ee62022-10-20 18:05:00 +0200144 }
145 }
146 }
147
148 std::lock_guard _l(mLock);
149 mActiveMelPatches[patch.mAudioPatch.id] = newPatch;
150}
151
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100152void AudioFlinger::MelReporter::startMelComputationForNewPatch(
153 audio_io_handle_t streamHandle, audio_port_handle_t deviceId) {
154 // Start the MEL calculation in the PlaybackThread
155 std::lock_guard _lAf(mAudioFlinger.mLock);
156 auto thread = mAudioFlinger.checkPlaybackThread_l(streamHandle);
157 if (thread != nullptr) {
158 thread->startMelComputation(mSoundDoseManager->getOrCreateProcessorForDevice(
159 deviceId,
160 streamHandle,
161 thread->mSampleRate,
162 thread->mChannelCount,
163 thread->mFormat));
164 }
165}
166
Vlad Popab042ee62022-10-20 18:05:00 +0200167void AudioFlinger::MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) {
168 ALOGV("%s", __func__);
169
170 ActiveMelPatch melPatch;
171 {
172 std::lock_guard _l(mLock);
173
174 auto patchIt = mActiveMelPatches.find(handle);
175 if (patchIt == mActiveMelPatches.end()) {
176 ALOGW(
177 "%s patch does not contain any mix sources with active MEL calculation",
178 __func__);
179 return;
180 }
181
182 melPatch = patchIt->second;
183 mActiveMelPatches.erase(patchIt);
184 }
185
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100186 for (const auto& deviceId : melPatch.deviceHandles) {
187 mSoundDoseManager->clearMapDeviceIdEntries(deviceId);
Vlad Popab042ee62022-10-20 18:05:00 +0200188 }
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100189 stopInternalMelComputationForStream(melPatch.streamHandle);
Vlad Popab042ee62022-10-20 18:05:00 +0200190}
191
Vlad Popae3fd1c22022-11-07 21:03:18 +0100192sp<media::ISoundDose> AudioFlinger::MelReporter::getSoundDoseInterface(
Vlad Popa63f047e2022-11-05 14:09:19 +0100193 const sp<media::ISoundDoseCallback>& callback) {
Vlad Popae3fd1c22022-11-07 21:03:18 +0100194 // no need to lock since getSoundDoseInterface is synchronized
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100195 return mSoundDoseManager->getSoundDoseInterface(callback);
196}
197
198void AudioFlinger::MelReporter::stopInternalMelComputation() {
199 ALOGV("%s", __func__);
Vlad Popa870f6d62022-12-22 12:15:47 +0100200 std::lock_guard _l(mLock);
201 mActiveMelPatches.clear();
202 mUseHalSoundDoseInterface = true;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100203}
204
205void AudioFlinger::MelReporter::stopInternalMelComputationForStream(audio_io_handle_t streamId) {
206 ALOGV("%s: stop internal mel for stream id: %d", __func__, streamId);
207
208 std::lock_guard _lAf(mAudioFlinger.mLock);
209 mSoundDoseManager->removeStreamProcessor(streamId);
210 auto thread = mAudioFlinger.checkPlaybackThread_l(streamId);
211 if (thread != nullptr) {
212 thread->stopMelComputation();
213 }
Vlad Popa63f047e2022-11-05 14:09:19 +0100214}
215
Vlad Popab042ee62022-10-20 18:05:00 +0200216std::string AudioFlinger::MelReporter::dump() {
217 std::lock_guard _l(mLock);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200218 std::string output("\nSound Dose:\n");
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100219 output.append(mSoundDoseManager->dump());
Vlad Popab042ee62022-10-20 18:05:00 +0200220 return output;
221}
222
223} // namespace android