Skip to content

Commit

Permalink
fix NPE on decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
pedroSG94 committed Oct 1, 2024
1 parent 1bc2b1f commit 5e0db87
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
11 changes: 10 additions & 1 deletion common/src/main/java/com/pedro/common/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.pedro.common
import android.hardware.camera2.CameraCharacteristics
import android.hardware.camera2.CaptureRequest
import android.media.MediaCodec
import android.media.MediaFormat
import android.os.Build
import android.os.Handler
import android.os.Looper
Expand Down Expand Up @@ -144,4 +145,12 @@ fun Throwable.validMessage(): String {

fun MediaCodec.BufferInfo.toMediaFrameInfo() = MediaFrame.Info(offset, size, presentationTimeUs, flags)

fun ByteBuffer.clone(): ByteBuffer = ByteBuffer.wrap(toByteArray())
fun ByteBuffer.clone(): ByteBuffer = ByteBuffer.wrap(toByteArray())

fun MediaFormat.getIntegerSafe(name: String): Int? {
return try { getInteger(name) } catch (e: Exception) { null }
}

fun MediaFormat.getLongSafe(name: String): Long? {
return try { getLong(name) } catch (e: Exception) { null }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.media.MediaFormat;
import android.util.Log;

import com.pedro.common.ExtensionsKt;
import com.pedro.encoder.Frame;
import com.pedro.encoder.input.audio.GetMicrophoneData;
import com.pedro.encoder.utils.CodecUtil;
Expand Down Expand Up @@ -63,10 +64,14 @@ protected boolean extract(MediaExtractor audioExtractor) {
}
}
if (mediaFormat != null) {
channels = mediaFormat.getInteger(MediaFormat.KEY_CHANNEL_COUNT);
final Integer channels = ExtensionsKt.getIntegerSafe(mediaFormat, MediaFormat.KEY_CHANNEL_COUNT);
final Integer sampleRate = ExtensionsKt.getIntegerSafe(mediaFormat, MediaFormat.KEY_SAMPLE_RATE);
final Long duration = ExtensionsKt.getLongSafe(mediaFormat, MediaFormat.KEY_DURATION);
if (channels == null || sampleRate == null) return false;
this.channels = channels;
isStereo = channels >= 2;
sampleRate = mediaFormat.getInteger(MediaFormat.KEY_SAMPLE_RATE);
duration = mediaFormat.getLong(MediaFormat.KEY_DURATION);
this.sampleRate = sampleRate;
this.duration = duration != null ? duration : -1;
fixBuffer();
return true;
//audio decoder not supported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public boolean isLoopMode() {
}

public double getDuration() {
if (duration < 0) return duration; //fail to extract duration from file.
return duration / 10E5;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.os.Build;
import android.view.Surface;

import com.pedro.common.ExtensionsKt;
import java.nio.ByteBuffer;

/**
Expand Down Expand Up @@ -51,10 +52,15 @@ protected boolean extract(MediaExtractor videoExtractor) {
}
}
if (mediaFormat != null) {
width = mediaFormat.getInteger(MediaFormat.KEY_WIDTH);
height = mediaFormat.getInteger(MediaFormat.KEY_HEIGHT);
duration = mediaFormat.getLong(MediaFormat.KEY_DURATION);
fps = mediaFormat.getInteger(MediaFormat.KEY_FRAME_RATE);
final Integer width = ExtensionsKt.getIntegerSafe(mediaFormat, MediaFormat.KEY_WIDTH);
final Integer height = ExtensionsKt.getIntegerSafe(mediaFormat, MediaFormat.KEY_HEIGHT);
final Long duration = ExtensionsKt.getLongSafe(mediaFormat, MediaFormat.KEY_DURATION);
final Integer fps = ExtensionsKt.getIntegerSafe(mediaFormat, MediaFormat.KEY_FRAME_RATE);
if (width == null || height == null) return false;
this.width = width;
this.height = height;
this.duration = duration != null ? duration : -1;
this.fps = fps != null ? fps : 30;
return true;
//video decoder not supported
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,14 +619,14 @@ public double getAudioTime() {
}

/**
* @return return duration in seconds. 0 if no streaming
* @return return duration in seconds. 0 if no streaming, -1 if can't extract it from file
*/
public double getVideoDuration() {
return videoDecoder.getDuration();
}

/**
* @return return duration in seconds. 0 if no streaming
* @return return duration in seconds. 0 if no streaming, -1 if can't extract it from file
*/
public double getAudioDuration() {
return audioDecoder.getDuration();
Expand Down

0 comments on commit 5e0db87

Please sign in to comment.