Skip to content

Commit 1a31e49

Browse files
authored
Do not automatically initialize DefaultVideoMetadataExtractor's data source (#5157)
This will cause a crash for image attachments
1 parent 074b238 commit 1a31e49

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/attachments/video/VideoMetadataExtractor.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface VideoMetadataExtractor : AutoCloseable {
2929

3030
@ContributesBinding(AppScope::class)
3131
class DefaultVideoMetadataExtractor @AssistedInject constructor(
32-
@ApplicationContext context: Context,
32+
@ApplicationContext private val context: Context,
3333
@Assisted private val uri: Uri,
3434
) : VideoMetadataExtractor {
3535
@ContributesBinding(AppScope::class)
@@ -38,15 +38,16 @@ class DefaultVideoMetadataExtractor @AssistedInject constructor(
3838
override fun create(uri: Uri): DefaultVideoMetadataExtractor
3939
}
4040

41-
private val mediaMetadataRetriever = MediaMetadataRetriever()
42-
43-
init {
44-
mediaMetadataRetriever.setDataSource(context, uri)
41+
// Don't use `by lazy` so we can catch any exceptions thrown during initialization
42+
private val mediaMetadataRetriever = lazy {
43+
MediaMetadataRetriever().apply {
44+
setDataSource(context, uri)
45+
}
4546
}
4647

4748
override fun getSize(): Result<Size> = runCatchingExceptions {
48-
val width = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt()
49-
val height = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toInt()
49+
val width = mediaMetadataRetriever.value.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toInt()
50+
val height = mediaMetadataRetriever.value.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toInt()
5051

5152
@Suppress("ComplexCondition")
5253
if (width != null && width > 0 && height != null && height > 0) {
@@ -57,12 +58,14 @@ class DefaultVideoMetadataExtractor @AssistedInject constructor(
5758
}
5859

5960
override fun getDuration(): Result<Long> = runCatchingExceptions {
60-
mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong()
61+
mediaMetadataRetriever.value.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)?.toLong()
6162
?.takeIf { it > 0L }
6263
?: error("Could not retrieve video duration from metadata")
6364
}
6465

6566
override fun close() {
66-
mediaMetadataRetriever.release()
67+
if (mediaMetadataRetriever.isInitialized()) {
68+
mediaMetadataRetriever.value.release()
69+
}
6770
}
6871
}

0 commit comments

Comments
 (0)