aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/RecordingTask.java
blob: 804485b3f99cf68b3f900b158731b0c665a32302 (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
/*
 * 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.media.tv.TvContract;
import android.media.tv.TvRecordingClient;
import android.net.Uri;
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 com.android.tv.common.SoftPreconditions;
import com.android.tv.data.Channel;
import com.android.tv.util.Clock;
import com.android.tv.util.Utils;

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
class RecordingTask extends TvRecordingClient.RecordingCallback
        implements Handler.Callback, DvrManager.Listener {
    private static final String TAG = "RecordingTask";
    private static final boolean DEBUG = false;

    @VisibleForTesting
    static final int MESSAGE_INIT = 1;
    @VisibleForTesting
    static final int MESSAGE_START_RECORDING = 2;
    @VisibleForTesting
    static final int MESSAGE_STOP_RECORDING = 3;

    @VisibleForTesting
    static final long MS_BEFORE_START = TimeUnit.SECONDS.toMillis(5);
    @VisibleForTesting
    static final long MS_AFTER_END = TimeUnit.SECONDS.toMillis(5);

    @VisibleForTesting
    enum State {
        NOT_STARTED,
        SESSION_ACQUIRED,
        CONNECTION_PENDING,
        CONNECTED,
        RECORDING_START_REQUESTED,
        RECORDING_STARTED,
        RECORDING_STOP_REQUESTED,
        ERROR,
        RELEASED,
    }
    private final DvrSessionManager mSessionManager;
    private final DvrManager mDvrManager;

    private final WritableDvrDataManager mDataManager;
    private final Handler mMainThreadHandler = new Handler(Looper.getMainLooper());
    private TvRecordingClient mTvRecordingClient;
    private Handler mHandler;
    private ScheduledRecording mScheduledRecording;
    private final Channel mChannel;
    private State mState = State.NOT_STARTED;
    private final Clock mClock;

    RecordingTask(ScheduledRecording scheduledRecording, Channel channel,
            DvrManager dvrManager, DvrSessionManager sessionManager,
            WritableDvrDataManager dataManager, Clock clock) {
        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 == Scheduler.HandlerWrapper.MESSAGE_REMOVE || mHandler != null,
                        TAG, "Null handler trying to handle " + msg);
        try {
            switch (msg.what) {
                case MESSAGE_INIT:
                    handleInit();
                    break;
                case MESSAGE_START_RECORDING:
                    handleStartRecording();
                    break;
                case MESSAGE_STOP_RECORDING:
                    handleStopRecording();
                    break;
                case Scheduler.HandlerWrapper.MESSAGE_REMOVE:
                    // Clear the handler
                    mHandler = null;
                    release();
                    return false;
                default:
                    SoftPreconditions.checkArgument(false, TAG, "unexpected message type " + msg);
            }
            return true;
        } catch (Exception e) {
            Log.w(TAG, "Error processing message " + msg + "  for " + mScheduledRecording, e);
            failAndQuit();
        }
        return false;
    }

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


    @Override
    public void onRecordingStopped(Uri recordedProgramUri) {
        super.onRecordingStopped(recordedProgramUri);
        mState = State.CONNECTED;
        updateRecording(ScheduledRecording.buildFrom(mScheduledRecording)
                .setState(ScheduledRecording.STATE_RECORDING_FINISHED).build());
        sendRemove();
    }

    @Override
    public void onError(int reason) {
        if (DEBUG) Log.d(TAG, "onError reason " + reason);
        super.onError(reason);
        // TODO(dvr) handle success
        switch (reason) {
            default:
                updateRecording(ScheduledRecording.buildFrom(mScheduledRecording)
                        .setState(ScheduledRecording.STATE_RECORDING_FAILED)
                        .build());
        }
        release();
        sendRemove();
    }

    private void handleInit() {
        if (DEBUG) Log.d(TAG, "handleInit " + mScheduledRecording);
        //TODO check recording preconditions

        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();
        if (mSessionManager.canAcquireDvrSession(inputId, mChannel)) {
            mTvRecordingClient = mSessionManager
                    .createTvRecordingClient("recordingTask-" + mScheduledRecording.getId(), this,
                            mHandler);
            mState = State.SESSION_ACQUIRED;
        } else {
            Log.w(TAG, "Unable to acquire a session for " + mScheduledRecording);
            failAndQuit();
            return;
        }
        mDvrManager.addListener(this, mHandler);
        mTvRecordingClient.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.sendEmptyMessage(Scheduler.HandlerWrapper.MESSAGE_REMOVE);
        }
    }

    private void handleStartRecording() {
        if (DEBUG) Log.d(TAG, "handleStartRecording " + mScheduledRecording);
        // TODO(DVR) handle errors
        long programId = mScheduledRecording.getProgramId();
        mTvRecordingClient.startRecording(programId == ScheduledRecording.ID_NOT_SET ? null
                : TvContract.buildProgramUri(programId));
        updateRecording(ScheduledRecording.buildFrom(mScheduledRecording)
                .setState(ScheduledRecording.STATE_RECORDING_IN_PROGRESS).build());
        mState = State.RECORDING_STARTED;

        if (mHandler == null || !sendEmptyMessageAtAbsoluteTime(MESSAGE_STOP_RECORDING,
                mScheduledRecording.getEndTimeMs() + MS_AFTER_END)) {
            mState = State.ERROR;
            return;
        }
    }

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

    @VisibleForTesting
    State getState() {
        return mState;
    }

    private void release() {
        if (mTvRecordingClient != null) {
           mSessionManager.releaseTvRecordingClient(mTvRecordingClient);
        }
        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) {
        updateRecording(ScheduledRecording.buildFrom(mScheduledRecording).setState(state).build());
    }

    @VisibleForTesting
    static Uri getIdAsMediaUri(ScheduledRecording scheduledRecording) {
            // TODO define the URI format
        return new Uri.Builder().appendPath(String.valueOf(scheduledRecording.getId())).build();
    }

    private void updateRecording(ScheduledRecording updatedScheduledRecording) {
        if (DEBUG) Log.d(TAG, "updateScheduledRecording " + updatedScheduledRecording);
        mScheduledRecording = updatedScheduledRecording;
        mMainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                mDataManager.updateScheduledRecording(mScheduledRecording);
            }
        });
    }

    @Override
    public void onStopRecordingRequested(ScheduledRecording recording) {
        if (recording.getId() != mScheduledRecording.getId()) {
            return;
        }
        switch (mState) {
            case RECORDING_STARTED:
                mHandler.removeMessages(MESSAGE_STOP_RECORDING);
                handleStopRecording();
                break;
            case RECORDING_STOP_REQUESTED:
                // Do nothing
                break;
            case NOT_STARTED:
            case SESSION_ACQUIRED:
            case CONNECTION_PENDING:
            case CONNECTED:
            case RECORDING_START_REQUESTED:
            case ERROR:
            case RELEASED:
            default:
                sendRemove();
                break;
        }
    }

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