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 #define RETURN_STATUS_IF_ERROR(x) \
25 { auto _tmp = (x); if (_tmp != OK) return _tmp; }
26
27 namespace android {
28
readFromParcel(const Parcel * parcel)29 status_t AudioProductStrategy::readFromParcel(const Parcel* parcel) {
30 media::AudioProductStrategy aidl;
31 RETURN_STATUS_IF_ERROR(aidl.readFromParcel(parcel));
32 *this = VALUE_OR_RETURN_STATUS(aidl2legacy_AudioProductStrategy(aidl));
33 return OK;
34 }
35
writeToParcel(Parcel * parcel) const36 status_t AudioProductStrategy::writeToParcel(Parcel* parcel) const {
37 media::AudioProductStrategy aidl = VALUE_OR_RETURN_STATUS(
38 legacy2aidl_AudioProductStrategy(*this));
39 return aidl.writeToParcel(parcel);
40 }
41
42 ConversionResult<media::AudioProductStrategy>
legacy2aidl_AudioProductStrategy(const AudioProductStrategy & legacy)43 legacy2aidl_AudioProductStrategy(const AudioProductStrategy& legacy) {
44 media::AudioProductStrategy aidl;
45 aidl.name = legacy.getName();
46 aidl.audioAttributes = VALUE_OR_RETURN(
47 convertContainer<std::vector<media::AudioAttributesEx>>(
48 legacy.getAudioAttributes(),
49 legacy2aidl_AudioAttributes_AudioAttributesEx));
50 aidl.id = VALUE_OR_RETURN(legacy2aidl_product_strategy_t_int32_t(legacy.getId()));
51 return aidl;
52 }
53
54 ConversionResult<AudioProductStrategy>
aidl2legacy_AudioProductStrategy(const media::AudioProductStrategy & aidl)55 aidl2legacy_AudioProductStrategy(const media::AudioProductStrategy& aidl) {
56 return AudioProductStrategy(
57 aidl.name,
58 VALUE_OR_RETURN(
59 convertContainer<std::vector<AudioAttributes>>(
60 aidl.audioAttributes,
61 aidl2legacy_AudioAttributesEx_AudioAttributes)),
62 VALUE_OR_RETURN(aidl2legacy_int32_t_product_strategy_t(aidl.id)));
63 }
64
65 // Keep in sync with android/media/audiopolicy/AudioProductStrategy#attributeMatches
attributesMatches(const audio_attributes_t refAttributes,const audio_attributes_t clientAttritubes)66 bool AudioProductStrategy::attributesMatches(const audio_attributes_t refAttributes,
67 const audio_attributes_t clientAttritubes)
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, the attributes match shall not
73 // select the default.
74 return false;
75 }
76 return ((refAttributes.usage == AUDIO_USAGE_UNKNOWN) ||
77 (clientAttritubes.usage == refAttributes.usage)) &&
78 ((refAttributes.content_type == AUDIO_CONTENT_TYPE_UNKNOWN) ||
79 (clientAttritubes.content_type == refAttributes.content_type)) &&
80 ((refAttributes.flags == AUDIO_FLAG_NONE) ||
81 (clientAttritubes.flags != AUDIO_FLAG_NONE &&
82 (clientAttritubes.flags & refAttributes.flags) == refAttributes.flags)) &&
83 ((strlen(refAttributes.tags) == 0) ||
84 (std::strcmp(clientAttritubes.tags, refAttributes.tags) == 0));
85 }
86
87 } // namespace android
88