Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8fd758e5 authored by Hangyu Kuang's avatar Hangyu Kuang Committed by Android (Google) Code Review
Browse files

Merge "TranscodingApi: Update job status upon receiving callback."

parents 8c96ba6e 4ccf2983
Loading
Loading
Loading
Loading
+27 −5
Original line number Diff line number Diff line
@@ -244,7 +244,7 @@ public final class MediaTranscodeManager {
            }

            // Updates the job progress.
            job.setJobProgress(newProgress);
            job.updateProgress(newProgress);

            // Notifies client the progress update.
            if (job.mProgressUpdateExecutor != null && job.mProgressUpdateListener != null) {
@@ -254,6 +254,21 @@ public final class MediaTranscodeManager {
        }
    }

    private void updateStatus(int jobId, int status) {
        synchronized (mPendingTranscodingJobs) {
            final TranscodingJob job = mPendingTranscodingJobs.get(jobId);

            if (job == null) {
                // This should not happen in reality.
                Log.e(TAG, "Job " + jobId + " is not in PendingJobs");
                return;
            }

            // Updates the job status.
            job.updateStatus(status);
        }
    }

    // Just forwards all the events to the event handler.
    private ITranscodingClientCallback mTranscodingClientCallback =
            new ITranscodingClientCallback.Stub() {
@@ -285,17 +300,17 @@ public final class MediaTranscodeManager {

                @Override
                public void onTranscodingStarted(int jobId) throws RemoteException {

                    updateStatus(jobId, TranscodingJob.STATUS_RUNNING);
                }

                @Override
                public void onTranscodingPaused(int jobId) throws RemoteException {

                    updateStatus(jobId, TranscodingJob.STATUS_PAUSED);
                }

                @Override
                public void onTranscodingResumed(int jobId) throws RemoteException {

                    updateStatus(jobId, TranscodingJob.STATUS_RUNNING);
                }

                @Override
@@ -683,11 +698,14 @@ public final class MediaTranscodeManager {
        public static final int STATUS_RUNNING = 2;
        /** The job is finished. */
        public static final int STATUS_FINISHED = 3;
        /** The job is paused. */
        public static final int STATUS_PAUSED = 4;

        @IntDef(prefix = { "STATUS_" }, value = {
                STATUS_PENDING,
                STATUS_RUNNING,
                STATUS_FINISHED,
                STATUS_PAUSED,
        })
        @Retention(RetentionPolicy.SOURCE)
        public @interface Status {}
@@ -843,9 +861,13 @@ public final class MediaTranscodeManager {
            return mResult;
        }

        private synchronized void setJobProgress(int newProgress) {
        private synchronized void updateProgress(int newProgress) {
            mProgress = newProgress;
        }

        private synchronized void updateStatus(int newStatus) {
            mStatus = newStatus;
        }
    }

    /**
+11 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
@@ -238,6 +239,7 @@ public class MediaTranscodeManagerTest
        Log.d(TAG, "Starting: testMediaTranscodeManager");

        Semaphore transcodeCompleteSemaphore = new Semaphore(0);
        final CountDownLatch statusLatch = new CountDownLatch(1);

        // Create a file Uri: file:///data/user/0/com.android.mediatranscodingtest/cache/temp.mp4
        // The full path of this file is:
@@ -276,12 +278,21 @@ public class MediaTranscodeManagerTest
                    public void onProgressUpdate(int newProgress) {
                        assertTrue("Invalid proress update", newProgress > mPreviousProgress);
                        assertTrue("Invalid proress update", newProgress <= 100);
                        if (newProgress > 0) {
                            statusLatch.countDown();
                        }
                        mPreviousProgress = newProgress;
                        progressUpdateCount.getAndIncrement();
                        Log.i(TAG, "Get progress update " + newProgress);
                    }
                });

        try {
            statusLatch.await();
            // The transcoding should not be finished yet as the clip is long.
            assertTrue("Invalid status", job.getStatus() == TranscodingJob.STATUS_RUNNING);
        } catch (InterruptedException e) { }

        Log.d(TAG, "testMediaTranscodeManager - Waiting for transcode to cancel.");
        boolean finishedOnTime = transcodeCompleteSemaphore.tryAcquire(
                TRANSCODE_TIMEOUT_SECONDS, TimeUnit.SECONDS);