aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/RecordingTask.java
blob: c3d236b05d1b54d2ed22cca2bf87d658a45bfbf9 (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
 * Copyright (C) 2015 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.annotation.TargetApi;
import android.content.Context;
import android.media.tv.TvContract;
import android.media.tv.TvInputManager;
import android.media.tv.TvRecordingClient.RecordingCallback;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.WorkerThread;
import android.util.Log;
import android.widget.Toast;

import com.android.tv.InputSessionManager;
import com.android.tv.InputSessionManager.RecordingSession;
import com.android.tv.R;
import com.android.tv.TvApplication;
import com.android.tv.common.SoftPreconditions;
import com.android.tv.data.Channel;
import com.android.tv.dvr.InputTaskScheduler.HandlerWrapper;
import com.android.tv.util.Clock;
import com.android.tv.util.Utils;

import java.util.Comparator;
import java.util.concurrent.TimeUnit;

/**
 * A Handler that actually starts and stop a recording at the right time.
 *
 * <p>This is run on the looper of thread named {@value DvrRecordingService#HANDLER_THREAD_NAME}.
 * There is only one looper so messages must be handled quickly or start a separate thread.
 */
@WorkerThread
@VisibleForTesting
@TargetApi(Build.VERSION_CODES.N)
public class RecordingTask extends RecordingCallback implements Handler.Callback,
        DvrManager.Listener {
    private static final String TAG = "RecordingTask";
    private static final boolean DEBUG = false;

    /**
     * Compares the end time in ascending order.
     */
    public static final Comparator<RecordingTask> END_TIME_COMPARATOR
            = new Comparator<RecordingTask>() {
        @Override
        public int compare(RecordingTask lhs, RecordingTask rhs) {
            return Long.compare(lhs.getEndTimeMs(), rhs.getEndTimeMs());
        }
    };

    /**
     * Compares ID in ascending order.
     */
    public static final Comparator<RecordingTask> ID_COMPARATOR
            = new Comparator<RecordingTask>() {
        @Override
        public int compare(RecordingTask lhs, RecordingTask rhs) {
            return Long.compare(lhs.getScheduleId(), rhs.getScheduleId());
        }
    };

    /**
     * Compares the priority in ascending order.
     */
    public static final Comparator<RecordingTask> PRIORITY_COMPARATOR
            = new Comparator<RecordingTask>() {
        @Override
        public int compare(RecordingTask lhs, RecordingTask rhs) {
            return Long.compare(lhs.getPriority(), rhs.getPriority());
        }
    };

    @VisibleForTesting
    static final int MSG_INITIALIZE = 1;
    @VisibleForTesting
    static final int MSG_START_RECORDING = 2;
    @VisibleForTesting
    static final int MSG_STOP_RECORDING = 3;
    /**
     * Message to update schedule.
     */
    public static final int MSG_UDPATE_SCHEDULE = 4;

    /**
     * The time when the start command will be sent before the recording starts.
     */
    public static final long RECORDING_EARLY_START_OFFSET_MS = TimeUnit.SECONDS.toMillis(3);
    /**
     * If the recording starts later than the scheduled start time or ends before the scheduled end
     * time, it's considered as clipped.
     */
    private static final long CLIPPED_THRESHOLD_MS = TimeUnit.MINUTES.toMillis(5);

    @VisibleForTesting
    enum State {
        NOT_STARTED,
        SESSION_ACQUIRED,
        CONNECTION_PENDING,
        CONNECTED,
        RECORDING_STARTED,
        RECORDING_STOP_REQUESTED,
        FINISHED,
        ERROR,
        RELEASED,
    }
    private final InputSessionManager mSessionManager;
    private final DvrManager mDvrManager;
    private final Context mContext;

    private final WritableDvrDataManager mDataManager;
    private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper());
    private RecordingSession mRecordingSession;
    private Handler mHandler;
    private ScheduledRecording mScheduledRecording;
    private final Channel mChannel;
    private State mState = State.NOT_STARTED;
    private final Clock mClock;
    private boolean mStartedWithClipping;
    private Uri mRecordedProgramUri;
    private boolean mCanceled;

    RecordingTask(Context context, ScheduledRecording scheduledRecording, Channel channel,
            DvrManager dvrManager, InputSessionManager sessionManager,
            WritableDvrDataManager dataManager, Clock clock) {
        mContext = context;
        mScheduledRecording = scheduledRecording;
        mChannel = channel;
        mSessionManager = sessionManager;
        mDataManager = dataManager;
        mClock = clock;
        mDvrManager = dvrManager;

        if (DEBUG) Log.d(TAG, "created recording task " + mScheduledRecording);
    }

    public void setHandler(Handler handler) {
        mHandler = handler;
    }

    @Override
    public boolean handleMessage(Message msg) {
        if (DEBUG) Log.d(TAG, "handleMessage " + msg);
        SoftPreconditions.checkState(msg.what == HandlerWrapper.MESSAGE_REMOVE || mHandler != null,
                TAG, "Null handler trying to handle " + msg);
        try {
            switch (msg.what) {
                case MSG_INITIALIZE:
                    handleInit();
                    break;
                case MSG_START_RECORDING:
                    handleStartRecording();
                    break;
                case MSG_STOP_RECORDING:
                    handleStopRecording();
                    break;
                case MSG_UDPATE_SCHEDULE:
                    handleUpdateSchedule((ScheduledRecording) msg.obj);
                    break;
                case HandlerWrapper.MESSAGE_REMOVE:
                    mHandler.removeCallbacksAndMessages(null);
                    mHandler = null;
                    release();
                    return false;
                default:
                    SoftPreconditions.checkArgument(false, TAG, "unexpected message type " + msg);
                    break;
            }
            return true;
        } catch (Exception e) {
            Log.w(TAG, "Error processing message " + msg + "  for " + mScheduledRecording, e);
            failAndQuit();
        }
        return false;
    }

    @Override
    public void onDisconnected(String inputId) {
        if (DEBUG) Log.d(TAG, "onDisconnected(" + inputId + ")");
        if (mRecordingSession != null && mState != State.FINISHED) {
            failAndQuit();
        }
    }

    @Override
    public void onConnectionFailed(String inputId) {
        if (DEBUG) Log.d(TAG, "onConnectionFailed(" + inputId + ")");
        if (mRecordingSession != null) {
            failAndQuit();
        }
    }

    @Override
    public void onTuned(Uri channelUri) {
        if (DEBUG) Log.d(TAG, "onTuned");
        if (mRecordingSession == null) {
            return;
        }
        mState = State.CONNECTED;
        if (mHandler == null || !sendEmptyMessageAtAbsoluteTime(MSG_START_RECORDING,
                mScheduledRecording.getStartTimeMs() - RECORDING_EARLY_START_OFFSET_MS)) {
            failAndQuit();
        }
    }

    @Override
    public void onRecordingStopped(Uri recordedProgramUri) {
        if (DEBUG) Log.d(TAG, "onRecordingStopped");
        if (mRecordingSession == null) {
            return;
        }
        mRecordedProgramUri = recordedProgramUri;
        mState = State.FINISHED;
        int state = ScheduledRecording.STATE_RECORDING_FINISHED;
        if (mStartedWithClipping || mScheduledRecording.getEndTimeMs() - CLIPPED_THRESHOLD_MS
                > mClock.currentTimeMillis()) {
            state = ScheduledRecording.STATE_RECORDING_CLIPPED;
        }
        updateRecordingState(state);
        sendRemove();
        if (mCanceled) {
            removeRecordedProgram();
        }
    }

    @Override
    public void onError(int reason) {
        if (DEBUG) Log.d(TAG, "onError reason " + reason);
        if (mRecordingSession == null) {
            return;
        }
        switch (reason) {
            case TvInputManager.RECORDING_ERROR_INSUFFICIENT_SPACE:
                mMainThreadHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if (TvApplication.getSingletons(mContext).getMainActivityWrapper()
                                .isResumed()) {
                            Toast.makeText(mContext.getApplicationContext(),
                                    R.string.dvr_error_insufficient_space_description,
                                    Toast.LENGTH_LONG)
                                    .show();
                        } else {
                            Utils.setRecordingFailedReason(mContext.getApplicationContext(),
                                    TvInputManager.RECORDING_ERROR_INSUFFICIENT_SPACE);
                        }
                    }
                });
                // Pass through
            default:
                failAndQuit();
                break;
        }
    }

    private void handleInit() {
        if (DEBUG) Log.d(TAG, "handleInit " + mScheduledRecording);
        if (mScheduledRecording.getEndTimeMs() < mClock.currentTimeMillis()) {
            Log.w(TAG, "End time already past, not recording " + mScheduledRecording);
            failAndQuit();
            return;
        }
        if (mChannel == null) {
            Log.w(TAG, "Null channel for " + mScheduledRecording);
            failAndQuit();
            return;
        }
        if (mChannel.getId() != mScheduledRecording.getChannelId()) {
            Log.w(TAG, "Channel" + mChannel + " does not match scheduled recording "
                    + mScheduledRecording);
            failAndQuit();
            return;
        }

        String inputId = mChannel.getInputId();
        mRecordingSession = mSessionManager.createRecordingSession(inputId,
                "recordingTask-" + mScheduledRecording.getId(), this,
                mHandler, mScheduledRecording.getEndTimeMs());
        mState = State.SESSION_ACQUIRED;
        mDvrManager.addListener(this, mHandler);
        mRecordingSession.tune(inputId, mChannel.getUri());
        mState = State.CONNECTION_PENDING;
    }

    private void failAndQuit() {
        if (DEBUG) Log.d(TAG, "failAndQuit");
        updateRecordingState(ScheduledRecording.STATE_RECORDING_FAILED);
        mState = State.ERROR;
        sendRemove();
    }

    private void sendRemove() {
        if (DEBUG) Log.d(TAG, "sendRemove");
        if (mHandler != null) {
            mHandler.sendMessageAtFrontOfQueue(mHandler.obtainMessage(
                    HandlerWrapper.MESSAGE_REMOVE));
        }
    }

    private void handleStartRecording() {
        if (DEBUG) Log.d(TAG, "handleStartRecording " + mScheduledRecording);
        long programId = mScheduledRecording.getProgramId();
        mRecordingSession.startRecording(programId == ScheduledRecording.ID_NOT_SET ? null
                : TvContract.buildProgramUri(programId));
        updateRecordingState(ScheduledRecording.STATE_RECORDING_IN_PROGRESS);
        // If it starts late, it's clipped.
        if (mScheduledRecording.getStartTimeMs() + CLIPPED_THRESHOLD_MS
                < mClock.currentTimeMillis()) {
            mStartedWithClipping = true;
        }
        mState = State.RECORDING_STARTED;

        if (!sendEmptyMessageAtAbsoluteTime(MSG_STOP_RECORDING,
                mScheduledRecording.getEndTimeMs())) {
            failAndQuit();
        }
    }

    private void handleStopRecording() {
        if (DEBUG) Log.d(TAG, "handleStopRecording " + mScheduledRecording);
        mRecordingSession.stopRecording();
        mState = State.RECORDING_STOP_REQUESTED;
    }

    private void handleUpdateSchedule(ScheduledRecording schedule) {
        mScheduledRecording = schedule;
        // Check end time only. The start time is checked in InputTaskScheduler.
        if (schedule.getEndTimeMs() != mScheduledRecording.getEndTimeMs()) {
            if (mRecordingSession != null) {
                mRecordingSession.setEndTimeMs(schedule.getEndTimeMs());
            }
            if (mState == State.RECORDING_STARTED) {
                mHandler.removeMessages(MSG_STOP_RECORDING);
                if (!sendEmptyMessageAtAbsoluteTime(MSG_STOP_RECORDING, schedule.getEndTimeMs())) {
                    failAndQuit();
                }
            }
        }
    }

    @VisibleForTesting
    State getState() {
        return mState;
    }

    private long getScheduleId() {
        return mScheduledRecording.getId();
    }

    /**
     * Returns the priority.
     */
    public long getPriority() {
        return mScheduledRecording.getPriority();
    }

    /**
     * Returns the start time of the recording.
     */
    public long getStartTimeMs() {
        return mScheduledRecording.getStartTimeMs();
    }

    /**
     * Returns the end time of the recording.
     */
    public long getEndTimeMs() {
        return mScheduledRecording.getEndTimeMs();
    }

    private void release() {
        if (mRecordingSession != null) {
            mSessionManager.releaseRecordingSession(mRecordingSession);
            mRecordingSession = null;
        }
        mDvrManager.removeListener(this);
    }

    private boolean sendEmptyMessageAtAbsoluteTime(int what, long when) {
        long now = mClock.currentTimeMillis();
        long delay = Math.max(0L, when - now);
        if (DEBUG) {
            Log.d(TAG, "Sending message " + what + " with a delay of " + delay / 1000
                    + " seconds to arrive at " + Utils.toIsoDateTimeString(when));
        }
        return mHandler.sendEmptyMessageDelayed(what, delay);
    }

    private void updateRecordingState(@ScheduledRecording.RecordingState int state) {
        if (DEBUG) Log.d(TAG, "Updating the state of " + mScheduledRecording + " to " + state);
        mScheduledRecording = ScheduledRecording.buildFrom(mScheduledRecording).setState(state)
                .build();
        runOnMainThread(new Runnable() {
            @Override
            public void run() {
                ScheduledRecording schedule = mDataManager.getScheduledRecording(
                        mScheduledRecording.getId());
                if (schedule == null) {
                    // Schedule has been deleted. Delete the recorded program.
                    removeRecordedProgram();
                } else  {
                    // Update the state based on the object in DataManager in case when it has been
                    // updated. mScheduledRecording will be updated from
                    // onScheduledRecordingStateChanged.
                    mDataManager.updateScheduledRecording(ScheduledRecording.buildFrom(schedule)
                            .setState(state).build());
                }
            }
        });
    }

    @Override
    public void onStopRecordingRequested(ScheduledRecording recording) {
        if (recording.getId() != mScheduledRecording.getId()) {
            return;
        }
        stop();
    }

    /**
     * Starts the task.
     */
    public void start() {
        mHandler.sendEmptyMessage(MSG_INITIALIZE);
    }

    /**
     * Stops the task.
     */
    public void stop() {
        if (DEBUG) Log.d(TAG, "stop");
        switch (mState) {
            case RECORDING_STARTED:
                mHandler.removeMessages(MSG_STOP_RECORDING);
                handleStopRecording();
                break;
            case RECORDING_STOP_REQUESTED:
                // Do nothing
                break;
            case NOT_STARTED:
            case SESSION_ACQUIRED:
            case CONNECTION_PENDING:
            case CONNECTED:
            case FINISHED:
            case ERROR:
            case RELEASED:
            default:
                sendRemove();
                break;
        }
    }

    /**
     * Cancels the task
     */
    public void cancel() {
        if (DEBUG) Log.d(TAG, "cancel");
        mCanceled = true;
        stop();
        removeRecordedProgram();
    }

    /**
     * Clean up the task.
     */
    public void cleanUp() {
        if (mState == State.RECORDING_STARTED || mState == State.RECORDING_STOP_REQUESTED) {
            updateRecordingState(ScheduledRecording.STATE_RECORDING_FAILED);
        }
        release();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
    }

    @Override
    public String toString() {
        return getClass().getName() + "(" + mScheduledRecording + ")";
    }

    private void removeRecordedProgram() {
        runOnMainThread(new Runnable() {
            @Override
            public void run() {
                if (mRecordedProgramUri != null) {
                    mDvrManager.removeRecordedProgram(mRecordedProgramUri);
                }
            }
        });
    }

    private void runOnMainThread(Runnable runnable) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            runnable.run();
        } else {
            mMainThreadHandler.post(runnable);
        }
    }
}