TvTrackInfo.java revision 10d285ac06b3d3060c7d90d3dc196d4ac8367467
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 int mAudioChannelCount;
46    private final int mAudioSampleRate;
47    private final int mVideoWidth;
48    private final int mVideoHeight;
49    private final Bundle mExtra;
50
51    private TvTrackInfo(int type, String id, String language, int audioChannelCount,
52            int audioSampleRate, int videoWidth, int videoHeight, Bundle extra) {
53        mType = type;
54        mId = id;
55        mLanguage = language;
56        mAudioChannelCount = audioChannelCount;
57        mAudioSampleRate = audioSampleRate;
58        mVideoWidth = videoWidth;
59        mVideoHeight = videoHeight;
60        mExtra = extra;
61    }
62
63    private TvTrackInfo(Parcel in) {
64        mType = in.readInt();
65        mId = in.readString();
66        mLanguage = in.readString();
67        mAudioChannelCount = in.readInt();
68        mAudioSampleRate = in.readInt();
69        mVideoWidth = in.readInt();
70        mVideoHeight = in.readInt();
71        mExtra = in.readBundle();
72    }
73
74    /**
75     * Returns the type of the track. The type should be one of the followings:
76     * {@link #TYPE_AUDIO}, {@link #TYPE_VIDEO} and {@link #TYPE_SUBTITLE}.
77     */
78    public final int getType() {
79        return mType;
80    }
81
82    /**
83     * Returns the ID of the track.
84     */
85    public final String getId() {
86        return mId;
87    }
88
89    /**
90     * Returns the language information encoded by either ISO 639-1 or ISO 639-2/T. If the language
91     * is unknown or could not be determined, the corresponding value will be {@code null}.
92     */
93    public final String getLanguage() {
94        return mLanguage;
95    }
96
97    /**
98     * Returns the audio channel count. Valid only for {@link #TYPE_AUDIO} tracks.
99     */
100    public final int getAudioChannelCount() {
101        if (mType != TYPE_AUDIO) {
102            throw new IllegalStateException("Not an audio track");
103        }
104        return mAudioChannelCount;
105    }
106
107    /**
108     * Returns the audio sample rate, in the unit of Hz. Valid only for {@link #TYPE_AUDIO} tracks.
109     */
110    public final int getAudioSampleRate() {
111        if (mType != TYPE_AUDIO) {
112            throw new IllegalStateException("Not an audio track");
113        }
114        return mAudioSampleRate;
115    }
116
117    /**
118     * Returns the width of the video, in the unit of pixels. Valid only for {@link #TYPE_VIDEO}
119     * tracks.
120     */
121    public final int getVideoWidth() {
122        if (mType != TYPE_VIDEO) {
123            throw new IllegalStateException("Not a video track");
124        }
125        return mVideoWidth;
126    }
127
128    /**
129     * Returns the height of the video, in the unit of pixels. Valid only for {@link #TYPE_VIDEO}
130     * tracks.
131     */
132    public final int getVideoHeight() {
133        if (mType != TYPE_VIDEO) {
134            throw new IllegalStateException("Not a video track");
135        }
136        return mVideoHeight;
137    }
138
139    /**
140     * Returns the extra information about the current track.
141     */
142    public final Bundle getExtra() {
143        return mExtra;
144    }
145
146    @Override
147    public int describeContents() {
148        return 0;
149    }
150
151    /**
152     * Used to package this object into a {@link Parcel}.
153     *
154     * @param dest The {@link Parcel} to be written.
155     * @param flags The flags used for parceling.
156     */
157    @Override
158    public void writeToParcel(Parcel dest, int flags) {
159        dest.writeInt(mType);
160        dest.writeString(mId);
161        dest.writeString(mLanguage);
162        dest.writeInt(mAudioChannelCount);
163        dest.writeInt(mAudioSampleRate);
164        dest.writeInt(mVideoWidth);
165        dest.writeInt(mVideoHeight);
166        dest.writeBundle(mExtra);
167    }
168
169    public static final Parcelable.Creator<TvTrackInfo> CREATOR =
170            new Parcelable.Creator<TvTrackInfo>() {
171                @Override
172                public TvTrackInfo createFromParcel(Parcel in) {
173                    return new TvTrackInfo(in);
174                }
175
176                @Override
177                public TvTrackInfo[] newArray(int size) {
178                    return new TvTrackInfo[size];
179                }
180            };
181
182    /**
183     * A builder class for creating {@link TvTrackInfo} objects.
184     */
185    public static final class Builder {
186        private final String mId;
187        private final int mType;
188        private String mLanguage;
189        private int mAudioChannelCount;
190        private int mAudioSampleRate;
191        private int mVideoWidth;
192        private int mVideoHeight;
193        private Bundle mExtra;
194
195        /**
196         * Create a {@link Builder}. Any field that should be included in the {@link TvTrackInfo}
197         * must be added.
198         *
199         * @param type The type of the track.
200         * @param id The ID of the track that uniquely identifies the current track among all the
201         *            other tracks in the same TV program.
202         */
203        public Builder(int type, String id) {
204            if (type != TYPE_AUDIO
205                    && type != TYPE_VIDEO
206                    && type != TYPE_SUBTITLE) {
207                throw new IllegalArgumentException("Unknown type: " + type);
208            }
209            if (id == null) {
210                throw new IllegalArgumentException("id cannot be null");
211            }
212            mType = type;
213            mId = id;
214        }
215
216        /**
217         * Sets the language information of the current track.
218         *
219         * @param language The language string encoded by either ISO 639-1 or ISO 639-2/T.
220         */
221        public final Builder setLanguage(String language) {
222            mLanguage = language;
223            return this;
224        }
225
226        /**
227         * Sets the audio channel count. Valid only for {@link #TYPE_AUDIO} tracks.
228         *
229         * @param audioChannelCount The audio channel count.
230         */
231        public final Builder setAudioChannelCount(int audioChannelCount) {
232            if (mType != TYPE_AUDIO) {
233                throw new IllegalStateException("Not an audio track");
234            }
235            mAudioChannelCount = audioChannelCount;
236            return this;
237        }
238
239        /**
240         * Sets the audio sample rate, in the unit of Hz. Valid only for {@link #TYPE_AUDIO}
241         * tracks.
242         *
243         * @param audioSampleRate The audio sample rate.
244         */
245        public final Builder setAudioSampleRate(int audioSampleRate) {
246            if (mType != TYPE_AUDIO) {
247                throw new IllegalStateException("Not an audio track");
248            }
249            mAudioSampleRate = audioSampleRate;
250            return this;
251        }
252
253        /**
254         * Sets the width of the video, in the unit of pixels. Valid only for {@link #TYPE_VIDEO}
255         * tracks.
256         *
257         * @param videoWidth The width of the video.
258         */
259        public final Builder setVideoWidth(int videoWidth) {
260            if (mType != TYPE_VIDEO) {
261                throw new IllegalStateException("Not a video track");
262            }
263            mVideoWidth = videoWidth;
264            return this;
265        }
266
267        /**
268         * Sets the height of the video, in the unit of pixels. Valid only for {@link #TYPE_VIDEO}
269         * tracks.
270         *
271         * @param videoHeight The height of the video.
272         */
273        public final Builder setVideoHeight(int videoHeight) {
274            if (mType != TYPE_VIDEO) {
275                throw new IllegalStateException("Not a video track");
276            }
277            mVideoHeight = videoHeight;
278            return this;
279        }
280
281        /**
282         * Sets the extra information about the current track.
283         *
284         * @param extra The extra information.
285         */
286        public final Builder setExtra(Bundle extra) {
287            mExtra = new Bundle(extra);
288            return this;
289        }
290
291        /**
292         * Creates a {@link TvTrackInfo} instance with the specified fields.
293         *
294         * @return The new {@link TvTrackInfo} instance
295         */
296        public TvTrackInfo build() {
297            return new TvTrackInfo(mType, mId, mLanguage, mAudioChannelCount, mAudioSampleRate,
298                    mVideoWidth, mVideoHeight, mExtra);
299        }
300    }
301}