From effc9b4839f3cc109fe3d8244022f3c898cd080b Mon Sep 17 00:00:00 2001 From: ztenghui Date: Tue, 12 Mar 2013 16:03:12 -0700 Subject: [PATCH] Add the orientation hint to the MediaMuxer bug:7991013 Change-Id: I7e3e513851589e4ba7983d2c416152b2b08cbcfb --- api/current.txt | 1 + media/java/android/media/MediaMuxer.java | 29 +++++++++++++++++++++++- media/jni/android_media_MediaMuxer.cpp | 21 +++++++++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/api/current.txt b/api/current.txt index f2fcbd6b7b670..4c2e3c517893b 100644 --- a/api/current.txt +++ b/api/current.txt @@ -11589,6 +11589,7 @@ package android.media { ctor public MediaMuxer(java.lang.String, int) throws java.io.IOException; method public int addTrack(android.media.MediaFormat); method public void release(); + method public void setOrientationHint(int); method public void start(); method public void stop(); method public void writeSampleData(int, java.nio.ByteBuffer, android.media.MediaCodec.BufferInfo); diff --git a/media/java/android/media/MediaMuxer.java b/media/java/android/media/MediaMuxer.java index c3cc1e66db634..1f5ca354635f7 100644 --- a/media/java/android/media/MediaMuxer.java +++ b/media/java/android/media/MediaMuxer.java @@ -91,6 +91,8 @@ final public class MediaMuxer { private static native void nativeStop(int nativeObject); private static native int nativeAddTrack(int nativeObject, String[] keys, Object[] values); + private static native void nativeSetOrientationHint(int nativeObject, + int degrees); private static native void nativeWriteSampleData(int nativeObject, int trackIndex, ByteBuffer byteBuf, int offset, int size, long presentationTimeUs, int flags); @@ -109,7 +111,7 @@ final public class MediaMuxer { private int mNativeObject; /** - * Constructor + * Constructor. * Creates a media muxer that writes to the specified path. * @param path The path of the output media file. * @param format The format of the output media file. @@ -138,6 +140,31 @@ final public class MediaMuxer { } } + /** + * Sets the orientation hint for output video playback. + *

This method should be called before {@link #start}. Calling this + * method will not rotate the video frame when muxer is generating the file, + * but add a composition matrix containing the rotation angle in the output + * video if the output format is + * {@link OutputFormat#MUXER_OUTPUT_MPEG_4} so that a video player can + * choose the proper orientation for playback. Note that some video players + * may choose to ignore the composition matrix in a video during playback. + * By default, the rotation degree is 0.

+ * @param degrees the angle to be rotated clockwise in degrees. + * The supported angles are 0, 90, 180, and 270 degrees. + */ + public void setOrientationHint(int degrees) { + if (degrees != 0 && degrees != 90 && degrees != 180 && degrees != 270) { + throw new IllegalArgumentException("Unsupported angle: " + degrees); + } + if (mState == MUXER_STATE_INITIALIZED) { + nativeSetOrientationHint(mNativeObject, degrees); + } else { + throw new IllegalStateException("Can't set rotation degrees due" + + " to wrong state."); + } + } + /** * Starts the muxer. *

Make sure this is called after {@link #addTrack} and before diff --git a/media/jni/android_media_MediaMuxer.cpp b/media/jni/android_media_MediaMuxer.cpp index 30ebb00a0ad56..7517e8561a20f 100644 --- a/media/jni/android_media_MediaMuxer.cpp +++ b/media/jni/android_media_MediaMuxer.cpp @@ -146,6 +146,24 @@ static jint android_media_MediaMuxer_native_setup( return int(muxer.get()); } +static void android_media_MediaMuxer_setOrientationHint( + JNIEnv *env, jclass clazz, jint nativeObject, jint degrees) { + sp muxer(reinterpret_cast(nativeObject)); + if (muxer == NULL) { + jniThrowException(env, "java/lang/IllegalStateException", + "Muxer was not set up correctly"); + return; + } + status_t err = muxer->setOrientationHint(degrees); + + if (err != OK) { + jniThrowException(env, "java/lang/IllegalStateException", + "Failed to set orientation hint"); + return; + } + +} + static void android_media_MediaMuxer_start(JNIEnv *env, jclass clazz, jint nativeObject) { sp muxer(reinterpret_cast(nativeObject)); @@ -195,6 +213,9 @@ static JNINativeMethod gMethods[] = { { "nativeAddTrack", "(I[Ljava/lang/String;[Ljava/lang/Object;)I", (void *)android_media_MediaMuxer_addTrack }, + { "nativeSetOrientationHint", "(II)V", + (void *)android_media_MediaMuxer_setOrientationHint}, + { "nativeStart", "(I)V", (void *)android_media_MediaMuxer_start}, { "nativeWriteSampleData", "(IILjava/nio/ByteBuffer;IIJI)V",