From 7541ff5d83a3e77cb533841a0326a241550b95d9 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Thu, 10 May 2012 10:54:15 -0700 Subject: Properly connect/disconnect to/from the native window in MediaCodec. Change-Id: Ib5bf90a3b81fca8ff2346235bc28a2bd0bc7bfb1 related-to-bug: 6472161 --- media/libstagefright/MediaCodec.cpp | 59 ++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 10 deletions(-) (limited to 'media/libstagefright/MediaCodec.cpp') diff --git a/media/libstagefright/MediaCodec.cpp b/media/libstagefright/MediaCodec.cpp index e032cfc..5b513a8 100644 --- a/media/libstagefright/MediaCodec.cpp +++ b/media/libstagefright/MediaCodec.cpp @@ -796,9 +796,6 @@ void MediaCodec::onMessageReceived(const sp &msg) { break; } - mReplyID = replyID; - setState(CONFIGURING); - sp obj; if (!msg->findObject("native-window", &obj)) { obj.clear(); @@ -810,15 +807,24 @@ void MediaCodec::onMessageReceived(const sp &msg) { if (obj != NULL) { format->setObject("native-window", obj); - if (mFlags & kFlagIsSoftwareCodec) { - mNativeWindow = - static_cast(obj.get()) - ->getSurfaceTextureClient(); + status_t err = setNativeWindow( + static_cast(obj.get()) + ->getSurfaceTextureClient()); + + if (err != OK) { + sp response = new AMessage; + response->setInt32("err", err); + + response->postReply(replyID); + break; } } else { - mNativeWindow.clear(); + setNativeWindow(NULL); } + mReplyID = replyID; + setState(CONFIGURING); + void *crypto; if (!msg->findPointer("crypto", &crypto)) { crypto = NULL; @@ -1180,12 +1186,12 @@ status_t MediaCodec::queueCSDInputBuffer(size_t bufferIndex) { } void MediaCodec::setState(State newState) { - if (newState == INITIALIZED) { + if (newState == INITIALIZED || newState == UNINITIALIZED) { delete mSoftRenderer; mSoftRenderer = NULL; mCrypto.clear(); - mNativeWindow.clear(); + setNativeWindow(NULL); mOutputFormat.clear(); mFlags &= ~kFlagOutputFormatChanged; @@ -1425,4 +1431,37 @@ ssize_t MediaCodec::dequeuePortBuffer(int32_t portIndex) { return index; } +status_t MediaCodec::setNativeWindow( + const sp &surfaceTextureClient) { + status_t err; + + if (mNativeWindow != NULL) { + err = native_window_api_disconnect( + mNativeWindow.get(), NATIVE_WINDOW_API_MEDIA); + + if (err != OK) { + ALOGW("native_window_api_disconnect returned an error: %s (%d)", + strerror(-err), err); + } + + mNativeWindow.clear(); + } + + if (surfaceTextureClient != NULL) { + err = native_window_api_connect( + surfaceTextureClient.get(), NATIVE_WINDOW_API_MEDIA); + + if (err != OK) { + ALOGE("native_window_api_connect returned an error: %s (%d)", + strerror(-err), err); + + return err; + } + + mNativeWindow = surfaceTextureClient; + } + + return OK; +} + } // namespace android -- cgit v1.1