Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ad60a0f6 authored by Wonsik Kim's avatar Wonsik Kim
Browse files

stagefright: reuse buffers instead of cloning

In buffer array mode, the buffer reference is kept in clients of
MediaCodec, so allocating new buffer for new input/output does not
work in that case.

Bug: 32577275
Bug: 32579231
Test: use screenrecord and investigate the output file.
Test: cts-tradefed run cts-dev -m CtsMediaTestCases -t android.media.cts.EncodeDecodeTest
Change-Id: I26d89f6b5735094062b8a2027bcab4754576d574
parent 93f52337
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -58,13 +58,13 @@ public:
    sp<AMessage> meta();
    sp<AMessage> format();

    virtual sp<MediaCodecBuffer> clone(const sp<AMessage> &format);
    void setFormat(const sp<AMessage> &format);

private:
    MediaCodecBuffer() = delete;

    const sp<AMessage> mMeta;
    const sp<AMessage> mFormat;
    sp<AMessage> mFormat;
    const sp<ABuffer> mBuffer;
    MediaBufferBase *mMediaBufferBase;
};
+3 −2
Original line number Diff line number Diff line
@@ -80,8 +80,9 @@ sp<AMessage> MediaCodecBuffer::format() {
    return mFormat;
}

sp<MediaCodecBuffer> MediaCodecBuffer::clone(const sp<AMessage> &format) {
    return new MediaCodecBuffer(format, mBuffer);
void MediaCodecBuffer::setFormat(const sp<AMessage> &format) {
    mMeta->clear();
    mFormat = format;
}

}  // namespace android
+4 −2
Original line number Diff line number Diff line
@@ -5552,7 +5552,8 @@ void ACodec::BaseState::postFillThisBuffer(BufferInfo *info) {
    notify->setInt32("what", CodecBase::kWhatFillThisBuffer);
    notify->setInt32("buffer-id", info->mBufferID);

    notify->setObject("buffer", info->mData->clone(mCodec->mInputFormat));
    info->mData->setFormat(mCodec->mInputFormat);
    notify->setObject("buffer", info->mData);
    info->mData.clear();

    sp<AMessage> reply = new AMessage(kWhatInputBufferFilled, mCodec);
@@ -5912,7 +5913,7 @@ bool ACodec::BaseState::onOMXFillBufferDone(

            sp<AMessage> reply =
                new AMessage(kWhatOutputBufferDrained, mCodec);
            sp<MediaCodecBuffer> buffer = info->mData->clone(mCodec->mOutputFormat);
            sp<MediaCodecBuffer> buffer = info->mData;

            if (mCodec->mOutputFormat != mCodec->mLastOutputFormat && rangeLength > 0) {
                // pretend that output format has changed on the first frame (we used to do this)
@@ -5926,6 +5927,7 @@ bool ACodec::BaseState::onOMXFillBufferDone(
                // data space) so that we can set it if and once the buffer is rendered.
                mCodec->addKeyFormatChangesToRenderBufferNotification(reply);
            }
            buffer->setFormat(mCodec->mOutputFormat);

            if (mCodec->usingMetadataOnEncoderOutput()) {
                native_handle_t *handle = NULL;
+0 −10
Original line number Diff line number Diff line
@@ -34,10 +34,6 @@ SharedMemoryBuffer::SharedMemoryBuffer(const sp<AMessage> &format, const sp<IMem
      mMemory(mem) {
}

sp<MediaCodecBuffer> SharedMemoryBuffer::clone(const sp<AMessage> &format) {
    return new SharedMemoryBuffer(format, mMemory);
}

SecureBuffer::SecureBuffer(const sp<AMessage> &format, const void *ptr, size_t size)
    : MediaCodecBuffer(format, new ABuffer(nullptr, size)),
      mPointer(ptr) {
@@ -50,12 +46,6 @@ SecureBuffer::SecureBuffer(
      mHandle(handle) {
}

sp<MediaCodecBuffer> SecureBuffer::clone(const sp<AMessage> &format) {
    return (mHandle == nullptr)
            ? new SecureBuffer(format, mPointer, capacity())
            : new SecureBuffer(format, mHandle, capacity());
}

void *SecureBuffer::getDestinationPointer() {
    return (void *)(mHandle == nullptr ? mPointer : mHandle->handle());
}
+1 −3
Original line number Diff line number Diff line
@@ -2390,7 +2390,6 @@ void MediaCodec::returnBuffersToCodecOnPort(int32_t portIndex, bool isReclaim) {
    }

    mAvailPortBuffers[portIndex].clear();
    mPortBufferArrays[portIndex].clear();
}

size_t MediaCodec::updateBuffers(
@@ -2414,8 +2413,7 @@ size_t MediaCodec::updateBuffers(

            if (portIndex == kPortIndexInput && mCrypto != NULL) {
                info->mSecureData = buffer;
                info->mData = new SharedMemoryBuffer(
                        buffer->format(), info->mSharedEncryptedBuffer);
                info->mData = mPortBufferArrays[portIndex][i];
            } else {
                info->mData = buffer;
            }
Loading