1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tv.tuner.exoplayer;
18 
19 import android.content.Context;
20 
21 import com.google.android.exoplayer.SampleSource;
22 import com.google.android.exoplayer.TrackRenderer;
23 import com.google.android.exoplayer.upstream.DataSource;
24 import com.android.tv.tuner.exoplayer.MpegTsPlayer.RendererBuilder;
25 import com.android.tv.tuner.exoplayer.MpegTsPlayer.RendererBuilderCallback;
26 import com.android.tv.tuner.exoplayer.ac3.Ac3PassthroughTrackRenderer;
27 import com.android.tv.tuner.exoplayer.buffer.BufferManager;
28 import com.android.tv.tuner.tvinput.PlaybackBufferListener;
29 
30 /**
31  * Builder for renderer objects for {@link MpegTsPlayer}.
32  */
33 public class MpegTsRendererBuilder implements RendererBuilder {
34     private final Context mContext;
35     private final BufferManager mBufferManager;
36     private final PlaybackBufferListener mBufferListener;
37 
MpegTsRendererBuilder(Context context, BufferManager bufferManager, PlaybackBufferListener bufferListener)38     public MpegTsRendererBuilder(Context context, BufferManager bufferManager,
39             PlaybackBufferListener bufferListener) {
40         mContext = context;
41         mBufferManager = bufferManager;
42         mBufferListener = bufferListener;
43     }
44 
45     @Override
buildRenderers(MpegTsPlayer mpegTsPlayer, DataSource dataSource, RendererBuilderCallback callback)46     public void buildRenderers(MpegTsPlayer mpegTsPlayer, DataSource dataSource,
47             RendererBuilderCallback callback) {
48         // Build the video and audio renderers.
49         SampleExtractor extractor = dataSource == null ?
50                 new MpegTsSampleExtractor(mBufferManager, mBufferListener) :
51                 new MpegTsSampleExtractor(dataSource, mBufferManager, mBufferListener);
52         SampleSource sampleSource = new MpegTsSampleSource(extractor);
53         MpegTsVideoTrackRenderer videoRenderer = new MpegTsVideoTrackRenderer(mContext,
54                 sampleSource, mpegTsPlayer.getMainHandler(), mpegTsPlayer);
55         // TODO: Only using Ac3PassthroughTrackRenderer for A/V sync issue. We will use
56         // {@link Ac3TrackRenderer} when we use ExoPlayer's extractor.
57         TrackRenderer audioRenderer = new Ac3PassthroughTrackRenderer(sampleSource,
58                 mpegTsPlayer.getMainHandler(), mpegTsPlayer);
59         Cea708TextTrackRenderer textRenderer = new Cea708TextTrackRenderer(sampleSource);
60 
61         TrackRenderer[] renderers = new TrackRenderer[MpegTsPlayer.RENDERER_COUNT];
62         renderers[MpegTsPlayer.TRACK_TYPE_VIDEO] = videoRenderer;
63         renderers[MpegTsPlayer.TRACK_TYPE_AUDIO] = audioRenderer;
64         renderers[MpegTsPlayer.TRACK_TYPE_TEXT] = textRenderer;
65         callback.onRenderers(null, renderers);
66     }
67 }
68