• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.jetpackcamera.utils
17 
18 import android.media.MediaMetadataRetriever
19 import android.media.MediaMetadataRetriever.METADATA_KEY_DURATION
20 import android.media.MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO
21 import android.media.MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO
22 import android.media.MediaMetadataRetriever.METADATA_KEY_MIMETYPE
23 import android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT
24 import android.media.MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH
25 import android.util.Rational
26 import android.util.Size
27 
useAndReleasenull28 inline fun <R> MediaMetadataRetriever.useAndRelease(
29     crossinline block: (MediaMetadataRetriever) -> R
30 ): R? {
31     try {
32         return block(this)
33     } finally {
34         release()
35     }
36 }
37 
hasAudionull38 fun MediaMetadataRetriever.hasAudio(): Boolean = extractMetadata(METADATA_KEY_HAS_AUDIO) == "yes"
39 
40 fun MediaMetadataRetriever.hasVideo(): Boolean = extractMetadata(METADATA_KEY_HAS_VIDEO) == "yes"
41 
42 fun MediaMetadataRetriever.getDurationMs(): Long =
43     checkNotNull(extractMetadata(METADATA_KEY_DURATION)?.toLong()) {
44         "duration unavailable"
45     }
46 
MediaMetadataRetrievernull47 fun MediaMetadataRetriever.getWidth(): Int =
48     checkNotNull(extractMetadata(METADATA_KEY_VIDEO_WIDTH)?.toInt()) {
49         "width unavailable"
50     }
51 
getHeightnull52 fun MediaMetadataRetriever.getHeight(): Int =
53     checkNotNull(extractMetadata(METADATA_KEY_VIDEO_HEIGHT)?.toInt()) {
54         "height information unavailable"
55     }
56 
getResolutionnull57 fun MediaMetadataRetriever.getResolution(): Size = Size(getWidth(), getHeight())
58 
59 fun MediaMetadataRetriever.getAspectRatio(): Rational = Rational(getWidth(), getHeight())
60 
61 fun MediaMetadataRetriever.getMimeType(): String = extractMetadata(METADATA_KEY_MIMETYPE)!!
62