summaryrefslogtreecommitdiffstats
path: root/media/java/android/media/audiopolicy/AudioMixingRule.java
blob: 02b03d2e2fa420492ee4c85c0b83eaf6f8011ec1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.media.audiopolicy;

import android.annotation.SystemApi;
import android.media.AudioAttributes;
import android.os.Parcel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;


/**
 * @hide
 *
 * Here's an example of creating a mixing rule for all media playback:
 * <pre>
 * AudioAttributes mediaAttr = new AudioAttributes.Builder()
 *         .setUsage(AudioAttributes.USAGE_MEDIA)
 *         .build();
 * AudioMixingRule mediaRule = new AudioMixingRule.Builder()
 *         .addRule(mediaAttr, AudioMixingRule.RULE_MATCH_ATTRIBUTE_USAGE)
 *         .build();
 * </pre>
 */
@SystemApi
public class AudioMixingRule {

    private AudioMixingRule(int mixType, ArrayList<AttributeMatchCriterion> criteria) {
        mCriteria = criteria;
        mTargetMixType = mixType;
    }

    /**
     * A rule requiring the usage information of the {@link AudioAttributes} to match.
     */
    @SystemApi
    public static final int RULE_MATCH_ATTRIBUTE_USAGE = 0x1;
    /**
     * A rule requiring the capture preset information of the {@link AudioAttributes} to match.
     */
    @SystemApi
    public static final int RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET = 0x1 << 1;

    private final static int RULE_EXCLUSION_MASK = 0x8000;
    /**
     * @hide
     * A rule requiring the usage information of the {@link AudioAttributes} to differ.
     */
    public static final int RULE_EXCLUDE_ATTRIBUTE_USAGE =
            RULE_EXCLUSION_MASK | RULE_MATCH_ATTRIBUTE_USAGE;
    /**
     * @hide
     * A rule requiring the capture preset information of the {@link AudioAttributes} to differ.
     */
    public static final int RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET =
            RULE_EXCLUSION_MASK | RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET;

    static final class AttributeMatchCriterion {
        AudioAttributes mAttr;
        int mRule;

        /** input parameters must be valid */
        AttributeMatchCriterion(AudioAttributes attributes, int rule) {
            mAttr = attributes;
            mRule = rule;
        }

        @Override
        public int hashCode() {
            return Objects.hash(mAttr, mRule);
        }

        void writeToParcel(Parcel dest) {
            dest.writeInt(mRule);
            if ((mRule == RULE_MATCH_ATTRIBUTE_USAGE) || (mRule == RULE_EXCLUDE_ATTRIBUTE_USAGE)) {
                dest.writeInt(mAttr.getUsage());
            } else {
                // capture preset rule
                dest.writeInt(mAttr.getCapturePreset());
            }
        }
    }

    private final int mTargetMixType;
    int getTargetMixType() { return mTargetMixType; }
    private final ArrayList<AttributeMatchCriterion> mCriteria;
    ArrayList<AttributeMatchCriterion> getCriteria() { return mCriteria; }

    @Override
    public int hashCode() {
        return Objects.hash(mTargetMixType, mCriteria);
    }

    private static boolean isValidSystemApiRule(int rule) {
        switch(rule) {
            case RULE_MATCH_ATTRIBUTE_USAGE:
            case RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET:
                return true;
            default:
                return false;
        }
    }

    private static boolean isValidIntRule(int rule) {
        switch(rule) {
            case RULE_MATCH_ATTRIBUTE_USAGE:
            case RULE_EXCLUDE_ATTRIBUTE_USAGE:
            case RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET:
            case RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET:
                return true;
            default:
                return false;
        }
    }

    private static boolean isPlayerRule(int rule) {
        return ((rule == RULE_MATCH_ATTRIBUTE_USAGE)
                || (rule == RULE_EXCLUDE_ATTRIBUTE_USAGE));
    }

    /**
     * Builder class for {@link AudioMixingRule} objects
     */
    @SystemApi
    public static class Builder {
        private ArrayList<AttributeMatchCriterion> mCriteria;
        private int mTargetMixType = AudioMix.MIX_TYPE_INVALID;

        /**
         * Constructs a new Builder with no rules.
         */
        @SystemApi
        public Builder() {
            mCriteria = new ArrayList<AttributeMatchCriterion>();
        }

        /**
         * Add a rule for the selection of which streams are mixed together.
         * @param attrToMatch a non-null AudioAttributes instance for which a contradictory
         *     rule hasn't been set yet.
         * @param rule {@link AudioMixingRule#RULE_MATCH_ATTRIBUTE_USAGE} or
         *     {@link AudioMixingRule#RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}.
         * @return the same Builder instance.
         * @throws IllegalArgumentException
         */
        @SystemApi
        public Builder addRule(AudioAttributes attrToMatch, int rule)
                throws IllegalArgumentException {
            if (!isValidSystemApiRule(rule)) {
                throw new IllegalArgumentException("Illegal rule value " + rule);
            }
            return addRuleInt(attrToMatch, rule);
        }

        /**
         * Add a rule by exclusion for the selection of which streams are mixed together.
         * <br>For instance the following code
         * <br><pre>
         * AudioAttributes mediaAttr = new AudioAttributes.Builder()
         *         .setUsage(AudioAttributes.USAGE_MEDIA)
         *         .build();
         * AudioMixingRule noMediaRule = new AudioMixingRule.Builder()
         *         .excludeRule(mediaAttr, AudioMixingRule.RULE_MATCH_ATTRIBUTE_USAGE)
         *         .build();
         * </pre>
         * <br>will create a rule which maps to any usage value, except USAGE_MEDIA.
         * @param attrToMatch a non-null AudioAttributes instance for which a contradictory
         *     rule hasn't been set yet.
         * @param rule {@link AudioMixingRule#RULE_MATCH_ATTRIBUTE_USAGE} or
         *     {@link AudioMixingRule#RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET}.
         * @return the same Builder instance.
         * @throws IllegalArgumentException
         */
        @SystemApi
        public Builder excludeRule(AudioAttributes attrToMatch, int rule)
                throws IllegalArgumentException {
            if (!isValidSystemApiRule(rule)) {
                throw new IllegalArgumentException("Illegal rule value " + rule);
            }
            return addRuleInt(attrToMatch, rule | RULE_EXCLUSION_MASK);
        }

        /**
         * Add or exclude a rule for the selection of which streams are mixed together.
         * @param attrToMatch a non-null AudioAttributes instance for which a contradictory
         *     rule hasn't been set yet.
         * @param rule one of {@link AudioMixingRule#RULE_EXCLUDE_ATTRIBUTE_USAGE},
         *     {@link AudioMixingRule#RULE_MATCH_ATTRIBUTE_USAGE},
         *     {@link AudioMixingRule#RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET} or
         *     {@link AudioMixingRule#RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET}.
         * @return the same Builder instance.
         * @throws IllegalArgumentException
         */
        Builder addRuleInt(AudioAttributes attrToMatch, int rule)
                throws IllegalArgumentException {
            if (attrToMatch == null) {
                throw new IllegalArgumentException("Illegal null AudioAttributes argument");
            }
            if (!isValidIntRule(rule)) {
                throw new IllegalArgumentException("Illegal rule value " + rule);
            } else {
                // as rules are added to the Builder, we verify they are consistent with the type
                // of mix being built. When adding the first rule, the mix type is MIX_TYPE_INVALID.
                if (mTargetMixType == AudioMix.MIX_TYPE_INVALID) {
                    if (isPlayerRule(rule)) {
                        mTargetMixType = AudioMix.MIX_TYPE_PLAYERS;
                    } else {
                        mTargetMixType = AudioMix.MIX_TYPE_RECORDERS;
                    }
                } else if (((mTargetMixType == AudioMix.MIX_TYPE_PLAYERS) && !isPlayerRule(rule))
                        || ((mTargetMixType == AudioMix.MIX_TYPE_RECORDERS) && isPlayerRule(rule)))
                {
                    throw new IllegalArgumentException("Incompatible rule for mix");
                }
            }
            synchronized (mCriteria) {
                Iterator<AttributeMatchCriterion> crIterator = mCriteria.iterator();
                while (crIterator.hasNext()) {
                    final AttributeMatchCriterion criterion = crIterator.next();
                    if ((rule == RULE_MATCH_ATTRIBUTE_USAGE)
                            || (rule == RULE_EXCLUDE_ATTRIBUTE_USAGE)) {
                        // "usage"-based rule
                        if (criterion.mAttr.getUsage() == attrToMatch.getUsage()) {
                            if (criterion.mRule == rule) {
                                // rule already exists, we're done
                                return this;
                            } else {
                                // criterion already exists with a another rule, it is incompatible
                                throw new IllegalArgumentException("Contradictory rule exists for "
                                        + attrToMatch);
                            }
                        }
                    } else if ((rule == RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET)
                            || (rule == RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET)) {
                        // "capture preset"-base rule
                        if (criterion.mAttr.getCapturePreset() == attrToMatch.getCapturePreset()) {
                            if (criterion.mRule == rule) {
                             // rule already exists, we're done
                                return this;
                            } else {
                                // criterion already exists with a another rule, it is incompatible
                                throw new IllegalArgumentException("Contradictory rule exists for "
                                        + attrToMatch);
                            }
                        }
                    }
                }
                // rule didn't exist, add it
                mCriteria.add(new AttributeMatchCriterion(attrToMatch, rule));
            }
            return this;
        }

        Builder addRuleFromParcel(Parcel in) throws IllegalArgumentException {
            int rule = in.readInt();
            AudioAttributes attr;
            if ((rule == RULE_MATCH_ATTRIBUTE_USAGE) || (rule == RULE_EXCLUDE_ATTRIBUTE_USAGE)) {
                int usage = in.readInt();
                attr = new AudioAttributes.Builder()
                        .setUsage(usage).build();
            } else if ((rule == RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET)
                    || (rule == RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET)) {
                int preset = in.readInt();
                attr = new AudioAttributes.Builder()
                        .setInternalCapturePreset(preset).build();
            } else {
                in.readInt(); // assume there was in int value to read as for now they come in pair
                throw new IllegalArgumentException("Illegal rule value " + rule + " in parcel");
            }
            return addRuleInt(attr, rule);
        }

        /**
         * Combines all of the matching and exclusion rules that have been set and return a new
         * {@link AudioMixingRule} object.
         * @return a new {@link AudioMixingRule} object
         */
        public AudioMixingRule build() {
            return new AudioMixingRule(mTargetMixType, mCriteria);
        }
    }
}