• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 
17 package com.android.photopicker.features.preparemedia
18 
19 import android.content.Context
20 import android.media.ApplicationMediaCapabilities
21 import android.net.Uri
22 import com.android.photopicker.data.PICKER_SEGMENT
23 import com.android.photopicker.data.PICKER_TRANSCODED_SEGMENT
24 import com.android.photopicker.data.model.Media
25 
26 /** Provides methods to help video transcode. */
27 interface Transcoder {
28 
29     /** Data class to hold details of the transcoding video. */
30     data class VideoInfo(
31         val duration: Int,
32         val colorStandard: Int,
33         val colorTransfer: Int,
34         val mimeType: Int,
35     )
36 
37     /**
38      * Checks if a transcode is required for the given video.
39      *
40      * @param context The context.
41      * @param mediaCapabilities The application media capabilities.
42      * @param video The video to check.
43      */
isTranscodeRequirednull44     fun isTranscodeRequired(
45         context: Context,
46         mediaCapabilities: ApplicationMediaCapabilities?,
47         video: Media.Video,
48     ): Boolean
49 
50     /** Returns details of the video that needs transcoding */
51     fun getTranscodingVideoInfo(): VideoInfo?
52 
53     companion object {
54 
55         /**
56          * Converts a picker provider URI to a transcoded URI.
57          *
58          * @param pickerUri The picker provider URI.
59          * @return The transcoded URI.
60          * @throws IllegalArgumentException If the picker provider URI is not valid.
61          */
62         fun toTranscodedUri(pickerUri: Uri): Uri {
63             val segments = pickerUri.pathSegments
64 
65             require(segments.size == 5) { "Unexpected picker provider URI: $pickerUri" }
66 
67             val builder = Uri.Builder().scheme(pickerUri.scheme).authority(pickerUri.authority)
68 
69             for (i in segments.indices) {
70                 if (PICKER_SEGMENT == segments[i]) {
71                     // Replace picker segment with picker transcoded segment.
72                     builder.appendPath(PICKER_TRANSCODED_SEGMENT)
73                 } else {
74                     builder.appendPath(segments[i])
75                 }
76             }
77 
78             return builder.build()
79         }
80     }
81 }
82