Skip to content

Commit

Permalink
Add audio and video values from Format to TransformationResult.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 500789076
  • Loading branch information
Samrobbo authored and christosts committed Jan 10, 2023
1 parent 8ad26c0 commit 8cac27a
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -194,7 +195,10 @@ public JSONObject asJsonObject() throws JSONException {
if (transformationResult.averageVideoBitrate != C.RATE_UNSET_INT) {
jsonObject.put("averageVideoBitrate", transformationResult.averageVideoBitrate);
}
if (transformationResult.durationMs != C.LENGTH_UNSET) {
if (transformationResult.channelCount != C.LENGTH_UNSET) {
jsonObject.put("channelCount", transformationResult.channelCount);
}
if (transformationResult.durationMs != C.TIME_UNSET) {
jsonObject.put("durationMs", transformationResult.durationMs);
}
if (elapsedTimeMs != C.TIME_UNSET) {
Expand All @@ -203,6 +207,15 @@ public JSONObject asJsonObject() throws JSONException {
if (transformationResult.fileSizeBytes != C.LENGTH_UNSET) {
jsonObject.put("fileSizeBytes", transformationResult.fileSizeBytes);
}
if (transformationResult.height != C.LENGTH_UNSET) {
jsonObject.put("height", transformationResult.height);
}
if (transformationResult.pcmEncoding != Format.NO_VALUE) {
jsonObject.put("pcmEncoding", transformationResult.pcmEncoding);
}
if (transformationResult.sampleRate != C.RATE_UNSET_INT) {
jsonObject.put("sampleRate", transformationResult.sampleRate);
}
if (ssim != TransformationTestResult.SSIM_UNSET) {
jsonObject.put("ssim", ssim);
}
Expand All @@ -212,6 +225,9 @@ public JSONObject asJsonObject() throws JSONException {
if (transformationResult.videoFrameCount > 0) {
jsonObject.put("videoFrameCount", transformationResult.videoFrameCount);
}
if (transformationResult.width != C.LENGTH_UNSET) {
jsonObject.put("width", transformationResult.width);
}
return jsonObject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@

import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.Objects;

/** Information about the result of a transformation. */
public final class TransformationResult {

/** A builder for {@link TransformationResult} instances. */
public static final class Builder {
private long durationMs;
private long fileSizeBytes;
private int averageAudioBitrate;
private int channelCount;
private @C.PcmEncoding int pcmEncoding;
private int sampleRate;
@Nullable private String audioDecoderName;
@Nullable private String audioEncoderName;
private int averageVideoBitrate;
private int height;
private int width;
private int videoFrameCount;
@Nullable private String videoDecoderName;
@Nullable private String videoEncoderName;
Expand All @@ -42,7 +47,12 @@ public Builder() {
durationMs = C.TIME_UNSET;
fileSizeBytes = C.LENGTH_UNSET;
averageAudioBitrate = C.RATE_UNSET_INT;
channelCount = C.LENGTH_UNSET;
pcmEncoding = Format.NO_VALUE;
sampleRate = C.RATE_UNSET_INT;
averageVideoBitrate = C.RATE_UNSET_INT;
height = C.LENGTH_UNSET;
width = C.LENGTH_UNSET;
}

/**
Expand Down Expand Up @@ -81,6 +91,37 @@ public Builder setAverageAudioBitrate(int averageAudioBitrate) {
return this;
}

/**
* Sets the channel count.
*
* <p>Must be positive or {@link C#LENGTH_UNSET}.
*/
@CanIgnoreReturnValue
public Builder setChannelCount(int channelCount) {
checkArgument(channelCount > 0 || channelCount == C.LENGTH_UNSET);
this.channelCount = channelCount;
return this;
}

/** Sets the {@link C.PcmEncoding}. */
@CanIgnoreReturnValue
public Builder setPcmEncoding(@C.PcmEncoding int pcmEncoding) {
this.pcmEncoding = pcmEncoding;
return this;
}

/**
* Sets the sample rate.
*
* <p>Must be positive or {@link C#RATE_UNSET_INT}.
*/
@CanIgnoreReturnValue
public Builder setSampleRate(int sampleRate) {
checkArgument(sampleRate > 0 || sampleRate == C.RATE_UNSET_INT);
this.sampleRate = sampleRate;
return this;
}

/** Sets the name of the audio decoder used. */
@CanIgnoreReturnValue
public Builder setAudioDecoderName(@Nullable String audioDecoderName) {
Expand All @@ -107,6 +148,30 @@ public Builder setAverageVideoBitrate(int averageVideoBitrate) {
return this;
}

/**
* Sets the height.
*
* <p>Must be positive or {@link C#LENGTH_UNSET}.
*/
@CanIgnoreReturnValue
public Builder setHeight(int height) {
checkArgument(height > 0 || height == C.LENGTH_UNSET);
this.height = height;
return this;
}

/**
* Sets the width.
*
* <p>Must be positive or {@link C#LENGTH_UNSET}.
*/
@CanIgnoreReturnValue
public Builder setWidth(int width) {
checkArgument(width > 0 || width == C.LENGTH_UNSET);
this.width = width;
return this;
}

/**
* Sets the number of video frames.
*
Expand Down Expand Up @@ -146,9 +211,14 @@ public TransformationResult build() {
durationMs,
fileSizeBytes,
averageAudioBitrate,
channelCount,
pcmEncoding,
sampleRate,
audioDecoderName,
audioEncoderName,
averageVideoBitrate,
height,
width,
videoFrameCount,
videoDecoderName,
videoEncoderName,
Expand All @@ -165,6 +235,12 @@ public TransformationResult build() {
* The average bitrate of the audio track data, or {@link C#RATE_UNSET_INT} if unset or unknown.
*/
public final int averageAudioBitrate;
/** The channel count of the audio, or {@link C#LENGTH_UNSET} if unset or unknown. */
public final int channelCount;
/* The {@link C.PcmEncoding} of the audio, or {@link Format#NO_VALUE} if unset or unknown. */
public final @C.PcmEncoding int pcmEncoding;
/** The sample rate of the audio, or {@link C#RATE_UNSET_INT} if unset or unknown. */
public final int sampleRate;
/** The name of the audio decoder used, or {@code null} if none were used. */
@Nullable public final String audioDecoderName;
/** The name of the audio encoder used, or {@code null} if none were used. */
Expand All @@ -174,6 +250,10 @@ public TransformationResult build() {
* The average bitrate of the video track data, or {@link C#RATE_UNSET_INT} if unset or unknown.
*/
public final int averageVideoBitrate;
/** The height of the video, or {@link C#LENGTH_UNSET} if unset or unknown. */
public final int height;
/** The width of the video, or {@link C#LENGTH_UNSET} if unset or unknown. */
public final int width;
/** The number of video frames. */
public final int videoFrameCount;
/** The name of the video decoder used, or {@code null} if none were used. */
Expand All @@ -191,19 +271,29 @@ private TransformationResult(
long durationMs,
long fileSizeBytes,
int averageAudioBitrate,
int channelCount,
@C.PcmEncoding int pcmEncoding,
int sampleRate,
@Nullable String audioDecoderName,
@Nullable String audioEncoderName,
int averageVideoBitrate,
int height,
int width,
int videoFrameCount,
@Nullable String videoDecoderName,
@Nullable String videoEncoderName,
@Nullable TransformationException transformationException) {
this.durationMs = durationMs;
this.fileSizeBytes = fileSizeBytes;
this.averageAudioBitrate = averageAudioBitrate;
this.channelCount = channelCount;
this.pcmEncoding = pcmEncoding;
this.sampleRate = sampleRate;
this.audioDecoderName = audioDecoderName;
this.audioEncoderName = audioEncoderName;
this.averageVideoBitrate = averageVideoBitrate;
this.height = height;
this.width = width;
this.videoFrameCount = videoFrameCount;
this.videoDecoderName = videoDecoderName;
this.videoEncoderName = videoEncoderName;
Expand All @@ -215,9 +305,14 @@ public Builder buildUpon() {
.setDurationMs(durationMs)
.setFileSizeBytes(fileSizeBytes)
.setAverageAudioBitrate(averageAudioBitrate)
.setChannelCount(channelCount)
.setPcmEncoding(pcmEncoding)
.setSampleRate(sampleRate)
.setAudioDecoderName(audioDecoderName)
.setAudioEncoderName(audioEncoderName)
.setAverageVideoBitrate(averageVideoBitrate)
.setHeight(height)
.setWidth(width)
.setVideoFrameCount(videoFrameCount)
.setVideoDecoderName(videoDecoderName)
.setVideoEncoderName(videoEncoderName)
Expand All @@ -236,9 +331,14 @@ public boolean equals(@Nullable Object o) {
return durationMs == result.durationMs
&& fileSizeBytes == result.fileSizeBytes
&& averageAudioBitrate == result.averageAudioBitrate
&& channelCount == result.channelCount
&& pcmEncoding == result.pcmEncoding
&& sampleRate == result.sampleRate
&& Objects.equals(audioDecoderName, result.audioDecoderName)
&& Objects.equals(audioEncoderName, result.audioEncoderName)
&& averageVideoBitrate == result.averageVideoBitrate
&& height == result.height
&& width == result.width
&& videoFrameCount == result.videoFrameCount
&& Objects.equals(videoDecoderName, result.videoDecoderName)
&& Objects.equals(videoEncoderName, result.videoEncoderName)
Expand All @@ -250,9 +350,14 @@ public int hashCode() {
int result = (int) durationMs;
result = 31 * result + (int) fileSizeBytes;
result = 31 * result + averageAudioBitrate;
result = 31 * result + channelCount;
result = 31 * result + pcmEncoding;
result = 31 * result + sampleRate;
result = 31 * result + Objects.hashCode(audioDecoderName);
result = 31 * result + Objects.hashCode(audioEncoderName);
result = 31 * result + averageVideoBitrate;
result = 31 * result + height;
result = 31 * result + width;
result = 31 * result + videoFrameCount;
result = 31 * result + Objects.hashCode(videoDecoderName);
result = 31 * result + Objects.hashCode(videoEncoderName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,25 @@ public SampleConsumer onTrackAdded(
public void onTrackEnded(
@C.TrackType int trackType, Format format, int averageBitrate, int sampleCount) {
if (trackType == C.TRACK_TYPE_AUDIO) {
transformationResultBuilder.setAverageAudioBitrate(averageBitrate);
transformationResultBuilder
.setAverageAudioBitrate(averageBitrate)
.setPcmEncoding(format.pcmEncoding);
if (format.channelCount != Format.NO_VALUE) {
transformationResultBuilder.setChannelCount(format.channelCount);
}
if (format.sampleRate != Format.NO_VALUE) {
transformationResultBuilder.setSampleRate(format.sampleRate);
}
} else if (trackType == C.TRACK_TYPE_VIDEO) {
transformationResultBuilder
.setVideoFrameCount(sampleCount)
.setAverageVideoBitrate(averageBitrate);
.setAverageVideoBitrate(averageBitrate)
.setVideoFrameCount(sampleCount);
if (format.height != Format.NO_VALUE) {
transformationResultBuilder.setHeight(format.height);
}
if (format.width != Format.NO_VALUE) {
transformationResultBuilder.setWidth(format.width);
}
}
}

Expand Down

0 comments on commit 8cac27a

Please sign in to comment.