1 /*
2  * Copyright (C) 2018 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 package com.android.car.audio;
17 
18 import android.media.AudioAttributes;
19 import android.media.AudioAttributes.AttributeUsage;
20 import android.media.AudioFormat;
21 import android.media.AudioManager;
22 import android.media.audiopolicy.AudioMix;
23 import android.media.audiopolicy.AudioMixingRule;
24 import android.media.audiopolicy.AudioPolicy;
25 import android.util.Log;
26 
27 import com.android.car.CarLog;
28 
29 import java.util.Arrays;
30 
31 /**
32  * Builds dynamic audio routing in a car from audio zone configuration.
33  */
34 /* package */ class CarAudioDynamicRouting {
35     // For legacy stream type based volume control.
36     // Values in STREAM_TYPES and STREAM_TYPE_USAGES should be aligned.
37     static final int[] STREAM_TYPES = new int[] {
38             AudioManager.STREAM_MUSIC,
39             AudioManager.STREAM_ALARM,
40             AudioManager.STREAM_RING
41     };
42     static final int[] STREAM_TYPE_USAGES = new int[] {
43             AudioAttributes.USAGE_MEDIA,
44             AudioAttributes.USAGE_ALARM,
45             AudioAttributes.USAGE_NOTIFICATION_RINGTONE
46     };
47 
48     private final CarAudioZone[] mCarAudioZones;
49 
CarAudioDynamicRouting(CarAudioZone[] carAudioZones)50     CarAudioDynamicRouting(CarAudioZone[] carAudioZones) {
51         mCarAudioZones = carAudioZones;
52     }
53 
setupAudioDynamicRouting(AudioPolicy.Builder builder)54     void setupAudioDynamicRouting(AudioPolicy.Builder builder) {
55         for (CarAudioZone zone : mCarAudioZones) {
56             for (CarVolumeGroup group : zone.getVolumeGroups()) {
57                 setupAudioDynamicRoutingForGroup(group, builder);
58             }
59         }
60     }
61 
62     /**
63      * Enumerates all physical buses in a given volume group and attach the mixing rules.
64      * @param group {@link CarVolumeGroup} instance to enumerate the buses with
65      * @param builder {@link AudioPolicy.Builder} to attach the mixing rules
66      */
setupAudioDynamicRoutingForGroup(CarVolumeGroup group, AudioPolicy.Builder builder)67     private void setupAudioDynamicRoutingForGroup(CarVolumeGroup group,
68             AudioPolicy.Builder builder) {
69         // Note that one can not register audio mix for same bus more than once.
70         for (String address : group.getAddresses()) {
71             boolean hasContext = false;
72             CarAudioDeviceInfo info = group.getCarAudioDeviceInfoForAddress(address);
73             AudioFormat mixFormat = new AudioFormat.Builder()
74                     .setSampleRate(info.getSampleRate())
75                     .setEncoding(info.getEncodingFormat())
76                     .setChannelMask(info.getChannelCount())
77                     .build();
78             AudioMixingRule.Builder mixingRuleBuilder = new AudioMixingRule.Builder();
79             for (int carAudioContext : group.getContextsForAddress(address)) {
80                 hasContext = true;
81                 int[] usages = CarAudioContext.getUsagesForContext(carAudioContext);
82                 for (int usage : usages) {
83                     AudioAttributes attributes = buildAttributesWithUsage(usage);
84                     mixingRuleBuilder.addRule(attributes,
85                             AudioMixingRule.RULE_MATCH_ATTRIBUTE_USAGE);
86                 }
87                 if (Log.isLoggable(CarLog.TAG_AUDIO, Log.DEBUG)) {
88                     Log.d(CarLog.TAG_AUDIO, String.format(
89                             "Address: %s AudioContext: %s sampleRate: %d channels: %d usages: %s",
90                             address, carAudioContext, info.getSampleRate(), info.getChannelCount(),
91                             Arrays.toString(usages)));
92                 }
93             }
94             if (hasContext) {
95                 // It's a valid case that an audio output address is defined in
96                 // audio_policy_configuration and no context is assigned to it.
97                 // In such case, do not build a policy mix with zero rules.
98                 AudioMix audioMix = new AudioMix.Builder(mixingRuleBuilder.build())
99                         .setFormat(mixFormat)
100                         .setDevice(info.getAudioDeviceInfo())
101                         .setRouteFlags(AudioMix.ROUTE_FLAG_RENDER)
102                         .build();
103                 builder.addMix(audioMix);
104             }
105         }
106     }
107 
buildAttributesWithUsage(@ttributeUsage int usage)108     private AudioAttributes buildAttributesWithUsage(@AttributeUsage int usage) {
109         AudioAttributes.Builder attributesBuilder = new AudioAttributes.Builder();
110         if (AudioAttributes.isSystemUsage(usage)) {
111             attributesBuilder.setSystemUsage(usage);
112         } else {
113             attributesBuilder.setUsage(usage);
114         }
115         return attributesBuilder.build();
116     }
117 }
118