summaryrefslogtreecommitdiff
path: root/apex/media/framework/java/android/media/MediaTranscodingManager.java
blob: 93d58d07f81af5efc62c932aeffb59c0c4132d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * 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 android.media;

import android.annotation.CallbackExecutor;
import android.annotation.IntDef;
import android.annotation.IntRange;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.app.ActivityManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.net.Uri;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.ServiceSpecificException;
import android.system.Os;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.modules.annotation.MinSdk;
import com.android.modules.utils.build.SdkLevel;

import java.io.FileNotFoundException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 Android 12 introduces Compatible media transcoding feature.  See
 <a href="https://developer.android.com/about/versions/12/features#compatible_media_transcoding">
 Compatible media transcoding</a>. MediaTranscodingManager provides an interface to the system's media
 transcoding service and can be used to transcode media files, e.g. transcoding a video from HEVC to
 AVC.

 <h3>Transcoding Types</h3>
 <h4>Video Transcoding</h4>
 When transcoding a video file, the video track will be transcoded based on the desired track format
 and the audio track will be pass through without any modification.
 <p class=note>
 Note that currently only support transcoding video file in mp4 format and with single video track.

 <h3>Transcoding Request</h3>
 <p>
 To transcode a media file, first create a {@link TranscodingRequest} through its builder class
 {@link VideoTranscodingRequest.Builder}. Transcode requests are then enqueue to the manager through
 {@link MediaTranscodingManager#enqueueRequest(
         TranscodingRequest, Executor, OnTranscodingFinishedListener)}
 TranscodeRequest are processed based on client process's priority and request priority. When a
 transcode operation is completed the caller is notified via its
 {@link OnTranscodingFinishedListener}.
 In the meantime the caller may use the returned TranscodingSession object to cancel or check the
 status of a specific transcode operation.
 <p>
 Here is an example where <code>Builder</code> is used to specify all parameters

 <pre class=prettyprint>
 VideoTranscodingRequest request =
    new VideoTranscodingRequest.Builder(srcUri, dstUri, videoFormat).build();
 }</pre>
 @hide
 */
@MinSdk(Build.VERSION_CODES.S)
@SystemApi
public final class MediaTranscodingManager {
    private static final String TAG = "MediaTranscodingManager";

    /** Maximum number of retry to connect to the service. */
    private static final int CONNECT_SERVICE_RETRY_COUNT = 100;

    /** Interval between trying to reconnect to the service. */
    private static final int INTERVAL_CONNECT_SERVICE_RETRY_MS = 40;

    /** Default bpp(bits-per-pixel) to use for calculating default bitrate. */
    private static final float BPP = 0.25f;

    /**
     * Listener that gets notified when a transcoding operation has finished.
     * This listener gets notified regardless of how the operation finished. It is up to the
     * listener implementation to check the result and take appropriate action.
     */
    @FunctionalInterface
    public interface OnTranscodingFinishedListener {
        /**
         * Called when the transcoding operation has finished. The receiver may use the
         * TranscodingSession to check the result, i.e. whether the operation succeeded, was
         * canceled or if an error occurred.
         *
         * @param session The TranscodingSession instance for the finished transcoding operation.
         */
        void onTranscodingFinished(@NonNull TranscodingSession session);
    }

    private final Context mContext;
    private ContentResolver mContentResolver;
    private final String mPackageName;
    private final int mPid;
    private final int mUid;
    private final boolean mIsLowRamDevice;
    private final ExecutorService mExecutor = Executors.newSingleThreadExecutor();
    private final HashMap<Integer, TranscodingSession> mPendingTranscodingSessions = new HashMap();
    private final Object mLock = new Object();
    @GuardedBy("mLock")
    @NonNull private ITranscodingClient mTranscodingClient = null;
    private static MediaTranscodingManager sMediaTranscodingManager;

    private void handleTranscodingFinished(int sessionId, TranscodingResultParcel result) {
        synchronized (mPendingTranscodingSessions) {
            // Gets the session associated with the sessionId and removes it from
            // mPendingTranscodingSessions.
            final TranscodingSession session = mPendingTranscodingSessions.remove(sessionId);

            if (session == null) {
                // This should not happen in reality.
                Log.e(TAG, "Session " + sessionId + " is not in Pendingsessions");
                return;
            }

            // Updates the session status and result.
            session.updateStatusAndResult(TranscodingSession.STATUS_FINISHED,
                    TranscodingSession.RESULT_SUCCESS,
                    TranscodingSession.ERROR_NONE);

            // Notifies client the session is done.
            if (session.mListener != null && session.mListenerExecutor != null) {
                session.mListenerExecutor.execute(
                        () -> session.mListener.onTranscodingFinished(session));
            }
        }
    }

    private void handleTranscodingFailed(int sessionId, int errorCode) {
        synchronized (mPendingTranscodingSessions) {
            // Gets the session associated with the sessionId and removes it from
            // mPendingTranscodingSessions.
            final TranscodingSession session = mPendingTranscodingSessions.remove(sessionId);

            if (session == null) {
                // This should not happen in reality.
                Log.e(TAG, "Session " + sessionId + " is not in Pendingsessions");
                return;
            }

            // Updates the session status and result.
            session.updateStatusAndResult(TranscodingSession.STATUS_FINISHED,
                    TranscodingSession.RESULT_ERROR, errorCode);

            // Notifies client the session failed.
            if (session.mListener != null && session.mListenerExecutor != null) {
                session.mListenerExecutor.execute(
                        () -> session.mListener.onTranscodingFinished(session));
            }
        }
    }

    private void handleTranscodingProgressUpdate(int sessionId, int newProgress) {
        synchronized (mPendingTranscodingSessions) {
            // Gets the session associated with the sessionId.
            final TranscodingSession session = mPendingTranscodingSessions.get(sessionId);

            if (session == null) {
                // This should not happen in reality.
                Log.e(TAG, "Session " + sessionId + " is not in Pendingsessions");
                return;
            }

            // Updates the session progress.
            session.updateProgress(newProgress);

            // Notifies client the progress update.
            if (session.mProgressUpdateExecutor != null
                    && session.mProgressUpdateListener != null) {
                session.mProgressUpdateExecutor.execute(
                        () -> session.mProgressUpdateListener.onProgressUpdate(session,
                                newProgress));
            }
        }
    }

    private IMediaTranscodingService getService(boolean retry) {
        // Do not try to get the service on pre-S. The service is lazy-start and getting the
        // service could block.
        if (!SdkLevel.isAtLeastS()) {
            return null;
        }
        // Do not try to get the service on AndroidGo (low-ram) devices.
        if (mIsLowRamDevice) {
            return null;
        }
        int retryCount = !retry ? 1 :  CONNECT_SERVICE_RETRY_COUNT;
        Log.i(TAG, "get service with retry " + retryCount);
        for (int count = 1;  count <= retryCount; count++) {
            Log.d(TAG, "Trying to connect to service. Try count: " + count);
            IMediaTranscodingService service = IMediaTranscodingService.Stub.asInterface(
                    MediaFrameworkInitializer
                    .getMediaServiceManager()
                    .getMediaTranscodingServiceRegisterer()
                    .get());
            if (service != null) {
                return service;
            }
            try {
                // Sleep a bit before retry.
                Thread.sleep(INTERVAL_CONNECT_SERVICE_RETRY_MS);
            } catch (InterruptedException ie) {
                /* ignore */
            }
        }
        Log.w(TAG, "Failed to get service");
        return null;
    }

    /*
     * Handle client binder died event.
     * Upon receiving a binder died event of the client, we will do the following:
     * 1) For the session that is running, notify the client that the session is failed with
     *    error code,  so client could choose to retry the session or not.
     *    TODO(hkuang): Add a new error code to signal service died error.
     * 2) For the sessions that is still pending or paused, we will resubmit the session
     *    once we successfully reconnect to the service and register a new client.
     * 3) When trying to connect to the service and register a new client. The service may need time
     *    to reboot or never boot up again. So we will retry for a number of times. If we still
     *    could not connect, we will notify client session failure for the pending and paused
     *    sessions.
     */
    private void onClientDied() {
        synchronized (mLock) {
            mTranscodingClient = null;
        }

        // Delegates the session notification and retry to the executor as it may take some time.
        mExecutor.execute(() -> {
            // List to track the sessions that we want to retry.
            List<TranscodingSession> retrySessions = new ArrayList<TranscodingSession>();

            // First notify the client of session failure for all the running sessions.
            synchronized (mPendingTranscodingSessions) {
                for (Map.Entry<Integer, TranscodingSession> entry :
                        mPendingTranscodingSessions.entrySet()) {
                    TranscodingSession session = entry.getValue();

                    if (session.getStatus() == TranscodingSession.STATUS_RUNNING) {
                        session.updateStatusAndResult(TranscodingSession.STATUS_FINISHED,
                                TranscodingSession.RESULT_ERROR,
                                TranscodingSession.ERROR_SERVICE_DIED);

                        // Remove the session from pending sessions.
                        mPendingTranscodingSessions.remove(entry.getKey());

                        if (session.mListener != null && session.mListenerExecutor != null) {
                            Log.i(TAG, "Notify client session failed");
                            session.mListenerExecutor.execute(
                                    () -> session.mListener.onTranscodingFinished(session));
                        }
                    } else if (session.getStatus() == TranscodingSession.STATUS_PENDING
                            || session.getStatus() == TranscodingSession.STATUS_PAUSED) {
                        // Add the session to retrySessions to handle them later.
                        retrySessions.add(session);
                    }
                }
            }

            // Try to register with the service once it boots up.
            IMediaTranscodingService service = getService(true /*retry*/);
            boolean haveTranscodingClient = false;
            if (service != null) {
                synchronized (mLock) {
                    mTranscodingClient = registerClient(service);
                    if (mTranscodingClient != null) {
                        haveTranscodingClient = true;
                    }
                }
            }

            for (TranscodingSession session : retrySessions) {
                // Notify the session failure if we fails to connect to the service or fail
                // to retry the session.
                if (!haveTranscodingClient) {
                    // TODO(hkuang): Return correct error code to the client.
                    handleTranscodingFailed(session.getSessionId(), 0 /*unused */);
                }

                try {
                    // Do not set hasRetried for retry initiated by MediaTranscodingManager.
                    session.retryInternal(false /*setHasRetried*/);
                } catch (Exception re) {
                    // TODO(hkuang): Return correct error code to the client.
                    handleTranscodingFailed(session.getSessionId(), 0 /*unused */);
                }
            }
        });
    }

    private void updateStatus(int sessionId, int status) {
        synchronized (mPendingTranscodingSessions) {
            final TranscodingSession session = mPendingTranscodingSessions.get(sessionId);

            if (session == null) {
                // This should not happen in reality.
                Log.e(TAG, "Session " + sessionId + " is not in Pendingsessions");
                return;
            }

            // Updates the session status.
            session.updateStatus(status);
        }
    }

    // Just forwards all the events to the event handler.
    private ITranscodingClientCallback mTranscodingClientCallback =
            new ITranscodingClientCallback.Stub() {
                // TODO(hkuang): Add more unit test to test difference file open mode.
                @Override
                public ParcelFileDescriptor openFileDescriptor(String fileUri, String mode)
                        throws RemoteException {
                    if (!mode.equals("r") && !mode.equals("w") && !mode.equals("rw")) {
                        Log.e(TAG, "Unsupport mode: " + mode);
                        return null;
                    }

                    Uri uri = Uri.parse(fileUri);
                    try {
                        AssetFileDescriptor afd = mContentResolver.openAssetFileDescriptor(uri,
                                mode);
                        if (afd != null) {
                            return afd.getParcelFileDescriptor();
                        }
                    } catch (FileNotFoundException e) {
                        Log.w(TAG, "Cannot find content uri: " + uri, e);
                    } catch (SecurityException e) {
                        Log.w(TAG, "Cannot open content uri: " + uri, e);
                    } catch (Exception e) {
                        Log.w(TAG, "Unknown content uri: " + uri, e);
                    }
                    return null;
                }

                @Override
                public void onTranscodingStarted(int sessionId) throws RemoteException {
                    updateStatus(sessionId, TranscodingSession.STATUS_RUNNING);
                }

                @Override
                public void onTranscodingPaused(int sessionId) throws RemoteException {
                    updateStatus(sessionId, TranscodingSession.STATUS_PAUSED);
                }

                @Override
                public void onTranscodingResumed(int sessionId) throws RemoteException {
                    updateStatus(sessionId, TranscodingSession.STATUS_RUNNING);
                }

                @Override
                public void onTranscodingFinished(int sessionId, TranscodingResultParcel result)
                        throws RemoteException {
                    handleTranscodingFinished(sessionId, result);
                }

                @Override
                public void onTranscodingFailed(int sessionId, int errorCode)
                        throws RemoteException {
                    handleTranscodingFailed(sessionId, errorCode);
                }

                @Override
                public void onAwaitNumberOfSessionsChanged(int sessionId, int oldAwaitNumber,
                        int newAwaitNumber) throws RemoteException {
                    //TODO(hkuang): Implement this.
                }

                @Override
                public void onProgressUpdate(int sessionId, int newProgress)
                        throws RemoteException {
                    handleTranscodingProgressUpdate(sessionId, newProgress);
                }
            };

    private ITranscodingClient registerClient(IMediaTranscodingService service) {
        synchronized (mLock) {
            try {
                // Registers the client with MediaTranscoding service.
                mTranscodingClient = service.registerClient(
                        mTranscodingClientCallback,
                        mPackageName,
                        mPackageName);

                if (mTranscodingClient != null) {
                    mTranscodingClient.asBinder().linkToDeath(() -> onClientDied(), /* flags */ 0);
                }
            } catch (Exception ex) {
                Log.e(TAG, "Failed to register new client due to exception " + ex);
                mTranscodingClient = null;
            }
        }
        return mTranscodingClient;
    }

    /**
     * @hide
     */
    public MediaTranscodingManager(@NonNull Context context) {
        mContext = context;
        mContentResolver = mContext.getContentResolver();
        mPackageName = mContext.getPackageName();
        mUid = Os.getuid();
        mPid = Os.getpid();
        mIsLowRamDevice = mContext.getSystemService(ActivityManager.class).isLowRamDevice();
    }

    /**
     * Abstract base class for all the TranscodingRequest.
     * <p> TranscodingRequest encapsulates the desired configuration for the transcoding.
     */
    public abstract static class TranscodingRequest {
        /**
         *
         * Default transcoding type.
         * @hide
         */
        public static final int TRANSCODING_TYPE_UNKNOWN = 0;

        /**
         * TRANSCODING_TYPE_VIDEO indicates that client wants to perform transcoding on a video.
         * <p>Note that currently only support transcoding video file in mp4 format.
         * @hide
         */
        public static final int TRANSCODING_TYPE_VIDEO = 1;

        /**
         * TRANSCODING_TYPE_IMAGE indicates that client wants to perform transcoding on an image.
         * @hide
         */
        public static final int TRANSCODING_TYPE_IMAGE = 2;

        /** @hide */
        @IntDef(prefix = {"TRANSCODING_TYPE_"}, value = {
                TRANSCODING_TYPE_UNKNOWN,
                TRANSCODING_TYPE_VIDEO,
                TRANSCODING_TYPE_IMAGE,
        })
        @Retention(RetentionPolicy.SOURCE)
        public @interface TranscodingType {}

        /**
         * Default value.
         *
         * @hide
         */
        public static final int PRIORITY_UNKNOWN = 0;
        /**
         * PRIORITY_REALTIME indicates that the transcoding request is time-critical and that the
         * client wants the transcoding result as soon as possible.
         * <p> Set PRIORITY_REALTIME only if the transcoding is time-critical as it will involve
         * performance penalty due to resource reallocation to prioritize the sessions with higher
         * priority.
         *
         * @hide
         */
        public static final int PRIORITY_REALTIME = 1;

        /**
         * PRIORITY_OFFLINE indicates the transcoding is not time-critical and the client does not
         * need the transcoding result as soon as possible.
         * <p>Sessions with PRIORITY_OFFLINE will be scheduled behind PRIORITY_REALTIME. Always set
         * to
         * PRIORITY_OFFLINE if client does not need the result as soon as possible and could accept
         * delay of the transcoding result.
         *
         * @hide
         *
         */
        public static final int PRIORITY_OFFLINE = 2;

        /** @hide */
        @IntDef(prefix = {"PRIORITY_"}, value = {
                PRIORITY_UNKNOWN,
                PRIORITY_REALTIME,
                PRIORITY_OFFLINE,
        })
        @Retention(RetentionPolicy.SOURCE)
        public @interface TranscodingPriority {}

        /** Uri of the source media file. */
        private @NonNull Uri mSourceUri;

        /** Uri of the destination media file. */
        private @NonNull Uri mDestinationUri;

        /** FileDescriptor of the source media file. */
        private @Nullable ParcelFileDescriptor mSourceFileDescriptor;

        /** FileDescriptor of the destination media file. */
        private @Nullable ParcelFileDescriptor mDestinationFileDescriptor;

        /**
         *  The UID of the client that the TranscodingRequest is for. Only privileged caller could
         *  set this Uid as only they could do the transcoding on behalf of the client.
         *  -1 means not available.
         */
        private int mClientUid = -1;

        /**
         *  The Pid of the client that the TranscodingRequest is for. Only privileged caller could
         *  set this Uid as only they could do the transcoding on behalf of the client.
         *  -1 means not available.
         */
        private int mClientPid = -1;

        /** Type of the transcoding. */
        private @TranscodingType int mType = TRANSCODING_TYPE_UNKNOWN;

        /** Priority of the transcoding. */
        private @TranscodingPriority int mPriority = PRIORITY_UNKNOWN;

        /**
         * Desired image format for the destination file.
         * <p> If this is null, source file's image track will be passed through and copied to the
         * destination file.
         * @hide
         */
        private @Nullable MediaFormat mImageFormat = null;

        @VisibleForTesting
        private TranscodingTestConfig mTestConfig = null;

        /**
         * Prevent public constructor access.
         */
        /* package private */ TranscodingRequest() {
        }

        private TranscodingRequest(Builder b) {
            mSourceUri = b.mSourceUri;
            mSourceFileDescriptor = b.mSourceFileDescriptor;
            mDestinationUri = b.mDestinationUri;
            mDestinationFileDescriptor = b.mDestinationFileDescriptor;
            mClientUid = b.mClientUid;
            mClientPid = b.mClientPid;
            mPriority = b.mPriority;
            mType = b.mType;
            mTestConfig = b.mTestConfig;
        }

        /**
         * Return the type of the transcoding.
         * @hide
         */
        @TranscodingType
        public int getType() {
            return mType;
        }

        /** Return source uri of the transcoding. */
        @NonNull
        public Uri getSourceUri() {
            return mSourceUri;
        }

        /**
         * Return source file descriptor of the transcoding.
         * This will be null if client has not provided it.
         */
        @Nullable
        public ParcelFileDescriptor getSourceFileDescriptor() {
            return mSourceFileDescriptor;
        }

        /** Return the UID of the client that this request is for. -1 means not available. */
        public int getClientUid() {
            return mClientUid;
        }

        /** Return the PID of the client that this request is for. -1 means not available. */
        public int getClientPid() {
            return mClientPid;
        }

        /** Return destination uri of the transcoding. */
        @NonNull
        public Uri getDestinationUri() {
            return mDestinationUri;
        }

        /**
         * Return destination file descriptor of the transcoding.
         * This will be null if client has not provided it.
         */
        @Nullable
        public ParcelFileDescriptor getDestinationFileDescriptor() {
            return mDestinationFileDescriptor;
        }

        /**
         * Return priority of the transcoding.
         * @hide
         */
        @TranscodingPriority
        public int getPriority() {
            return mPriority;
        }

        /**
         * Return TestConfig of the transcoding.
         * @hide
         */
        @Nullable
        public TranscodingTestConfig getTestConfig() {
            return mTestConfig;
        }

        abstract void writeFormatToParcel(TranscodingRequestParcel parcel);

        /* Writes the TranscodingRequest to a parcel. */
        private TranscodingRequestParcel writeToParcel(@NonNull Context context) {
            TranscodingRequestParcel parcel = new TranscodingRequestParcel();
            switch (mPriority) {
            case PRIORITY_OFFLINE:
                parcel.priority = TranscodingSessionPriority.kUnspecified;
                break;
            case PRIORITY_REALTIME:
            case PRIORITY_UNKNOWN:
            default:
                parcel.priority = TranscodingSessionPriority.kNormal;
                break;
            }
            parcel.transcodingType = mType;
            parcel.sourceFilePath = mSourceUri.toString();
            parcel.sourceFd = mSourceFileDescriptor;
            parcel.destinationFilePath = mDestinationUri.toString();
            parcel.destinationFd = mDestinationFileDescriptor;
            parcel.clientUid = mClientUid;
            parcel.clientPid = mClientPid;
            if (mClientUid < 0) {
                parcel.clientPackageName = context.getPackageName();
            } else {
                String packageName = context.getPackageManager().getNameForUid(mClientUid);
                // PackageName is optional as some uid does not have package name. Set to
                // "Unavailable" string in this case.
                if (packageName == null) {
                    Log.w(TAG, "Failed to find package for uid: " + mClientUid);
                    packageName = "Unavailable";
                }
                parcel.clientPackageName = packageName;
            }
            writeFormatToParcel(parcel);
            if (mTestConfig != null) {
                parcel.isForTesting = true;
                parcel.testConfig = mTestConfig;
            }
            return parcel;
        }

        /**
         * Builder to build a {@link TranscodingRequest} object.
         *
         * @param <T> The subclass to be built.
         */
        abstract static class Builder<T extends Builder<T>> {
            private @NonNull Uri mSourceUri;
            private @NonNull Uri mDestinationUri;
            private @Nullable ParcelFileDescriptor mSourceFileDescriptor = null;
            private @Nullable ParcelFileDescriptor mDestinationFileDescriptor = null;
            private int mClientUid = -1;
            private int mClientPid = -1;
            private @TranscodingType int mType = TRANSCODING_TYPE_UNKNOWN;
            private @TranscodingPriority int mPriority = PRIORITY_UNKNOWN;
            private TranscodingTestConfig mTestConfig;

            abstract T self();

            /**
             * Creates a builder for building {@link TranscodingRequest}s.
             *
             * Client must set the source Uri. If client also provides the source fileDescriptor
             * through is provided by {@link #setSourceFileDescriptor(ParcelFileDescriptor)},
             * TranscodingSession will use the fd instead of calling back to the client to open the
             * sourceUri.
             *
             *
             * @param type The transcoding type.
             * @param sourceUri Content uri for the source media file.
             * @param destinationUri Content uri for the destination media file.
             *
             */
            private Builder(@TranscodingType int type, @NonNull Uri sourceUri,
                    @NonNull Uri destinationUri) {
                mType = type;

                if (sourceUri == null || Uri.EMPTY.equals(sourceUri)) {
                    throw new IllegalArgumentException(
                            "You must specify a non-empty source Uri.");
                }
                mSourceUri = sourceUri;

                if (destinationUri == null || Uri.EMPTY.equals(destinationUri)) {
                    throw new IllegalArgumentException(
                            "You must specify a non-empty destination Uri.");
                }
                mDestinationUri = destinationUri;
            }

            /**
             * Specifies the fileDescriptor opened from the source media file.
             *
             * This call is optional. If the source fileDescriptor is provided, TranscodingSession
             * will use it directly instead of opening the uri from {@link #Builder(int, Uri, Uri)}.
             * It is client's responsibility to make sure the fileDescriptor is opened from the
             * source uri.
             * @param fileDescriptor a {@link ParcelFileDescriptor} opened from source media file.
             * @return The same builder instance.
             * @throws IllegalArgumentException if fileDescriptor is invalid.
             */
            @NonNull
            public T setSourceFileDescriptor(@NonNull ParcelFileDescriptor fileDescriptor) {
                if (fileDescriptor == null || fileDescriptor.getFd() < 0) {
                    throw new IllegalArgumentException(
                            "Invalid source descriptor.");
                }
                mSourceFileDescriptor = fileDescriptor;
                return self();
            }

            /**
             * Specifies the fileDescriptor opened from the destination media file.
             *
             * This call is optional. If the destination fileDescriptor is provided,
             * TranscodingSession will use it directly instead of opening the source uri from
             * {@link #Builder(int, Uri, Uri)} upon transcoding starts. It is client's
             * responsibility to make sure the fileDescriptor is opened from the destination uri.
             * @param fileDescriptor a {@link ParcelFileDescriptor} opened from destination media
             *                       file.
             * @return The same builder instance.
             * @throws IllegalArgumentException if fileDescriptor is invalid.
             */
            @NonNull
            public T setDestinationFileDescriptor(
                    @NonNull ParcelFileDescriptor fileDescriptor) {
                if (fileDescriptor == null || fileDescriptor.getFd() < 0) {
                    throw new IllegalArgumentException(
                            "Invalid destination descriptor.");
                }
                mDestinationFileDescriptor = fileDescriptor;
                return self();
            }

            /**
             * Specify the UID of the client that this request is for.
             * <p>
             * Only privilege caller with android.permission.WRITE_MEDIA_STORAGE could forward the
             * pid. Note that the permission check happens on the service side upon starting the
             * transcoding. If the client does not have the permission, the transcoding will fail.
             *
             * @param uid client Uid.
             * @return The same builder instance.
             * @throws IllegalArgumentException if uid is invalid.
             */
            @NonNull
            public T setClientUid(int uid) {
                if (uid < 0) {
                    throw new IllegalArgumentException("Invalid Uid");
                }
                mClientUid = uid;
                return self();
            }

            /**
             * Specify the pid of the client that this request is for.
             * <p>
             * Only privilege caller with android.permission.WRITE_MEDIA_STORAGE could forward the
             * pid. Note that the permission check happens on the service side upon starting the
             * transcoding. If the client does not have the permission, the transcoding will fail.
             *
             * @param pid client Pid.
             * @return The same builder instance.
             * @throws IllegalArgumentException if pid is invalid.
             */
            @NonNull
            public T setClientPid(int pid) {
                if (pid < 0) {
                    throw new IllegalArgumentException("Invalid pid");
                }
                mClientPid = pid;
                return self();
            }

            /**
             * Specifies the priority of the transcoding.
             *
             * @param priority Must be one of the {@code PRIORITY_*}
             * @return The same builder instance.
             * @throws IllegalArgumentException if flags is invalid.
             * @hide
             */
            @NonNull
            public T setPriority(@TranscodingPriority int priority) {
                if (priority != PRIORITY_OFFLINE && priority != PRIORITY_REALTIME) {
                    throw new IllegalArgumentException("Invalid priority: " + priority);
                }
                mPriority = priority;
                return self();
            }

            /**
             * Sets the delay in processing this request.
             * @param config test config.
             * @return The same builder instance.
             * @hide
             */
            @VisibleForTesting
            @NonNull
            public T setTestConfig(@NonNull TranscodingTestConfig config) {
                mTestConfig = config;
                return self();
            }
        }

        /**
         * Abstract base class for all the format resolvers.
         */
        abstract static class MediaFormatResolver {
            private @NonNull ApplicationMediaCapabilities mClientCaps;

            /**
             * Prevents public constructor access.
             */
            /* package private */ MediaFormatResolver() {
            }

            /**
             * Constructs MediaFormatResolver object.
             *
             * @param clientCaps An ApplicationMediaCapabilities object containing the client's
             *                   capabilities.
             */
            MediaFormatResolver(@NonNull ApplicationMediaCapabilities clientCaps) {
                if (clientCaps == null) {
                    throw new IllegalArgumentException("Client capabilities must not be null");
                }
                mClientCaps = clientCaps;
            }

            /**
             * Returns the client capabilities.
             */
            @NonNull
            /* package */ ApplicationMediaCapabilities getClientCapabilities() {
                return mClientCaps;
            }

            abstract boolean shouldTranscode();
        }

        /**
         * VideoFormatResolver for deciding if video transcoding is needed, and if so, the track
         * formats to use.
         */
        public static class VideoFormatResolver extends MediaFormatResolver {
            private static final int BIT_RATE = 20000000;            // 20Mbps

            private MediaFormat mSrcVideoFormatHint;
            private MediaFormat mSrcAudioFormatHint;

            /**
             * Constructs a new VideoFormatResolver object.
             *
             * @param clientCaps An ApplicationMediaCapabilities object containing the client's
             *                   capabilities.
             * @param srcVideoFormatHint A MediaFormat object containing information about the
             *                           source's video track format that could affect the
             *                           transcoding decision. Such information could include video
             *                           codec types, color spaces, whether special format info (eg.
             *                           slow-motion markers) are present, etc.. If a particular
             *                           information is not present, it will not be used to make the
             *                           decision.
             */
            public VideoFormatResolver(@NonNull ApplicationMediaCapabilities clientCaps,
                    @NonNull MediaFormat srcVideoFormatHint) {
                super(clientCaps);
                mSrcVideoFormatHint = srcVideoFormatHint;
            }

            /**
             * Constructs a new VideoFormatResolver object.
             *
             * @param clientCaps An ApplicationMediaCapabilities object containing the client's
             *                   capabilities.
             * @param srcVideoFormatHint A MediaFormat object containing information about the
             *                           source's video track format that could affect the
             *                           transcoding decision. Such information could include video
             *                           codec types, color spaces, whether special format info (eg.
             *                           slow-motion markers) are present, etc.. If a particular
             *                           information is not present, it will not be used to make the
             *                           decision.
             * @param srcAudioFormatHint A MediaFormat object containing information about the
             *                           source's audio track format that could affect the
             *                           transcoding decision.
             * @hide
             */
            VideoFormatResolver(@NonNull ApplicationMediaCapabilities clientCaps,
                    @NonNull MediaFormat srcVideoFormatHint,
                    @NonNull MediaFormat srcAudioFormatHint) {
                super(clientCaps);
                mSrcVideoFormatHint = srcVideoFormatHint;
                mSrcAudioFormatHint = srcAudioFormatHint;
            }

            /**
             * Returns whether the source content should be transcoded.
             *
             * @return true if the source should be transcoded.
             */
            public boolean shouldTranscode() {
                boolean supportHevc = getClientCapabilities().isVideoMimeTypeSupported(
                        MediaFormat.MIMETYPE_VIDEO_HEVC);
                if (!supportHevc && MediaFormat.MIMETYPE_VIDEO_HEVC.equals(
                        mSrcVideoFormatHint.getString(MediaFormat.KEY_MIME))) {
                    return true;
                }
                // TODO: add more checks as needed below.
                return false;
            }

            /**
             * Retrieves the video track format to be used on
             * {@link VideoTranscodingRequest.Builder#setVideoTrackFormat(MediaFormat)} for this
             * configuration.
             *
             * @return the video track format to be used if transcoding should be performed,
             *         and null otherwise.
             */
            @Nullable
            public MediaFormat resolveVideoFormat() {
                if (!shouldTranscode()) {
                    return null;
                }

                MediaFormat videoTrackFormat = new MediaFormat(mSrcVideoFormatHint);
                videoTrackFormat.setString(MediaFormat.KEY_MIME, MediaFormat.MIMETYPE_VIDEO_AVC);

                int width = mSrcVideoFormatHint.getInteger(MediaFormat.KEY_WIDTH);
                int height = mSrcVideoFormatHint.getInteger(MediaFormat.KEY_HEIGHT);
                if (width <= 0 || height <= 0) {
                    throw new IllegalArgumentException(
                            "Source Width and height must be larger than 0");
                }

                float frameRate = 30.0f; // default to 30fps.
                if (mSrcVideoFormatHint.containsKey(MediaFormat.KEY_FRAME_RATE)) {
                    frameRate = mSrcVideoFormatHint.getFloat(MediaFormat.KEY_FRAME_RATE);
                    if (frameRate <= 0) {
                        throw new IllegalArgumentException(
                                "frameRate must be larger than 0");
                    }
                }

                int bitrate = getAVCBitrate(width, height, frameRate);
                videoTrackFormat.setInteger(MediaFormat.KEY_BIT_RATE, bitrate);
                return videoTrackFormat;
            }

            /**
             * Generate a default bitrate with the fixed bpp(bits-per-pixel) 0.25.
             * This maps to:
             * 1080P@30fps -> 16Mbps
             * 1080P@60fps-> 32Mbps
             * 4K@30fps -> 62Mbps
             */
            private static int getDefaultBitrate(int width, int height, float frameRate) {
                return (int) (width * height * frameRate * BPP);
            }

            /**
             * Query the bitrate from CamcorderProfile. If there are two profiles that match the
             * width/height/framerate, we will use the higher one to get better quality.
             * Return default bitrate if could not find any match profile.
             */
            private static int getAVCBitrate(int width, int height, float frameRate) {
                int bitrate = -1;
                int[] cameraIds = {0, 1};

                // Profiles ordered in decreasing order of preference.
                int[] preferQualities = {
                        CamcorderProfile.QUALITY_2160P,
                        CamcorderProfile.QUALITY_1080P,
                        CamcorderProfile.QUALITY_720P,
                        CamcorderProfile.QUALITY_480P,
                        CamcorderProfile.QUALITY_LOW,
                };

                for (int cameraId : cameraIds) {
                    for (int quality : preferQualities) {
                        // Check if camera id has profile for the quality level.
                        if (!CamcorderProfile.hasProfile(cameraId, quality)) {
                            continue;
                        }
                        CamcorderProfile profile = CamcorderProfile.get(cameraId, quality);
                        // Check the width/height/framerate/codec, also consider portrait case.
                        if (((width == profile.videoFrameWidth
                                && height == profile.videoFrameHeight)
                                || (height == profile.videoFrameWidth
                                && width == profile.videoFrameHeight))
                                && (int) frameRate == profile.videoFrameRate
                                && profile.videoCodec == MediaRecorder.VideoEncoder.H264) {
                            if (bitrate < profile.videoBitRate) {
                                bitrate = profile.videoBitRate;
                            }
                            break;
                        }
                    }
                }

                if (bitrate == -1) {
                    Log.w(TAG, "Failed to find CamcorderProfile for w: " + width + "h: " + height
                            + " fps: "
                            + frameRate);
                    bitrate = getDefaultBitrate(width, height, frameRate);
                }
                Log.d(TAG, "Using bitrate " + bitrate + " for " + width + " " + height + " "
                        + frameRate);
                return bitrate;
            }

            /**
             * Retrieves the audio track format to be used for transcoding.
             *
             * @return the audio track format to be used if transcoding should be performed, and
             *         null otherwise.
             * @hide
             */
            @Nullable
            public MediaFormat resolveAudioFormat() {
                if (!shouldTranscode()) {
                    return null;
                }
                // Audio transcoding is not supported yet, always return null.
                return null;
            }
        }
    }

    /**
     * VideoTranscodingRequest encapsulates the configuration for transcoding a video.
     */
    public static final class VideoTranscodingRequest extends TranscodingRequest {
        /**
         * Desired output video format of the destination file.
         * <p> If this is null, source file's video track will be passed through and copied to the
         * destination file.
         */
        private @Nullable MediaFormat mVideoTrackFormat = null;

        /**
         * Desired output audio format of the destination file.
         * <p> If this is null, source file's audio track will be passed through and copied to the
         * destination file.
         */
        private @Nullable MediaFormat mAudioTrackFormat = null;

        private VideoTranscodingRequest(VideoTranscodingRequest.Builder builder) {
            super(builder);
            mVideoTrackFormat = builder.mVideoTrackFormat;
            mAudioTrackFormat = builder.mAudioTrackFormat;
        }

        /**
         * Return the video track format of the transcoding.
         * This will be null if client has not specified the video track format.
         */
        @NonNull
        public MediaFormat getVideoTrackFormat() {
            return mVideoTrackFormat;
        }

        @Override
        void writeFormatToParcel(TranscodingRequestParcel parcel) {
            parcel.requestedVideoTrackFormat = convertToVideoTrackFormat(mVideoTrackFormat);
        }

        /* Converts the MediaFormat to TranscodingVideoTrackFormat. */
        private static TranscodingVideoTrackFormat convertToVideoTrackFormat(MediaFormat format) {
            if (format == null) {
                throw new IllegalArgumentException("Invalid MediaFormat");
            }

            TranscodingVideoTrackFormat trackFormat = new TranscodingVideoTrackFormat();

            if (format.containsKey(MediaFormat.KEY_MIME)) {
                String mime = format.getString(MediaFormat.KEY_MIME);
                if (MediaFormat.MIMETYPE_VIDEO_AVC.equals(mime)) {
                    trackFormat.codecType = TranscodingVideoCodecType.kAvc;
                } else if (MediaFormat.MIMETYPE_VIDEO_HEVC.equals(mime)) {
                    trackFormat.codecType = TranscodingVideoCodecType.kHevc;
                } else {
                    throw new UnsupportedOperationException("Only support transcode to avc/hevc");
                }
            }

            if (format.containsKey(MediaFormat.KEY_BIT_RATE)) {
                int bitrateBps = format.getInteger(MediaFormat.KEY_BIT_RATE);
                if (bitrateBps <= 0) {
                    throw new IllegalArgumentException("Bitrate must be larger than 0");
                }
                trackFormat.bitrateBps = bitrateBps;
            }

            if (format.containsKey(MediaFormat.KEY_WIDTH) && format.containsKey(
                    MediaFormat.KEY_HEIGHT)) {
                int width = format.getInteger(MediaFormat.KEY_WIDTH);
                int height = format.getInteger(MediaFormat.KEY_HEIGHT);
                if (width <= 0 || height <= 0) {
                    throw new IllegalArgumentException("Width and height must be larger than 0");
                }
                // TODO: Validate the aspect ratio after adding scaling.
                trackFormat.width = width;
                trackFormat.height = height;
            }

            if (format.containsKey(MediaFormat.KEY_PROFILE)) {
                int profile = format.getInteger(MediaFormat.KEY_PROFILE);
                if (profile <= 0) {
                    throw new IllegalArgumentException("Invalid codec profile");
                }
                // TODO: Validate the profile according to codec type.
                trackFormat.profile = profile;
            }

            if (format.containsKey(MediaFormat.KEY_LEVEL)) {
                int level = format.getInteger(MediaFormat.KEY_LEVEL);
                if (level <= 0) {
                    throw new IllegalArgumentException("Invalid codec level");
                }
                // TODO: Validate the level according to codec type.
                trackFormat.level = level;
            }

            return trackFormat;
        }

        /**
         * Builder class for {@link VideoTranscodingRequest}.
         */
        public static final class Builder extends
                TranscodingRequest.Builder<VideoTranscodingRequest.Builder> {
            /**
             * Desired output video format of the destination file.
             * <p> If this is null, source file's video track will be passed through and
             * copied to the destination file.
             */
            private @Nullable MediaFormat mVideoTrackFormat = null;

            /**
             * Desired output audio format of the destination file.
             * <p> If this is null, source file's audio track will be passed through and copied
             * to the destination file.
             */
            private @Nullable MediaFormat mAudioTrackFormat = null;

            /**
             * Creates a builder for building {@link VideoTranscodingRequest}s.
             *
             * <p> Client could only specify the settings that matters to them, e.g. codec format or
             * bitrate. And by default, transcoding will preserve the original video's settings
             * (bitrate, framerate, resolution) if not provided.
             * <p>Note that some settings may silently fail to apply if the device does not support
             * them.
             * @param sourceUri Content uri for the source media file.
             * @param destinationUri Content uri for the destination media file.
             * @param videoFormat MediaFormat containing the settings that client wants override in
             *                    the original video's video track.
             * @throws IllegalArgumentException if videoFormat is invalid.
             */
            public Builder(@NonNull Uri sourceUri, @NonNull Uri destinationUri,
                    @NonNull MediaFormat videoFormat) {
                super(TRANSCODING_TYPE_VIDEO, sourceUri, destinationUri);
                setVideoTrackFormat(videoFormat);
            }

            @Override
            @NonNull
            public Builder setClientUid(int uid) {
                super.setClientUid(uid);
                return self();
            }

            @Override
            @NonNull
            public Builder setClientPid(int pid) {
                super.setClientPid(pid);
                return self();
            }

            @Override
            @NonNull
            public Builder setSourceFileDescriptor(@NonNull ParcelFileDescriptor fd) {
                super.setSourceFileDescriptor(fd);
                return self();
            }

            @Override
            @NonNull
            public Builder setDestinationFileDescriptor(@NonNull ParcelFileDescriptor fd) {
                super.setDestinationFileDescriptor(fd);
                return self();
            }

            private void setVideoTrackFormat(@NonNull MediaFormat videoFormat) {
                if (videoFormat == null) {
                    throw new IllegalArgumentException("videoFormat must not be null");
                }

                // Check if the MediaFormat is for video by looking at the MIME type.
                String mime = videoFormat.containsKey(MediaFormat.KEY_MIME)
                        ? videoFormat.getString(MediaFormat.KEY_MIME) : null;
                if (mime == null || !mime.startsWith("video/")) {
                    throw new IllegalArgumentException("Invalid video format: wrong mime type");
                }

                mVideoTrackFormat = videoFormat;
            }

            /**
             * @return a new {@link TranscodingRequest} instance successfully initialized
             * with all the parameters set on this <code>Builder</code>.
             * @throws UnsupportedOperationException if the parameters set on the
             *                                       <code>Builder</code> were incompatible, or
             *                                       if they are not supported by the
             *                                       device.
             */
            @NonNull
            public VideoTranscodingRequest build() {
                return new VideoTranscodingRequest(this);
            }

            @Override
            VideoTranscodingRequest.Builder self() {
                return this;
            }
        }
    }

    /**
     * Handle to an enqueued transcoding operation. An instance of this class represents a single
     * enqueued transcoding operation. The caller can use that instance to query the status or
     * progress, and to get the result once the operation has completed.
     */
    public static final class TranscodingSession {
        /** The session is enqueued but not yet running. */
        public static final int STATUS_PENDING = 1;
        /** The session is currently running. */
        public static final int STATUS_RUNNING = 2;
        /** The session is finished. */
        public static final int STATUS_FINISHED = 3;
        /** The session is paused. */
        public static final int STATUS_PAUSED = 4;

        /** @hide */
        @IntDef(prefix = { "STATUS_" }, value = {
                STATUS_PENDING,
                STATUS_RUNNING,
                STATUS_FINISHED,
                STATUS_PAUSED,
        })
        @Retention(RetentionPolicy.SOURCE)
        public @interface Status {}

        /** The session does not have a result yet. */
        public static final int RESULT_NONE = 1;
        /** The session completed successfully. */
        public static final int RESULT_SUCCESS = 2;
        /** The session encountered an error while running. */
        public static final int RESULT_ERROR = 3;
        /** The session was canceled by the caller. */
        public static final int RESULT_CANCELED = 4;

        /** @hide */
        @IntDef(prefix = { "RESULT_" }, value = {
                RESULT_NONE,
                RESULT_SUCCESS,
                RESULT_ERROR,
                RESULT_CANCELED,
        })
        @Retention(RetentionPolicy.SOURCE)
        public @interface Result {}


        // The error code exposed here should be in sync with:
        // frameworks/av/media/libmediatranscoding/aidl/android/media/TranscodingErrorCode.aidl
        /** @hide */
        @IntDef(prefix = { "TRANSCODING_SESSION_ERROR_" }, value = {
                ERROR_NONE,
                ERROR_DROPPED_BY_SERVICE,
                ERROR_SERVICE_DIED})
        @Retention(RetentionPolicy.SOURCE)
        public @interface TranscodingSessionErrorCode{}
        /**
         * Constant indicating that no error occurred.
         */
        public static final int ERROR_NONE = 0;

        /**
         * Constant indicating that the session is dropped by Transcoding service due to hitting
         * the limit, e.g. too many back to back transcoding happen in a short time frame.
         */
        public static final int ERROR_DROPPED_BY_SERVICE = 1;

        /**
         * Constant indicating the backing transcoding service is died. Client should enqueue the
         * the request again.
         */
        public static final int ERROR_SERVICE_DIED = 2;

        /** Listener that gets notified when the progress changes. */
        @FunctionalInterface
        public interface OnProgressUpdateListener {
            /**
             * Called when the progress changes. The progress is in percentage between 0 and 1,
             * where 0 means the session has not yet started and 100 means that it has finished.
             *
             * @param session      The session associated with the progress.
             * @param progress The new progress ranging from 0 ~ 100 inclusive.
             */
            void onProgressUpdate(@NonNull TranscodingSession session,
                    @IntRange(from = 0, to = 100) int progress);
        }

        private final MediaTranscodingManager mManager;
        private Executor mListenerExecutor;
        private OnTranscodingFinishedListener mListener;
        private int mSessionId = -1;
        // Lock for internal state.
        private final Object mLock = new Object();
        @GuardedBy("mLock")
        private Executor mProgressUpdateExecutor = null;
        @GuardedBy("mLock")
        private OnProgressUpdateListener mProgressUpdateListener = null;
        @GuardedBy("mLock")
        private int mProgress = 0;
        @GuardedBy("mLock")
        private int mProgressUpdateInterval = 0;
        @GuardedBy("mLock")
        private @Status int mStatus = STATUS_PENDING;
        @GuardedBy("mLock")
        private @Result int mResult = RESULT_NONE;
        @GuardedBy("mLock")
        private @TranscodingSessionErrorCode int mErrorCode = ERROR_NONE;
        @GuardedBy("mLock")
        private boolean mHasRetried = false;
        // The original request that associated with this session.
        private final TranscodingRequest mRequest;

        private TranscodingSession(
                @NonNull MediaTranscodingManager manager,
                @NonNull TranscodingRequest request,
                @NonNull TranscodingSessionParcel parcel,
                @NonNull @CallbackExecutor Executor executor,
                @NonNull OnTranscodingFinishedListener listener) {
            Objects.requireNonNull(manager, "manager must not be null");
            Objects.requireNonNull(parcel, "parcel must not be null");
            Objects.requireNonNull(executor, "listenerExecutor must not be null");
            Objects.requireNonNull(listener, "listener must not be null");
            mManager = manager;
            mSessionId = parcel.sessionId;
            mListenerExecutor = executor;
            mListener = listener;
            mRequest = request;
        }

        /**
         * Set a progress listener.
         * @param executor The executor on which listener will be invoked.
         * @param listener The progress listener.
         */
        public void setOnProgressUpdateListener(
                @NonNull @CallbackExecutor Executor executor,
                @Nullable OnProgressUpdateListener listener) {
            synchronized (mLock) {
                Objects.requireNonNull(executor, "listenerExecutor must not be null");
                Objects.requireNonNull(listener, "listener must not be null");
                mProgressUpdateExecutor = executor;
                mProgressUpdateListener = listener;
            }
        }

        private void updateStatusAndResult(@Status int sessionStatus,
                @Result int sessionResult, @TranscodingSessionErrorCode int errorCode) {
            synchronized (mLock) {
                mStatus = sessionStatus;
                mResult = sessionResult;
                mErrorCode = errorCode;
            }
        }

        /**
         * Retrieve the error code associated with the RESULT_ERROR.
         */
        public @TranscodingSessionErrorCode int getErrorCode() {
            synchronized (mLock) {
                return mErrorCode;
            }
        }

        /**
         * Resubmit the transcoding session to the service.
         * Note that only the session that fails or gets cancelled could be retried and each session
         * could be retried only once. After that, Client need to enqueue a new request if they want
         * to try again.
         *
         * @return true if successfully resubmit the job to service. False otherwise.
         * @throws UnsupportedOperationException if the retry could not be fulfilled.
         * @hide
         */
        public boolean retry() {
            return retryInternal(true /*setHasRetried*/);
        }

        // TODO(hkuang): Add more test for it.
        private boolean retryInternal(boolean setHasRetried) {
            synchronized (mLock) {
                if (mStatus == STATUS_PENDING || mStatus == STATUS_RUNNING) {
                    throw new UnsupportedOperationException(
                            "Failed to retry as session is in processing");
                }

                if (mHasRetried) {
                    throw new UnsupportedOperationException("Session has been retried already");
                }

                // Get the client interface.
                ITranscodingClient client = mManager.getTranscodingClient();
                if (client == null) {
                    Log.e(TAG, "Service rebooting. Try again later");
                    return false;
                }

                synchronized (mManager.mPendingTranscodingSessions) {
                    try {
                        // Submits the request to MediaTranscoding service.
                        TranscodingSessionParcel sessionParcel = new TranscodingSessionParcel();
                        if (!client.submitRequest(mRequest.writeToParcel(mManager.mContext),
                                                  sessionParcel)) {
                            mHasRetried = true;
                            throw new UnsupportedOperationException("Failed to enqueue request");
                        }

                        // Replace the old session id wit the new one.
                        mSessionId = sessionParcel.sessionId;
                        // Adds the new session back into pending sessions.
                        mManager.mPendingTranscodingSessions.put(mSessionId, this);
                    } catch (RemoteException re) {
                        return false;
                    }
                    mStatus = STATUS_PENDING;
                    mHasRetried = setHasRetried ? true : false;
                }
            }
            return true;
        }

        /**
         * Cancels the transcoding session and notify the listener.
         * If the session happened to finish before being canceled this call is effectively a no-op
         * and will not update the result in that case.
         */
        public void cancel() {
            synchronized (mLock) {
                // Check if the session is finished already.
                if (mStatus != STATUS_FINISHED) {
                    try {
                        ITranscodingClient client = mManager.getTranscodingClient();
                        // The client may be gone.
                        if (client != null) {
                            client.cancelSession(mSessionId);
                        }
                    } catch (RemoteException re) {
                        //TODO(hkuang): Find out what to do if failing to cancel the session.
                        Log.e(TAG, "Failed to cancel the session due to exception:  " + re);
                    }
                    mStatus = STATUS_FINISHED;
                    mResult = RESULT_CANCELED;

                    // Notifies client the session is canceled.
                    mListenerExecutor.execute(() -> mListener.onTranscodingFinished(this));
                }
            }
        }

        /**
         * Gets the progress of the transcoding session. The progress is between 0 and 100, where 0
         * means that the session has not yet started and 100 means that it is finished. For the
         * cancelled session, the progress will be the last updated progress before it is cancelled.
         * @return The progress.
         */
        @IntRange(from = 0, to = 100)
        public int getProgress() {
            synchronized (mLock) {
                return mProgress;
            }
        }

        /**
         * Gets the status of the transcoding session.
         * @return The status.
         */
        public @Status int getStatus() {
            synchronized (mLock) {
                return mStatus;
            }
        }

        /**
         * Adds a client uid that is also waiting for this transcoding session.
         * <p>
         * Only privilege caller with android.permission.WRITE_MEDIA_STORAGE could add the
         * uid. Note that the permission check happens on the service side upon starting the
         * transcoding. If the client does not have the permission, the transcoding will fail.
         * @param uid  the additional client uid to be added.
         * @return true if successfully added, false otherwise.
         */
        public boolean addClientUid(int uid) {
            if (uid < 0) {
                throw new IllegalArgumentException("Invalid Uid");
            }

            // Get the client interface.
            ITranscodingClient client = mManager.getTranscodingClient();
            if (client == null) {
                Log.e(TAG, "Service is dead...");
                return false;
            }

            try {
                if (!client.addClientUid(mSessionId, uid)) {
                    Log.e(TAG, "Failed to add client uid");
                    return false;
                }
            } catch (Exception ex) {
                Log.e(TAG, "Failed to get client uids due to " + ex);
                return false;
            }
            return true;
        }

        /**
         * Query all the client that waiting for this transcoding session
         * @return a list containing all the client uids.
         */
        @NonNull
        public List<Integer> getClientUids() {
            List<Integer> uidList = new ArrayList<Integer>();

            // Get the client interface.
            ITranscodingClient client = mManager.getTranscodingClient();
            if (client == null) {
                Log.e(TAG, "Service is dead...");
                return uidList;
            }

            try {
                int[] clientUids  = client.getClientUids(mSessionId);
                for (int i : clientUids) {
                    uidList.add(i);
                }
            } catch (Exception ex) {
                Log.e(TAG, "Failed to get client uids due to " + ex);
            }

            return uidList;
        }

        /**
         * Gets sessionId of the transcoding session.
         * @return session id.
         */
        public int getSessionId() {
            return mSessionId;
        }

        /**
         * Gets the result of the transcoding session.
         * @return The result.
         */
        public @Result int getResult() {
            synchronized (mLock) {
                return mResult;
            }
        }

        @Override
        public String toString() {
            String result;
            String status;

            switch (mResult) {
                case RESULT_NONE:
                    result = "RESULT_NONE";
                    break;
                case RESULT_SUCCESS:
                    result = "RESULT_SUCCESS";
                    break;
                case RESULT_ERROR:
                    result = "RESULT_ERROR(" + mErrorCode + ")";
                    break;
                case RESULT_CANCELED:
                    result = "RESULT_CANCELED";
                    break;
                default:
                    result = String.valueOf(mResult);
                    break;
            }

            switch (mStatus) {
                case STATUS_PENDING:
                    status = "STATUS_PENDING";
                    break;
                case STATUS_PAUSED:
                    status = "STATUS_PAUSED";
                    break;
                case STATUS_RUNNING:
                    status = "STATUS_RUNNING";
                    break;
                case STATUS_FINISHED:
                    status = "STATUS_FINISHED";
                    break;
                default:
                    status = String.valueOf(mStatus);
                    break;
            }
            return String.format(" session: {id: %d, status: %s, result: %s, progress: %d}",
                    mSessionId, status, result, mProgress);
        }

        private void updateProgress(int newProgress) {
            synchronized (mLock) {
                mProgress = newProgress;
            }
        }

        private void updateStatus(int newStatus) {
            synchronized (mLock) {
                mStatus = newStatus;
            }
        }
    }

    private ITranscodingClient getTranscodingClient() {
        synchronized (mLock) {
            return mTranscodingClient;
        }
    }

    /**
     * Enqueues a TranscodingRequest for execution.
     * <p> Upon successfully accepting the request, MediaTranscodingManager will return a
     * {@link TranscodingSession} to the client. Client should use {@link TranscodingSession} to
     * track the progress and get the result.
     * <p> MediaTranscodingManager will return null if fails to accept the request due to service
     * rebooting. Client could retry again after receiving null.
     *
     * @param transcodingRequest The TranscodingRequest to enqueue.
     * @param listenerExecutor   Executor on which the listener is notified.
     * @param listener           Listener to get notified when the transcoding session is finished.
     * @return A TranscodingSession for this operation.
     * @throws UnsupportedOperationException if the request could not be fulfilled.
     */
    @Nullable
    public TranscodingSession enqueueRequest(
            @NonNull TranscodingRequest transcodingRequest,
            @NonNull @CallbackExecutor Executor listenerExecutor,
            @NonNull OnTranscodingFinishedListener listener) {
        Log.i(TAG, "enqueueRequest called.");
        Objects.requireNonNull(transcodingRequest, "transcodingRequest must not be null");
        Objects.requireNonNull(listenerExecutor, "listenerExecutor must not be null");
        Objects.requireNonNull(listener, "listener must not be null");

        // Converts the request to TranscodingRequestParcel.
        TranscodingRequestParcel requestParcel = transcodingRequest.writeToParcel(mContext);

        Log.i(TAG, "Getting transcoding request " + transcodingRequest.getSourceUri());

        // Submits the request to MediaTranscoding service.
        try {
            TranscodingSessionParcel sessionParcel = new TranscodingSessionParcel();
            // Synchronizes the access to mPendingTranscodingSessions to make sure the session Id is
            // inserted in the mPendingTranscodingSessions in the callback handler.
            synchronized (mPendingTranscodingSessions) {
                synchronized (mLock) {
                    if (mTranscodingClient == null) {
                        // Try to register with the service again.
                        IMediaTranscodingService service = getService(false /*retry*/);
                        if (service == null) {
                            Log.w(TAG, "Service rebooting. Try again later");
                            return null;
                        }
                        mTranscodingClient = registerClient(service);
                        // If still fails, throws an exception to tell client to try later.
                        if (mTranscodingClient == null) {
                            Log.w(TAG, "Service rebooting. Try again later");
                            return null;
                        }
                    }

                    if (!mTranscodingClient.submitRequest(requestParcel, sessionParcel)) {
                        throw new UnsupportedOperationException("Failed to enqueue request");
                    }
                }

                // Wraps the TranscodingSessionParcel into a TranscodingSession and returns it to
                // client for tracking.
                TranscodingSession session = new TranscodingSession(this, transcodingRequest,
                        sessionParcel,
                        listenerExecutor,
                        listener);

                // Adds the new session into pending sessions.
                mPendingTranscodingSessions.put(session.getSessionId(), session);
                return session;
            }
        } catch (RemoteException ex) {
            Log.w(TAG, "Service rebooting. Try again later");
            return null;
        } catch (ServiceSpecificException ex) {
            throw new UnsupportedOperationException(
                    "Failed to submit request to Transcoding service. Error: " + ex);
        }
    }
}