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 
17 #define LOG_TAG "AudioProductStrategy"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 #include <media/AudioProductStrategy.h>
21 #include <media/VolumeGroupAttributes.h>
22 #include <media/PolicyAidlConversion.h>
23 
24 namespace android {
25 
readFromParcel(const Parcel * parcel)26 status_t AudioProductStrategy::readFromParcel(const Parcel* parcel) {
27     media::AudioProductStrategy aidl;
28     RETURN_STATUS_IF_ERROR(aidl.readFromParcel(parcel));
29     *this = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioProductStrategy(aidl));
30     return OK;
31 }
32 
writeToParcel(Parcel * parcel) const33 status_t AudioProductStrategy::writeToParcel(Parcel* parcel) const {
34     media::AudioProductStrategy aidl = VALUE_OR_RETURN_STATUS(
35             legacy2aidl_AudioProductStrategy(*this));
36     return aidl.writeToParcel(parcel);
37 }
38 
39 ConversionResult<media::AudioProductStrategy>
legacy2aidl_AudioProductStrategy(const AudioProductStrategy & legacy)40 legacy2aidl_AudioProductStrategy(const AudioProductStrategy& legacy) {
41     media::AudioProductStrategy aidl;
42     aidl.name = legacy.getName();
43     aidl.audioAttributes = VALUE_OR_RETURN(
44             convertContainer<std::vector<media::AudioAttributesEx>>(
45                     legacy.getVolumeGroupAttributes(),
46                     legacy2aidl_VolumeGroupAttributes_AudioAttributesEx));
47     aidl.id = VALUE_OR_RETURN(legacy2aidl_product_strategy_t_int32_t(legacy.getId()));
48     return aidl;
49 }
50 
51 ConversionResult<AudioProductStrategy>
aidl2legacy_AudioProductStrategy(const media::AudioProductStrategy & aidl)52 aidl2legacy_AudioProductStrategy(const media::AudioProductStrategy& aidl) {
53     return AudioProductStrategy(
54             aidl.name,
55             VALUE_OR_RETURN(
56                     convertContainer<std::vector<VolumeGroupAttributes>>(
57                             aidl.audioAttributes,
58                             aidl2legacy_AudioAttributesEx_VolumeGroupAttributes)),
59             VALUE_OR_RETURN(aidl2legacy_int32_t_product_strategy_t(aidl.id)));
60 }
61 
62 // Keep in sync with android/media/audiopolicy/AudioProductStrategy#attributeMatches
attributesMatchesScore(audio_attributes_t refAttributes,audio_attributes_t clientAttritubes)63 int AudioProductStrategy::attributesMatchesScore(audio_attributes_t refAttributes,
64                                                  audio_attributes_t clientAttritubes)
65 {
66     refAttributes.flags = static_cast<audio_flags_mask_t>(
67             refAttributes.flags & AUDIO_FLAGS_AFFECT_STRATEGY_SELECTION);
68     clientAttritubes.flags = static_cast<audio_flags_mask_t>(
69             clientAttritubes.flags & AUDIO_FLAGS_AFFECT_STRATEGY_SELECTION);
70     if (refAttributes == clientAttritubes) {
71         return MATCH_EQUALS;
72     }
73     if (refAttributes == AUDIO_ATTRIBUTES_INITIALIZER) {
74         // The default product strategy is the strategy that holds default attributes by convention.
75         // All attributes that fail to match will follow the default strategy for routing.
76         // Choosing the default must be done as a fallback,so return a default (zero) score to
77         // allow identify the fallback.
78         return MATCH_ON_DEFAULT_SCORE;
79     }
80     int score = MATCH_ON_DEFAULT_SCORE;
81     if (refAttributes.usage == AUDIO_USAGE_UNKNOWN) {
82         score |= MATCH_ON_DEFAULT_SCORE;
83     } else if (clientAttritubes.usage == refAttributes.usage) {
84         score |= MATCH_ON_USAGE_SCORE;
85     } else {
86         return NO_MATCH;
87     }
88     if (refAttributes.content_type == AUDIO_CONTENT_TYPE_UNKNOWN) {
89         score |= MATCH_ON_DEFAULT_SCORE;
90     } else if (clientAttritubes.content_type == refAttributes.content_type) {
91         score |= MATCH_ON_CONTENT_TYPE_SCORE;
92     } else {
93         return NO_MATCH;
94     }
95     if (strlen(refAttributes.tags) == 0) {
96         score |= MATCH_ON_DEFAULT_SCORE;
97     } else if (std::strcmp(clientAttritubes.tags, refAttributes.tags) == 0) {
98         score |= MATCH_ON_TAGS_SCORE;
99     } else {
100         return NO_MATCH;
101     }
102     if (refAttributes.flags == AUDIO_FLAG_NONE) {
103         score |= MATCH_ON_DEFAULT_SCORE;
104     } else if ((clientAttritubes.flags != AUDIO_FLAG_NONE)
105             && ((clientAttritubes.flags & refAttributes.flags) == refAttributes.flags)) {
106         score |= MATCH_ON_FLAGS_SCORE;
107     } else {
108         return NO_MATCH;
109     }
110     return score;
111 }
112 
113 } // namespace android
114