1 /*
2  * Copyright (C) 2020 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 package com.android.server.hdmi;
18 
19 import android.util.Log;
20 
21 import com.android.server.hdmi.HdmiUtils.CodecSad;
22 import com.android.server.hdmi.HdmiUtils.DeviceConfig;
23 import com.google.common.hash.HashCode;
24 
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 import org.xmlpull.v1.XmlPullParserException;
29 
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 @RunWith(JUnit4.class)
38 /**
39  * Reads short audio descriptors from a configuration file and outputs to a log
40  * for use by host side tests.
41  */
42 public class SadConfigurationReaderTest {
43 
44     private static final String TAG = "SadConfigurationReaderTest";
45 
46     // Variable should be copy of SHORT_AUDIO_DESCRIPTOR_CONFIG_PATH in
47     // frameworks/base/services/core/java/com/android/server/hdmi/
48     //  HdmiCecLocalDeviceAudioSystem.java
49     private final String SHORT_AUDIO_DESCRIPTOR_CONFIG_PATH = "/vendor/etc/sadConfig.xml";
50 
51     @Test
parseSadConfigXML()52     public void parseSadConfigXML() {
53         List<DeviceConfig> deviceConfigs = null;
54         File file = new File(SHORT_AUDIO_DESCRIPTOR_CONFIG_PATH);
55         if (file.exists()) {
56             try {
57                 InputStream in = new FileInputStream(file);
58                 deviceConfigs = HdmiUtils.ShortAudioDescriptorXmlParser.parse(in);
59                 in.close();
60             } catch (IOException e) {
61                 Log.e(TAG, "Error reading file: " + file.getAbsolutePath(), e);
62             } catch (XmlPullParserException e) {
63                 Log.e(TAG, "Unable to parse file: " + file.getAbsolutePath(), e);
64             }
65         } else {
66             Log.e(TAG, "No config file present at " + file.getAbsolutePath());
67             return;
68         }
69         DeviceConfig deviceConfigToUse = null;
70         if (deviceConfigs != null && deviceConfigs.size() > 0) {
71             for (DeviceConfig deviceConfig : deviceConfigs) {
72                 if (deviceConfig.name.equals("VX_AUDIO_DEVICE_IN_HDMI_ARC")) {
73                     deviceConfigToUse = deviceConfig;
74                     break;
75                 }
76             }
77             if (deviceConfigToUse == null) {
78                 Log.w(TAG, "sadConfig.xml does not have required device info for "
79                                    + "VX_AUDIO_DEVICE_IN_HDMI_ARC");
80                 return;
81             }
82             List<Integer> audioCodecFormats = new ArrayList<>();
83             List<String> shortAudioDescriptors = new ArrayList<>();
84             for (CodecSad codecSad : deviceConfigToUse.supportedCodecs) {
85                 audioCodecFormats.add(codecSad.audioCodec);
86                 shortAudioDescriptors.add(HashCode.fromBytes(codecSad.sad).toString());
87             }
88             String audioCodecFormatsString = audioCodecFormats.toString();
89             String shortAudioDescriptorsString = shortAudioDescriptors.toString();
90             Log.i(TAG, "Supported Audio Formats");
91             Log.i(TAG, audioCodecFormatsString.substring(1, audioCodecFormatsString.length() - 1));
92             Log.i(TAG, shortAudioDescriptorsString
93                                .substring(1, shortAudioDescriptorsString.length() - 1));
94         }
95     }
96 }
97