1 /*
2  * Copyright (C) 2014 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 "AudioPolicy"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20 #include <media/AudioPolicy.h>
21 
22 namespace android {
23 
24 //
25 //  AttributeMatchCriterion implementation
26 //
AttributeMatchCriterion(audio_usage_t usage,audio_source_t source,uint32_t rule)27 AttributeMatchCriterion::AttributeMatchCriterion(audio_usage_t usage,
28                                                  audio_source_t source,
29                                                  uint32_t rule)
30 : mRule(rule)
31 {
32     if (mRule == RULE_MATCH_ATTRIBUTE_USAGE ||
33             mRule == RULE_EXCLUDE_ATTRIBUTE_USAGE) {
34         mAttr.mUsage = usage;
35     } else {
36         mAttr.mSource = source;
37     }
38 }
39 
readFromParcel(Parcel * parcel)40 status_t AttributeMatchCriterion::readFromParcel(Parcel *parcel)
41 {
42     mRule = parcel->readInt32();
43     if (mRule == RULE_MATCH_ATTRIBUTE_USAGE ||
44             mRule == RULE_EXCLUDE_ATTRIBUTE_USAGE) {
45         mAttr.mUsage = (audio_usage_t)parcel->readInt32();
46     } else {
47         mAttr.mSource = (audio_source_t)parcel->readInt32();
48     }
49     return NO_ERROR;
50 }
51 
writeToParcel(Parcel * parcel) const52 status_t AttributeMatchCriterion::writeToParcel(Parcel *parcel) const
53 {
54     parcel->writeInt32(mRule);
55     parcel->writeInt32(mAttr.mUsage);
56     return NO_ERROR;
57 }
58 
59 //
60 //  AudioMix implementation
61 //
62 
readFromParcel(Parcel * parcel)63 status_t AudioMix::readFromParcel(Parcel *parcel)
64 {
65     mMixType = parcel->readInt32();
66     mFormat.sample_rate = (uint32_t)parcel->readInt32();
67     mFormat.channel_mask = (audio_channel_mask_t)parcel->readInt32();
68     mFormat.format = (audio_format_t)parcel->readInt32();
69     mRouteFlags = parcel->readInt32();
70     mRegistrationId = parcel->readString8();
71     mCbFlags = (uint32_t)parcel->readInt32();
72     size_t size = (size_t)parcel->readInt32();
73     if (size > MAX_CRITERIA_PER_MIX) {
74         size = MAX_CRITERIA_PER_MIX;
75     }
76     for (size_t i = 0; i < size; i++) {
77         AttributeMatchCriterion criterion;
78         if (criterion.readFromParcel(parcel) == NO_ERROR) {
79             mCriteria.add(criterion);
80         }
81     }
82     return NO_ERROR;
83 }
84 
writeToParcel(Parcel * parcel) const85 status_t AudioMix::writeToParcel(Parcel *parcel) const
86 {
87     parcel->writeInt32(mMixType);
88     parcel->writeInt32(mFormat.sample_rate);
89     parcel->writeInt32(mFormat.channel_mask);
90     parcel->writeInt32(mFormat.format);
91     parcel->writeInt32(mRouteFlags);
92     parcel->writeString8(mRegistrationId);
93     parcel->writeInt32(mCbFlags);
94     size_t size = mCriteria.size();
95     if (size > MAX_CRITERIA_PER_MIX) {
96         size = MAX_CRITERIA_PER_MIX;
97     }
98     size_t sizePosition = parcel->dataPosition();
99     parcel->writeInt32(size);
100     size_t finalSize = size;
101     for (size_t i = 0; i < size; i++) {
102         size_t position = parcel->dataPosition();
103         if (mCriteria[i].writeToParcel(parcel) != NO_ERROR) {
104             parcel->setDataPosition(position);
105             finalSize--;
106         }
107     }
108     if (size != finalSize) {
109         size_t position = parcel->dataPosition();
110         parcel->setDataPosition(sizePosition);
111         parcel->writeInt32(finalSize);
112         parcel->setDataPosition(position);
113     }
114     return NO_ERROR;
115 }
116 
117 } // namespace android
118