51 lines
2.2 KiB
Kotlin
51 lines
2.2 KiB
Kotlin
package android.media
|
|
object AudioFormat {
|
|
const val CHANNEL_IN_MONO = 16
|
|
const val ENCODING_PCM_16BIT = 2
|
|
}
|
|
class MediaRecorder { object AudioSource { const val VOICE_COMMUNICATION = 7 } }
|
|
class AudioRecord(source: Int, rate: Int, channel: Int, encoding: Int, buffer: Int) {
|
|
companion object {
|
|
const val STATE_INITIALIZED = 1
|
|
const val RECORDSTATE_RECORDING = 3
|
|
const val READ_BLOCKING = 0
|
|
val activeDevices = java.util.concurrent.atomic.AtomicInteger()
|
|
val overlaps = java.util.concurrent.atomic.AtomicInteger()
|
|
@Volatile var scenario = "init"
|
|
@Volatile var partialReady = java.util.concurrent.CountDownLatch(1)
|
|
@Volatile var released = java.util.concurrent.CountDownLatch(1)
|
|
fun getMinBufferSize(rate: Int, channel: Int, encoding: Int) = 640
|
|
}
|
|
init { if (activeDevices.incrementAndGet() > 1) overlaps.incrementAndGet() }
|
|
private val mode = scenario
|
|
val state = if (mode == "init") 0 else STATE_INITIALIZED
|
|
var recordingState = 1
|
|
val audioSessionId = 1
|
|
fun startRecording() { if (mode == "start") throw SecurityException("synthetic mic revocation"); recordingState = RECORDSTATE_RECORDING }
|
|
private val stopped = java.util.concurrent.CountDownLatch(1)
|
|
private val ready = partialReady
|
|
private val done = released
|
|
private val chunks = when (mode) {
|
|
"partial" -> listOf(2, 0, 126, 200, 312, 638, 2, 100)
|
|
"fresh" -> listOf(640, 100)
|
|
else -> emptyList()
|
|
}
|
|
private var chunk = 0
|
|
private var streamOffset = 0
|
|
fun read(frame: ByteArray, offset: Int, size: Int, mode: Int): Int {
|
|
if (chunks.isEmpty()) return -3
|
|
if (chunk == chunks.size) {
|
|
ready.countDown()
|
|
check(stopped.await(2, java.util.concurrent.TimeUnit.SECONDS))
|
|
return -3
|
|
}
|
|
val count = chunks[chunk++]
|
|
check(count <= size && offset + count <= frame.size)
|
|
repeat(count) { frame[offset + it] = ((streamOffset + it) % 251).toByte() }
|
|
streamOffset += count
|
|
return count
|
|
}
|
|
fun stop() { recordingState = 1; stopped.countDown() }
|
|
fun release() { Thread.sleep(20); activeDevices.decrementAndGet(); done.countDown() }
|
|
}
|