From 420246479b8dfbfdd583564655f2bda5a36a7912 Mon Sep 17 00:00:00 2001 From: Jamie Gennis Date: Tue, 22 Feb 2011 18:33:06 -0800 Subject: Fix error recovery in Stagefright init. This change fixes the error path of OMXCodec::allocateOutputBuffersFromNativeWindow so that it cancels the correct number of buffers if a dequeueBuffer operation fails. Change-Id: Ib7cdcdf24f1718bc070ad218a5980949c0e942eb --- media/libstagefright/ACodec.cpp | 26 ++++++++++++-------------- media/libstagefright/OMXCodec.cpp | 33 +++++++++++++++------------------ 2 files changed, 27 insertions(+), 32 deletions(-) (limited to 'media') diff --git a/media/libstagefright/ACodec.cpp b/media/libstagefright/ACodec.cpp index b0ae3d8..2bbb320 100644 --- a/media/libstagefright/ACodec.cpp +++ b/media/libstagefright/ACodec.cpp @@ -459,16 +459,12 @@ status_t ACodec::allocateOutputBuffersFromNativeWindow() { return err; } - // XXX TODO: Do something so the ANativeWindow knows that we'll need to get - // the same set of buffers. - LOGV("[%s] Allocating %lu buffers from a native window of size %lu on " "output port", mComponentName.c_str(), def.nBufferCountActual, def.nBufferSize); // Dequeue buffers and send them to OMX - OMX_U32 i; - for (i = 0; i < def.nBufferCountActual; i++) { + for (OMX_U32 i = 0; i < def.nBufferCountActual; i++) { android_native_buffer_t *buf; err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf); if (err != 0) { @@ -477,23 +473,26 @@ status_t ACodec::allocateOutputBuffersFromNativeWindow() { } sp graphicBuffer(new GraphicBuffer(buf, false)); + BufferInfo info; + info.mStatus = BufferInfo::OWNED_BY_US; + info.mData = new ABuffer(0); + info.mGraphicBuffer = graphicBuffer; + mBuffers[kPortIndexOutput].push(info); + IOMX::buffer_id bufferId; err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer, &bufferId); if (err != 0) { + LOGE("registering GraphicBuffer %lu with OMX IL component failed: " + "%d", i, err); break; } + mBuffers[kPortIndexOutput].editItemAt(i).mBufferID = bufferId; + LOGV("[%s] Registered graphic buffer with ID %p (pointer = %p)", mComponentName.c_str(), bufferId, graphicBuffer.get()); - - BufferInfo info; - info.mBufferID = bufferId; - info.mStatus = BufferInfo::OWNED_BY_US; - info.mData = new ABuffer(0); - info.mGraphicBuffer = graphicBuffer; - mBuffers[kPortIndexOutput].push(info); } OMX_U32 cancelStart; @@ -503,7 +502,7 @@ status_t ACodec::allocateOutputBuffersFromNativeWindow() { // If an error occurred while dequeuing we need to cancel any buffers // that were dequeued. cancelStart = 0; - cancelEnd = i; + cancelEnd = mBuffers[kPortIndexOutput].size(); } else { // Return the last two buffers to the native window. // XXX TODO: The number of buffers the native window owns should @@ -2286,4 +2285,3 @@ void ACodec::FlushingState::changeStateIfWeOwnAllBuffers() { } } // namespace android - diff --git a/media/libstagefright/OMXCodec.cpp b/media/libstagefright/OMXCodec.cpp index 5d502e7..00b1310 100644 --- a/media/libstagefright/OMXCodec.cpp +++ b/media/libstagefright/OMXCodec.cpp @@ -1769,15 +1769,11 @@ status_t OMXCodec::allocateOutputBuffersFromNativeWindow() { return err; } - // XXX TODO: Do something so the ANativeWindow knows that we'll need to get - // the same set of buffers. - CODEC_LOGI("allocating %lu buffers from a native window of size %lu on " "output port", def.nBufferCountActual, def.nBufferSize); // Dequeue buffers and send them to OMX - OMX_U32 i; - for (i = 0; i < def.nBufferCountActual; i++) { + for (OMX_U32 i = 0; i < def.nBufferCountActual; i++) { android_native_buffer_t* buf; err = mNativeWindow->dequeueBuffer(mNativeWindow.get(), &buf); if (err != 0) { @@ -1786,36 +1782,37 @@ status_t OMXCodec::allocateOutputBuffersFromNativeWindow() { } sp graphicBuffer(new GraphicBuffer(buf, false)); + BufferInfo info; + info.mData = NULL; + info.mSize = def.nBufferSize; + info.mStatus = OWNED_BY_US; + info.mMem = NULL; + info.mMediaBuffer = new MediaBuffer(graphicBuffer); + info.mMediaBuffer->setObserver(this); + mPortBuffers[kPortIndexOutput].push(info); + IOMX::buffer_id bufferId; err = mOMX->useGraphicBuffer(mNode, kPortIndexOutput, graphicBuffer, &bufferId); if (err != 0) { + CODEC_LOGE("registering GraphicBuffer with OMX IL component " + "failed: %d", err); break; } + mPortBuffers[kPortIndexOutput].editItemAt(i).mBuffer = bufferId; + CODEC_LOGV("registered graphic buffer with ID %p (pointer = %p)", bufferId, graphicBuffer.get()); - - BufferInfo info; - info.mData = NULL; - info.mSize = def.nBufferSize; - info.mBuffer = bufferId; - info.mStatus = OWNED_BY_US; - info.mMem = NULL; - info.mMediaBuffer = new MediaBuffer(graphicBuffer); - info.mMediaBuffer->setObserver(this); - - mPortBuffers[kPortIndexOutput].push(info); } OMX_U32 cancelStart; OMX_U32 cancelEnd; - if (err != 0) { // If an error occurred while dequeuing we need to cancel any buffers // that were dequeued. cancelStart = 0; - cancelEnd = i; + cancelEnd = mPortBuffers[kPortIndexOutput].size(); } else { // Return the last two buffers to the native window. // XXX TODO: The number of buffers the native window owns should probably be -- cgit v1.1