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

Commit 9185c90a authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Transcoding API: Add support for progress update."

parents 9e07b814 be355a31
Loading
Loading
Loading
Loading
+27 −14
Original line number Diff line number Diff line
@@ -225,13 +225,35 @@ public final class MediaTranscodeManager {
            job.updateStatusAndResult(TranscodingJob.STATUS_FINISHED,
                    TranscodingJob.RESULT_ERROR);

            // Notifies client the job is done.
            // Notifies client the job failed.
            if (job.mListener != null && job.mListenerExecutor != null) {
                job.mListenerExecutor.execute(() -> job.mListener.onTranscodingFinished(job));
            }
        }
    }

    private void handleTranscodingProgressUpdate(int jobId, int newProgress) {
        synchronized (mPendingTranscodingJobs) {
            // Gets the job associated with the jobId.
            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 progress.
            job.setJobProgress(newProgress);

            // Notifies client the progress update.
            if (job.mProgressUpdateExecutor != null && job.mProgressUpdateListener != null) {
                job.mProgressUpdateExecutor.execute(
                        () -> job.mProgressUpdateListener.onProgressUpdate(newProgress));
            }
        }
    }

    // Just forwards all the events to the event handler.
    private ITranscodingClientCallback mTranscodingClientCallback =
            new ITranscodingClientCallback.Stub() {
@@ -294,8 +316,8 @@ public final class MediaTranscodeManager {
                }

                @Override
                public void onProgressUpdate(int jobId, int progress) throws RemoteException {
                    //TODO(hkuang): Implement this.
                public void onProgressUpdate(int jobId, int newProgress) throws RemoteException {
                    handleTranscodingProgressUpdate(jobId, newProgress);
                }
            };

@@ -821,18 +843,9 @@ public final class MediaTranscodeManager {
            return mResult;
        }

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

            // Notify listener.
            OnProgressUpdateListener onProgressUpdateListener = mProgressUpdateListener;
            if (mProgressUpdateListener != null) {
                mProgressUpdateExecutor.execute(
                        () -> onProgressUpdateListener.onProgressUpdate(mProgress));
            }
        }
    }

    /**
+59 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/*
 * Functional tests for MediaTranscodeManager in the media framework.
@@ -230,5 +231,63 @@ public class MediaTranscodeManagerTest
                30, TimeUnit.MILLISECONDS);
        assertTrue("Fails to cancel transcoding", finishedOnTime);
    }


    @Test
    public void testTranscodingProgressUpdate() throws Exception {
        Log.d(TAG, "Starting: testMediaTranscodeManager");

        Semaphore transcodeCompleteSemaphore = new Semaphore(0);

        // Create a file Uri: file:///data/user/0/com.android.mediatranscodingtest/cache/temp.mp4
        // The full path of this file is:
        // /data/user/0/com.android.mediatranscodingtest/cache/temp.mp4
        Uri destinationUri = Uri.parse(ContentResolver.SCHEME_FILE + "://"
                + mContext.getCacheDir().getAbsolutePath() + "/HevcTranscode.mp4");

        TranscodingRequest request =
                new TranscodingRequest.Builder()
                        .setSourceUri(mSourceHEVCVideoUri)
                        .setDestinationUri(destinationUri)
                        .setType(MediaTranscodeManager.TRANSCODING_TYPE_VIDEO)
                        .setPriority(MediaTranscodeManager.PRIORITY_REALTIME)
                        .setVideoTrackFormat(createMediaFormat())
                        .build();
        Executor listenerExecutor = Executors.newSingleThreadExecutor();

        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);
                    transcodeCompleteSemaphore.release();
                });
        assertNotNull(job);

        AtomicInteger progressUpdateCount = new AtomicInteger(0);

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

                    @Override
                    public void onProgressUpdate(int newProgress) {
                        assertTrue("Invalid proress update", newProgress > mPreviousProgress);
                        assertTrue("Invalid proress update", newProgress <= 100);
                        mPreviousProgress = newProgress;
                        progressUpdateCount.getAndIncrement();
                        Log.i(TAG, "Get progress update " + newProgress);
                    }
                });

        Log.d(TAG, "testMediaTranscodeManager - Waiting for transcode to cancel.");
        boolean finishedOnTime = transcodeCompleteSemaphore.tryAcquire(
                TRANSCODE_TIMEOUT_SECONDS, TimeUnit.SECONDS);
        assertTrue("Transcode failed to complete in time.", finishedOnTime);
        assertTrue("Failed to receive at least 10 progress updates",
                progressUpdateCount.get() > 10);
    }
}