Fix rendering voice message if the waveform data is corrupted.

This commit is contained in:
Onuray Sahin 2021-09-08 13:20:56 +03:00
parent c6a99f1bb1
commit 1df867f345
5 changed files with 14 additions and 11 deletions

1
changelog.d/3983.bugfix Normal file
View File

@ -0,0 +1 @@
Voice Message - Cannot render voice message if the waveform data is corrupted

View File

@ -36,7 +36,7 @@ data class ContentAttachmentData(
val queryUri: Uri, val queryUri: Uri,
val mimeType: String?, val mimeType: String?,
val type: Type, val type: Type,
val waveform: List<Int>? = null val waveform: List<Int?>? = null
) : Parcelable { ) : Parcelable {
@JsonClass(generateAdapter = false) @JsonClass(generateAdapter = false)

View File

@ -32,5 +32,5 @@ data class AudioWaveformInfo(
* List of integers between zero and 1024, inclusive. * List of integers between zero and 1024, inclusive.
*/ */
@Json(name = "waveform") @Json(name = "waveform")
val waveform: List<Int>? = null val waveform: List<Int?>? = null
) )

View File

@ -33,7 +33,7 @@ internal class WaveFormSanitizer @Inject constructor() {
* The array should have no less than 30 elements and no more than 120. * The array should have no less than 30 elements and no more than 120.
* List of integers between zero and 1024, inclusive. * List of integers between zero and 1024, inclusive.
*/ */
fun sanitize(waveForm: List<Int>?): List<Int>? { fun sanitize(waveForm: List<Int?>?): List<Int>? {
if (waveForm.isNullOrEmpty()) { if (waveForm.isNullOrEmpty()) {
return null return null
} }
@ -46,7 +46,7 @@ internal class WaveFormSanitizer @Inject constructor() {
val repeatTimes = ceil(MIN_NUMBER_OF_VALUES / waveForm.size.toDouble()).toInt() val repeatTimes = ceil(MIN_NUMBER_OF_VALUES / waveForm.size.toDouble()).toInt()
waveForm.map { value -> waveForm.map { value ->
repeat(repeatTimes) { repeat(repeatTimes) {
sizeInRangeList.add(value) sizeInRangeList.add(value ?: 0)
} }
} }
} }
@ -54,12 +54,12 @@ internal class WaveFormSanitizer @Inject constructor() {
val keepOneOf = ceil(waveForm.size.toDouble() / MAX_NUMBER_OF_VALUES).toInt() val keepOneOf = ceil(waveForm.size.toDouble() / MAX_NUMBER_OF_VALUES).toInt()
waveForm.mapIndexed { idx, value -> waveForm.mapIndexed { idx, value ->
if (idx % keepOneOf == 0) { if (idx % keepOneOf == 0) {
sizeInRangeList.add(value) sizeInRangeList.add(value ?: 0)
} }
} }
} }
else -> { else -> {
sizeInRangeList.addAll(waveForm) sizeInRangeList.addAll(waveForm.filterNotNull())
} }
} }

View File

@ -622,11 +622,13 @@ class MessageItemFactory @Inject constructor(
.highlighted(highlight) .highlighted(highlight)
} }
private fun List<Int>?.toFft(): List<Int>? { private fun List<Int?>?.toFft(): List<Int>? {
return this?.map { return this
// Value comes from AudioRecordView.maxReportableAmp, and 1024 is the max value in the Matrix spec ?.filterNotNull()
it * 22760 / 1024 ?.map {
} // Value comes from AudioRecordView.maxReportableAmp, and 1024 is the max value in the Matrix spec
it * 22760 / 1024
}
} }
companion object { companion object {