Compute file size from chunk length

This commit is contained in:
Florian Renaud 2022-10-14 12:23:02 +02:00
parent fb9c747a20
commit 64e6a2bfab
5 changed files with 19 additions and 11 deletions

View file

@ -20,4 +20,7 @@ object VoiceBroadcastConstants {
/** Voice Broadcast State Event. */
const val STATE_ROOM_VOICE_BROADCAST_INFO = "io.element.voice_broadcast_info"
/** Default voice broadcast chunk duration, in seconds */
const val DEFAULT_CHUNK_LENGTH = 5
}

View file

@ -23,6 +23,8 @@ interface VoiceBroadcastRecorder : VoiceRecorder {
var listener: Listener?
fun startRecord(roomId: String, chunkLength: Int)
fun interface Listener {
fun onVoiceMessageCreated(file: File)
}

View file

@ -28,7 +28,7 @@ class VoiceBroadcastRecorderQ(
context: Context,
) : AbstractVoiceRecorderQ(context), VoiceBroadcastRecorder {
private val maxFileSize = 25_000L // 0,025 Mb = 25 Kb ~= 6s
private var maxFileSize = 25_000L // 0,025 Mb = 25 Kb ~= 6s
override var listener: VoiceBroadcastRecorder.Listener? = null
@ -49,6 +49,11 @@ class VoiceBroadcastRecorderQ(
}
}
override fun startRecord(roomId: String, chunkLength: Int) {
maxFileSize = (chunkLength * 0.004166).toLong() // TODO change this approximate conversion
startRecord(roomId)
}
override fun stopRecord() {
super.stopRecord()
notifyOutputFileCreated()

View file

@ -41,7 +41,7 @@ data class MessageVoiceBroadcastInfoContent(
/** The [VoiceBroadcastState] value. **/
@Json(name = "state") val voiceBroadcastStateStr: String = "",
/** The length of the voice chunks in seconds. **/
@Json(name = "chunk_length") val chunkLength: Long? = null,
@Json(name = "chunk_length") val chunkLength: Int? = null,
) : MessageContent {
val voiceBroadcastState: VoiceBroadcastState? = VoiceBroadcastState.values()

View file

@ -17,7 +17,6 @@
package im.vector.app.features.voicebroadcast.usecase
import android.content.Context
import android.os.Build
import androidx.core.content.FileProvider
import im.vector.app.core.resources.BuildMeta
import im.vector.app.features.attachments.toContentAttachmentData
@ -66,25 +65,24 @@ class StartVoiceBroadcastUseCase @Inject constructor(
private suspend fun startVoiceBroadcast(room: Room) {
Timber.d("## StartVoiceBroadcastUseCase: Send new voice broadcast info state event")
val chunkLength = VoiceBroadcastConstants.DEFAULT_CHUNK_LENGTH // Todo Get the length from the room settings
val eventId = room.stateService().sendStateEvent(
eventType = VoiceBroadcastConstants.STATE_ROOM_VOICE_BROADCAST_INFO,
stateKey = session.myUserId,
body = MessageVoiceBroadcastInfoContent(
voiceBroadcastStateStr = VoiceBroadcastState.STARTED.value,
chunkLength = 5L, // TODO Get length from voice broadcast settings
chunkLength = chunkLength,
).toContent()
)
startRecording(room, eventId)
startRecording(room, eventId, chunkLength)
}
private fun startRecording(room: Room, eventId: String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
voiceBroadcastRecorder?.listener = VoiceBroadcastRecorder.Listener { file ->
sendVoiceFile(room, file, eventId)
}
voiceBroadcastRecorder?.startRecord(room.roomId)
private fun startRecording(room: Room, eventId: String, chunkLength: Int) {
voiceBroadcastRecorder?.listener = VoiceBroadcastRecorder.Listener { file ->
sendVoiceFile(room, file, eventId)
}
voiceBroadcastRecorder?.startRecord(room.roomId, chunkLength)
}
private fun sendVoiceFile(room: Room, voiceMessageFile: File, referenceEventId: String) {