• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.tv.util;
18 
19 import java.util.concurrent.TimeUnit;
20 
21 /** A class that includes convenience methods for time shift plays. */
22 public class TimeShiftUtils {
23     private static final String TAG = "TimeShiftUtils";
24     private static final boolean DEBUG = false;
25 
26     private static final long SHORT_PROGRAM_THRESHOLD_MILLIS = TimeUnit.MINUTES.toMillis(46);
27     private static final int[] SHORT_PROGRAM_SPEED_FACTORS = new int[] {2, 4, 12, 48};
28     private static final int[] LONG_PROGRAM_SPEED_FACTORS = new int[] {2, 8, 32, 128};
29 
30     /**
31      * The maximum play speed level support by time shift play. In other words, the valid speed
32      * levels are ranged from 0 to MAX_SPEED_LEVEL (included).
33      */
34     public static final int MAX_SPEED_LEVEL = SHORT_PROGRAM_SPEED_FACTORS.length - 1;
35 
36     /**
37      * Returns real speeds used in time shift play. This method is only for fast-forwarding and
38      * rewinding. The normal play speed is not addressed here.
39      *
40      * @param speedLevel the valid value is ranged from 0 to {@link #MAX_SPEED_LEVEL}.
41      * @param programDurationMillis the length of program under playing.
42      * @throws IndexOutOfBoundsException if speed level is out of its range.
43      */
getPlaybackSpeed(int speedLevel, long programDurationMillis)44     public static int getPlaybackSpeed(int speedLevel, long programDurationMillis)
45             throws IndexOutOfBoundsException {
46         return (programDurationMillis > SHORT_PROGRAM_THRESHOLD_MILLIS)
47                 ? LONG_PROGRAM_SPEED_FACTORS[speedLevel]
48                 : SHORT_PROGRAM_SPEED_FACTORS[speedLevel];
49     }
50 
51     /**
52      * Returns the maxium possible play speed according to the program's length.
53      *
54      * @param programDurationMillis the length of program under playing.
55      */
getMaxPlaybackSpeed(long programDurationMillis)56     public static int getMaxPlaybackSpeed(long programDurationMillis) {
57         return (programDurationMillis > SHORT_PROGRAM_THRESHOLD_MILLIS)
58                 ? LONG_PROGRAM_SPEED_FACTORS[MAX_SPEED_LEVEL]
59                 : SHORT_PROGRAM_SPEED_FACTORS[MAX_SPEED_LEVEL];
60     }
61 }
62