Skip to content

Commit 8c783d6

Browse files
committed
Review sleep constants and behavior
1 parent 0fed4a5 commit 8c783d6

File tree

9 files changed

+32
-29
lines changed

9 files changed

+32
-29
lines changed

lib/src/main/java/com/otaliastudios/transcoder/internal/Segment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal class Segment(
2828
when(val s = state ?: return false) {
2929
is State.Ok -> return false
3030
is State.Retry -> return false
31-
is State.Wait -> return s.withSleep
31+
is State.Wait -> return s.sleep
3232
}
3333
}
3434

lib/src/main/java/com/otaliastudios/transcoder/internal/audio/AudioEngine.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@ internal class AudioEngine(
6363

6464
override fun drain(): State<EncoderData> {
6565
if (chunks.isEmpty()) {
66+
// nothing was enqueued
6667
log.i("drain(): no chunks, waiting...")
67-
return State.Wait(true)
68+
return State.Wait(false)
6869
}
6970
val (outBytes, outId) = next.buffer() ?: return run {
71+
// dequeueInputBuffer failed
7072
log.i("drain(): no next buffer, waiting...")
7173
State.Wait(true)
7274
}

lib/src/main/java/com/otaliastudios/transcoder/internal/codec/Decoder.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal class Decoder(
6262
}
6363

6464
override fun buffer(): Pair<ByteBuffer, Int>? {
65-
val id = codec.dequeueInputBuffer(0)
65+
val id = codec.dequeueInputBuffer(100)
6666
return if (id >= 0) {
6767
dequeuedInputs++
6868
buffers.getInputBuffer(id) to id
@@ -88,7 +88,7 @@ internal class Decoder(
8888
}
8989

9090
override fun drain(): State<DecoderData> {
91-
val result = codec.dequeueOutputBuffer(info, 0)
91+
val result = codec.dequeueOutputBuffer(info, 100)
9292
return when (result) {
9393
INFO_TRY_AGAIN_LATER -> {
9494
log.i("drain(): got INFO_TRY_AGAIN_LATER, waiting.")
@@ -116,6 +116,7 @@ internal class Decoder(
116116
}
117117
if (isEos) State.Eos(data) else State.Ok(data)
118118
} else {
119+
// frame was dropped, no need to sleep
119120
codec.releaseOutputBuffer(result, false)
120121
State.Wait(false)
121122
}.also {

lib/src/main/java/com/otaliastudios/transcoder/internal/codec/Encoder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ internal class Encoder(
7373
}
7474

7575
override fun buffer(): Pair<ByteBuffer, Int>? {
76-
val id = codec.dequeueInputBuffer(0)
76+
val id = codec.dequeueInputBuffer(100)
7777
return if (id >= 0) {
7878
dequeuedInputs++
7979
buffers.getInputBuffer(id) to id
@@ -107,7 +107,7 @@ internal class Encoder(
107107
}
108108

109109
override fun drain(): State<WriterData> {
110-
val timeoutUs = if (eosReceivedButNotEnqueued) 5000L else 0L
110+
val timeoutUs = if (eosReceivedButNotEnqueued) 5000L else 100L
111111
return when (val result = codec.dequeueOutputBuffer(info, timeoutUs)) {
112112
INFO_TRY_AGAIN_LATER -> {
113113
if (eosReceivedButNotEnqueued) {

lib/src/main/java/com/otaliastudios/transcoder/internal/data/Reader.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ internal class Reader(
2727
private inline fun nextBufferOrWait(action: (ByteBuffer, Int) -> State<ReaderData>): State<ReaderData> {
2828
val buffer = next.buffer()
2929
if (buffer == null) {
30+
// dequeueInputBuffer failed
3031
log.v("Returning State.Wait because buffer is null.")
3132
return State.Wait(true)
3233
} else {

lib/src/main/java/com/otaliastudios/transcoder/internal/pipeline/Pipeline.kt

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,26 @@ internal class Pipeline private constructor(name: String, private val chain: Lis
2626
if (index < head) return@forEachIndexed
2727
val fresh = head == 0 || index != head
2828

29-
when(val okOrWaitState = executeStep(state, step, fresh)) {
30-
is State.Wait -> return State.Wait(okOrWaitState.withSleep)
31-
is State.Ok -> state = okOrWaitState
32-
is State.Retry -> throw RuntimeException("Unexpected")
29+
fun executeStep(fresh: Boolean): State.Wait<Any>? {
30+
return when (val newState = step.step(state, fresh)) {
31+
is State.Eos -> {
32+
state = newState
33+
log.i("execute(): EOS from ${step.name} (#$index/${chain.size}).")
34+
headState = newState
35+
headIndex = index + 1
36+
null
37+
}
38+
is State.Ok -> {
39+
state = newState
40+
null
41+
}
42+
is State.Retry -> executeStep(fresh = false)
43+
is State.Wait -> return newState
44+
}
3345
}
3446

35-
// log.v("execute(): executed ${step.name} (#$index/${chain.size}). result=$state")
36-
if (state is State.Eos) {
37-
log.i("execute(): EOS from ${step.name} (#$index/${chain.size}).")
38-
headState = state
39-
headIndex = index + 1
40-
}
47+
val wait = executeStep(fresh)
48+
if (wait != null) return State.Wait(wait.sleep)
4149
}
4250
return when {
4351
chain.isEmpty() -> State.Eos(Unit)
@@ -50,15 +58,6 @@ internal class Pipeline private constructor(name: String, private val chain: Lis
5058
chain.forEach { it.release() }
5159
}
5260

53-
private fun executeStep(previous: State.Ok<Any>, step: AnyStep, fresh: Boolean): State<Any> {
54-
val state = step.step(previous, fresh)
55-
return when (state) {
56-
is State.Ok -> state
57-
is State.Retry -> executeStep(previous, step, fresh = false)
58-
is State.Wait -> state
59-
}
60-
}
61-
6261
companion object {
6362
@Suppress("UNCHECKED_CAST")
6463
internal fun build(name: String, builder: () -> Builder<*, Channel> = { Builder<Unit, Channel>() }): Pipeline {

lib/src/main/java/com/otaliastudios/transcoder/internal/pipeline/State.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ internal sealed class State<out T> {
1313
}
1414

1515
// couldn't run, but might in the future
16-
class Wait<T>(val withSleep: Boolean) : State<T>() {
17-
override fun toString() = "State.Wait(withSleep: $withSleep)"
16+
class Wait<T>(val sleep: Boolean) : State<T>() {
17+
override fun toString() = "State.Wait($sleep)"
1818
}
1919

2020
// call again as soon as possible

lib/src/main/java/com/otaliastudios/transcoder/internal/thumbnails/DefaultThumbnailsEngine.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ internal class DefaultThumbnailsEngine(
134134
}
135135

136136
companion object {
137-
private val WAIT_MS = 10L
137+
private val WAIT_MS = 2L
138138
private val PROGRESS_LOOPS = 10L
139139
}
140140
}

lib/src/main/java/com/otaliastudios/transcoder/internal/transcode/DefaultTranscodeEngine.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ internal class DefaultTranscodeEngine(
149149

150150

151151
companion object {
152-
private val WAIT_MS = 10L
152+
private val WAIT_MS = 2L
153153
private val PROGRESS_LOOPS = 10L
154154
}
155155
}

0 commit comments

Comments
 (0)