Skip to content

Commit faac929

Browse files
theandi666Android (Google) Code Review
authored andcommitted
Merge "New API to set the video rendering mode on a MediaCodec instance." into jb-dev
2 parents a549218 + b12a539 commit faac929

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

api/current.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11001,6 +11001,7 @@ package android.media {
1100111001
method public final void queueSecureInputBuffer(int, int, android.media.MediaCodec.CryptoInfo, long, int) throws android.media.MediaCodec.CryptoException;
1100211002
method public final void release();
1100311003
method public final void releaseOutputBuffer(int, boolean);
11004+
method public final void setVideoScalingMode(int);
1100411005
method public final void start();
1100511006
method public final void stop();
1100611007
field public static int CONFIGURE_FLAG_ENCODE;
@@ -11012,6 +11013,8 @@ package android.media {
1101211013
field public static final int INFO_TRY_AGAIN_LATER = -1; // 0xffffffff
1101311014
field public static final int MODE_AES_CTR = 1; // 0x1
1101411015
field public static final int MODE_UNENCRYPTED = 0; // 0x0
11016+
field public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING = 2; // 0x2
11017+
field public static final int VIDEO_SCALING_MODE_STRETCH_TO_FIT = 1; // 0x1
1101511018
}
1101611019

1101711020
public static final class MediaCodec.BufferInfo {

media/java/android/media/MediaCodec.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,19 @@ public ByteBuffer[] getOutputBuffers() {
442442
return getBuffers(false /* input */);
443443
}
444444

445+
/** The content is scaled to the surface dimensions */
446+
public static final int VIDEO_SCALING_MODE_STRETCH_TO_FIT = 1;
447+
448+
/** The content is scaled, maintaining its aspect ratio, the whole
449+
surface area is used, content may be cropped
450+
*/
451+
public static final int VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING = 2;
452+
453+
/** If a surface has been specified in a previous call to {@link #configure}
454+
specifies the scaling mode to use. The default is "stretch to fit".
455+
*/
456+
public native final void setVideoScalingMode(int mode);
457+
445458
private native final ByteBuffer[] getBuffers(boolean input);
446459

447460
private static native final void native_init();

media/jni/android_media_MediaCodec.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <media/stagefright/foundation/AString.h>
4040
#include <media/stagefright/MediaErrors.h>
4141

42+
#include <system/window.h>
43+
4244
namespace android {
4345

4446
// Keep these in sync with their equivalents in MediaCodec.java !!!
@@ -111,16 +113,21 @@ status_t JMediaCodec::configure(
111113
int flags) {
112114
sp<SurfaceTextureClient> client;
113115
if (surfaceTexture != NULL) {
114-
client = new SurfaceTextureClient(surfaceTexture);
116+
mSurfaceTextureClient = new SurfaceTextureClient(surfaceTexture);
117+
} else {
118+
mSurfaceTextureClient.clear();
115119
}
116-
return mCodec->configure(format, client, crypto, flags);
120+
121+
return mCodec->configure(format, mSurfaceTextureClient, crypto, flags);
117122
}
118123

119124
status_t JMediaCodec::start() {
120125
return mCodec->start();
121126
}
122127

123128
status_t JMediaCodec::stop() {
129+
mSurfaceTextureClient.clear();
130+
124131
return mCodec->stop();
125132
}
126133

@@ -227,6 +234,12 @@ status_t JMediaCodec::getBuffers(
227234
return OK;
228235
}
229236

237+
void JMediaCodec::setVideoScalingMode(int mode) {
238+
if (mSurfaceTextureClient != NULL) {
239+
native_window_set_scaling_mode(mSurfaceTextureClient.get(), mode);
240+
}
241+
}
242+
230243
} // namespace android
231244

232245
////////////////////////////////////////////////////////////////////////////////
@@ -663,6 +676,24 @@ static jobjectArray android_media_MediaCodec_getBuffers(
663676
return NULL;
664677
}
665678

679+
static void android_media_MediaCodec_setVideoScalingMode(
680+
JNIEnv *env, jobject thiz, jint mode) {
681+
sp<JMediaCodec> codec = getMediaCodec(env, thiz);
682+
683+
if (codec == NULL) {
684+
jniThrowException(env, "java/lang/IllegalStateException", NULL);
685+
return;
686+
}
687+
688+
if (mode != NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW
689+
&& mode != NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
690+
jniThrowException(env, "java/lang/InvalidArgumentException", NULL);
691+
return;
692+
}
693+
694+
codec->setVideoScalingMode(mode);
695+
}
696+
666697
static void android_media_MediaCodec_native_init(JNIEnv *env) {
667698
jclass clazz = env->FindClass("android/media/MediaCodec");
668699
CHECK(clazz != NULL);
@@ -765,6 +796,9 @@ static JNINativeMethod gMethods[] = {
765796
{ "getBuffers", "(Z)[Ljava/nio/ByteBuffer;",
766797
(void *)android_media_MediaCodec_getBuffers },
767798

799+
{ "setVideoScalingMode", "(I)V",
800+
(void *)android_media_MediaCodec_setVideoScalingMode },
801+
768802
{ "native_init", "()V", (void *)android_media_MediaCodec_native_init },
769803

770804
{ "native_setup", "(Ljava/lang/String;ZZ)V",

media/jni/android_media_MediaCodec.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ struct AString;
3232
struct ICrypto;
3333
struct ISurfaceTexture;
3434
struct MediaCodec;
35+
struct SurfaceTextureClient;
3536

3637
struct JMediaCodec : public RefBase {
3738
JMediaCodec(
@@ -80,12 +81,15 @@ struct JMediaCodec : public RefBase {
8081
status_t getBuffers(
8182
JNIEnv *env, bool input, jobjectArray *bufArray) const;
8283

84+
void setVideoScalingMode(int mode);
85+
8386
protected:
8487
virtual ~JMediaCodec();
8588

8689
private:
8790
jclass mClass;
8891
jweak mObject;
92+
sp<SurfaceTextureClient> mSurfaceTextureClient;
8993

9094
sp<ALooper> mLooper;
9195
sp<MediaCodec> mCodec;

0 commit comments

Comments
 (0)