1/*
2 * Copyright (C) 2017 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 */
16package com.android.server.usb.descriptors;
17
18import com.android.server.usb.descriptors.report.ReportCanvas;
19
20/**
21 * @hide
22 * An audio class-specific Format II interface.
23 * see Frmts10.pdf section 2.3
24 */
25public final class Usb10ASFormatII extends UsbASFormat {
26    private static final String TAG = "Usb10ASFormatII";
27
28    private int mMaxBitRate; // 4:2 Indicates the maximum number of bits per second this
29                            // interface can handle. Expressed in kbits/s.
30    private int mSamplesPerFrame;   // 6:2 Indicates the number of PCM audio samples contained
31                                    // in one encoded audio frame.
32    private byte mSamFreqType;  // Indicates how the sampling frequency can be programmed:
33                                // 0: Continuous sampling frequency
34                                // 1..255: The number of discrete sampling frequencies supported
35                                // by the isochronous data endpoint of the AudioStreaming
36                                // interface (ns)
37    private int[] mSampleRates; // if mSamFreqType == 0, there will be 2 values:
38                                // the min & max rates. otherwise mSamFreqType rates.
39                                // All 3-byte values. All rates in Hz
40
41    public Usb10ASFormatII(int length, byte type, byte subtype, byte formatType, int subclass) {
42        super(length, type, subtype, formatType, subclass);
43    }
44
45    public int getMaxBitRate() {
46        return mMaxBitRate;
47    }
48
49    public int getSamplesPerFrame() {
50        return mSamplesPerFrame;
51    }
52
53    public byte getSamFreqType() {
54        return mSamFreqType;
55    }
56
57    public int[] getSampleRates() {
58        return mSampleRates;
59    }
60
61    @Override
62    public int parseRawDescriptors(ByteStream stream) {
63        mMaxBitRate = stream.unpackUsbShort();
64        mSamplesPerFrame = stream.unpackUsbShort();
65        mSamFreqType = stream.getByte();
66        int numFreqs = mSamFreqType == 0 ? 2 : mSamFreqType;
67        mSampleRates = new int[numFreqs];
68        for (int index = 0; index < numFreqs; index++) {
69            mSampleRates[index] = stream.unpackUsbTriple();
70        }
71
72        return mLength;
73    }
74
75    @Override
76    public void report(ReportCanvas canvas) {
77        super.report(canvas);
78
79        canvas.openList();
80        canvas.writeListItem("Max Bit Rate: " + getMaxBitRate());
81        canvas.writeListItem("Samples Per Frame: " + getMaxBitRate());
82        byte sampleFreqType = getSamFreqType();
83        int[] sampleRates = getSampleRates();
84        canvas.writeListItem("Sample Freq Type: " + sampleFreqType);
85        canvas.openList();
86        if (sampleFreqType == 0) {
87            canvas.writeListItem("min: " + sampleRates[0]);
88            canvas.writeListItem("max: " + sampleRates[1]);
89        } else {
90            for (int index = 0; index < sampleFreqType; index++) {
91                canvas.writeListItem("" + sampleRates[index]);
92            }
93        }
94        canvas.closeList();
95
96        canvas.closeList();
97    }
98
99}
100