Monday, 25 January 2021

Android MediaExtractor fails to return sample

I am trying to decode a video onto a surface in Android using MediaCodec and MediaExtractor.

I use the setDataSource(context, uri, null) method on the MediaExtractor object (extractor below) to set the data source. The uri comes from the Android file picker for videos. This call doesn't seem to throw any exceptions.

In the onInputBufferAvailable() call from the decoder, I then read a new sample from the extractor but the extractor.readSampleData(..) call randomly fails to read the correct data and starts returning buffers of size -1. Ideally, the size should only be -1 when the whole file has been read.

object : MediaCodec.Callback() {
    override fun onInputBufferAvailable(codec: MediaCodec, index: Int) {
        val buffer = codec.getInputBuffer(index)!!
        try {
            val size = extractor.readSampleData(buffer, 0)
            if (size > 0) {
                decoder.queueInputBuffer(
                    index,
                    0,
                    size,
                    extractor.sampleTime,
                    sampleFlags
                )
                extractor.advance()
            } else if (size == 0) {
                Timber.d("Size 0 sample received from extractor")
            } else if (size == -1) {
            // Size is -1 when no more samples are available                     
        } catch (exception: Exception) {
             Timber.e(exception)
        }
    }

This doesn't happen all the time but frequent enough. So far, I have also only observed this on the Android 9 OS.

Maybe this helps : https://issuetracker.unity3d.com/issues/android-video-freezes-while-playing-when-a-video-is-loaded-from-streamingassets-folder



from Android MediaExtractor fails to return sample

No comments:

Post a Comment