• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/AudioAttributes.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.getAudioAttributes(),
46                     legacy2aidl_AudioAttributes_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<AudioAttributes>>(
57                             aidl.audioAttributes,
58                             aidl2legacy_AudioAttributesEx_AudioAttributes)),
59             VALUE_OR_RETURN(aidl2legacy_int32_t_product_strategy_t(aidl.id)));
60 }
61 
62 // Keep in sync with android/media/audiopolicy/AudioProductStrategy#attributeMatches
attributesMatches(const audio_attributes_t refAttributes,const audio_attributes_t clientAttritubes)63 bool AudioProductStrategy::attributesMatches(const audio_attributes_t refAttributes,
64                                         const audio_attributes_t clientAttritubes)
65 {
66     if (refAttributes == AUDIO_ATTRIBUTES_INITIALIZER) {
67         // The default product strategy is the strategy that holds default attributes by convention.
68         // All attributes that fail to match will follow the default strategy for routing.
69         // Choosing the default must be done as a fallback, the attributes match shall not
70         // select the default.
71         return false;
72     }
73     return ((refAttributes.usage == AUDIO_USAGE_UNKNOWN) ||
74             (clientAttritubes.usage == refAttributes.usage)) &&
75             ((refAttributes.content_type == AUDIO_CONTENT_TYPE_UNKNOWN) ||
76              (clientAttritubes.content_type == refAttributes.content_type)) &&
77             ((refAttributes.flags == AUDIO_FLAG_NONE) ||
78              (clientAttritubes.flags != AUDIO_FLAG_NONE &&
79             (clientAttritubes.flags & refAttributes.flags) == refAttributes.flags)) &&
80             ((strlen(refAttributes.tags) == 0) ||
81              (std::strcmp(clientAttritubes.tags, refAttributes.tags) == 0));
82 }
83 
84 } // namespace android
85