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.annotation.SystemApi;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.util.Log;
23
24/**
25 * @hide
26 */
27@SystemApi
28public class TvStreamConfig implements Parcelable {
29    static final String TAG = TvStreamConfig.class.getSimpleName();
30
31    public final static int STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE = 1;
32    public final static int STREAM_TYPE_BUFFER_PRODUCER = 2;
33
34    private int mStreamId;
35    private int mType;
36    private int mMaxWidth;
37    private int mMaxHeight;
38    /**
39     * Generations are incremented once framework receives STREAM_CONFIGURATION_CHANGED event from
40     * HAL module. Framework should throw away outdated configurations and get new configurations
41     * via tv_input_device::get_stream_configurations().
42     */
43    private int mGeneration;
44
45    public static final Parcelable.Creator<TvStreamConfig> CREATOR =
46            new Parcelable.Creator<TvStreamConfig>() {
47        @Override
48        public TvStreamConfig createFromParcel(Parcel source) {
49            try {
50                return new Builder().
51                        streamId(source.readInt()).
52                        type(source.readInt()).
53                        maxWidth(source.readInt()).
54                        maxHeight(source.readInt()).
55                        generation(source.readInt()).build();
56            } catch (Exception e) {
57                Log.e(TAG, "Exception creating TvStreamConfig from parcel", e);
58                return null;
59            }
60        }
61
62        @Override
63        public TvStreamConfig[] newArray(int size) {
64            return new TvStreamConfig[size];
65        }
66    };
67
68    private TvStreamConfig() {}
69
70    public int getStreamId() {
71        return mStreamId;
72    }
73
74    public int getType() {
75        return mType;
76    }
77
78    public int getMaxWidth() {
79        return mMaxWidth;
80    }
81
82    public int getMaxHeight() {
83        return mMaxHeight;
84    }
85
86    public int getGeneration() {
87        return mGeneration;
88    }
89
90    @Override
91    public String toString() {
92        StringBuilder b = new StringBuilder(128);
93        b.append("TvStreamConfig {");
94        b.append("mStreamId=").append(mStreamId).append(";");
95        b.append("mType=").append(mType).append(";");
96        b.append("mGeneration=").append(mGeneration).append("}");
97        return b.toString();
98    }
99
100    // Parcelable
101    @Override
102    public int describeContents() {
103        return 0;
104    }
105
106    @Override
107    public void writeToParcel(Parcel dest, int flags) {
108        dest.writeInt(mStreamId);
109        dest.writeInt(mType);
110        dest.writeInt(mMaxWidth);
111        dest.writeInt(mMaxHeight);
112        dest.writeInt(mGeneration);
113    }
114
115    /**
116     * A helper class for creating a TvStreamConfig object.
117     */
118    public static final class Builder {
119        private Integer mStreamId;
120        private Integer mType;
121        private Integer mMaxWidth;
122        private Integer mMaxHeight;
123        private Integer mGeneration;
124
125        public Builder() {
126        }
127
128        public Builder streamId(int streamId) {
129            mStreamId = streamId;
130            return this;
131        }
132
133        public Builder type(int type) {
134            mType = type;
135            return this;
136        }
137
138        public Builder maxWidth(int maxWidth) {
139            mMaxWidth = maxWidth;
140            return this;
141        }
142
143        public Builder maxHeight(int maxHeight) {
144            mMaxHeight = maxHeight;
145            return this;
146        }
147
148        public Builder generation(int generation) {
149            mGeneration = generation;
150            return this;
151        }
152
153        public TvStreamConfig build() {
154            if (mStreamId == null || mType == null || mMaxWidth == null || mMaxHeight == null
155                    || mGeneration == null) {
156                throw new UnsupportedOperationException();
157            }
158
159            TvStreamConfig config = new TvStreamConfig();
160            config.mStreamId = mStreamId;
161            config.mType = mType;
162            config.mMaxWidth = mMaxWidth;
163            config.mMaxHeight = mMaxHeight;
164            config.mGeneration = mGeneration;
165            return config;
166        }
167    }
168
169    @Override
170    public boolean equals(Object obj) {
171        if (obj == null) return false;
172        if (!(obj instanceof TvStreamConfig)) return false;
173
174        TvStreamConfig config = (TvStreamConfig) obj;
175        return config.mGeneration == mGeneration
176            && config.mStreamId == mStreamId
177            && config.mType == mType
178            && config.mMaxWidth == mMaxWidth
179            && config.mMaxHeight == mMaxHeight;
180    }
181}
182