From 3038ecf5d4f8babe81fce8cc711c07edd28c3f98 Mon Sep 17 00:00:00 2001
From: kazuya okamoto <kazuya.226@studist.jp>
Date: Thu, 23 Dec 2021 16:31:22 +0900
Subject: [PATCH 1/3] Fix when it is not buffer.hasRemaining

---
 .../com/otaliastudios/transcoder/internal/audio/chunks.kt    | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/src/main/java/com/otaliastudios/transcoder/internal/audio/chunks.kt b/lib/src/main/java/com/otaliastudios/transcoder/internal/audio/chunks.kt
index 52eaa357..552e7bcb 100644
--- a/lib/src/main/java/com/otaliastudios/transcoder/internal/audio/chunks.kt
+++ b/lib/src/main/java/com/otaliastudios/transcoder/internal/audio/chunks.kt
@@ -25,8 +25,9 @@ internal class ChunkQueue(private val sampleRate: Int, private val channels: Int
     fun isEmpty() = queue.isEmpty()
 
     fun enqueue(buffer: ShortBuffer, timeUs: Long, timeStretch: Double, release: () -> Unit) {
-        require(buffer.hasRemaining())
-        queue.addLast(Chunk(buffer, timeUs, timeStretch, release))
+        if (buffer.hasRemaining()) {
+            queue.addLast(Chunk(buffer, timeUs, timeStretch, release))
+        }
     }
 
     fun enqueueEos() {

From d683aabf488fa49bcea92558b3e0b198cffda056 Mon Sep 17 00:00:00 2001
From: kazuya okamoto <kazuya.226@studist.jp>
Date: Thu, 23 Dec 2021 16:56:31 +0900
Subject: [PATCH 2/3] Fix Failed to stop the muxer

---
 .../transcoder/internal/data/Writer.kt         | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/lib/src/main/java/com/otaliastudios/transcoder/internal/data/Writer.kt b/lib/src/main/java/com/otaliastudios/transcoder/internal/data/Writer.kt
index 1d8fc899..12aa1c2f 100644
--- a/lib/src/main/java/com/otaliastudios/transcoder/internal/data/Writer.kt
+++ b/lib/src/main/java/com/otaliastudios/transcoder/internal/data/Writer.kt
@@ -39,14 +39,16 @@ internal class Writer(
     override fun step(state: State.Ok<WriterData>, fresh: Boolean): State<Unit> {
         val (buffer, timestamp, flags) = state.value
         val eos = state is State.Eos
-        info.set(
-                buffer.position(),
-                buffer.remaining(),
-                timestamp,
-                if (eos) {
-                    flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM
-                } else flags
-        )
+        if (eos) {
+            info.set(0, 0, 0, flags and MediaCodec.BUFFER_FLAG_END_OF_STREAM)
+        } else {
+            info.set(
+                    buffer.position(),
+                    buffer.remaining(),
+                    timestamp,
+                    flags
+            )
+        }
         sink.writeTrack(track, buffer, info)
         state.value.release()
         return if (eos) State.Eos(Unit) else State.Ok(Unit)

From e6c501b0369232d514d1f2495b02ba2b71386f53 Mon Sep 17 00:00:00 2001
From: tateisu <tateisu@gmail.com>
Date: Fri, 24 Dec 2021 12:42:43 +0900
Subject: [PATCH 3/3] fix FilePathDataSource crash before initialize.

---
 .../otaliastudios/transcoder/source/DataSourceWrapper.java | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/lib/src/main/java/com/otaliastudios/transcoder/source/DataSourceWrapper.java b/lib/src/main/java/com/otaliastudios/transcoder/source/DataSourceWrapper.java
index 04adf79c..368a340c 100644
--- a/lib/src/main/java/com/otaliastudios/transcoder/source/DataSourceWrapper.java
+++ b/lib/src/main/java/com/otaliastudios/transcoder/source/DataSourceWrapper.java
@@ -96,13 +96,16 @@ public void releaseTrack(@NonNull TrackType type) {
 
     @Override
     public boolean isInitialized() {
-        return mSource.isInitialized();
+        return mSource != null && mSource.isInitialized();
     }
 
     @Override
     public void initialize() {
         // Make it easier for subclasses to put their logic in initialize safely.
-        if (!mSource.isInitialized()) {
+        if (!isInitialized()) {
+            if (mSource == null) {
+                throw new NullPointerException("DataSourceWrapper's source is not set!");
+            }
             mSource.initialize();
         }
     }
