summaryrefslogtreecommitdiff
path: root/framework/src/android/telephony/imsmedia/AudioConfig.java
blob: ec08a573981b5e86aa2ef618b72ee9472970eb54 (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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/**
 * Copyright (C) 2022 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.telephony.imsmedia;

import android.annotation.IntDef;
import android.os.Parcel;
import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Objects;

/**
 * The class represents RTP (Real Time Control) configuration for audio stream.
 *
 * @hide
 */
public final class AudioConfig extends RtpConfig {
    /** Adaptive Multi-Rate */
    public static final int CODEC_AMR = android.hardware.radio.ims.media.CodecType.AMR;
    /** Adaptive Multi-Rate Wide Band */
    public static final int CODEC_AMR_WB = android.hardware.radio.ims.media.CodecType.AMR_WB;
    /** Enhanced Voice Services */
    public static final int CODEC_EVS = android.hardware.radio.ims.media.CodecType.EVS;
    /** G.711 A-law i.e. Pulse Code Modulation using A-law */
    public static final int CODEC_PCMA = android.hardware.radio.ims.media.CodecType.PCMA;
    /** G.711 μ-law i.e. Pulse Code Modulation using μ-law */
    public static final int CODEC_PCMU = android.hardware.radio.ims.media.CodecType.PCMU;

    /** @hide */
    @IntDef(
        flag = true,
        value = {
           CODEC_AMR,
           CODEC_AMR_WB,
           CODEC_EVS,
           CODEC_PCMA,
           CODEC_PCMU,
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface CodecType {}

    private byte pTimeMillis;
    private int maxPtimeMillis;
    private boolean dtxEnabled;
    private @CodecType int codecType;
    private byte mDtmfTxPayloadTypeNumber;
    private byte mDtmfRxPayloadTypeNumber;
    private byte dtmfSamplingRateKHz;
    @Nullable
    private AmrParams amrParams;
    @Nullable
    private EvsParams evsParams;

    /** @hide */
    AudioConfig(Parcel in) {
        super(RtpConfig.TYPE_AUDIO, in);
        pTimeMillis = in.readByte();
        maxPtimeMillis = in.readInt();
        dtxEnabled = in.readBoolean();
        codecType = in.readInt();
        mDtmfTxPayloadTypeNumber = in.readByte();
        mDtmfRxPayloadTypeNumber = in.readByte();
        dtmfSamplingRateKHz = in.readByte();
        amrParams = in.readParcelable(AmrParams.class.getClassLoader(), AmrParams.class);
        evsParams = in.readParcelable(EvsParams.class.getClassLoader(), EvsParams.class);
    }

    /** @hide */
    AudioConfig(Builder builder) {
        super(RtpConfig.TYPE_AUDIO, builder);
        this.pTimeMillis = builder.pTimeMillis;
        this.maxPtimeMillis = builder.maxPtimeMillis;
        this.dtxEnabled = builder.dtxEnabled;
        this.codecType = builder.codecType;
        this.mDtmfTxPayloadTypeNumber = builder.mDtmfTxPayloadTypeNumber;
        this.mDtmfRxPayloadTypeNumber = builder.mDtmfRxPayloadTypeNumber;
        this.dtmfSamplingRateKHz = builder.dtmfSamplingRateKHz;
        this.amrParams = builder.amrParams;
        this.evsParams = builder.evsParams;
    }

    /** @hide **/
    public byte getPtimeMillis() {
        return pTimeMillis;
    }

    /** @hide **/
    public void setPtimeMillis(byte pTimeMillis) {
        this.pTimeMillis = pTimeMillis;
    }

    /** @hide **/
    public int getMaxPtimeMillis() {
        return maxPtimeMillis;
    }

    /** @hide **/
    public void setMaxPtimeMillis(int maxPtimeMillis) {
        this.maxPtimeMillis = maxPtimeMillis;
    }

    /** @hide **/
    public boolean getDtxEnabled() {
        return dtxEnabled;
    }

    /** @hide **/
    public void setDtxEnabled(boolean dtxEnabled) {
        this.dtxEnabled = dtxEnabled;
    }

    /** @hide **/
    public int getCodecType() {
        return codecType;
    }

    /** @hide **/
    public void setCodecType(int codecType) {
        this.codecType = codecType;
    }

    /** @hide **/
    public byte getTxDtmfPayloadTypeNumber() {
        return mDtmfTxPayloadTypeNumber;
    }

    /** @hide **/
    public byte getRxDtmfPayloadTypeNumber() {
        return mDtmfRxPayloadTypeNumber;
    }

    /** @hide **/
    public void setTxDtmfPayloadTypeNumber(byte dtmfTxPayloadTypeNumber) {
        this.mDtmfTxPayloadTypeNumber = dtmfTxPayloadTypeNumber;
    }

    /** @hide **/
    public void setRxDtmfPayloadTypeNumber(byte dtmfRxPayloadTypeNumber) {
        this.mDtmfRxPayloadTypeNumber = dtmfRxPayloadTypeNumber;
    }

    /** @hide **/
    public byte getDtmfSamplingRateKHz() {
        return dtmfSamplingRateKHz;
    }

    /** @hide **/
    public void setDtmfSamplingRateKHz(byte dtmfSamplingRateKHz) {
        this.dtmfSamplingRateKHz = dtmfSamplingRateKHz;
    }

    /** @hide **/
    public AmrParams getAmrParams() {
        return amrParams;
    }

    /** @hide **/
    public EvsParams getEvsParams() {
        return evsParams;
    }

    @NonNull
    @Override
    public String toString() {
        return super.toString() + " AudioConfig: {pTimeMillis=" + pTimeMillis
                + ", maxPtimeMillis=" + maxPtimeMillis
                + ", dtxEnabled=" + dtxEnabled
                + ", codecType=" + codecType
                + ", mDtmfTxPayloadTypeNumber=" + mDtmfTxPayloadTypeNumber
                + ", mDtmfRxPayloadTypeNumber=" + mDtmfRxPayloadTypeNumber
                + ", dtmfSamplingRateKHz=" + dtmfSamplingRateKHz
                + ", amrParams=" + amrParams
                + ", evsParams=" + evsParams
                + " }";
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), pTimeMillis, maxPtimeMillis,
                dtxEnabled, codecType, mDtmfTxPayloadTypeNumber,
                mDtmfRxPayloadTypeNumber, dtmfSamplingRateKHz, amrParams, evsParams);
    }

    @Override
    public boolean equals(@Nullable Object o) {
        if (o == null || !(o instanceof AudioConfig) || hashCode() != o.hashCode()) {
            return false;
        }

        if (this == o) {
            return true;
        }

        AudioConfig s = (AudioConfig) o;

        if (!super.equals(s)) {
            return false;
        }

        return (pTimeMillis == s.pTimeMillis
                && maxPtimeMillis == s.maxPtimeMillis
                && dtxEnabled == s.dtxEnabled
                && codecType == s.codecType
                && mDtmfTxPayloadTypeNumber == s.mDtmfTxPayloadTypeNumber
                && mDtmfRxPayloadTypeNumber == s.mDtmfRxPayloadTypeNumber
                && dtmfSamplingRateKHz == s.dtmfSamplingRateKHz
                && Objects.equals(amrParams, s.amrParams)
                && Objects.equals(evsParams, s.evsParams));
    }

    /**
     * {@link Parcelable#describeContents}
     */
    public int describeContents() {
        return 0;
    }

    /**
     * {@link Parcelable#writeToParcel}
     */
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, RtpConfig.TYPE_AUDIO);
        dest.writeByte(pTimeMillis);
        dest.writeInt(maxPtimeMillis);
        dest.writeBoolean(dtxEnabled);
        dest.writeInt(codecType);
        dest.writeByte(mDtmfTxPayloadTypeNumber);
        dest.writeByte(mDtmfRxPayloadTypeNumber);
        dest.writeByte(dtmfSamplingRateKHz);
        dest.writeParcelable(amrParams, 0);
        dest.writeParcelable(evsParams, 0);
    }

    public static final @NonNull Parcelable.Creator<AudioConfig>
            CREATOR = new Parcelable.Creator() {
                public AudioConfig createFromParcel(Parcel in) {
                    in.readInt();   //skip
                    return new AudioConfig(in);
                }

                public AudioConfig[] newArray(int size) {
                    return new AudioConfig[size];
                }
            };

    /**
     * Provides a convenient way to set the fields of a {@link AudioConfig}
     * when creating a new instance.
     */
    public static final class Builder extends RtpConfig.AbstractBuilder<Builder> {
        private byte pTimeMillis;
        private int maxPtimeMillis;
        private boolean dtxEnabled;
        private @CodecType int codecType;
        private byte mDtmfTxPayloadTypeNumber;
        private byte mDtmfRxPayloadTypeNumber;
        private byte dtmfSamplingRateKHz;
        @Nullable
        private AmrParams amrParams;
        @Nullable
        private EvsParams evsParams;

        /**
         * Default constructor for Builder.
         */
        public Builder() {
        }

        @Override
        Builder self() {
            return this;
        }

        /**
         * Set the packet time i.e. recommended length of time in milliseconds
         * represented by the media in each packet, see RFC 4566
         *
         * @param pTimeMillis packet time
         * @return The same instance of the builder
         */
        public Builder setPtimeMillis(final byte pTimeMillis) {
            this.pTimeMillis = pTimeMillis;
            return this;
        }

        /**
         * Set the maximum amount of media that can be encapsulated in each packet
         * represented in milliseconds, see RFC 4566
         *
         * @param maxPtimeMillis maximum packet time
         * @return The same instance of the builder
         */
        public Builder setMaxPtimeMillis(final int maxPtimeMillis) {
            this.maxPtimeMillis = maxPtimeMillis;
            return this;
        }

        /**
         * Set whether discontinuous transmission (DTX) is enabled or not
         *
         * @param dtxEnabled {@code true} if DTX enabled
         * @return The same instance of the builder
         */
        public Builder setDtxEnabled(final boolean dtxEnabled) {
            this.dtxEnabled = dtxEnabled;
            return this;
        }

        /**
         * Set the negotiated audio codec
         *
         * @param codecType codec type
         * @return The same instance of the builder
         */
        public Builder setCodecType(final @CodecType int codecType) {
            this.codecType = codecType;
            return this;
        }

        /**
         * Set the dynamic Tx payload type number to be used to transmit DTMF RTP packets.
         * The values is in the range from 96 to 127 chosen during the session establishment.
         * The PT value of the RTP header of all DTMF packets shall be set with this value.
         *
         * @param dtmfTxPayloadTypeNumber Payload type number for the Tx DTMF packets
         * @return The same instance of the builder
         */
        public Builder setTxDtmfPayloadTypeNumber(final byte dtmfTxPayloadTypeNumber) {
            this.mDtmfTxPayloadTypeNumber = dtmfTxPayloadTypeNumber;
            return this;
        }

        /**
         * Set the dynamic Rx payload type number to be used for DTMF received RTP packets.
         * The values is in the range from 96 to 127 chosen during the session establishment.
         * The PT value of the RTP header of all DTMF packets shall be set with this value.
         *
         * @param dtmfRxPayloadTypeNumber Payload type number for the Rx DTMF packets
         * @return The same instance of the builder
         */
        public Builder setRxDtmfPayloadTypeNumber(final byte dtmfRxPayloadTypeNumber) {
            this.mDtmfRxPayloadTypeNumber = dtmfRxPayloadTypeNumber;
            return this;
        }

        /**
         * Set the DTMF sampling rate on this media stream
         *
         * @param dtmfSamplingRateKHz DTMF sampling rate
         * @return The same instance of the builder
         */
        public Builder setDtmfSamplingRateKHz(final byte dtmfSamplingRateKHz) {
            this.dtmfSamplingRateKHz = dtmfSamplingRateKHz;
            return this;
        }

        /**
         * Set the AMR codec parameters, see {@link AmrParams}
         *
         * @param amrParams AMR parameters
         * @return The same instance of the builder
         */
        public Builder setAmrParams(final AmrParams amrParams) {
            this.amrParams = amrParams;
            return this;
        }

        /**
         * Set the EVS codec parameters, see {@link EvsParams}
         *
         * @param evsParams EVS parameters.
         * @return The same instance of the builder
         */
        public Builder setEvsParams(final EvsParams evsParams) {
            this.evsParams = evsParams;
            return this;
        }

        /**
         * Build the AudioConfig.
         *
         * @return the AudioConfig object.
         */
        public @NonNull AudioConfig build() {
            // TODO validation
            return new AudioConfig(this);
        }
    }
}