Voice Broadcast - Introduce io.element.voice_broadcast_chunk key in voice messages

This commit is contained in:
Florian Renaud 2022-10-18 15:45:44 +02:00
parent fbf242756e
commit 1647fe233f
5 changed files with 48 additions and 10 deletions

View file

@ -16,11 +16,16 @@
package im.vector.app.features.voicebroadcast
import org.matrix.android.sdk.api.session.room.model.message.MessageAudioContent
object VoiceBroadcastConstants {
/** Voice Broadcast State Event. */
const val STATE_ROOM_VOICE_BROADCAST_INFO = "io.element.voice_broadcast_info"
/** Custom key passed to the [MessageAudioContent] with Voice Broadcast information. */
const val VOICE_BROADCAST_CHUNK_KEY = "io.element.voice_broadcast_chunk"
/** Default voice broadcast chunk duration, in seconds. */
const val DEFAULT_CHUNK_LENGTH_IN_SECONDS = 120
const val DEFAULT_CHUNK_LENGTH_IN_SECONDS = 10
}

View file

@ -16,14 +16,18 @@
package im.vector.app.features.voicebroadcast
import org.matrix.android.sdk.api.session.events.model.RelationType
import im.vector.app.features.voicebroadcast.model.VoiceBroadcastChunk
import org.matrix.android.sdk.api.session.events.model.Content
import org.matrix.android.sdk.api.session.events.model.getRelationContent
import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.message.MessageAudioEvent
fun MessageAudioEvent?.isVoiceBroadcast() = this?.getVoiceBroadcastEventId() != null
fun MessageAudioEvent?.isVoiceBroadcast() = this?.root?.getClearContent()?.get(VoiceBroadcastConstants.VOICE_BROADCAST_CHUNK_KEY) != null
fun MessageAudioEvent.getVoiceBroadcastEventId(): String? = if (isVoiceBroadcast()) root.getRelationContent()?.eventId else null
fun MessageAudioEvent.getVoiceBroadcastEventId(): String? =
// TODO Improve this condition by checking the referenced event type
root.takeIf { content.voiceMessageIndicator != null }
?.getRelationContent()?.takeIf { it.type == RelationType.REFERENCE }
?.eventId
fun MessageAudioEvent.getVoiceBroadcastChunk(): VoiceBroadcastChunk? {
@Suppress("UNCHECKED_CAST")
return (root.getClearContent()?.get(VoiceBroadcastConstants.VOICE_BROADCAST_CHUNK_KEY) as? Content).toModel<VoiceBroadcastChunk>()
}
val MessageAudioEvent.sequence: Int? get() = getVoiceBroadcastChunk()?.sequence

View file

@ -85,7 +85,7 @@ class VoiceBroadcastPlayer @Inject constructor(
private fun updatePlaylist(room: Room, eventId: String) {
val timelineEvents = room.timelineService().getTimelineEventsRelatedTo(RelationType.REFERENCE, eventId)
val audioEvents = timelineEvents.mapNotNull { it.root.asMessageAudioEvent() }
playlist = audioEvents.sortedBy { it.root.originServerTs }
playlist = audioEvents.sortedBy { it.getVoiceBroadcastChunk()?.sequence?.toLong() ?: it.root.originServerTs }
}
private fun startPlayback() {

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.voicebroadcast.model
import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class VoiceBroadcastChunk(
@Json(name = "sequence") val sequence: Int? = null
)

View file

@ -23,6 +23,7 @@ import im.vector.app.features.attachments.toContentAttachmentData
import im.vector.app.features.voicebroadcast.VoiceBroadcastConstants
import im.vector.app.features.voicebroadcast.VoiceBroadcastRecorder
import im.vector.app.features.voicebroadcast.model.MessageVoiceBroadcastInfoContent
import im.vector.app.features.voicebroadcast.model.VoiceBroadcastChunk
import im.vector.app.features.voicebroadcast.model.VoiceBroadcastState
import im.vector.app.features.voicebroadcast.model.asVoiceBroadcastEvent
import im.vector.lib.multipicker.utils.toMultiPickerAudioType
@ -98,7 +99,10 @@ class StartVoiceBroadcastUseCase @Inject constructor(
attachment = audioType.toContentAttachmentData(isVoiceMessage = true),
compressBeforeSending = false,
roomIds = emptySet(),
relatesTo = RelationDefaultContent(RelationType.REFERENCE, referenceEventId)
relatesTo = RelationDefaultContent(RelationType.REFERENCE, referenceEventId),
additionalContent = mapOf(
VoiceBroadcastConstants.VOICE_BROADCAST_CHUNK_KEY to VoiceBroadcastChunk(sequence = sequence).toContent()
)
)
}
}