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  */
16 package com.android.cts.verifier.audio.peripheralprofile;
17 
18 import android.media.AudioDeviceInfo;
19 import android.media.AudioFormat;
20 import android.util.Log;
21 
22 public class USBDeviceInfoHelper {
23     @SuppressWarnings("unused")
24     private static final String TAG = "USBDeviceInfoHelper";
25 
26     private static final String kUSBPrefix = "USB-Audio - ";
27 
28     // TODO - we can't treat the maximum channel count the same for inputs and outputs
calcMaxChannelCount(AudioDeviceInfo deviceInfo)29     public static int calcMaxChannelCount(AudioDeviceInfo deviceInfo) {
30         if (deviceInfo == null) {
31             return 2;   // for testing....
32         }
33 
34         int maxChanCount = 0;
35         int[] counts = deviceInfo.getChannelCounts();
36         for (int chanCount : counts) {
37             if (chanCount > maxChanCount) {
38                 maxChanCount = chanCount;
39             }
40         }
41         return maxChanCount;
42     }
43 
44     // TODO This should be in a library module devoted to channel management, not USB.
getPlayChanMask(AudioDeviceInfo deviceInfo)45     public static int getPlayChanMask(AudioDeviceInfo deviceInfo) {
46         int numChans = calcMaxChannelCount(deviceInfo);
47         switch (numChans) {
48             case 1:
49                 return AudioFormat.CHANNEL_OUT_MONO;
50 
51             case 2:
52                 return AudioFormat.CHANNEL_OUT_STEREO;
53 
54             case 4:
55                 return AudioFormat.CHANNEL_OUT_QUAD;
56 
57             default:
58                 // Huh!
59                 Log.e(TAG, "getPlayChanMask() Unsupported number of channels: " + numChans);
60                 return AudioFormat.CHANNEL_OUT_STEREO;
61         }
62     }
63 
64     // TODO This should be in a library module devoted to channel management, not USB.
getIndexedChanMask(int numChannels)65     public static int getIndexedChanMask(int numChannels) {
66         return 0x80000000 | numChannels;
67     }
68 
69     // TODO This should be in a library module devoted to channel management, not USB.
getRecordChanMask(AudioDeviceInfo deviceInfo)70     public static int getRecordChanMask(AudioDeviceInfo deviceInfo) {
71         int numChans = calcMaxChannelCount(deviceInfo);
72         switch (numChans) {
73             case 1:
74                 return AudioFormat.CHANNEL_IN_MONO;
75 
76             case 2:
77                 return AudioFormat.CHANNEL_IN_STEREO;
78 
79             default:
80                 // Huh!
81                 return AudioFormat.CHANNEL_OUT_STEREO;
82         }
83     }
84 
extractDeviceName(String productName)85     public static String extractDeviceName(String productName) {
86         return productName.startsWith(kUSBPrefix)
87                 ? productName.substring(kUSBPrefix.length())
88                 : productName;
89     }
90 }
91