aboutsummaryrefslogtreecommitdiff
path: root/sample/src/com/example/sampletvinput/BaseTvInputService.java
blob: 39d79e6736d3ba9ccef8ac9940d405785a18e677 (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
/*
 * Copyright (C) 2014 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.example.sampletvinput;

import android.content.ComponentName;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.TrackInfo;
import android.media.tv.TvContentRating;
import android.media.tv.TvContract;
import android.media.tv.TvContract.Programs;
import android.media.tv.TvInputManager;
import android.media.tv.TvInputService;
import android.media.tv.TvTrackInfo;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.util.LongSparseArray;
import android.view.KeyEvent;
import android.view.Surface;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

abstract public class BaseTvInputService extends TvInputService {
    private static final String TAG = "BaseTvInputService";
    private static final boolean DEBUG = true;

    private final LongSparseArray<ChannelInfo> mChannelMap = new LongSparseArray<ChannelInfo>();
    private String mSelectedAudioTrack;
    private String mSelectedVideoTrack;
    private String mSelectedSubtitleTrack;
    private final Handler mHandler = new Handler();

    protected List<ChannelInfo> mChannels;
    private Uri mChannelUri;

    private boolean mIsPlaying;

    @Override
    public void onCreate() {
        if (DEBUG) Log.d(TAG, "onCreate()");
        super.onCreate();

        // TODO: Build channel map when the session is connected. At that point, we will come to
        // know the input ID.
        buildChannelMap();
        setTheme(android.R.style.Theme_Holo_Light_NoActionBar);
    }

    @Override
    public void onDestroy() {
        if (DEBUG) Log.d(TAG, "onDestroy()");
        super.onDestroy();
    }

    @Override
    public Session onCreateSession(String inputId) {
        if (DEBUG) Log.d(TAG, "onCreateSession(inputId=" + inputId + ")");
        return new BaseTvInputSessionImpl();
    }

    abstract public List<ChannelInfo> createSampleChannels();

    private synchronized void buildChannelMap() {
        Uri uri = TvContract.buildChannelsUriForInput(Utils.getInputIdFromComponentName(this,
                new ComponentName(this, this.getClass())), false);
        String[] projection = {
                TvContract.Channels._ID,
                TvContract.Channels.COLUMN_DISPLAY_NUMBER
        };
        // TODO: The app should write all channel information into DB and reconstruct channel map
        // from it.
        mChannels = createSampleChannels();
        if (mChannels == null || mChannels.isEmpty()) {
            Log.w(TAG, "No channel list.");
            return;
        }
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(uri, projection, null, null, null);

            if (cursor == null || cursor.getCount() == 0) {
                Log.e(TAG, "Couldn't find the channel list.");
                return;
            }

            while (cursor.moveToNext()) {
                long channelId = cursor.getLong(0);
                String channelNumber = cursor.getString(1);
                if (DEBUG) Log.d(TAG, "Channel mapping: ID(" + channelId + ") -> " + channelNumber);
                mChannelMap.put(channelId, getChannelByNumber(channelNumber, false));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    private ChannelInfo getChannelByNumber(String channelNumber, boolean isRetry) {
        for (ChannelInfo info : mChannels) {
            if (info.mNumber.equals(channelNumber)) {
                return info;
            }
        }
        if (!isRetry) {
            buildChannelMap();
            return getChannelByNumber(channelNumber, true);
        }
        throw new IllegalArgumentException("Unknown channel: " + channelNumber);
    }

    private ChannelInfo getChannelByUri(Uri channelUri, boolean isRetry) {
        ChannelInfo info = mChannelMap.get(ContentUris.parseId(channelUri));
        if (info == null) {
            if (!isRetry) {
                buildChannelMap();
                return getChannelByUri(channelUri, true);
            }
            throw new IllegalArgumentException("Unknown channel: " + channelUri);
        }
        return info;
    }

    class BaseTvInputSessionImpl extends TvInputService.Session {
        private MediaPlayer mPlayer;
        private float mVolume;
        private boolean mMute;
        private Map<String, TvTrackInfo> mTracks;


        protected BaseTvInputSessionImpl() {
            mPlayer = new MediaPlayer();
            mVolume = 1.0f;
            mMute = false;

            mPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
                @Override
                public boolean onInfo(MediaPlayer player, int what, int arg) {
                    if (what == MediaPlayer.MEDIA_INFO_BUFFERING_START) {
                        notifyVideoUnavailable(
                                TvInputManager.VIDEO_UNAVAILABLE_REASON_BUFFERING);
                        return true;
                    } else if (what == MediaPlayer.MEDIA_INFO_BUFFERING_END) {
                        notifyVideoAvailable();
                        return true;
                    }
                    return false;
                }
            });
        }

        @Override
        public void onRelease() {
            if (mPlayer != null) {
                mPlayer.release();
                mPlayer = null;
            }
        }

        @Override
        public boolean onSetSurface(Surface surface) {
            if (DEBUG) Log.d(TAG, "onSetSurface(" + surface + ")");
            mPlayer.setSurface(surface);
            return true;
        }

        @Override
        public void onSetStreamVolume(float volume) {
            if (DEBUG) Log.d(TAG, "onSetStreamVolume(" + volume + ")");
            mVolume = volume;
            mPlayer.setVolume(volume, volume);
        }

        private boolean setDataSource(MediaPlayer player, ChannelInfo channel) {
            ProgramInfo program = channel.mProgram;
            try {
                if (program.mUrl != null) {
                    player.setDataSource(program.mUrl);
                } else {
                    AssetFileDescriptor afd = getResources().openRawResourceFd(program.mResourceId);
                    if (afd == null) {
                        return false;
                    }
                    player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
                            afd.getDeclaredLength());
                    afd.close();
                }
                // Android media player does not support looping for HLS.
                if (program.mUrl == null || !program.mUrl.startsWith("http")) {
                    player.setLooping(true);
                }
            } catch (IllegalArgumentException | IllegalStateException | IOException e) {
                // Do nothing.
            }
            return true;
        }

        private boolean changeChannel(Uri channelUri) {
            final ChannelInfo channel = getChannelByUri(channelUri, false);
            if (!startPlayback(channel)) {
                return false;
            }
            // Create empty program information and insert it into the database.
            // Delay intentionally to see whether the updated program information dynamically
            // replaces the previous one on the channel banner (for testing). This is to simulate
            // the actual case where we get parsed program data only after tuning is done.
            final long DELAY_FOR_TESTING_IN_MILLIS = 1000; // 1 second
            mHandler.postDelayed(
                    new AddProgramRunnable(channelUri, channel.mProgram),
                    DELAY_FOR_TESTING_IN_MILLIS);
            return true;
        }

        private boolean startPlayback(final ChannelInfo channel) {
            mIsPlaying = false;
            mPlayer.reset();
            notifyVideoUnavailable(TvInputManager.VIDEO_UNAVAILABLE_REASON_TUNING);
            if (!setDataSource(mPlayer, channel)) {
                if (DEBUG) Log.d(TAG, "Failed to set the data source");
                return false;
            }
            try {
                mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer player) {
                        if (mPlayer != null && !mPlayer.isPlaying()) {
                            int duration = mPlayer.getDuration();
                            if (duration > 0) {
                                int seekPosition = (int) (System.currentTimeMillis() % duration);
                                mPlayer.seekTo(seekPosition);
                            }
                            MediaPlayer.TrackInfo[] tracks = mPlayer.getTrackInfo();
                            setupTrackInfo(tracks, channel);
                            notifyVideoAvailable();
                            try {
                                mPlayer.start();
                                mIsPlaying = true;
                            } catch (IllegalStateException e) {
                                mIsPlaying = false;
                            }
                        }
                    }
                });
                mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                // Loop only if onPrepared has been called normally.
                                if (mIsPlaying) {
                                    startPlayback(channel);
                                }
                            }
                        });
                    }
                });
                mPlayer.prepareAsync();
            } catch (IllegalStateException e1) {
                return false;
            }
            return true;
        }

        @Override
        public boolean onTune(Uri channelUri) {
            if (DEBUG) Log.d(TAG, "onTune(" + channelUri + ")");
            return changeChannel(channelUri);
        }

        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if (event.getKeyCode() == KeyEvent.KEYCODE_M) {
                mMute = !mMute;
                if (mMute) {
                    mPlayer.setVolume(0.0f, 0.0f);
                } else {
                    mPlayer.setVolume(mVolume, mVolume);
                }
                return true;
            } else if (event.getKeyCode() == KeyEvent.KEYCODE_C) {
                // This simulates the case TV input changes the channel without tune request from
                // TV app. e.g. Channel change from STB.
                if (mChannelUri != null) {
                    int index = mChannelMap.indexOfKey(ContentUris.parseId(mChannelUri));
                    long nextChannelId = mChannelMap.keyAt((index + 1) % mChannelMap.size());
                    Uri nextChannelUri = TvContract.buildChannelUri(nextChannelId);
                    changeChannel(TvContract.buildChannelUri(nextChannelId));
                    notifyChannelRetuned(nextChannelUri);
                }
                return true;
            }
            return false;
        }

        @Override
        public void onSetCaptionEnabled(boolean enabled) {
            if (DEBUG) Log.d(TAG, "onSetCaptionEnabled(" + enabled + ")");
        }

        @Override
        public boolean onSelectTrack(int type, String trackId) {
            Log.d(TAG, "onSelectTrack(" + type + " ," + trackId + ")");
            if (mPlayer != null) {
                if (type == TvTrackInfo.TYPE_SUBTITLE) {
                    // SelectTrack only works on subtitle tracks.
                    if (trackId == null) {
                        if (mSelectedSubtitleTrack != null) {
                            mPlayer.deselectTrack(Integer.parseInt(mSelectedSubtitleTrack));
                        }
                    } else {
                        TvTrackInfo track = mTracks.get(trackId);
                        if (track == null) {
                            return false;
                        }
                        mPlayer.selectTrack(Integer.parseInt(trackId));
                        mSelectedSubtitleTrack = trackId;
                    }
                    notifyTrackSelected(TvTrackInfo.TYPE_SUBTITLE, trackId);
                    return true;
                }
            }
            return false;
        }

        private void setupTrackInfo(MediaPlayer.TrackInfo[] infos, ChannelInfo channel) {
            Map<String, TvTrackInfo> tracks = new HashMap<String, TvTrackInfo>();
            // Add subtitle tracks from the real media.
            int i;
            for (i = 0; i < infos.length; ++i) {
                if (infos[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT
                        || infos[i].getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_SUBTITLE) {
                    tracks.put(Integer.toString(i), new TvTrackInfo.Builder(
                            TvTrackInfo.TYPE_SUBTITLE, Integer.toString(i))
                            .setLanguage("und".equals(infos[i].getLanguage()) ? null
                                    : infos[i].getLanguage())
                            .build());
                }
                Log.d(TAG, "tracks " + i + " " + infos[i].getTrackType() + " "
                        + infos[i].getLanguage());
            }
            // Add predefine video and audio track.
            mSelectedVideoTrack = Integer.toString(i++);
            tracks.put(mSelectedVideoTrack, new TvTrackInfo.Builder(
                    TvTrackInfo.TYPE_VIDEO, mSelectedVideoTrack)
                    .setVideoWidth(channel.mVideoWidth)
                    .setVideoHeight(channel.mVideoHeight)
                    .build());
            mSelectedAudioTrack = Integer.toString(i++);
            tracks.put(mSelectedAudioTrack, new TvTrackInfo.Builder(TvTrackInfo.TYPE_AUDIO,
                    mSelectedAudioTrack)
                    .setAudioChannelCount(channel.mAudioChannel)
                    .build());

            mTracks = tracks;
            notifyTracksChanged(new ArrayList<TvTrackInfo>(mTracks.values()));
            notifyTrackSelected(TvTrackInfo.TYPE_VIDEO, mSelectedVideoTrack);
            notifyTrackSelected(TvTrackInfo.TYPE_AUDIO, mSelectedAudioTrack);
        }

        private class AddProgramRunnable implements Runnable {
            private static final int PROGRAM_REPEAT_COUNT = 24;
            private final Uri mChannelUri;
            private final ProgramInfo mProgram;

            public AddProgramRunnable(Uri channelUri, ProgramInfo program) {
                mChannelUri = channelUri;
                mProgram = program;
            }

            @Override
            public void run() {
                if (mProgram.mDurationSec == 0) {
                    return;
                }
                long nowSec = System.currentTimeMillis() / 1000;
                long startTimeSec = nowSec
                        - positiveMod((nowSec - mProgram.mStartTimeSec), mProgram.mDurationSec);
                ContentValues values = new ContentValues();
                values.put(Programs.COLUMN_CHANNEL_ID, ContentUris.parseId(mChannelUri));
                values.put(Programs.COLUMN_TITLE, mProgram.mTitle);
                values.put(Programs.COLUMN_SHORT_DESCRIPTION, mProgram.mDescription);
                values.put(Programs.COLUMN_CONTENT_RATING,
                        Utils.contentRatingsToString(mProgram.mContentRatings));
                if (!TextUtils.isEmpty(mProgram.mPosterArtUri)) {
                    values.put(Programs.COLUMN_POSTER_ART_URI, mProgram.mPosterArtUri);
                }

                for (int i = 0; i < PROGRAM_REPEAT_COUNT; ++i) {
                    if (!hasProgramInfo((startTimeSec + i * mProgram.mDurationSec + 1) * 1000)) {
                        values.put(Programs.COLUMN_START_TIME_UTC_MILLIS,
                                (startTimeSec + i * mProgram.mDurationSec) * 1000);
                        values.put(Programs.COLUMN_END_TIME_UTC_MILLIS,
                                (startTimeSec + (i + 1) * mProgram.mDurationSec) * 1000);
                        getContentResolver().insert(TvContract.Programs.CONTENT_URI, values);
                    }
                }
            }

            private long positiveMod(long x, long modulo) {
                return ((x % modulo) + modulo)  % modulo;
            }

            private boolean hasProgramInfo(long timeMs) {
                Uri uri = TvContract.buildProgramsUriForChannel(mChannelUri, timeMs, timeMs);
                String[] projection = { TvContract.Programs._ID };
                Cursor cursor = null;
                try {
                    cursor = getContentResolver().query(uri, projection, null, null, null);
                    if (cursor.getCount() > 0) {
                        return true;
                    }
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
                return false;
            }
        }
    }

    public static final class ChannelInfo {
        public final String mNumber;
        public final String mName;
        public final String mLogoUrl;
        public final int mVideoWidth;
        public final int mVideoHeight;
        public final int mAudioChannel;
        public final boolean mHasClosedCaption;
        public final ProgramInfo mProgram;

        public ChannelInfo(String number, String name, String logoUrl, int videoWidth,
                int videoHeight, int audioChannel, boolean hasClosedCaption, ProgramInfo program) {
            mNumber = number;
            mName = name;
            mLogoUrl = logoUrl;
            mVideoWidth = videoWidth;
            mVideoHeight = videoHeight;
            mAudioChannel = audioChannel;
            mHasClosedCaption = hasClosedCaption;
            mProgram = program;
        }
    }

    public static final class ProgramInfo {
        public final String mTitle;
        public final String mPosterArtUri;
        public final String mDescription;
        public final long mStartTimeSec;
        public final long mDurationSec;
        public final TvContentRating[] mContentRatings;
        public final String mUrl;
        public final int mResourceId;

        public ProgramInfo(String title, String posterArtUri, String description, long startTimeSec,
                long durationSec, TvContentRating[] contentRatings, String url, int resourceId) {
            mTitle = title;
            mPosterArtUri = posterArtUri;
            mDescription = description;
            mStartTimeSec = startTimeSec;
            mDurationSec = durationSec;
            mContentRatings = contentRatings;
            mUrl = url;
            mResourceId = resourceId;
        }
    }
}