aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/tuner/tvinput/TunerSession.java
blob: 5c61402e549a1fb41f9ad91b1b29fed674b3cb80 (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
/*
 * 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.tuner.tvinput;

import android.annotation.TargetApi;
import android.content.Context;
import android.media.PlaybackParams;
import android.media.tv.TvContentRating;
import android.media.tv.TvInputManager;
import android.media.tv.TvInputService;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.exoplayer.audio.AudioCapabilities;
import com.android.tv.tuner.R;
import com.android.tv.tuner.cc.CaptionLayout;
import com.android.tv.tuner.cc.CaptionTrackRenderer;
import com.android.tv.tuner.data.Cea708Data.CaptionEvent;
import com.android.tv.tuner.data.nano.Track.AtscCaptionTrack;
import com.android.tv.tuner.exoplayer.buffer.BufferManager;
import com.android.tv.tuner.data.TunerChannel;
import com.android.tv.tuner.util.GlobalSettingsUtils;
import com.android.tv.tuner.util.StatusTextUtils;
import com.android.tv.tuner.util.SystemPropertiesProxy;

/**
 * Provides a tuner TV input session. It handles Overlay UI works. Main tuner input functions
 * are implemented in {@link TunerSessionWorker}.
 */
public class TunerSession extends TvInputService.Session implements Handler.Callback {
    private static final String TAG = "TunerSession";
    private static final boolean DEBUG = false;
    private static final String USBTUNER_SHOW_DEBUG = "persist.tv.tuner.show_debug";

    public static final int MSG_UI_SHOW_MESSAGE = 1;
    public static final int MSG_UI_HIDE_MESSAGE = 2;
    public static final int MSG_UI_SHOW_AUDIO_UNPLAYABLE = 3;
    public static final int MSG_UI_HIDE_AUDIO_UNPLAYABLE = 4;
    public static final int MSG_UI_PROCESS_CAPTION_TRACK = 5;
    public static final int MSG_UI_START_CAPTION_TRACK = 6;
    public static final int MSG_UI_STOP_CAPTION_TRACK = 7;
    public static final int MSG_UI_RESET_CAPTION_TRACK = 8;
    public static final int MSG_UI_SET_STATUS_TEXT = 9;
    public static final int MSG_UI_TOAST_RESCAN_NEEDED = 10;

    private final Context mContext;
    private final Handler mUiHandler;
    private final View mOverlayView;
    private final TextView mMessageView;
    private final TextView mStatusView;
    private final TextView mAudioStatusView;
    private final ViewGroup mMessageLayout;
    private final CaptionTrackRenderer mCaptionTrackRenderer;
    private final TunerSessionWorker mSessionWorker;
    private boolean mReleased = false;
    private boolean mPlayPaused;
    private long mTuneStartTimestamp;

    public TunerSession(Context context, ChannelDataManager channelDataManager,
            BufferManager bufferManager) {
        super(context);
        mContext = context;
        mUiHandler = new Handler(this);
        LayoutInflater inflater = (LayoutInflater)
                context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mOverlayView = inflater.inflate(R.layout.ut_overlay_view, null);
        mMessageLayout = (ViewGroup) mOverlayView.findViewById(R.id.message_layout);
        mMessageLayout.setVisibility(View.INVISIBLE);
        mMessageView = (TextView) mOverlayView.findViewById(R.id.message);
        mStatusView = (TextView) mOverlayView.findViewById(R.id.tuner_status);
        boolean showDebug = SystemPropertiesProxy.getBoolean(USBTUNER_SHOW_DEBUG, false);
        mStatusView.setVisibility(showDebug ? View.VISIBLE : View.INVISIBLE);
        mAudioStatusView = (TextView) mOverlayView.findViewById(R.id.audio_status);
        mAudioStatusView.setVisibility(View.INVISIBLE);
        mAudioStatusView.setText(Html.fromHtml(StatusTextUtils.getAudioWarningInHTML(
                context.getString(R.string.ut_surround_sound_disabled))));
        CaptionLayout captionLayout = (CaptionLayout) mOverlayView.findViewById(R.id.caption);
        mCaptionTrackRenderer = new CaptionTrackRenderer(captionLayout);
        mSessionWorker = new TunerSessionWorker(context, channelDataManager,
                bufferManager, this);
    }

    public boolean isReleased() {
        return mReleased;
    }

    @Override
    public View onCreateOverlayView() {
        return mOverlayView;
    }

    @Override
    public boolean onSelectTrack(int type, String trackId) {
        mSessionWorker.sendMessage(TunerSessionWorker.MSG_SELECT_TRACK, type, 0, trackId);
        return false;
    }

    @Override
    public void onSetCaptionEnabled(boolean enabled) {
        mSessionWorker.setCaptionEnabled(enabled);
    }

    @Override
    public void onSetStreamVolume(float volume) {
        mSessionWorker.setStreamVolume(volume);
    }

    @Override
    public boolean onSetSurface(Surface surface) {
        mSessionWorker.setSurface(surface);
        return true;
    }

    @Override
    public void onTimeShiftPause() {
        mSessionWorker.sendMessage(TunerSessionWorker.MSG_TIMESHIFT_PAUSE);
        mPlayPaused = true;
    }

    @Override
    public void onTimeShiftResume() {
        mSessionWorker.sendMessage(TunerSessionWorker.MSG_TIMESHIFT_RESUME);
        mPlayPaused = false;
    }

    @Override
    public void onTimeShiftSeekTo(long timeMs) {
        if (DEBUG) Log.d(TAG, "Timeshift seekTo requested position: " + timeMs / 1000);
        mSessionWorker.sendMessage(TunerSessionWorker.MSG_TIMESHIFT_SEEK_TO,
                mPlayPaused ? 1 : 0, 0, timeMs);
    }

    @Override
    public void onTimeShiftSetPlaybackParams(PlaybackParams params) {
        mSessionWorker.sendMessage(
                TunerSessionWorker.MSG_TIMESHIFT_SET_PLAYBACKPARAMS, params);
    }

    @Override
    public long onTimeShiftGetStartPosition() {
        return mSessionWorker.getStartPosition();
    }

    @Override
    public long onTimeShiftGetCurrentPosition() {
        return mSessionWorker.getCurrentPosition();
    }

    @Override
    public boolean onTune(Uri channelUri) {
        if (DEBUG) {
            Log.d(TAG, "onTune to " + channelUri != null ? channelUri.toString() : "");
        }
        if (channelUri == null) {
            Log.w(TAG, "onTune() is failed due to null channelUri.");
            mSessionWorker.stopTune();
            return false;
        }
        mTuneStartTimestamp = SystemClock.elapsedRealtime();
        mSessionWorker.tune(channelUri);
        mPlayPaused = false;
        return true;
    }

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public void onTimeShiftPlay(Uri recordUri) {
        if (recordUri == null) {
            Log.w(TAG, "onTimeShiftPlay() is failed due to null channelUri.");
            mSessionWorker.stopTune();
            return;
        }
        mTuneStartTimestamp = SystemClock.elapsedRealtime();
        mSessionWorker.tune(recordUri);
        mPlayPaused = false;
    }

    @Override
    public void onUnblockContent(TvContentRating unblockedRating) {
        mSessionWorker.sendMessage(TunerSessionWorker.MSG_UNBLOCKED_RATING,
                unblockedRating);
    }

    @Override
    public void onRelease() {
        if (DEBUG) {
            Log.d(TAG, "onRelease");
        }
        mReleased = true;
        mSessionWorker.release();
        mUiHandler.removeCallbacksAndMessages(null);
    }

    /**
     * Sets {@link AudioCapabilities}.
     */
    public void setAudioCapabilities(AudioCapabilities audioCapabilities) {
        mSessionWorker.sendMessage(TunerSessionWorker.MSG_AUDIO_CAPABILITIES_CHANGED,
                audioCapabilities);
    }

    @Override
    public void notifyVideoAvailable() {
        super.notifyVideoAvailable();
        if (mTuneStartTimestamp != 0) {
            Log.i(TAG, "[Profiler] Video available in "
                    + (SystemClock.elapsedRealtime() - mTuneStartTimestamp) + " ms");
            mTuneStartTimestamp = 0;
        }
    }

    @Override
    public void notifyVideoUnavailable(int reason) {
        super.notifyVideoUnavailable(reason);
        if (reason != TvInputManager.VIDEO_UNAVAILABLE_REASON_BUFFERING
                && reason != TvInputManager.VIDEO_UNAVAILABLE_REASON_WEAK_SIGNAL) {
            notifyTimeShiftStatusChanged(TvInputManager.TIME_SHIFT_STATUS_UNAVAILABLE);
        }
    }

    public void sendUiMessage(int message) {
        mUiHandler.sendEmptyMessage(message);
    }

    public void sendUiMessage(int message, Object object) {
        mUiHandler.obtainMessage(message, object).sendToTarget();
    }

    public void sendUiMessage(int message, int arg1, int arg2, Object object) {
        mUiHandler.obtainMessage(message, arg1, arg2, object).sendToTarget();
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_UI_SHOW_MESSAGE: {
                mMessageView.setText((String) msg.obj);
                mMessageLayout.setVisibility(View.VISIBLE);
                return true;
            }
            case MSG_UI_HIDE_MESSAGE: {
                mMessageLayout.setVisibility(View.INVISIBLE);
                return true;
            }
            case MSG_UI_SHOW_AUDIO_UNPLAYABLE: {
                // Showing message of enabling surround sound only when global surround sound
                // setting is "never".
                final int value = GlobalSettingsUtils.getEncodedSurroundOutputSettings(mContext);
                if (value == GlobalSettingsUtils.ENCODED_SURROUND_OUTPUT_NEVER) {
                    mAudioStatusView.setVisibility(View.VISIBLE);
                } else {
                    Log.e(TAG, "Audio is unavailable, surround sound setting is " + value);
                }
                return true;
            }
            case MSG_UI_HIDE_AUDIO_UNPLAYABLE: {
                mAudioStatusView.setVisibility(View.INVISIBLE);
                return true;
            }
            case MSG_UI_PROCESS_CAPTION_TRACK: {
                mCaptionTrackRenderer.processCaptionEvent((CaptionEvent) msg.obj);
                return true;
            }
            case MSG_UI_START_CAPTION_TRACK: {
                mCaptionTrackRenderer.start((AtscCaptionTrack) msg.obj);
                return true;
            }
            case MSG_UI_STOP_CAPTION_TRACK: {
                mCaptionTrackRenderer.stop();
                return true;
            }
            case MSG_UI_RESET_CAPTION_TRACK: {
                mCaptionTrackRenderer.reset();
                return true;
            }
            case MSG_UI_SET_STATUS_TEXT: {
                mStatusView.setText((CharSequence) msg.obj);
                return true;
            }
            case MSG_UI_TOAST_RESCAN_NEEDED: {
                Toast.makeText(mContext, R.string.ut_rescan_needed, Toast.LENGTH_LONG).show();
                return true;
            }
        }
        return false;
    }
}