blob: 6fc756b275015ce53811093d41d7888740a14da7 [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 Popa2900c0a2022-10-24 13:38:00 +020023#include <audio_utils/power.h>
24#include <utils/Log.h>
25
Vlad Popab042ee62022-10-20 18:05:00 +020026namespace android {
27
28bool AudioFlinger::MelReporter::shouldComputeMelForDeviceType(audio_devices_t device) {
29 switch (device) {
30 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
31 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
32 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
33 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
34 case AUDIO_DEVICE_OUT_HEARING_AID:
35 case AUDIO_DEVICE_OUT_USB_HEADSET:
36 case AUDIO_DEVICE_OUT_BLE_HEADSET:
37 case AUDIO_DEVICE_OUT_BLE_BROADCAST:
38 return true;
39 default:
40 return false;
41 }
42}
43
44void AudioFlinger::MelReporter::onCreateAudioPatch(audio_patch_handle_t handle,
45 const PatchPanel::Patch& patch) {
46 ALOGV("%s: handle %d mHalHandle %d device sink %08x",
47 __func__, handle, patch.mHalHandle,
48 patch.mAudioPatch.num_sinks > 0 ? patch.mAudioPatch.sinks[0].ext.device.type : 0);
49 if (patch.mAudioPatch.num_sources == 0
50 || patch.mAudioPatch.sources[0].type != AUDIO_PORT_TYPE_MIX) {
51 ALOGW("%s: patch does not contain any mix sources", __func__);
52 return;
53 }
54
55 audio_io_handle_t streamHandle = patch.mAudioPatch.sources[0].ext.mix.handle;
56 ActiveMelPatch newPatch;
57 newPatch.streamHandle = streamHandle;
58 for (int i = 0; i < patch.mAudioPatch.num_sinks; ++ i) {
59 if (patch.mAudioPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE
60 && shouldComputeMelForDeviceType(patch.mAudioPatch.sinks[i].ext.device.type)) {
61 audio_port_handle_t deviceId = patch.mAudioPatch.sinks[i].id;
62 newPatch.deviceHandles.push_back(deviceId);
63
64 // Start the MEL calculation in the PlaybackThread
65 std::lock_guard _lAf(mAudioFlinger.mLock);
66 auto thread = mAudioFlinger.checkPlaybackThread_l(streamHandle);
67 if (thread != nullptr) {
Vlad Popaf09e93f2022-10-31 16:27:12 +010068 thread->startMelComputation(mSoundDoseManager.getOrCreateProcessorForDevice(
Vlad Popab042ee62022-10-20 18:05:00 +020069 deviceId,
Vlad Popaf09e93f2022-10-31 16:27:12 +010070 newPatch.streamHandle,
71 thread->mSampleRate,
72 thread->mChannelCount,
73 thread->mFormat));
Vlad Popab042ee62022-10-20 18:05:00 +020074 }
75 }
76 }
77
78 std::lock_guard _l(mLock);
79 mActiveMelPatches[patch.mAudioPatch.id] = newPatch;
80}
81
82void AudioFlinger::MelReporter::onReleaseAudioPatch(audio_patch_handle_t handle) {
83 ALOGV("%s", __func__);
84
85 ActiveMelPatch melPatch;
86 {
87 std::lock_guard _l(mLock);
88
89 auto patchIt = mActiveMelPatches.find(handle);
90 if (patchIt == mActiveMelPatches.end()) {
91 ALOGW(
92 "%s patch does not contain any mix sources with active MEL calculation",
93 __func__);
94 return;
95 }
96
97 melPatch = patchIt->second;
98 mActiveMelPatches.erase(patchIt);
99 }
100
101 // Stop MEL calculation for the PlaybackThread
102 std::lock_guard _lAf(mAudioFlinger.mLock);
Vlad Popaf09e93f2022-10-31 16:27:12 +0100103 mSoundDoseManager.removeStreamProcessor(melPatch.streamHandle);
Vlad Popab042ee62022-10-20 18:05:00 +0200104 auto thread = mAudioFlinger.checkPlaybackThread_l(melPatch.streamHandle);
105 if (thread != nullptr) {
106 thread->stopMelComputation();
107 }
Vlad Popab042ee62022-10-20 18:05:00 +0200108}
109
110std::string AudioFlinger::MelReporter::dump() {
111 std::lock_guard _l(mLock);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200112 std::string output("\nSound Dose:\n");
113 output.append(mSoundDoseManager.dump());
Vlad Popab042ee62022-10-20 18:05:00 +0200114 return output;
115}
116
117} // namespace android