1 /*
2  * Copyright (C) 2016 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.source;
18 
19 import android.content.Context;
20 
21 import com.android.tv.tuner.data.nano.Channel;
22 import com.android.tv.tuner.data.TunerChannel;
23 import com.android.tv.tuner.tvinput.EventDetector;
24 
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27 
28 /**
29  * Manages {@link DataSource} for playback and recording.
30  * The class hides handling of {@link TunerHal} and {@link TsStreamer} from other classes.
31  * One TsDataSourceManager should be created for per session.
32  */
33 public class TsDataSourceManager {
34     private static String TAG = "TsDataSourceManager";
35 
36     private static final Object sLock = new Object();
37     private static final Map<TsDataSource, TsStreamer> sTsStreamers =
38             new ConcurrentHashMap<>();
39 
40     private static int sSequenceId;
41 
42     private final int mId;
43     private final boolean mIsRecording;
44     private final TunerTsStreamerManager mTunerStreamerManager =
45             TunerTsStreamerManager.getInstance();
46 
47     private boolean mKeepTuneStatus;
48 
49     /**
50      * Creates TsDataSourceManager to create and release {@link DataSource} which will be
51      * used for playing and recording.
52      * @param isRecording {@code true} when for recording, {@code false} otherwise
53      * @return {@link TsDataSourceManager}
54      */
createSourceManager(boolean isRecording)55     public static TsDataSourceManager createSourceManager(boolean isRecording) {
56         int id;
57         synchronized (sLock) {
58             id = ++sSequenceId;
59         }
60         return new TsDataSourceManager(id, isRecording);
61     }
62 
TsDataSourceManager(int id, boolean isRecording)63     private TsDataSourceManager(int id, boolean isRecording) {
64         mId = id;
65         mIsRecording = isRecording;
66         mKeepTuneStatus = true;
67     }
68 
69     /**
70      * Creates or retrieves {@link TsDataSource} for playing or recording
71      * @param context a {@link Context} instance
72      * @param channel to play or record
73      * @param eventListener for program information which will be scanned from MPEG2-TS stream
74      * @return {@link TsDataSource} which will provide the specified channel stream
75      */
createDataSource(Context context, TunerChannel channel, EventDetector.EventListener eventListener)76     public TsDataSource createDataSource(Context context, TunerChannel channel,
77             EventDetector.EventListener eventListener) {
78         if (channel.getType() == Channel.TYPE_FILE) {
79             // MPEG2 TS captured stream file recording is not supported.
80             if (mIsRecording) {
81                 return null;
82             }
83             FileTsStreamer streamer = new FileTsStreamer(eventListener);
84             if (streamer.startStream(channel)) {
85                 TsDataSource source = streamer.createDataSource();
86                 sTsStreamers.put(source, streamer);
87                 return source;
88             }
89             return null;
90         }
91         return mTunerStreamerManager.createDataSource(context, channel, eventListener,
92                 mId, !mIsRecording && mKeepTuneStatus);
93     }
94 
95     /**
96      * Releases the specified {@link TsDataSource} and underlying {@link TunerHal}.
97      * @param source to release
98      */
releaseDataSource(TsDataSource source)99     public void releaseDataSource(TsDataSource source) {
100         if (source instanceof TunerTsStreamer.TunerDataSource) {
101             mTunerStreamerManager.releaseDataSource(
102                     source, mId, !mIsRecording && mKeepTuneStatus);
103         } else if (source instanceof FileTsStreamer.FileDataSource) {
104             FileTsStreamer streamer = (FileTsStreamer) sTsStreamers.get(source);
105             if (streamer != null) {
106                 sTsStreamers.remove(source);
107                 streamer.stopStream();
108             }
109         }
110     }
111 
112     /**
113      * Indicates that the current session has pending tunes.
114      */
setHasPendingTune()115     public void setHasPendingTune() {
116         mTunerStreamerManager.setHasPendingTune(mId);
117     }
118 
119     /**
120      * Indicates whether the underlying {@link TunerHal} should be kept or not when data source
121      * is being released.
122      * TODO: If b/30750953 is fixed, we can remove this function.
123      * @param keepTuneStatus underlying {@link TunerHal} will be reused when data source releasing.
124      */
setKeepTuneStatus(boolean keepTuneStatus)125     public void setKeepTuneStatus(boolean keepTuneStatus) {
126         mKeepTuneStatus = keepTuneStatus;
127     }
128 
129     /**
130      * Releases persistent resources.
131      */
release()132     public void release() {
133         mTunerStreamerManager.release(mId);
134     }
135 }
136