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

Commit e884f72d authored by Andy Hung's avatar Andy Hung Committed by android-build-merger
Browse files

Merge "Stop SoundPool decode if NdkMediaCodec returns null buffers" into nyc-dev am: 9a99629e

am: d1776ff6

* commit 'd1776ff6':
  Stop SoundPool decode if NdkMediaCodec returns null buffers

Change-Id: Ib3283069f7279e63370104c35966585d2da22bff
parents be81f515 d1776ff6
Loading
Loading
Loading
Loading
+31 −10
Original line number Original line Diff line number Diff line
@@ -554,6 +554,10 @@ static status_t decode(int fd, int64_t offset, int64_t length,
                    if (bufidx >= 0) {
                    if (bufidx >= 0) {
                        size_t bufsize;
                        size_t bufsize;
                        uint8_t *buf = AMediaCodec_getInputBuffer(codec, bufidx, &bufsize);
                        uint8_t *buf = AMediaCodec_getInputBuffer(codec, bufidx, &bufsize);
                        if (buf == nullptr) {
                            ALOGE("AMediaCodec_getInputBuffer returned nullptr, short decode");
                            break;
                        }
                        int sampleSize = AMediaExtractor_readSampleData(ex, buf, bufsize);
                        int sampleSize = AMediaExtractor_readSampleData(ex, buf, bufsize);
                        ALOGV("read %d", sampleSize);
                        ALOGV("read %d", sampleSize);
                        if (sampleSize < 0) {
                        if (sampleSize < 0) {
@@ -563,10 +567,16 @@ static status_t decode(int fd, int64_t offset, int64_t length,
                        }
                        }
                        int64_t presentationTimeUs = AMediaExtractor_getSampleTime(ex);
                        int64_t presentationTimeUs = AMediaExtractor_getSampleTime(ex);


                        AMediaCodec_queueInputBuffer(codec, bufidx,
                        media_status_t mstatus = AMediaCodec_queueInputBuffer(codec, bufidx,
                                0 /* offset */, sampleSize, presentationTimeUs,
                                0 /* offset */, sampleSize, presentationTimeUs,
                                sawInputEOS ? AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM : 0);
                                sawInputEOS ? AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM : 0);
                        AMediaExtractor_advance(ex);
                        if (mstatus != AMEDIA_OK) {
                            // AMEDIA_ERROR_UNKNOWN == { -ERANGE -EINVAL -EACCES }
                            ALOGE("AMediaCodec_queueInputBuffer returned status %d, short decode",
                                    (int)mstatus);
                            break;
                        }
                        (void)AMediaExtractor_advance(ex);
                    }
                    }
                }
                }


@@ -581,6 +591,10 @@ static status_t decode(int fd, int64_t offset, int64_t length,
                    ALOGV("got decoded buffer size %d", info.size);
                    ALOGV("got decoded buffer size %d", info.size);


                    uint8_t *buf = AMediaCodec_getOutputBuffer(codec, status, NULL /* out_size */);
                    uint8_t *buf = AMediaCodec_getOutputBuffer(codec, status, NULL /* out_size */);
                    if (buf == nullptr) {
                        ALOGE("AMediaCodec_getOutputBuffer returned nullptr, short decode");
                        break;
                    }
                    size_t dataSize = info.size;
                    size_t dataSize = info.size;
                    if (dataSize > available) {
                    if (dataSize > available) {
                        dataSize = available;
                        dataSize = available;
@@ -589,7 +603,14 @@ static status_t decode(int fd, int64_t offset, int64_t length,
                    writePos += dataSize;
                    writePos += dataSize;
                    written += dataSize;
                    written += dataSize;
                    available -= dataSize;
                    available -= dataSize;
                    AMediaCodec_releaseOutputBuffer(codec, status, false /* render */);
                    media_status_t mstatus = AMediaCodec_releaseOutputBuffer(
                            codec, status, false /* render */);
                    if (mstatus != AMEDIA_OK) {
                        // AMEDIA_ERROR_UNKNOWN == { -ERANGE -EINVAL -EACCES }
                        ALOGE("AMediaCodec_releaseOutputBuffer returned status %d, short decode",
                                (int)mstatus);
                        break;
                    }
                    if (available == 0) {
                    if (available == 0) {
                        // there might be more data, but there's no space for it
                        // there might be more data, but there's no space for it
                        sawOutputEOS = true;
                        sawOutputEOS = true;
@@ -610,21 +631,21 @@ static status_t decode(int fd, int64_t offset, int64_t length,
                }
                }
            }
            }


            AMediaCodec_stop(codec);
            (void)AMediaCodec_stop(codec);
            AMediaCodec_delete(codec);
            (void)AMediaCodec_delete(codec);
            AMediaExtractor_delete(ex);
            (void)AMediaExtractor_delete(ex);
            if (!AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, (int32_t*) rate) ||
            if (!AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_SAMPLE_RATE, (int32_t*) rate) ||
                    !AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT, numChannels)) {
                    !AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_CHANNEL_COUNT, numChannels)) {
                AMediaFormat_delete(format);
                (void)AMediaFormat_delete(format);
                return UNKNOWN_ERROR;
                return UNKNOWN_ERROR;
            }
            }
            AMediaFormat_delete(format);
            (void)AMediaFormat_delete(format);
            *memsize = written;
            *memsize = written;
            return OK;
            return OK;
        }
        }
        AMediaFormat_delete(format);
        (void)AMediaFormat_delete(format);
    }
    }
    AMediaExtractor_delete(ex);
    (void)AMediaExtractor_delete(ex);
    return UNKNOWN_ERROR;
    return UNKNOWN_ERROR;
}
}