1 /*
2  * Copyright (C) 2022 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.mediav2.common.cts;
18 
19 import android.graphics.ImageFormat;
20 import android.media.AudioFormat;
21 
22 /**
23  * Class to hold raw resource attributes.
24  */
25 public class RawResource {
26     public final String mFileName;
27     public final boolean mIsAudio;
28     public final int mWidth;
29     public final int mHeight;
30     public final int mColorFormat;
31     public final int mSampleRate;
32     public final int mChannelCount;
33     public final int mBytesPerSample;
34     public final int mAudioEncoding;
35 
RawResource(Builder builder)36     private RawResource(Builder builder) {
37         if (builder.mFileName == null || builder.mFileName.isEmpty()) {
38             throw new IllegalArgumentException("Invalid raw resource file name");
39         }
40         if (builder.mIsAudio && (builder.mSampleRate <= 0 || builder.mChannelCount <= 0
41                 || builder.mBytesPerSample <= 0)) {
42             throw new IllegalArgumentException("Invalid arguments for raw audio resource");
43         }
44         if (!builder.mIsAudio && (builder.mWidth <= 0 || builder.mHeight <= 0
45                 || builder.mBytesPerSample <= 0)) {
46             throw new IllegalArgumentException("Invalid arguments for raw video resource");
47         }
48         mFileName = builder.mFileName;
49         mIsAudio = builder.mIsAudio;
50         mWidth = builder.mWidth;
51         mHeight = builder.mHeight;
52         mColorFormat = builder.mColorFormat;
53         mSampleRate = builder.mSampleRate;
54         mChannelCount = builder.mChannelCount;
55         mBytesPerSample = builder.mBytesPerSample;
56         mAudioEncoding = builder.mAudioEncoding;
57     }
58 
59     public static class Builder {
60         private String mFileName;
61         private boolean mIsAudio;
62         private int mWidth;
63         private int mHeight;
64         private int mColorFormat = ImageFormat.UNKNOWN;
65         private int mSampleRate;
66         private int mChannelCount;
67         private int mBytesPerSample;
68         private int mAudioEncoding = AudioFormat.ENCODING_INVALID;
69 
setFileName(String fileName, boolean isAudio)70         public Builder setFileName(String fileName, boolean isAudio) {
71             this.mFileName = fileName;
72             this.mIsAudio = isAudio;
73             return this;
74         }
75 
setDimension(int width, int height)76         public Builder setDimension(int width, int height) {
77             this.mWidth = width;
78             this.mHeight = height;
79             return this;
80         }
81 
setColorFormat(int colorFormat)82         public Builder setColorFormat(int colorFormat) {
83             this.mColorFormat = colorFormat;
84             return this;
85         }
86 
setSampleRate(int sampleRate)87         public Builder setSampleRate(int sampleRate) {
88             this.mSampleRate = sampleRate;
89             return this;
90         }
91 
setChannelCount(int channelCount)92         public Builder setChannelCount(int channelCount) {
93             this.mChannelCount = channelCount;
94             return this;
95         }
96 
setBytesPerSample(int bytesPerSample)97         public Builder setBytesPerSample(int bytesPerSample) {
98             this.mBytesPerSample = bytesPerSample;
99             return this;
100         }
101 
setAudioEncoding(int audioEncoding)102         public Builder setAudioEncoding(int audioEncoding) {
103             this.mAudioEncoding = audioEncoding;
104             return this;
105         }
106 
build()107         public RawResource build() {
108             return new RawResource(this);
109         }
110     }
111 }
112