1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android-base/logging.h>
18 
19 #include "../include/CarAudioConfigurationUtils.h"
20 
21 #include <aidl/android/media/audio/common/AudioAttributes.h>
22 #include <aidl/android/media/audio/common/AudioUsage.h>
23 
24 #include <tuple>
25 #include <vector>
26 
27 using ::aidl::android::hardware::automotive::audiocontrol::AudioZoneContext;
28 using ::aidl::android::hardware::automotive::audiocontrol::AudioZoneContextInfo;
29 
30 using aidl::android::media::audio::common::AudioAttributes;
31 using aidl::android::media::audio::common::AudioUsage;
32 using aidl::android::media::audio::common::AudioUsage::ALARM;
33 using aidl::android::media::audio::common::AudioUsage::ANNOUNCEMENT;
34 using aidl::android::media::audio::common::AudioUsage::ASSISTANCE_ACCESSIBILITY;
35 using aidl::android::media::audio::common::AudioUsage::ASSISTANCE_NAVIGATION_GUIDANCE;
36 using aidl::android::media::audio::common::AudioUsage::ASSISTANCE_SONIFICATION;
37 using aidl::android::media::audio::common::AudioUsage::ASSISTANT;
38 using aidl::android::media::audio::common::AudioUsage::CALL_ASSISTANT;
39 using aidl::android::media::audio::common::AudioUsage::EMERGENCY;
40 using aidl::android::media::audio::common::AudioUsage::GAME;
41 using aidl::android::media::audio::common::AudioUsage::MEDIA;
42 using aidl::android::media::audio::common::AudioUsage::NOTIFICATION;
43 using aidl::android::media::audio::common::AudioUsage::NOTIFICATION_EVENT;
44 using aidl::android::media::audio::common::AudioUsage::NOTIFICATION_TELEPHONY_RINGTONE;
45 using aidl::android::media::audio::common::AudioUsage::SAFETY;
46 using aidl::android::media::audio::common::AudioUsage::UNKNOWN;
47 using aidl::android::media::audio::common::AudioUsage::VEHICLE_STATUS;
48 using aidl::android::media::audio::common::AudioUsage::VOICE_COMMUNICATION;
49 using aidl::android::media::audio::common::AudioUsage::VOICE_COMMUNICATION_SIGNALLING;
50 
51 namespace android {
52 namespace hardware {
53 namespace audiocontrol {
54 namespace internal {
55 
createAudioAttributes(const std::vector<AudioUsage> & usages)56 std::vector<AudioAttributes> createAudioAttributes(const std::vector<AudioUsage>& usages) {
57     std::vector<AudioAttributes> audioAttributes;
58     for (const auto& usage : usages) {
59         AudioAttributes attributes;
60         attributes.usage = usage;
61         audioAttributes.push_back(attributes);
62     }
63     return audioAttributes;
64 }
65 
createAudioZoneContextInfo(const std::string & name,int id,const std::vector<AudioUsage> & usages)66 AudioZoneContextInfo createAudioZoneContextInfo(const std::string& name, int id,
67                                                 const std::vector<AudioUsage>& usages) {
68     AudioZoneContextInfo info;
69     info.name = name;
70     info.id = id;
71     info.audioAttributes = createAudioAttributes(usages);
72     return info;
73 }
74 
createAudioZoneContextInfo(const std::vector<AudioZoneContextInfo> & info)75 AudioZoneContext createAudioZoneContextInfo(const std::vector<AudioZoneContextInfo>& info) {
76     AudioZoneContext context;
77     context.audioContextInfos.insert(context.audioContextInfos.begin(), info.begin(), info.end());
78     return context;
79 }
80 
getDefaultCarAudioContext()81 AudioZoneContext getDefaultCarAudioContext() {
82     // For legacy reasons, context names are lower case here.
83     static const AudioZoneContext kDefaultContext = createAudioZoneContextInfo(
84             {createAudioZoneContextInfo("music", 1, {UNKNOWN, MEDIA, GAME}),
85              createAudioZoneContextInfo("navigation", 2, {ASSISTANCE_NAVIGATION_GUIDANCE}),
86              createAudioZoneContextInfo("voice_command", 3, {ASSISTANCE_ACCESSIBILITY, ASSISTANT}),
87              createAudioZoneContextInfo("call_ring", 4, {NOTIFICATION_TELEPHONY_RINGTONE}),
88              createAudioZoneContextInfo(
89                      "call", 5,
90                      {VOICE_COMMUNICATION, CALL_ASSISTANT, VOICE_COMMUNICATION_SIGNALLING}),
91              createAudioZoneContextInfo("alarm", 6, {ALARM}),
92              createAudioZoneContextInfo("notification", 7, {NOTIFICATION, NOTIFICATION_EVENT}),
93              createAudioZoneContextInfo("system_sound", 8, {ASSISTANCE_SONIFICATION}),
94              createAudioZoneContextInfo("emergency", 9, {EMERGENCY}),
95              createAudioZoneContextInfo("safety", 10, {SAFETY}),
96              createAudioZoneContextInfo("vehicle_status", 11, {VEHICLE_STATUS}),
97              createAudioZoneContextInfo("announcement", 12, {ANNOUNCEMENT})});
98     return kDefaultContext;
99 }
100 
101 }  // namespace internal
102 }  // namespace audiocontrol
103 }  // namespace hardware
104 }  // namespace android
105