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