1 /*
2  * Copyright (C) 2017 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 org.mozilla.thirdparty.com.google.android.exoplayer2.drm;
17 
18 import android.util.Pair;
19 import androidx.annotation.Nullable;
20 import org.mozilla.thirdparty.com.google.android.exoplayer2.C;
21 import java.util.Map;
22 
23 /**
24  * Utility methods for Widevine.
25  */
26 public final class WidevineUtil {
27 
28   /** Widevine specific key status field name for the remaining license duration, in seconds. */
29   public static final String PROPERTY_LICENSE_DURATION_REMAINING = "LicenseDurationRemaining";
30   /** Widevine specific key status field name for the remaining playback duration, in seconds. */
31   public static final String PROPERTY_PLAYBACK_DURATION_REMAINING = "PlaybackDurationRemaining";
32 
WidevineUtil()33   private WidevineUtil() {}
34 
35   /**
36    * Returns license and playback durations remaining in seconds.
37    *
38    * @param drmSession The drm session to query.
39    * @return A {@link Pair} consisting of the remaining license and playback durations in seconds,
40    *     or null if called before the session has been opened or after it's been released.
41    */
getLicenseDurationRemainingSec( DrmSession<?> drmSession)42   public static @Nullable Pair<Long, Long> getLicenseDurationRemainingSec(
43       DrmSession<?> drmSession) {
44     Map<String, String> keyStatus = drmSession.queryKeyStatus();
45     if (keyStatus == null) {
46       return null;
47     }
48     return new Pair<>(getDurationRemainingSec(keyStatus, PROPERTY_LICENSE_DURATION_REMAINING),
49         getDurationRemainingSec(keyStatus, PROPERTY_PLAYBACK_DURATION_REMAINING));
50   }
51 
getDurationRemainingSec(Map<String, String> keyStatus, String property)52   private static long getDurationRemainingSec(Map<String, String> keyStatus, String property) {
53     if (keyStatus != null) {
54       try {
55         String value = keyStatus.get(property);
56         if (value != null) {
57           return Long.parseLong(value);
58         }
59       } catch (NumberFormatException e) {
60         // do nothing.
61       }
62     }
63     return C.TIME_UNSET;
64   }
65 
66 }
67