Add setting final progress at export error or cancellation

The change includes setting the final progress percentage when the export completes with error or is cancelled. The value for the progress is collected by calling `Transformer.getProgress()` before `transformerInternal` is set to null.

PiperOrigin-RevId: 713227568
This commit is contained in:
shahddaghash 2025-01-08 03:21:18 -08:00 committed by Copybara-Service
parent d894217a4e
commit 1bea11637b
2 changed files with 46 additions and 14 deletions

View File

@ -25,6 +25,7 @@ import android.media.metrics.MediaMetricsManager;
import android.util.SparseIntArray;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.media3.common.C;
import androidx.media3.common.util.SystemClock;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@ -131,25 +132,43 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
editingSession.close();
}
/** Called when export completes with an error. */
public void onExportError(ExportException exportException) {
/**
* Called when export completes with an error.
*
* @param progressPercentage The progress of the export operation in percent. Value is {@link
* C#PERCENTAGE_UNSET} if unknown or between 0 and 100 inclusive.
* @param exportException The {@link ExportException} describing the exception.
*/
public void onExportError(int progressPercentage, ExportException exportException) {
if (editingSession == null) {
return;
}
editingSession.reportEditingEndedEvent(
EditingEndedEvent.Builder editingEndedEventBuilder =
createEditingEndedEventBuilder(EditingEndedEvent.FINAL_STATE_ERROR)
.setErrorCode(getEditingEndedEventErrorCode(exportException.errorCode))
.build());
.setErrorCode(getEditingEndedEventErrorCode(exportException.errorCode));
if (progressPercentage != C.PERCENTAGE_UNSET) {
editingEndedEventBuilder.setFinalProgressPercent(progressPercentage);
}
editingSession.reportEditingEndedEvent(editingEndedEventBuilder.build());
editingSession.close();
}
/** Called when export is cancelled. */
public void onExportCancelled() {
/**
* Called when export is cancelled.
*
* @param progressPercentage The progress of the export operation in percent. Value is {@link
* C#PERCENTAGE_UNSET} if unknown or between 0 and 100 inclusive.
*/
public void onExportCancelled(int progressPercentage) {
if (editingSession == null) {
return;
}
editingSession.reportEditingEndedEvent(
createEditingEndedEventBuilder(EditingEndedEvent.FINAL_STATE_CANCELED).build());
EditingEndedEvent.Builder editingEndedEventBuilder =
createEditingEndedEventBuilder(EditingEndedEvent.FINAL_STATE_CANCELED);
if (progressPercentage != C.PERCENTAGE_UNSET) {
editingEndedEventBuilder.setFinalProgressPercent(progressPercentage);
}
editingSession.reportEditingEndedEvent(editingEndedEventBuilder.build());
editingSession.close();
}

View File

@ -1168,10 +1168,17 @@ public final class Transformer {
try {
transformerInternal.cancel();
} finally {
ProgressHolder progressHolder = new ProgressHolder();
int progressState = getProgress(progressHolder);
transformerInternal = null;
}
if (canCollectEditingMetrics()) {
checkNotNull(editingMetricsCollector).onExportCancelled();
int progressPercentage =
(progressState == PROGRESS_STATE_AVAILABLE)
? progressHolder.progress
: C.PERCENTAGE_UNSET;
checkNotNull(editingMetricsCollector).onExportCancelled(progressPercentage);
}
}
if (getResumeMetadataFuture != null && !getResumeMetadataFuture.isDone()) {
@ -1616,7 +1623,13 @@ public final class Transformer {
listener.onError(checkNotNull(composition), exportResultBuilder.build(), exception));
listeners.flushEvents();
if (canCollectEditingMetrics()) {
checkNotNull(editingMetricsCollector).onExportError(exception);
ProgressHolder progressHolder = new ProgressHolder();
int progressState = getProgress(progressHolder);
int progressPercentage =
(progressState == PROGRESS_STATE_AVAILABLE)
? progressHolder.progress
: C.PERCENTAGE_UNSET;
checkNotNull(editingMetricsCollector).onExportError(progressPercentage, exception);
}
transformerState = TRANSFORMER_STATE_PROCESS_FULL_INPUT;
}
@ -1691,8 +1704,8 @@ public final class Transformer {
}
exportResultBuilder.setExportException(exportException);
transformerInternal = null;
onExportCompletedWithError(exportException);
transformerInternal = null;
}
// MuxerWrapper.Listener implementation