aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/DvrPlayer.java
blob: 5656655c2bf5e77fd13f05dbfaec6e12c73b5a9d (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
/*
 * Copyright (C) 2016 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 com.android.tv.dvr;

import android.media.PlaybackParams;
import android.media.tv.TvContentRating;
import android.media.tv.TvInputManager;
import android.media.tv.TvTrackInfo;
import android.media.tv.TvView;
import android.media.session.PlaybackState;
import android.util.Log;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class DvrPlayer {
    private static final String TAG = "DvrPlayer";
    private static final boolean DEBUG = false;

    /**
     * The max rewinding speed supported by DVR player.
     */
    public static final int MAX_REWIND_SPEED = 256;
    /**
     * The max fast-forwarding speed supported by DVR player.
     */
    public static final int MAX_FAST_FORWARD_SPEED = 256;

    private static final long SEEK_POSITION_MARGIN_MS = TimeUnit.SECONDS.toMillis(2);
    private static final long REWIND_POSITION_MARGIN_MS = 32;  // Workaround value. b/29994826

    private RecordedProgram mProgram;
    private long mInitialSeekPositionMs;
    private final TvView mTvView;
    private DvrPlayerCallback mCallback;
    private AspectRatioChangedListener mAspectRatioChangedListener;
    private ContentBlockedListener mContentBlockedListener;
    private float mAspectRatio = Float.NaN;
    private int mPlaybackState = PlaybackState.STATE_NONE;
    private long mTimeShiftCurrentPositionMs;
    private boolean mPauseOnPrepared;
    private final PlaybackParams mPlaybackParams = new PlaybackParams();
    private final DvrPlayerCallback mEmptyCallback = new DvrPlayerCallback();
    private long mStartPositionMs = TvInputManager.TIME_SHIFT_INVALID_TIME;
    private boolean mTimeShiftPlayAvailable;

    public static class DvrPlayerCallback {
        /**
         * Called when the playback position is changed. The normal updating frequency is
         * around 1 sec., which is restricted to the implementation of
         * {@link android.media.tv.TvInputService}.
         */
        public void onPlaybackPositionChanged(long positionMs) { }
        /**
         * Called when the playback state or the playback speed is changed.
         */
        public void onPlaybackStateChanged(int playbackState, int playbackSpeed) { }
        /**
         * Called when the playback toward the end.
         */
        public void onPlaybackEnded() { }
    }

    public interface AspectRatioChangedListener {
        /**
         * Called when the Video's aspect ratio is changed.
         */
        void onAspectRatioChanged(float videoAspectRatio);
    }

    public interface ContentBlockedListener {
        /**
         * Called when the Video's aspect ratio is changed.
         */
        void onContentBlocked(TvContentRating rating);
    }

    public DvrPlayer(TvView tvView) {
        mTvView = tvView;
        mPlaybackParams.setSpeed(1.0f);
        setTvViewCallbacks();
        setCallback(null);
    }

    /**
     * Prepares playback.
     *
     * @param doPlay indicates DVR player do or do not start playback after media is prepared.
     */
    public void prepare(boolean doPlay) throws IllegalStateException {
        if (DEBUG) Log.d(TAG, "prepare()");
        if (mProgram == null) {
            throw new IllegalStateException("Recorded program not set");
        } else if (mPlaybackState != PlaybackState.STATE_NONE) {
            throw new IllegalStateException("Playback is already prepared");
        }
        mTvView.timeShiftPlay(mProgram.getInputId(), mProgram.getUri());
        mPlaybackState = PlaybackState.STATE_CONNECTING;
        mPauseOnPrepared = !doPlay;
        mCallback.onPlaybackStateChanged(mPlaybackState, 1);
    }

    /**
     * Resumes playback.
     */
    public void play() throws IllegalStateException {
        if (DEBUG) Log.d(TAG, "play()");
        if (!isPlaybackPrepared()) {
            throw new IllegalStateException("Recorded program not set or video not ready yet");
        }
        switch (mPlaybackState) {
            case PlaybackState.STATE_FAST_FORWARDING:
            case PlaybackState.STATE_REWINDING:
                setPlaybackSpeed(1);
                break;
            default:
                mTvView.timeShiftResume();
        }
        mPlaybackState = PlaybackState.STATE_PLAYING;
        mCallback.onPlaybackStateChanged(mPlaybackState, 1);
    }

    /**
     * Pauses playback.
     */
    public void pause() throws IllegalStateException {
        if (DEBUG) Log.d(TAG, "pause()");
        if (!isPlaybackPrepared()) {
            throw new IllegalStateException("Recorded program not set or playback not started yet");
        }
        switch (mPlaybackState) {
            case PlaybackState.STATE_FAST_FORWARDING:
            case PlaybackState.STATE_REWINDING:
                setPlaybackSpeed(1);
                // falls through
            case PlaybackState.STATE_PLAYING:
                mTvView.timeShiftPause();
                mPlaybackState = PlaybackState.STATE_PAUSED;
                break;
            default:
                break;
        }
        mCallback.onPlaybackStateChanged(mPlaybackState, 1);
    }

    /**
     * Fast-forwards playback with the given speed. If the given speed is larger than
     * {@value #MAX_FAST_FORWARD_SPEED}, uses {@value #MAX_FAST_FORWARD_SPEED}.
     */
    public void fastForward(int speed) throws IllegalStateException {
        if (DEBUG) Log.d(TAG, "fastForward()");
        if (!isPlaybackPrepared()) {
            throw new IllegalStateException("Recorded program not set or playback not started yet");
        }
        if (speed <= 0) {
            throw new IllegalArgumentException("Speed cannot be negative or 0");
        }
        if (mTimeShiftCurrentPositionMs >= mProgram.getDurationMillis() - SEEK_POSITION_MARGIN_MS) {
            return;
        }
        speed = Math.min(speed, MAX_FAST_FORWARD_SPEED);
        if (DEBUG) Log.d(TAG, "Let's play with speed: " + speed);
        setPlaybackSpeed(speed);
        mPlaybackState = PlaybackState.STATE_FAST_FORWARDING;
        mCallback.onPlaybackStateChanged(mPlaybackState, speed);
    }

    /**
     * Rewinds playback with the given speed. If the given speed is larger than
     * {@value #MAX_REWIND_SPEED}, uses {@value #MAX_REWIND_SPEED}.
     */
    public void rewind(int speed) throws IllegalStateException {
        if (DEBUG) Log.d(TAG, "rewind()");
        if (!isPlaybackPrepared()) {
            throw new IllegalStateException("Recorded program not set or playback not started yet");
        }
        if (speed <= 0) {
            throw new IllegalArgumentException("Speed cannot be negative or 0");
        }
        if (mTimeShiftCurrentPositionMs <= REWIND_POSITION_MARGIN_MS) {
            return;
        }
        speed = Math.min(speed, MAX_REWIND_SPEED);
        if (DEBUG) Log.d(TAG, "Let's play with speed: " + speed);
        setPlaybackSpeed(-speed);
        mPlaybackState = PlaybackState.STATE_REWINDING;
        mCallback.onPlaybackStateChanged(mPlaybackState, speed);
    }

    /**
     * Seeks playback to the specified position.
     */
    public void seekTo(long positionMs) throws IllegalStateException {
        if (DEBUG) Log.d(TAG, "seekTo()");
        if (!isPlaybackPrepared()) {
            throw new IllegalStateException("Recorded program not set or playback not started yet");
        }
        if (mProgram == null || mPlaybackState == PlaybackState.STATE_NONE) {
            return;
        }
        positionMs = getRealSeekPosition(positionMs, SEEK_POSITION_MARGIN_MS);
        if (DEBUG) Log.d(TAG, "Now: " + getPlaybackPosition() + ", shift to: " + positionMs);
        mTvView.timeShiftSeekTo(positionMs + mStartPositionMs);
        if (mPlaybackState == PlaybackState.STATE_FAST_FORWARDING ||
                mPlaybackState == PlaybackState.STATE_REWINDING) {
            mPlaybackState = PlaybackState.STATE_PLAYING;
            mTvView.timeShiftResume();
            mCallback.onPlaybackStateChanged(mPlaybackState, 1);
        }
    }

    /**
     * Resets playback.
     */
    public void reset() {
        if (DEBUG) Log.d(TAG, "reset()");
        mCallback.onPlaybackStateChanged(PlaybackState.STATE_NONE, 1);
        mPlaybackState = PlaybackState.STATE_NONE;
        mTvView.reset();
        mTimeShiftPlayAvailable = false;
        mStartPositionMs = TvInputManager.TIME_SHIFT_INVALID_TIME;
        mTimeShiftCurrentPositionMs = 0;
        mPlaybackParams.setSpeed(1.0f);
        mProgram = null;
    }

    /**
     * Sets callbacks for playback.
     */
    public void setCallback(DvrPlayerCallback callback) {
        if (callback != null) {
            mCallback = callback;
        } else {
            mCallback = mEmptyCallback;
        }
    }

    /**
     * Sets listener to aspect ratio changing.
     */
    public void setAspectRatioChangedListener(AspectRatioChangedListener listener) {
        mAspectRatioChangedListener = listener;
    }

    /**
     * Sets listener to content blocking.
     */
    public void setContentBlockedListener(ContentBlockedListener listener) {
        mContentBlockedListener = listener;
    }

    /**
     * Sets recorded programs for playback. If the player is playing another program, stops it.
     */
    public void setProgram(RecordedProgram program, long initialSeekPositionMs) {
        if (mProgram != null && mProgram.equals(program)) {
            return;
        }
        if (mPlaybackState != PlaybackState.STATE_NONE) {
            reset();
        }
        mInitialSeekPositionMs = initialSeekPositionMs;
        mProgram = program;
    }

    /**
     * Returns the recorded program now playing.
     */
    public RecordedProgram getProgram() {
        return mProgram;
    }

    /**
     * Returns the currrent playback posistion in msecs.
     */
    public long getPlaybackPosition() {
        return mTimeShiftCurrentPositionMs;
    }

    /**
     * Returns the playback speed currently used.
     */
    public int getPlaybackSpeed() {
        return (int) mPlaybackParams.getSpeed();
    }

    /**
     * Returns the playback state defined in {@link android.media.session.PlaybackState}.
     */
    public int getPlaybackState() {
        return mPlaybackState;
    }

    /**
     * Returns if playback of the recorded program is started.
     */
    public boolean isPlaybackPrepared() {
        return mPlaybackState != PlaybackState.STATE_NONE
                && mPlaybackState != PlaybackState.STATE_CONNECTING;
    }

    private void setPlaybackSpeed(int speed) {
        mPlaybackParams.setSpeed(speed);
        mTvView.timeShiftSetPlaybackParams(mPlaybackParams);
    }

    private long getRealSeekPosition(long seekPositionMs, long endMarginMs) {
        return Math.max(0, Math.min(seekPositionMs, mProgram.getDurationMillis() - endMarginMs));
    }

    private void setTvViewCallbacks() {
        mTvView.setTimeShiftPositionCallback(new TvView.TimeShiftPositionCallback() {
            @Override
            public void onTimeShiftStartPositionChanged(String inputId, long timeMs) {
                if (DEBUG) Log.d(TAG, "onTimeShiftStartPositionChanged:" + timeMs);
                mStartPositionMs = timeMs;
                if (mTimeShiftPlayAvailable) {
                    resumeToWatchedPositionIfNeeded();
                }
            }

            @Override
            public void onTimeShiftCurrentPositionChanged(String inputId, long timeMs) {
                if (DEBUG) Log.d(TAG, "onTimeShiftCurrentPositionChanged: " + timeMs);
                if (!mTimeShiftPlayAvailable) {
                    // Workaround of b/31436263
                    return;
                }
                // Workaround of b/32211561, TIF won't report start position when TIS report
                // its start position as 0. In that case, we have to do the prework of playback
                // on the first time we get current position, and the start position should be 0
                // at that time.
                if (mStartPositionMs == TvInputManager.TIME_SHIFT_INVALID_TIME) {
                    mStartPositionMs = 0;
                    resumeToWatchedPositionIfNeeded();
                }
                timeMs -= mStartPositionMs;
                if (mPlaybackState == PlaybackState.STATE_REWINDING
                        && timeMs <= REWIND_POSITION_MARGIN_MS) {
                    play();
                } else {
                    mTimeShiftCurrentPositionMs = getRealSeekPosition(timeMs, 0);
                    mCallback.onPlaybackPositionChanged(mTimeShiftCurrentPositionMs);
                    if (timeMs >= mProgram.getDurationMillis()) {
                        pause();
                        mCallback.onPlaybackEnded();
                    }
                }
            }
        });
        mTvView.setCallback(new TvView.TvInputCallback() {
            @Override
            public void onTimeShiftStatusChanged(String inputId, int status) {
                if (DEBUG) Log.d(TAG, "onTimeShiftStatusChanged:" + status);
                if (status == TvInputManager.TIME_SHIFT_STATUS_AVAILABLE
                        && mPlaybackState == PlaybackState.STATE_CONNECTING) {
                    mTimeShiftPlayAvailable = true;
                }
            }

            @Override
            public void onTrackSelected(String inputId, int type, String trackId) {
                if (trackId == null || type != TvTrackInfo.TYPE_VIDEO
                        || mAspectRatioChangedListener == null) {
                    return;
                }
                List<TvTrackInfo> trackInfos = mTvView.getTracks(TvTrackInfo.TYPE_VIDEO);
                if (trackInfos != null) {
                    for (TvTrackInfo trackInfo : trackInfos) {
                        if (trackInfo.getId().equals(trackId)) {
                            float videoAspectRatio = trackInfo.getVideoPixelAspectRatio()
                                    * trackInfo.getVideoWidth() / trackInfo.getVideoHeight();
                            if (DEBUG) Log.d(TAG, "Aspect Ratio: " + videoAspectRatio);
                            if (!Float.isNaN(videoAspectRatio)
                                    && mAspectRatio != videoAspectRatio) {
                                mAspectRatioChangedListener
                                        .onAspectRatioChanged(videoAspectRatio);
                                mAspectRatio = videoAspectRatio;
                                return;
                            }
                        }
                    }
                }
            }

            @Override
            public void onContentBlocked(String inputId, TvContentRating rating) {
                if (mContentBlockedListener != null) {
                    mContentBlockedListener.onContentBlocked(rating);
                }
            }
        });
    }

    private void resumeToWatchedPositionIfNeeded() {
        if (mInitialSeekPositionMs != TvInputManager.TIME_SHIFT_INVALID_TIME) {
            mTvView.timeShiftSeekTo(getRealSeekPosition(mInitialSeekPositionMs,
                    SEEK_POSITION_MARGIN_MS) + mStartPositionMs);
            mInitialSeekPositionMs = TvInputManager.TIME_SHIFT_INVALID_TIME;
        }
        if (mPauseOnPrepared) {
            mTvView.timeShiftPause();
            mPlaybackState = PlaybackState.STATE_PAUSED;
            mPauseOnPrepared = false;
        } else {
            mTvView.timeShiftResume();
            mPlaybackState = PlaybackState.STATE_PLAYING;
        }
        mCallback.onPlaybackStateChanged(mPlaybackState, 1);
    }
}