INDExOS / media-for-mobile

Media for Mobile
Other
456 stars 178 forks source link

Taking care of the possible null value that can be returned by mediaExtractor.getTrackFormat #100

Open develric opened 3 years ago

develric commented 3 years ago

Hi there πŸ‘‹ . We have this issue here where we get the following:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.m4m.domain.MediaFormat.getMimeType()' on a null object reference
        at org.m4m.domain.MediaSource.getMediaFormatByType(MediaSource.java:166)
        at org.m4m.MediaFile.getVideoFormat(MediaFile.java:112)
        at org.m4m.MediaFileInfo.prepareMediaFile(MediaFileInfo.java:135)
        at org.m4m.MediaFileInfo.setUri(MediaFileInfo.java:88)

Looking into it, we are getting that Null Pointer Exception in some cases/conditions:

If I'm not wrong, the issue seems related to the fact that the mediaExtractor.getTrackFormat can return a null value:

@Override
    public MediaFormat getTrackFormat(int index) {
        if (mediaExtractor.getTrackFormat(index).getString(android.media.MediaFormat.KEY_MIME).contains("video")) {
            return new VideoFormatAndroid(mediaExtractor.getTrackFormat(index));
        } else if (mediaExtractor.getTrackFormat(index).getString(android.media.MediaFormat.KEY_MIME).contains("audio")) {
            return new AudioFormatAndroid(mediaExtractor.getTrackFormat(index));
        }
        return null;
    }

and in one case I could verify we had 3 tracks rescued by mediaExtractor.getTrackFormat with values

the first raw is a possible scenario when we can get the crash returning a null value.

Linking to the lib code of this PR I could verify that all worked ok for me. Also checked the samples in the lib and AFAIU seems working ok.

I see the lib is already checking that possible null return value in all the other places in MediaSource.java so the modification should just add that check all over the places where it's used. I was wondering if you could review this PR and confirm if it can be fine to merge (I'm not familiar with your code base so happy to get your feedbacks).

Thanks for your attention and time πŸ™‡β€β™‚οΈ.