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