Support extracting thumbnail from rotated video tracks

Change-Id: Ife0a2536aaac5ff1efdf1035b9d2c892773ee16c
diff --git a/include/private/media/VideoFrame.h b/include/private/media/VideoFrame.h
index 9c35274ba..3aff0c6 100644
--- a/include/private/media/VideoFrame.h
+++ b/include/private/media/VideoFrame.h
@@ -120,6 +120,7 @@
     uint32_t mDisplayHeight;
     uint32_t mSize;            // Number of bytes in mData
     uint8_t* mData;            // Actual binary data
+    int32_t  mRotationAngle;   // rotation angle, clockwise
 };
 
 }; // namespace android
diff --git a/media/jni/android_media_MediaMetadataRetriever.cpp b/media/jni/android_media_MediaMetadataRetriever.cpp
index efea802..63e9dc8 100644
--- a/media/jni/android_media_MediaMetadataRetriever.cpp
+++ b/media/jni/android_media_MediaMetadataRetriever.cpp
@@ -36,6 +36,7 @@
     jfieldID context;
     jclass bitmapClazz;
     jmethodID bitmapConstructor;
+    jmethodID createBitmapMethod;
 };
 
 static fields_t fields;
@@ -174,6 +175,41 @@
         return NULL;
     }
 
+    jobject matrix = NULL;
+    if (videoFrame->mRotationAngle != 0) {
+        LOGD("Create a rotation matrix: %d degrees", videoFrame->mRotationAngle);
+        jclass matrixClazz = env->FindClass("android/graphics/Matrix");
+        if (matrixClazz == NULL) {
+            jniThrowException(env, "java/lang/RuntimeException",
+                "Can't find android/graphics/Matrix");
+            return NULL;
+        }
+        jmethodID matrixConstructor =
+            env->GetMethodID(matrixClazz, "<init>", "()V");
+        if (matrixConstructor == NULL) {
+            jniThrowException(env, "java/lang/RuntimeException",
+                "Can't find Matrix constructor");
+            return NULL;
+        }
+        matrix =
+            env->NewObject(matrixClazz, matrixConstructor);
+        if (matrix == NULL) {
+            LOGE("Could not create a Matrix object");
+            return NULL;
+        }
+
+        LOGV("Rotate the matrix: %d degrees", videoFrame->mRotationAngle);
+        jmethodID setRotateMethod =
+                env->GetMethodID(matrixClazz, "setRotate", "(F)V");
+        if (setRotateMethod == NULL) {
+            jniThrowException(env, "java/lang/RuntimeException",
+                "Can't find Matrix setRotate method");
+            return NULL;
+        }
+        env->CallVoidMethod(matrix, setRotateMethod, 1.0 * videoFrame->mRotationAngle);
+        env->DeleteLocalRef(matrixClazz);
+    }
+
     // Create a SkBitmap to hold the pixels
     SkBitmap *bitmap = new SkBitmap();
     if (bitmap == NULL) {
@@ -191,7 +227,19 @@
     // Since internally SkBitmap uses reference count to manage the reference to
     // its pixels, it is important that the pixels (along with SkBitmap) be
     // available after creating the Bitmap is returned to Java app.
-    return env->NewObject(fields.bitmapClazz, fields.bitmapConstructor, (int) bitmap, true, NULL, -1);
+    jobject jSrcBitmap = env->NewObject(fields.bitmapClazz,
+            fields.bitmapConstructor, (int) bitmap, true, NULL, -1);
+
+    LOGV("Return a new bitmap constructed with the rotation matrix");
+    return env->CallStaticObjectMethod(
+                fields.bitmapClazz, fields.createBitmapMethod,
+                jSrcBitmap,                     // source Bitmap
+                0,                              // x
+                0,                              // y
+                videoFrame->mDisplayWidth,      // width
+                videoFrame->mDisplayHeight,     // height
+                matrix,                         // transform matrix
+                false);                         // filter
 }
 
 static jbyteArray android_media_MediaMetadataRetriever_extractAlbumArt(JNIEnv *env, jobject thiz)
@@ -291,6 +339,15 @@
         jniThrowException(env, "java/lang/RuntimeException", "Can't find Bitmap constructor");
         return;
     }
+    fields.createBitmapMethod =
+            env->GetStaticMethodID(fields.bitmapClazz, "createBitmap",
+                    "(Landroid/graphics/Bitmap;IIIILandroid/graphics/Matrix;Z)"
+                    "Landroid/graphics/Bitmap;");
+    if (fields.createBitmapMethod == NULL) {
+        jniThrowException(env, "java/lang/RuntimeException",
+                "Can't find Bitmap.createBitmap method");
+        return;
+    }
 }
 
 static void android_media_MediaMetadataRetriever_native_setup(JNIEnv *env, jobject thiz)
diff --git a/media/libmediaplayerservice/MetadataRetrieverClient.cpp b/media/libmediaplayerservice/MetadataRetrieverClient.cpp
index ca229fa..39fce81 100644
--- a/media/libmediaplayerservice/MetadataRetrieverClient.cpp
+++ b/media/libmediaplayerservice/MetadataRetrieverClient.cpp
@@ -253,6 +253,8 @@
     frameCopy->mDisplayWidth = frame->mDisplayWidth;
     frameCopy->mDisplayHeight = frame->mDisplayHeight;
     frameCopy->mSize = frame->mSize;
+    frameCopy->mRotationAngle = frame->mRotationAngle;
+    LOGV("rotation: %d", frameCopy->mRotationAngle);
     frameCopy->mData = (uint8_t *)frameCopy + sizeof(VideoFrame);
     memcpy(frameCopy->mData, frame->mData, frame->mSize);
     delete frame;  // Fix memory leakage
diff --git a/media/libstagefright/StagefrightMetadataRetriever.cpp b/media/libstagefright/StagefrightMetadataRetriever.cpp
index a800a93..9b2dec9 100644
--- a/media/libstagefright/StagefrightMetadataRetriever.cpp
+++ b/media/libstagefright/StagefrightMetadataRetriever.cpp
@@ -191,6 +191,11 @@
     CHECK(meta->findInt32(kKeyWidth, &width));
     CHECK(meta->findInt32(kKeyHeight, &height));
 
+    int32_t rotationAngle;
+    if (!trackMeta->findInt32(kKeyRotation, &rotationAngle)) {
+        rotationAngle = 0;  // By default, no rotation
+    }
+
     VideoFrame *frame = new VideoFrame;
     frame->mWidth = width;
     frame->mHeight = height;
@@ -198,6 +203,7 @@
     frame->mDisplayHeight = height;
     frame->mSize = width * height * 2;
     frame->mData = new uint8_t[frame->mSize];
+    frame->mRotationAngle = rotationAngle;
 
     int32_t srcFormat;
     CHECK(meta->findInt32(kKeyColorFormat, &srcFormat));