diff --git a/common/src/main/java/com/pedro/common/Extensions.kt b/common/src/main/java/com/pedro/common/Extensions.kt index 559694f1b..8b60f991c 100644 --- a/common/src/main/java/com/pedro/common/Extensions.kt +++ b/common/src/main/java/com/pedro/common/Extensions.kt @@ -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 @@ -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()) \ No newline at end of file +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 } +} \ No newline at end of file diff --git a/encoder/src/main/java/com/pedro/encoder/input/decoder/AudioDecoder.java b/encoder/src/main/java/com/pedro/encoder/input/decoder/AudioDecoder.java index 53b949e80..8d54b7741 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/decoder/AudioDecoder.java +++ b/encoder/src/main/java/com/pedro/encoder/input/decoder/AudioDecoder.java @@ -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; @@ -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 diff --git a/encoder/src/main/java/com/pedro/encoder/input/decoder/BaseDecoder.java b/encoder/src/main/java/com/pedro/encoder/input/decoder/BaseDecoder.java index 31d9e9adc..9357af1e2 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/decoder/BaseDecoder.java +++ b/encoder/src/main/java/com/pedro/encoder/input/decoder/BaseDecoder.java @@ -210,6 +210,7 @@ public boolean isLoopMode() { } public double getDuration() { + if (duration < 0) return duration; //fail to extract duration from file. return duration / 10E5; } diff --git a/encoder/src/main/java/com/pedro/encoder/input/decoder/VideoDecoder.java b/encoder/src/main/java/com/pedro/encoder/input/decoder/VideoDecoder.java index f915e33f1..be2eaf0e1 100644 --- a/encoder/src/main/java/com/pedro/encoder/input/decoder/VideoDecoder.java +++ b/encoder/src/main/java/com/pedro/encoder/input/decoder/VideoDecoder.java @@ -21,6 +21,7 @@ import android.os.Build; import android.view.Surface; +import com.pedro.common.ExtensionsKt; import java.nio.ByteBuffer; /** @@ -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 { diff --git a/library/src/main/java/com/pedro/library/base/FromFileBase.java b/library/src/main/java/com/pedro/library/base/FromFileBase.java index 8ae321999..68abe3221 100644 --- a/library/src/main/java/com/pedro/library/base/FromFileBase.java +++ b/library/src/main/java/com/pedro/library/base/FromFileBase.java @@ -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();