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

Commit bd12a846 authored by hkuang's avatar hkuang
Browse files

transcoding: Address API council comments Part2

1. Create base class MediaFormat resolver for VideoFormatResolver
and upcoming ImageFormatResolver in T.
2. Move the setter of client capabilities and video format hint into
VideoFormatResolver's constructor.
3. Remove minProgressUpdateInterval as it is not implemented yet.

Bug: 181551684
Test: atest  CtsMediaTranscodingTestCases:MediaTranscodeManagerTest
Change-Id: Icefb302d6ffc22182bed6a26daca7fc621ae54c5
parent 7453bda3
Loading
Loading
Loading
Loading
+2 −5
Original line number Diff line number Diff line
@@ -18,11 +18,9 @@ package android.media {
    method @NonNull public android.net.Uri getSourceUri();
  }

  public static class MediaTranscodeManager.TranscodingRequest.MediaFormatResolver {
    ctor public MediaTranscodeManager.TranscodingRequest.MediaFormatResolver();
  public static class MediaTranscodeManager.TranscodingRequest.VideoFormatResolver {
    ctor public MediaTranscodeManager.TranscodingRequest.VideoFormatResolver(@NonNull android.media.ApplicationMediaCapabilities, @NonNull android.media.MediaFormat);
    method @Nullable public android.media.MediaFormat resolveVideoFormat();
    method @NonNull public android.media.MediaTranscodeManager.TranscodingRequest.MediaFormatResolver setClientCapabilities(@NonNull android.media.ApplicationMediaCapabilities);
    method @NonNull public android.media.MediaTranscodeManager.TranscodingRequest.MediaFormatResolver setSourceVideoFormatHint(@NonNull android.media.MediaFormat);
    method public boolean shouldTranscode();
  }

@@ -34,7 +32,6 @@ package android.media {
    method public int getSessionId();
    method public int getStatus();
    method public void setOnProgressUpdateListener(@NonNull java.util.concurrent.Executor, @Nullable android.media.MediaTranscodeManager.TranscodingSession.OnProgressUpdateListener);
    method public void setOnProgressUpdateListener(int, @NonNull java.util.concurrent.Executor, @Nullable android.media.MediaTranscodeManager.TranscodingSession.OnProgressUpdateListener);
    field public static final int ERROR_DROPPED_BY_SERVICE = 1; // 0x1
    field public static final int ERROR_NONE = 0; // 0x0
    field public static final int ERROR_SERVICE_DIED = 2; // 0x2
+70 −71
Original line number Diff line number Diff line
@@ -819,83 +819,102 @@ public final class MediaTranscodeManager {
        }

        /**
         * Helper class for deciding if transcoding is needed, and if so, the track
         * formats to use.
         * Abstract base class for all the format resolvers.
         */
        public static class MediaFormatResolver {
            private static final int BIT_RATE = 20000000;            // 20Mbps
        abstract static class MediaFormatResolver {
            private @NonNull ApplicationMediaCapabilities mClientCaps;

            private MediaFormat mSrcVideoFormatHint;
            private MediaFormat mSrcAudioFormatHint;
            private ApplicationMediaCapabilities mClientCaps;
            /**
             * Prevents public constructor access.
             */
            /* package private */ MediaFormatResolver() {
            }

            /**
             * Sets the abilities of the client consuming the media. Must be called
             * before {@link #shouldTranscode()} or {@link #resolveVideoFormat()}.
             * Constructs MediaFormatResolver object.
             *
             * @param clientCaps An ApplicationMediaCapabilities object containing the client's
             *                   capabilities.
             * @return the same VideoFormatResolver instance.
             */
            @NonNull
            public MediaFormatResolver setClientCapabilities(
                    @NonNull ApplicationMediaCapabilities clientCaps) {
            MediaFormatResolver(@NonNull ApplicationMediaCapabilities clientCaps) {
                if (clientCaps == null) {
                    throw new IllegalArgumentException("Client capabilities must not be null");
                }
                mClientCaps = clientCaps;
                return this;
            }

            /**
             * Sets the video format hint about the source. Must be called before
             * {@link #shouldTranscode()} or {@link #resolveVideoFormat()}.
             *
             * @param format A MediaFormat object containing information about the source's
             *               video track format that could affect the transcoding decision.
             *               Such information could include video codec types, color spaces,
             *               whether special format info (eg. slow-motion markers) are present,
             *               etc.. If a particular information is not present, it will not be
             *               used to make the decision.
             * @return the same MediaFormatResolver instance.
             * Returns the client capabilities.
             */
            @NonNull
            public MediaFormatResolver setSourceVideoFormatHint(@NonNull MediaFormat format) {
                mSrcVideoFormatHint = format;
                return this;
            /* package */ ApplicationMediaCapabilities getClientCapabilities() {
                return mClientCaps;
            }

            abstract boolean shouldTranscode();
        }

        /**
         * VideoFormatResolver for deciding if video transcoding is needed, and if so, the track
         * formats to use.
         */
        public static class VideoFormatResolver extends MediaFormatResolver {
            private static final int BIT_RATE = 20000000;            // 20Mbps

            private MediaFormat mSrcVideoFormatHint;
            private MediaFormat mSrcAudioFormatHint;

            /**
             * Sets the audio format hint about the source.
             * Constructs a new VideoFormatResolver object.
             *
             * @param format A MediaFormat object containing information about the source's
             *               audio track format that could affect the transcoding decision.
             * @return the same MediaFormatResolver instance.
             * @param clientCaps An ApplicationMediaCapabilities object containing the client's
             *                   capabilities.
             * @param srcVideoFormatHint A MediaFormat object containing information about the
             *                           source's video track format that could affect the
             *                           transcoding decision. Such information could include video
             *                           codec types, color spaces, whether special format info (eg.
             *                           slow-motion markers) are present, etc.. If a particular
             *                           information is not present, it will not be used to make the
             *                           decision.
             */
            public VideoFormatResolver(@NonNull ApplicationMediaCapabilities clientCaps,
                    @NonNull MediaFormat srcVideoFormatHint) {
                super(clientCaps);
                mSrcVideoFormatHint = srcVideoFormatHint;
            }

            /**
             * Constructs a new VideoFormatResolver object.
             *
             * @param clientCaps An ApplicationMediaCapabilities object containing the client's
             *                   capabilities.
             * @param srcVideoFormatHint A MediaFormat object containing information about the
             *                           source's video track format that could affect the
             *                           transcoding decision. Such information could include video
             *                           codec types, color spaces, whether special format info (eg.
             *                           slow-motion markers) are present, etc.. If a particular
             *                           information is not present, it will not be used to make the
             *                           decision.
             * @param srcAudioFormatHint A MediaFormat object containing information about the
             *                           source's audio track format that could affect the
             *                           transcoding decision.
             * @hide
             */
            @NonNull
            public MediaFormatResolver setSourceAudioFormatHint(@NonNull MediaFormat format) {
                mSrcAudioFormatHint = format;
                return this;
            VideoFormatResolver(@NonNull ApplicationMediaCapabilities clientCaps,
                    @NonNull MediaFormat srcVideoFormatHint,
                    @NonNull MediaFormat srcAudioFormatHint) {
                super(clientCaps);
                mSrcVideoFormatHint = srcVideoFormatHint;
                mSrcAudioFormatHint = srcAudioFormatHint;
            }

            /**
             * Returns whether the source content should be transcoded.
             *
             * @return true if the source should be transcoded.
             * @throws UnsupportedOperationException
             *         if {@link #setClientCapabilities(ApplicationMediaCapabilities)}
             *         or {@link #setSourceVideoFormatHint(MediaFormat)} was not called.
             */
            public boolean shouldTranscode() {
                if (mClientCaps == null) {
                    throw new UnsupportedOperationException(
                            "Client caps must be set!");
                }
                // Video src hint must be provided, audio src hint is not used right now.
                if (mSrcVideoFormatHint == null) {
                    throw new UnsupportedOperationException(
                            "Source video format hint must be set!");
                }

                boolean supportHevc = mClientCaps.isVideoMimeTypeSupported(
                boolean supportHevc = getClientCapabilities().isVideoMimeTypeSupported(
                        MediaFormat.MIMETYPE_VIDEO_HEVC);
                if (!supportHevc && MediaFormat.MIMETYPE_VIDEO_HEVC.equals(
                        mSrcVideoFormatHint.getString(MediaFormat.KEY_MIME))) {
@@ -907,13 +926,11 @@ public final class MediaTranscodeManager {

            /**
             * Retrieves the video track format to be used on
             * {@link Builder#setVideoTrackFormat(MediaFormat)} for this configuration.
             * {@link VideoTranscodingRequest.Builder#setVideoTrackFormat(MediaFormat)} for this
             * configuration.
             *
             * @return the video track format to be used if transcoding should be performed,
             *         and null otherwise.
             * @throws UnsupportedOperationException
             *         if {@link #setClientCapabilities(ApplicationMediaCapabilities)}
             *         or {@link #setSourceVideoFormatHint(MediaFormat)} was not called.
             */
            @Nullable
            public MediaFormat resolveVideoFormat() {
@@ -1012,9 +1029,6 @@ public final class MediaTranscodeManager {
             *
             * @return the audio track format to be used if transcoding should be performed, and
             *         null otherwise.
             * @throws UnsupportedOperationException
             *         if {@link #setClientCapabilities(ApplicationMediaCapabilities)}
             *         or {@link #setSourceVideoFormatHint(MediaFormat)} was not called.
             * @hide
             */
            @Nullable
@@ -1363,21 +1377,6 @@ public final class MediaTranscodeManager {
        public void setOnProgressUpdateListener(
                @NonNull @CallbackExecutor Executor executor,
                @Nullable OnProgressUpdateListener listener) {
            setOnProgressUpdateListener(
                    0 /* minProgressUpdateInterval */,
                    executor, listener);
        }

        /**
         * Set a progress listener with specified progress update interval.
         * @param minProgressUpdateInterval The minimum interval between each progress update.
         * @param executor The executor on which listener will be invoked.
         * @param listener The progress listener.
         */
        public void setOnProgressUpdateListener(
                int minProgressUpdateInterval,
                @NonNull @CallbackExecutor Executor executor,
                @Nullable OnProgressUpdateListener listener) {
            synchronized (mLock) {
                Objects.requireNonNull(executor, "listenerExecutor must not be null");
                Objects.requireNonNull(listener, "listener must not be null");