TvTrackInfo.java revision 071b2c72a88eb11e83dcd9c3abd117b30a42d911
1/* 2 * Copyright (C) 2014 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 17package android.media.tv; 18 19import android.os.Bundle; 20import android.os.Parcel; 21import android.os.Parcelable; 22 23/** 24 * Encapsulates the format of tracks played in {@link TvInputService}. 25 */ 26public final class TvTrackInfo implements Parcelable { 27 /** 28 * The type value for audio tracks. 29 */ 30 public static final int TYPE_AUDIO = 0; 31 32 /** 33 * The type value for video tracks. 34 */ 35 public static final int TYPE_VIDEO = 1; 36 37 /** 38 * The type value for subtitle tracks. 39 */ 40 public static final int TYPE_SUBTITLE = 2; 41 42 private final int mType; 43 private final String mId; 44 private final String mLanguage; 45 private final String mDescription; 46 private final int mAudioChannelCount; 47 private final int mAudioSampleRate; 48 private final int mVideoWidth; 49 private final int mVideoHeight; 50 private final float mVideoFrameRate; 51 private final Bundle mExtra; 52 53 private TvTrackInfo(int type, String id, String language, String description, 54 int audioChannelCount, int audioSampleRate, int videoWidth, int videoHeight, 55 float videoFrameRate, Bundle extra) { 56 mType = type; 57 mId = id; 58 mLanguage = language; 59 mDescription = description; 60 mAudioChannelCount = audioChannelCount; 61 mAudioSampleRate = audioSampleRate; 62 mVideoWidth = videoWidth; 63 mVideoHeight = videoHeight; 64 mVideoFrameRate = videoFrameRate; 65 mExtra = extra; 66 } 67 68 private TvTrackInfo(Parcel in) { 69 mType = in.readInt(); 70 mId = in.readString(); 71 mLanguage = in.readString(); 72 mDescription = in.readString(); 73 mAudioChannelCount = in.readInt(); 74 mAudioSampleRate = in.readInt(); 75 mVideoWidth = in.readInt(); 76 mVideoHeight = in.readInt(); 77 mVideoFrameRate = in.readFloat(); 78 mExtra = in.readBundle(); 79 } 80 81 /** 82 * Returns the type of the track. The type should be one of the followings: 83 * {@link #TYPE_AUDIO}, {@link #TYPE_VIDEO} and {@link #TYPE_SUBTITLE}. 84 */ 85 public final int getType() { 86 return mType; 87 } 88 89 /** 90 * Returns the ID of the track. 91 */ 92 public final String getId() { 93 return mId; 94 } 95 96 /** 97 * Returns the language information encoded by either ISO 639-1 or ISO 639-2/T. If the language 98 * is unknown or could not be determined, the corresponding value will be {@code null}. 99 */ 100 public final String getLanguage() { 101 return mLanguage; 102 } 103 104 /** 105 * Returns a user readable description for the current track. 106 */ 107 public final String getDescription() { 108 return mDescription; 109 } 110 111 /** 112 * Returns the audio channel count. Valid only for {@link #TYPE_AUDIO} tracks. 113 */ 114 public final int getAudioChannelCount() { 115 if (mType != TYPE_AUDIO) { 116 throw new IllegalStateException("Not an audio track"); 117 } 118 return mAudioChannelCount; 119 } 120 121 /** 122 * Returns the audio sample rate, in the unit of Hz. Valid only for {@link #TYPE_AUDIO} tracks. 123 */ 124 public final int getAudioSampleRate() { 125 if (mType != TYPE_AUDIO) { 126 throw new IllegalStateException("Not an audio track"); 127 } 128 return mAudioSampleRate; 129 } 130 131 /** 132 * Returns the width of the video, in the unit of pixels. Valid only for {@link #TYPE_VIDEO} 133 * tracks. 134 */ 135 public final int getVideoWidth() { 136 if (mType != TYPE_VIDEO) { 137 throw new IllegalStateException("Not a video track"); 138 } 139 return mVideoWidth; 140 } 141 142 /** 143 * Returns the height of the video, in the unit of pixels. Valid only for {@link #TYPE_VIDEO} 144 * tracks. 145 */ 146 public final int getVideoHeight() { 147 if (mType != TYPE_VIDEO) { 148 throw new IllegalStateException("Not a video track"); 149 } 150 return mVideoHeight; 151 } 152 153 /** 154 * Returns the frame rate of the video, in the unit of fps (frames per second). Valid only for 155 * {@link #TYPE_VIDEO} tracks. 156 */ 157 public final float getVideoFrameRate() { 158 if (mType != TYPE_VIDEO) { 159 throw new IllegalStateException("Not a video track"); 160 } 161 return mVideoFrameRate; 162 } 163 164 /** 165 * Returns the extra information about the current track. 166 */ 167 public final Bundle getExtra() { 168 return mExtra; 169 } 170 171 @Override 172 public int describeContents() { 173 return 0; 174 } 175 176 /** 177 * Used to package this object into a {@link Parcel}. 178 * 179 * @param dest The {@link Parcel} to be written. 180 * @param flags The flags used for parceling. 181 */ 182 @Override 183 public void writeToParcel(Parcel dest, int flags) { 184 dest.writeInt(mType); 185 dest.writeString(mId); 186 dest.writeString(mLanguage); 187 dest.writeString(mDescription); 188 dest.writeInt(mAudioChannelCount); 189 dest.writeInt(mAudioSampleRate); 190 dest.writeInt(mVideoWidth); 191 dest.writeInt(mVideoHeight); 192 dest.writeFloat(mVideoFrameRate); 193 dest.writeBundle(mExtra); 194 } 195 196 public static final Parcelable.Creator<TvTrackInfo> CREATOR = 197 new Parcelable.Creator<TvTrackInfo>() { 198 @Override 199 public TvTrackInfo createFromParcel(Parcel in) { 200 return new TvTrackInfo(in); 201 } 202 203 @Override 204 public TvTrackInfo[] newArray(int size) { 205 return new TvTrackInfo[size]; 206 } 207 }; 208 209 /** 210 * A builder class for creating {@link TvTrackInfo} objects. 211 */ 212 public static final class Builder { 213 private final String mId; 214 private final int mType; 215 private String mLanguage; 216 private String mDescription; 217 private int mAudioChannelCount; 218 private int mAudioSampleRate; 219 private int mVideoWidth; 220 private int mVideoHeight; 221 private float mVideoFrameRate; 222 private Bundle mExtra; 223 224 /** 225 * Create a {@link Builder}. Any field that should be included in the {@link TvTrackInfo} 226 * must be added. 227 * 228 * @param type The type of the track. 229 * @param id The ID of the track that uniquely identifies the current track among all the 230 * other tracks in the same TV program. 231 */ 232 public Builder(int type, String id) { 233 if (type != TYPE_AUDIO 234 && type != TYPE_VIDEO 235 && type != TYPE_SUBTITLE) { 236 throw new IllegalArgumentException("Unknown type: " + type); 237 } 238 if (id == null) { 239 throw new IllegalArgumentException("id cannot be null"); 240 } 241 mType = type; 242 mId = id; 243 } 244 245 /** 246 * Sets the language information of the current track. 247 * 248 * @param language The language string encoded by either ISO 639-1 or ISO 639-2/T. 249 */ 250 public final Builder setLanguage(String language) { 251 mLanguage = language; 252 return this; 253 } 254 255 /** 256 * Sets a user readable description for the current track. 257 * 258 * @param description The user readable description. 259 */ 260 public final Builder setDescription(String description) { 261 mDescription = description; 262 return this; 263 } 264 265 /** 266 * Sets the audio channel count. Valid only for {@link #TYPE_AUDIO} tracks. 267 * 268 * @param audioChannelCount The audio channel count. 269 */ 270 public final Builder setAudioChannelCount(int audioChannelCount) { 271 if (mType != TYPE_AUDIO) { 272 throw new IllegalStateException("Not an audio track"); 273 } 274 mAudioChannelCount = audioChannelCount; 275 return this; 276 } 277 278 /** 279 * Sets the audio sample rate, in the unit of Hz. Valid only for {@link #TYPE_AUDIO} 280 * tracks. 281 * 282 * @param audioSampleRate The audio sample rate. 283 */ 284 public final Builder setAudioSampleRate(int audioSampleRate) { 285 if (mType != TYPE_AUDIO) { 286 throw new IllegalStateException("Not an audio track"); 287 } 288 mAudioSampleRate = audioSampleRate; 289 return this; 290 } 291 292 /** 293 * Sets the width of the video, in the unit of pixels. Valid only for {@link #TYPE_VIDEO} 294 * tracks. 295 * 296 * @param videoWidth The width of the video. 297 */ 298 public final Builder setVideoWidth(int videoWidth) { 299 if (mType != TYPE_VIDEO) { 300 throw new IllegalStateException("Not a video track"); 301 } 302 mVideoWidth = videoWidth; 303 return this; 304 } 305 306 /** 307 * Sets the height of the video, in the unit of pixels. Valid only for {@link #TYPE_VIDEO} 308 * tracks. 309 * 310 * @param videoHeight The height of the video. 311 */ 312 public final Builder setVideoHeight(int videoHeight) { 313 if (mType != TYPE_VIDEO) { 314 throw new IllegalStateException("Not a video track"); 315 } 316 mVideoHeight = videoHeight; 317 return this; 318 } 319 320 /** 321 * Sets the frame rate of the video, in the unit fps (frames per rate). Valid only for 322 * {@link #TYPE_VIDEO} tracks. 323 * 324 * @param videoFrameRate The frame rate of the video. 325 */ 326 public final Builder setVideoFrameRate(float videoFrameRate) { 327 if (mType != TYPE_VIDEO) { 328 throw new IllegalStateException("Not a video track"); 329 } 330 mVideoFrameRate = videoFrameRate; 331 return this; 332 } 333 334 /** 335 * Sets the extra information about the current track. 336 * 337 * @param extra The extra information. 338 */ 339 public final Builder setExtra(Bundle extra) { 340 mExtra = new Bundle(extra); 341 return this; 342 } 343 344 /** 345 * Creates a {@link TvTrackInfo} instance with the specified fields. 346 * 347 * @return The new {@link TvTrackInfo} instance 348 */ 349 public TvTrackInfo build() { 350 return new TvTrackInfo(mType, mId, mLanguage, mDescription, mAudioChannelCount, 351 mAudioSampleRate, mVideoWidth, mVideoHeight, mVideoFrameRate, mExtra); 352 } 353 } 354} 355