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