1 /* 2 * Copyright 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 package com.google.android.exoplayer2.transformer; 17 18 import static com.google.android.exoplayer2.util.Assertions.checkArgument; 19 20 import androidx.annotation.Nullable; 21 import com.google.android.exoplayer2.C; 22 23 /** Information about the result of a successful transformation. */ 24 public final class TransformationResult { 25 26 /** A builder for {@link TransformationResult} instances. */ 27 public static final class Builder { 28 private long durationMs; 29 private long fileSizeBytes; 30 private int averageAudioBitrate; 31 private int averageVideoBitrate; 32 private int videoFrameCount; 33 Builder()34 public Builder() { 35 durationMs = C.TIME_UNSET; 36 fileSizeBytes = C.LENGTH_UNSET; 37 averageAudioBitrate = C.RATE_UNSET_INT; 38 averageVideoBitrate = C.RATE_UNSET_INT; 39 } 40 41 /** 42 * Sets the duration of the video in milliseconds. 43 * 44 * <p>Input must be positive or {@link C#TIME_UNSET}. 45 */ setDurationMs(long durationMs)46 public Builder setDurationMs(long durationMs) { 47 checkArgument(durationMs > 0 || durationMs == C.TIME_UNSET); 48 this.durationMs = durationMs; 49 return this; 50 } 51 52 /** 53 * Sets the file size in bytes. 54 * 55 * <p>Input must be positive or {@link C#LENGTH_UNSET}. 56 */ setFileSizeBytes(long fileSizeBytes)57 public Builder setFileSizeBytes(long fileSizeBytes) { 58 checkArgument(fileSizeBytes > 0 || fileSizeBytes == C.LENGTH_UNSET); 59 this.fileSizeBytes = fileSizeBytes; 60 return this; 61 } 62 63 /** 64 * Sets the average audio bitrate. 65 * 66 * <p>Input must be positive or {@link C#RATE_UNSET_INT}. 67 */ setAverageAudioBitrate(int averageAudioBitrate)68 public Builder setAverageAudioBitrate(int averageAudioBitrate) { 69 checkArgument(averageAudioBitrate > 0 || averageAudioBitrate == C.RATE_UNSET_INT); 70 this.averageAudioBitrate = averageAudioBitrate; 71 return this; 72 } 73 74 /** 75 * Sets the average video bitrate. 76 * 77 * <p>Input must be positive or {@link C#RATE_UNSET_INT}. 78 */ setAverageVideoBitrate(int averageVideoBitrate)79 public Builder setAverageVideoBitrate(int averageVideoBitrate) { 80 checkArgument(averageVideoBitrate > 0 || averageVideoBitrate == C.RATE_UNSET_INT); 81 this.averageVideoBitrate = averageVideoBitrate; 82 return this; 83 } 84 85 /** 86 * Sets the number of video frames. 87 * 88 * <p>Input must be positive or {@code 0}. 89 */ setVideoFrameCount(int videoFrameCount)90 public Builder setVideoFrameCount(int videoFrameCount) { 91 checkArgument(videoFrameCount >= 0); 92 this.videoFrameCount = videoFrameCount; 93 return this; 94 } 95 build()96 public TransformationResult build() { 97 return new TransformationResult( 98 durationMs, fileSizeBytes, averageAudioBitrate, averageVideoBitrate, videoFrameCount); 99 } 100 } 101 102 /** The duration of the file in milliseconds, or {@link C#TIME_UNSET} if unset or unknown. */ 103 public final long durationMs; 104 /** The size of the file in bytes, or {@link C#LENGTH_UNSET} if unset or unknown. */ 105 public final long fileSizeBytes; 106 /** 107 * The average bitrate of the audio track data, or {@link C#RATE_UNSET_INT} if unset or unknown. 108 */ 109 public final int averageAudioBitrate; 110 /** 111 * The average bitrate of the video track data, or {@link C#RATE_UNSET_INT} if unset or unknown. 112 */ 113 public final int averageVideoBitrate; 114 /** The number of video frames. */ 115 public final int videoFrameCount; 116 TransformationResult( long durationMs, long fileSizeBytes, int averageAudioBitrate, int averageVideoBitrate, int videoFrameCount)117 private TransformationResult( 118 long durationMs, 119 long fileSizeBytes, 120 int averageAudioBitrate, 121 int averageVideoBitrate, 122 int videoFrameCount) { 123 this.durationMs = durationMs; 124 this.fileSizeBytes = fileSizeBytes; 125 this.averageAudioBitrate = averageAudioBitrate; 126 this.averageVideoBitrate = averageVideoBitrate; 127 this.videoFrameCount = videoFrameCount; 128 } 129 buildUpon()130 public Builder buildUpon() { 131 return new Builder() 132 .setDurationMs(durationMs) 133 .setFileSizeBytes(fileSizeBytes) 134 .setAverageAudioBitrate(averageAudioBitrate) 135 .setAverageVideoBitrate(averageVideoBitrate) 136 .setVideoFrameCount(videoFrameCount); 137 } 138 139 @Override equals(@ullable Object o)140 public boolean equals(@Nullable Object o) { 141 if (this == o) { 142 return true; 143 } 144 if (!(o instanceof TransformationResult)) { 145 return false; 146 } 147 TransformationResult result = (TransformationResult) o; 148 return durationMs == result.durationMs 149 && fileSizeBytes == result.fileSizeBytes 150 && averageAudioBitrate == result.averageAudioBitrate 151 && averageVideoBitrate == result.averageVideoBitrate 152 && videoFrameCount == result.videoFrameCount; 153 } 154 155 @Override hashCode()156 public int hashCode() { 157 int result = (int) durationMs; 158 result = 31 * result + (int) fileSizeBytes; 159 result = 31 * result + averageAudioBitrate; 160 result = 31 * result + averageVideoBitrate; 161 result = 31 * result + videoFrameCount; 162 return result; 163 } 164 } 165