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

Commit 7ee8e7fe authored by hkuang's avatar hkuang Committed by Hangyu Kuang
Browse files

Update TranscodingJob to TranscodingSession.

No functional change.

bug: 154733526
Test: Unit test
Change-Id: Iaf76ea902b46c127ab7474486de6ddecbd9a8589
parent 3d5a1838
Loading
Loading
Loading
Loading
+32 −25
Original line number Diff line number Diff line
@@ -20,8 +20,8 @@ import android.content.ContentResolver;
import android.content.Context;
import android.media.MediaFormat;
import android.media.MediaTranscodeManager;
import android.media.MediaTranscodeManager.TranscodingJob;
import android.media.MediaTranscodeManager.TranscodingRequest;
import android.media.MediaTranscodeManager.TranscodingSession;
import android.media.MediaTranscodingException;
import android.net.Uri;
import android.os.Bundle;
@@ -198,7 +198,7 @@ public class MediaTranscodeManagerDiedTest
        }

        Semaphore transcodeCompleteSemaphore = new Semaphore(0);
        Semaphore jobStartedSemaphore = new Semaphore(0);
        Semaphore sessionStartedSemaphore = new Semaphore(0);

        // Transcode a 15 seconds video, so that the transcoding is not finished when we kill the
        // service.
@@ -219,47 +219,52 @@ public class MediaTranscodeManagerDiedTest

        Log.i(TAG, "transcoding to " + createMediaFormat());

        TranscodingJob job = mMediaTranscodeManager.enqueueRequest(request, listenerExecutor,
                transcodingJob -> {
                    Log.d(TAG, "Transcoding completed with result: " + transcodingJob.getResult());
        TranscodingSession session = mMediaTranscodeManager.enqueueRequest(request,
                listenerExecutor,
                TranscodingSession -> {
                    Log.d(TAG,
                            "Transcoding completed with result: " + TranscodingSession.getResult());
                    transcodeCompleteSemaphore.release();
                });
        assertNotNull(job);
        assertNotNull(session);

        AtomicInteger progressUpdateCount = new AtomicInteger(0);

        // Set progress update executor and use the same executor as result listener.
        job.setOnProgressUpdateListener(listenerExecutor,
                new TranscodingJob.OnProgressUpdateListener() {
        session.setOnProgressUpdateListener(listenerExecutor,
                new TranscodingSession.OnProgressUpdateListener() {
                    @Override
                    public void onProgressUpdate(TranscodingJob job, int newProgress) {
                    public void onProgressUpdate(TranscodingSession session, int newProgress) {
                        if (newProgress > 0) {
                            jobStartedSemaphore.release();
                            sessionStartedSemaphore.release();
                        }
                    }
                });

        // Wait for progress update so the job is in running state.
        jobStartedSemaphore.tryAcquire(TRANSCODE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
        assertTrue("Job is not running", job.getStatus() == TranscodingJob.STATUS_RUNNING);
        // Wait for progress update so the session is in running state.
        sessionStartedSemaphore.tryAcquire(TRANSCODE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
        assertTrue("session is not running",
                session.getStatus() == TranscodingSession.STATUS_RUNNING);

        // Kills the service and expects receiving failure of the job.
        // Kills the service and expects receiving failure of the session.
        executeShellCommand("pkill -f media.transcoding");

        Log.d(TAG, "testMediaTranscodeManager - Waiting for transcode result.");
        boolean finishedOnTime = transcodeCompleteSemaphore.tryAcquire(
                TRANSCODE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
        assertTrue("Invalid job status", job.getStatus() == TranscodingJob.STATUS_FINISHED);
        assertTrue("Invalid job result", job.getResult()== TranscodingJob.RESULT_ERROR);
        assertTrue("Invalid session status",
                session.getStatus() == TranscodingSession.STATUS_FINISHED);
        assertTrue("Invalid session result",
                session.getResult() == TranscodingSession.RESULT_ERROR);


        boolean retryJob = false;
        boolean retrysession = false;
        // Wait till service is available again.
        Log.d(TAG, "Retry the failed transcoding job");
        while (!retryJob) {
        Log.d(TAG, "Retry the failed transcoding session");
        while (!retrysession) {
            try {
                job.retry();
                // Break out when job retry succeeds.
                session.retry();
                // Break out when session retry succeeds.
                break;
            } catch (MediaTranscodingException.ServiceNotAvailableException ex) {
                // Sleep for 10 milliseconds to wait.
@@ -269,9 +274,11 @@ public class MediaTranscodeManagerDiedTest

        finishedOnTime = transcodeCompleteSemaphore.tryAcquire(
                TRANSCODE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
        // Check the make sure job is successfully finished after retry.
        assertTrue("Invalid job status", job.getStatus() == TranscodingJob.STATUS_FINISHED);
        assertTrue("Invalid job result", job.getResult() == TranscodingJob.RESULT_SUCCESS);
        // Check the make sure session is successfully finished after retry.
        assertTrue("Invalid session status",
                session.getStatus() == TranscodingSession.STATUS_FINISHED);
        assertTrue("Invalid session result",
                session.getResult() == TranscodingSession.RESULT_SUCCESS);
    }
}
+47 −36
Original line number Diff line number Diff line
@@ -22,9 +22,9 @@ import android.content.ContentResolver;
import android.content.Context;
import android.media.MediaFormat;
import android.media.MediaTranscodeManager;
import android.media.MediaTranscodeManager.TranscodingJob;
import android.media.MediaTranscodeManager.TranscodingRequest;
import android.media.MediaTranscodeManager.TranscodingRequest.MediaFormatResolver;
import android.media.MediaTranscodeManager.TranscodingSession;
import android.media.TranscodingTestConfig;
import android.net.Uri;
import android.os.Bundle;
@@ -337,23 +337,25 @@ public class MediaTranscodeManagerTest
                        .build();
        Executor listenerExecutor = Executors.newSingleThreadExecutor();

        TranscodingJob job = mMediaTranscodeManager.enqueueRequest(request, listenerExecutor,
                transcodingJob -> {
                    Log.d(TAG, "Transcoding completed with result: " + transcodingJob.getResult());
        TranscodingSession session = mMediaTranscodeManager.enqueueRequest(request,
                listenerExecutor,
                TranscodingSession -> {
                    Log.d(TAG,
                            "Transcoding completed with result: " + TranscodingSession.getResult());
                    assertTrue("Transcoding should failed.",
                            transcodingJob.getResult() == expectedResult);
                            TranscodingSession.getResult() == expectedResult);
                    transcodeCompleteSemaphore.release();
                });
        assertNotNull(job);
        assertNotNull(session);

        if (job != null) {
        if (session != null) {
            Log.d(TAG, "testMediaTranscodeManager - Waiting for transcode to complete.");
            boolean finishedOnTime = transcodeCompleteSemaphore.tryAcquire(
                    TRANSCODE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
            assertTrue("Transcode failed to complete in time.", finishedOnTime);
        }

        if (expectedResult == TranscodingJob.RESULT_SUCCESS) {
        if (expectedResult == TranscodingSession.RESULT_SUCCESS) {
            // Checks the destination file get generated.
            File file = new File(dstUri.getPath());
            assertTrue("Failed to create destination file", file.exists());
@@ -379,7 +381,8 @@ public class MediaTranscodeManagerTest
                + mContext.getPackageName() + "/temp.mp4");
        Log.d(TAG, "Transcoding to destination: " + destinationUri);

        testTranscodingWithExpectResult(invalidSrcUri, destinationUri, TranscodingJob.RESULT_ERROR);
        testTranscodingWithExpectResult(invalidSrcUri, destinationUri,
                TranscodingSession.RESULT_ERROR);
    }

    // Tests transcoding to a uri in res folder and expects failure as we could not write to res
@@ -396,7 +399,7 @@ public class MediaTranscodeManagerTest
        Log.d(TAG, "Transcoding to destination: " + destinationUri);

        testTranscodingWithExpectResult(mSourceHEVCVideoUri, destinationUri,
                TranscodingJob.RESULT_ERROR);
                TranscodingSession.RESULT_ERROR);
    }

    // Tests transcoding to a uri in internal storage folder and expects success.
@@ -411,7 +414,7 @@ public class MediaTranscodeManagerTest
                + mContext.getCacheDir().getAbsolutePath() + "/temp.mp4");

        testTranscodingWithExpectResult(mSourceHEVCVideoUri, destinationUri,
                TranscodingJob.RESULT_SUCCESS);
                TranscodingSession.RESULT_SUCCESS);
    }

    // Tests transcoding to a uri in internal files directory and expects success.
@@ -425,7 +428,7 @@ public class MediaTranscodeManagerTest
        Log.i(TAG, "Transcoding to files dir: " + destinationUri);

        testTranscodingWithExpectResult(mSourceHEVCVideoUri, destinationUri,
                TranscodingJob.RESULT_SUCCESS);
                TranscodingSession.RESULT_SUCCESS);
    }

    // Tests transcoding to a uri in external files directory and expects success.
@@ -438,7 +441,7 @@ public class MediaTranscodeManagerTest
        Log.i(TAG, "Transcoding to files dir: " + destinationUri);

        testTranscodingWithExpectResult(mSourceHEVCVideoUri, destinationUri,
                TranscodingJob.RESULT_SUCCESS);
                TranscodingSession.RESULT_SUCCESS);
    }

    @Test
@@ -481,15 +484,17 @@ public class MediaTranscodeManagerTest

        Log.i(TAG, "transcoding to " + videoTrackFormat);

        TranscodingJob job = mMediaTranscodeManager.enqueueRequest(request, listenerExecutor,
                transcodingJob -> {
                    Log.d(TAG, "Transcoding completed with result: " + transcodingJob.getResult());
                    assertEquals(transcodingJob.getResult(), TranscodingJob.RESULT_SUCCESS);
        TranscodingSession session = mMediaTranscodeManager.enqueueRequest(request,
                listenerExecutor,
                TranscodingSession -> {
                    Log.d(TAG,
                            "Transcoding completed with result: " + TranscodingSession.getResult());
                    assertEquals(TranscodingSession.getResult(), TranscodingSession.RESULT_SUCCESS);
                    transcodeCompleteSemaphore.release();
                });
        assertNotNull(job);
        assertNotNull(session);

        if (job != null) {
        if (session != null) {
            Log.d(TAG, "testMediaTranscodeManager - Waiting for transcode to cancel.");
            boolean finishedOnTime = transcodeCompleteSemaphore.tryAcquire(
                    TRANSCODE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
@@ -527,18 +532,21 @@ public class MediaTranscodeManagerTest
                        .build();
        Executor listenerExecutor = Executors.newSingleThreadExecutor();

        TranscodingJob job = mMediaTranscodeManager.enqueueRequest(request, listenerExecutor,
                transcodingJob -> {
                    Log.d(TAG, "Transcoding completed with result: " + transcodingJob.getResult());
                    assertEquals(transcodingJob.getResult(), TranscodingJob.RESULT_CANCELED);
        TranscodingSession session = mMediaTranscodeManager.enqueueRequest(request,
                listenerExecutor,
                TranscodingSession -> {
                    Log.d(TAG,
                            "Transcoding completed with result: " + TranscodingSession.getResult());
                    assertEquals(TranscodingSession.getResult(),
                            TranscodingSession.RESULT_CANCELED);
                    transcodeCompleteSemaphore.release();
                });
        assertNotNull(job);
        assertNotNull(session);

        // TODO(hkuang): Wait for progress update before calling cancel to make sure transcoding is
        // started.

        job.cancel();
        session.cancel();
        Log.d(TAG, "testMediaTranscodeManager - Waiting for transcode to cancel.");
        boolean finishedOnTime = transcodeCompleteSemaphore.tryAcquire(
                30, TimeUnit.MILLISECONDS);
@@ -571,23 +579,25 @@ public class MediaTranscodeManagerTest

        Log.i(TAG, "transcoding to " + createMediaFormat());

        TranscodingJob job = mMediaTranscodeManager.enqueueRequest(request, listenerExecutor,
                transcodingJob -> {
                    Log.d(TAG, "Transcoding completed with result: " + transcodingJob.getResult());
                    assertEquals(transcodingJob.getResult(), TranscodingJob.RESULT_SUCCESS);
        TranscodingSession session = mMediaTranscodeManager.enqueueRequest(request,
                listenerExecutor,
                TranscodingSession -> {
                    Log.d(TAG,
                            "Transcoding completed with result: " + TranscodingSession.getResult());
                    assertEquals(TranscodingSession.getResult(), TranscodingSession.RESULT_SUCCESS);
                    transcodeCompleteSemaphore.release();
                });
        assertNotNull(job);
        assertNotNull(session);

        AtomicInteger progressUpdateCount = new AtomicInteger(0);

        // Set progress update executor and use the same executor as result listener.
        job.setOnProgressUpdateListener(listenerExecutor,
                new TranscodingJob.OnProgressUpdateListener() {
        session.setOnProgressUpdateListener(listenerExecutor,
                new TranscodingSession.OnProgressUpdateListener() {
                    int mPreviousProgress = 0;

                    @Override
                    public void onProgressUpdate(TranscodingJob job, int newProgress) {
                    public void onProgressUpdate(TranscodingSession session, int newProgress) {
                        assertTrue("Invalid proress update", newProgress > mPreviousProgress);
                        assertTrue("Invalid proress update", newProgress <= 100);
                        if (newProgress > 0) {
@@ -602,8 +612,9 @@ public class MediaTranscodeManagerTest
        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) { }
            assertTrue("Invalid status", session.getStatus() == TranscodingSession.STATUS_RUNNING);
        } catch (InterruptedException e) {
        }

        Log.d(TAG, "testMediaTranscodeManager - Waiting for transcode to cancel.");
        boolean finishedOnTime = transcodeCompleteSemaphore.tryAcquire(
+10 −7
Original line number Diff line number Diff line
@@ -20,8 +20,8 @@ import android.content.ContentResolver;
import android.content.Context;
import android.media.MediaFormat;
import android.media.MediaTranscodeManager;
import android.media.MediaTranscodeManager.TranscodingJob;
import android.media.MediaTranscodeManager.TranscodingRequest;
import android.media.MediaTranscodeManager.TranscodingSession;
import android.media.MediaTranscodingException;
import android.net.Uri;
import android.test.ActivityInstrumentationTestCase2;
@@ -129,17 +129,20 @@ public class MediaTranscodingBenchmark
            Executor listenerExecutor = Executors.newSingleThreadExecutor();

            long startTimeMs = System.currentTimeMillis();
            TranscodingJob job = mMediaTranscodeManager.enqueueRequest(request, listenerExecutor,
                    transcodingJob -> {
            TranscodingSession session = mMediaTranscodeManager.enqueueRequest(request,
                    listenerExecutor,
                    TranscodingSession -> {
                        Log.d(TAG,
                                "Transcoding completed with result: " + transcodingJob.getResult());
                        assertEquals(transcodingJob.getResult(), TranscodingJob.RESULT_SUCCESS);
                                "Transcoding completed with result: "
                                        + TranscodingSession.getResult());
                        assertEquals(TranscodingSession.getResult(),
                                TranscodingSession.RESULT_SUCCESS);
                        transcodingTime.set(System.currentTimeMillis() - startTimeMs);
                        totalTimeMs.addAndGet(transcodingTime.get());
                        transcodeCompleteSemaphore.release();
                    });

            if (job != null) {
            if (session != null) {
                Log.d(TAG, "testMediaTranscodeManager - Waiting for transcode to complete.");
                boolean finishedOnTime = transcodeCompleteSemaphore.tryAcquire(
                        timeoutSeconds, TimeUnit.SECONDS);