1 /* 2 * Copyright 2019 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 android.media.tv.tuner; 18 19 import android.annotation.Nullable; 20 import android.hardware.tv.tuner.DemuxAlpFilterType; 21 import android.hardware.tv.tuner.DemuxIpFilterType; 22 import android.hardware.tv.tuner.DemuxMmtpFilterType; 23 import android.hardware.tv.tuner.DemuxTlvFilterType; 24 import android.hardware.tv.tuner.DemuxTsFilterType; 25 import android.media.tv.tuner.filter.Filter; 26 27 /** 28 * Utility class for tuner framework. 29 * 30 * @hide 31 */ 32 public final class TunerUtils { 33 34 /** 35 * Gets the corresponding filter subtype constant defined in tuner HAL. 36 * 37 * @param mainType filter main type. 38 * @param subtype filter subtype. 39 */ getFilterSubtype(@ilter.Type int mainType, @Filter.Subtype int subtype)40 public static int getFilterSubtype(@Filter.Type int mainType, @Filter.Subtype int subtype) { 41 if (mainType == Filter.TYPE_TS) { 42 switch (subtype) { 43 case Filter.SUBTYPE_UNDEFINED: 44 return DemuxTsFilterType.UNDEFINED; 45 case Filter.SUBTYPE_SECTION: 46 return DemuxTsFilterType.SECTION; 47 case Filter.SUBTYPE_PES: 48 return DemuxTsFilterType.PES; 49 case Filter.SUBTYPE_TS: 50 return DemuxTsFilterType.TS; 51 case Filter.SUBTYPE_AUDIO: 52 return DemuxTsFilterType.AUDIO; 53 case Filter.SUBTYPE_VIDEO: 54 return DemuxTsFilterType.VIDEO; 55 case Filter.SUBTYPE_PCR: 56 return DemuxTsFilterType.PCR; 57 case Filter.SUBTYPE_RECORD: 58 return DemuxTsFilterType.RECORD; 59 case Filter.SUBTYPE_TEMI: 60 return DemuxTsFilterType.TEMI; 61 default: 62 break; 63 } 64 } else if (mainType == Filter.TYPE_MMTP) { 65 switch (subtype) { 66 case Filter.SUBTYPE_UNDEFINED: 67 return DemuxMmtpFilterType.UNDEFINED; 68 case Filter.SUBTYPE_SECTION: 69 return DemuxMmtpFilterType.SECTION; 70 case Filter.SUBTYPE_PES: 71 return DemuxMmtpFilterType.PES; 72 case Filter.SUBTYPE_MMTP: 73 return DemuxMmtpFilterType.MMTP; 74 case Filter.SUBTYPE_AUDIO: 75 return DemuxMmtpFilterType.AUDIO; 76 case Filter.SUBTYPE_VIDEO: 77 return DemuxMmtpFilterType.VIDEO; 78 case Filter.SUBTYPE_RECORD: 79 return DemuxMmtpFilterType.RECORD; 80 case Filter.SUBTYPE_DOWNLOAD: 81 return DemuxMmtpFilterType.DOWNLOAD; 82 default: 83 break; 84 } 85 86 } else if (mainType == Filter.TYPE_IP) { 87 switch (subtype) { 88 case Filter.SUBTYPE_UNDEFINED: 89 return DemuxIpFilterType.UNDEFINED; 90 case Filter.SUBTYPE_SECTION: 91 return DemuxIpFilterType.SECTION; 92 case Filter.SUBTYPE_NTP: 93 return DemuxIpFilterType.NTP; 94 case Filter.SUBTYPE_IP_PAYLOAD: 95 return DemuxIpFilterType.IP_PAYLOAD; 96 case Filter.SUBTYPE_IP: 97 return DemuxIpFilterType.IP; 98 case Filter.SUBTYPE_PAYLOAD_THROUGH: 99 return DemuxIpFilterType.PAYLOAD_THROUGH; 100 default: 101 break; 102 } 103 } else if (mainType == Filter.TYPE_TLV) { 104 switch (subtype) { 105 case Filter.SUBTYPE_UNDEFINED: 106 return DemuxTlvFilterType.UNDEFINED; 107 case Filter.SUBTYPE_SECTION: 108 return DemuxTlvFilterType.SECTION; 109 case Filter.SUBTYPE_TLV: 110 return DemuxTlvFilterType.TLV; 111 case Filter.SUBTYPE_PAYLOAD_THROUGH: 112 return DemuxTlvFilterType.PAYLOAD_THROUGH; 113 default: 114 break; 115 } 116 } else if (mainType == Filter.TYPE_ALP) { 117 switch (subtype) { 118 case Filter.SUBTYPE_UNDEFINED: 119 return DemuxAlpFilterType.UNDEFINED; 120 case Filter.SUBTYPE_SECTION: 121 return DemuxAlpFilterType.SECTION; 122 case Filter.SUBTYPE_PTP: 123 return DemuxAlpFilterType.PTP; 124 case Filter.SUBTYPE_PAYLOAD_THROUGH: 125 return DemuxAlpFilterType.PAYLOAD_THROUGH; 126 default: 127 break; 128 } 129 } 130 throw new IllegalArgumentException( 131 "Invalid filter types. Main type=" + mainType + ", subtype=" + subtype); 132 } 133 134 /** 135 * Gets an throwable instance for the corresponding result. 136 */ 137 @Nullable throwExceptionForResult( @uner.Result int r, @Nullable String msg)138 public static void throwExceptionForResult( 139 @Tuner.Result int r, @Nullable String msg) { 140 if (msg == null) { 141 msg = ""; 142 } 143 switch (r) { 144 case Tuner.RESULT_SUCCESS: 145 return; 146 case Tuner.RESULT_INVALID_ARGUMENT: 147 throw new IllegalArgumentException(msg); 148 case Tuner.RESULT_INVALID_STATE: 149 throw new IllegalStateException(msg); 150 case Tuner.RESULT_NOT_INITIALIZED: 151 throw new IllegalStateException("Invalid state: not initialized. " + msg); 152 case Tuner.RESULT_OUT_OF_MEMORY: 153 throw new OutOfMemoryError(msg); 154 case Tuner.RESULT_UNAVAILABLE: 155 throw new IllegalStateException("Invalid state: resource unavailable. " + msg); 156 case Tuner.RESULT_UNKNOWN_ERROR: 157 throw new RuntimeException("Unknown error" + msg); 158 default: 159 break; 160 } 161 throw new RuntimeException("Unexpected result " + r + ". " + msg); 162 } 163 164 /** 165 * Checks the state of a resource instance. 166 * 167 * @throws IllegalStateException if the resource has already been closed. 168 */ checkResourceState(String name, boolean closed)169 public static void checkResourceState(String name, boolean closed) { 170 if (closed) { 171 throw new IllegalStateException(name + " has been closed"); 172 } 173 } 174 175 /** 176 * Checks the accessibility of a resource instance. 177 * 178 * @throws IllegalStateException if the resource has already been inaccessible. 179 */ checkResourceAccessible(String name, boolean accessible)180 public static void checkResourceAccessible(String name, boolean accessible) { 181 if (!accessible) { 182 throw new IllegalStateException(name + " is inaccessible"); 183 } 184 } 185 TunerUtils()186 private TunerUtils() {} 187 } 188