1 /*
2  * Copyright 2021 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 androidx.camera.video;
18 
19 import androidx.annotation.IntDef;
20 
21 import com.google.auto.value.AutoValue;
22 
23 import org.jspecify.annotations.NonNull;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * A class represents the strategy that will be adopted when the device does not support all the
30  * desired {@link Quality} in {@link QualitySelector} in order to select the quality as possible.
31  */
32 public class FallbackStrategy {
33 
34     // Restrict access to sealed class
FallbackStrategy()35     private FallbackStrategy() {
36     }
37 
38     /**
39      * Returns a fallback strategy that will choose the quality that is closest to and higher
40      * than the input quality.
41      *
42      * <p>If that can not result in a supported quality, choose the quality that is closest to
43      * and lower than the input quality.
44      */
higherQualityOrLowerThan(@onNull Quality quality)45     public static @NonNull FallbackStrategy higherQualityOrLowerThan(@NonNull Quality quality) {
46         return new AutoValue_FallbackStrategy_RuleStrategy(quality,
47                 FALLBACK_RULE_HIGHER_OR_LOWER);
48     }
49 
50     /**
51      * Returns a fallback strategy that will choose the quality that is closest to and higher
52      * than the input quality.
53      */
higherQualityThan(@onNull Quality quality)54     public static @NonNull FallbackStrategy higherQualityThan(@NonNull Quality quality) {
55         return new AutoValue_FallbackStrategy_RuleStrategy(quality,
56                 FALLBACK_RULE_HIGHER);
57     }
58 
59     /**
60      * Returns a fallback strategy that will choose the quality that is closest to and lower than
61      * the input quality.
62      *
63      * <p>If that can not result in a supported quality, choose the quality that is closest to
64      * and higher than the input quality.
65      */
lowerQualityOrHigherThan(@onNull Quality quality)66     public static @NonNull FallbackStrategy lowerQualityOrHigherThan(@NonNull Quality quality) {
67         return new AutoValue_FallbackStrategy_RuleStrategy(quality,
68                 FALLBACK_RULE_LOWER_OR_HIGHER);
69     }
70 
71     /**
72      * Returns a fallback strategy that will choose the quality that is closest to and lower
73      * than the input quality.
74      */
lowerQualityThan(@onNull Quality quality)75     public static @NonNull FallbackStrategy lowerQualityThan(@NonNull Quality quality) {
76         return new AutoValue_FallbackStrategy_RuleStrategy(quality,
77                 FALLBACK_RULE_LOWER);
78     }
79 
80     static final int FALLBACK_RULE_NONE = 0;
81 
82     /**
83      * Choose the quality that is closest to and higher than the desired quality. If that can not
84      * result in a supported quality, choose the quality that is closest to and lower than the
85      * desired quality.
86      */
87     static final int FALLBACK_RULE_HIGHER_OR_LOWER = 1;
88 
89     /**
90      * Choose the quality that is closest to and higher than the desired quality.
91      */
92     static final int FALLBACK_RULE_HIGHER = 2;
93 
94     /**
95      * Choose the quality that is closest to and lower than the desired quality. If that can not
96      * result in a supported quality, choose the quality that is closest to and higher than the
97      * desired quality.
98      */
99     static final int FALLBACK_RULE_LOWER_OR_HIGHER = 3;
100 
101     /**
102      * Choose the quality that is closest to and lower than the desired quality.
103      */
104     static final int FALLBACK_RULE_LOWER = 4;
105 
106     /**
107      * The fallback strategies when desired quality is not supported.
108      */
109     @Retention(RetentionPolicy.SOURCE)
110     @IntDef({FALLBACK_RULE_NONE,
111             FALLBACK_RULE_HIGHER_OR_LOWER,
112             FALLBACK_RULE_HIGHER,
113             FALLBACK_RULE_LOWER_OR_HIGHER,
114             FALLBACK_RULE_LOWER
115     })
116     @interface FallbackRule {
117     }
118 
119     /**
120      * The strategy that no fallback strategy will be applied.
121      */
122     static final FallbackStrategy NONE =
123             new AutoValue_FallbackStrategy_RuleStrategy(Quality.NONE, FALLBACK_RULE_NONE);
124 
125     @AutoValue
126     abstract static class RuleStrategy extends FallbackStrategy {
getFallbackQuality()127         abstract @NonNull Quality getFallbackQuality();
128 
129         @FallbackRule
getFallbackRule()130         abstract int getFallbackRule();
131     }
132 }
133