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(const audio_attributes_t refAttributes,const audio_attributes_t clientAttritubes)63 int AudioProductStrategy::attributesMatchesScore(const audio_attributes_t refAttributes,
64 const audio_attributes_t clientAttritubes)
65 {
66 if (refAttributes == clientAttritubes) {
67 return MATCH_EQUALS;
68 }
69 if (refAttributes == AUDIO_ATTRIBUTES_INITIALIZER) {
70 // The default product strategy is the strategy that holds default attributes by convention.
71 // All attributes that fail to match will follow the default strategy for routing.
72 // Choosing the default must be done as a fallback,so return a default (zero) score to
73 // allow identify the fallback.
74 return MATCH_ON_DEFAULT_SCORE;
75 }
76 int score = MATCH_ON_DEFAULT_SCORE;
77 if (refAttributes.usage == AUDIO_USAGE_UNKNOWN) {
78 score |= MATCH_ON_DEFAULT_SCORE;
79 } else if (clientAttritubes.usage == refAttributes.usage) {
80 score |= MATCH_ON_USAGE_SCORE;
81 } else {
82 return NO_MATCH;
83 }
84 if (refAttributes.content_type == AUDIO_CONTENT_TYPE_UNKNOWN) {
85 score |= MATCH_ON_DEFAULT_SCORE;
86 } else if (clientAttritubes.content_type == refAttributes.content_type) {
87 score |= MATCH_ON_CONTENT_TYPE_SCORE;
88 } else {
89 return NO_MATCH;
90 }
91 if (strlen(refAttributes.tags) == 0) {
92 score |= MATCH_ON_DEFAULT_SCORE;
93 } else if (std::strcmp(clientAttritubes.tags, refAttributes.tags) == 0) {
94 score |= MATCH_ON_TAGS_SCORE;
95 } else {
96 return NO_MATCH;
97 }
98 if (refAttributes.flags == AUDIO_FLAG_NONE) {
99 score |= MATCH_ON_DEFAULT_SCORE;
100 } else if ((clientAttritubes.flags != AUDIO_FLAG_NONE)
101 && ((clientAttritubes.flags & refAttributes.flags) == refAttributes.flags)) {
102 score |= MATCH_ON_FLAGS_SCORE;
103 } else {
104 return NO_MATCH;
105 }
106 return score;
107 }
108
109 } // namespace android
110