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