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 #ifndef DVB_MANAGER_H_
18 #define DVB_MANAGER_H_
19 
20 #include <jni.h>
21 #include <map>
22 
23 #include "mutex.h"
24 #include "tunertvinput_jni.h"
25 
26 class DvbManager {
27     static const int NUM_POLLFDS = 1;
28     static const int FE_LOCK_CHECK_INTERNAL_US = 100 * 1000;
29     static const int FE_CONSECUTIVE_LOCK_SUCCESS_COUNT = 1;
30     static const int DVB_ERROR_RETRY_INTERVAL_MS = 100 * 1000;
31     static const int DVB_TUNE_STOP_DELAY_MS = 100 * 1000;
32     static const int FE_POLL_TIMEOUT_MS = 100;
33     static const int PAT_PID = 0;
34     static const int DVB_API_VERSION_UNDEFINED = -1;
35     static const int DVB_API_VERSION3 = 3;
36     static const int DVB_API_VERSION5 = 5;
37 
38     static const int FILTER_TYPE_OTHER =
39         com_android_tv_tuner_TunerHal_FILTER_TYPE_OTHER;
40     static const int FILTER_TYPE_AUDIO =
41         com_android_tv_tuner_TunerHal_FILTER_TYPE_AUDIO;
42     static const int FILTER_TYPE_VIDEO =
43         com_android_tv_tuner_TunerHal_FILTER_TYPE_VIDEO;
44     static const int FILTER_TYPE_PCR =
45         com_android_tv_tuner_TunerHal_FILTER_TYPE_PCR;
46 
47     static const int DELIVERY_SYSTEM_UNDEFINED =
48         com_android_tv_tuner_TunerHal_DELIVERY_SYSTEM_UNDEFINED;
49     static const int DELIVERY_SYSTEM_ATSC =
50         com_android_tv_tuner_TunerHal_DELIVERY_SYSTEM_ATSC;
51     static const int DELIVERY_SYSTEM_DVBC =
52         com_android_tv_tuner_TunerHal_DELIVERY_SYSTEM_DVBC;
53     static const int DELIVERY_SYSTEM_DVBS =
54         com_android_tv_tuner_TunerHal_DELIVERY_SYSTEM_DVBS;
55     static const int DELIVERY_SYSTEM_DVBS2 =
56         com_android_tv_tuner_TunerHal_DELIVERY_SYSTEM_DVBS2;
57     static const int DELIVERY_SYSTEM_DVBT =
58         com_android_tv_tuner_TunerHal_DELIVERY_SYSTEM_DVBT;
59     static const int DELIVERY_SYSTEM_DVBT2 =
60         com_android_tv_tuner_TunerHal_DELIVERY_SYSTEM_DVBT2;
61 
62     int mFeFd;
63     int mDvrFd;
64     int mPatFilterFd;
65     int mDvbApiVersion;
66     int mDeliverySystemTypes[8];
67     bool mFeHasLock;
68     // Flag for pending tune request. Used for canceling the current tune operation.
69     bool volatile mHasPendingTune;
70     std::map<int, int> mPidFilters;
71     Mutex mFilterLock;
72     jmethodID mOpenDvbFrontEndMethodID;
73     jmethodID mOpenDvbDemuxMethodID;
74     jmethodID mOpenDvbDvrMethodID;
75 
76 public:
77     DvbManager(JNIEnv *env, jobject thiz);
78     ~DvbManager();
79     int tune(JNIEnv *env, jobject thiz,
80             const int frequency, const char *modulationStr, int timeout_ms);
81     int tune(JNIEnv *env, jobject thiz,
82             const int deliverySystemType, const int frequency,
83             const char *modulationStr, int timeout_ms);
84     int stopTune();
85     int readTsStream(JNIEnv *env, jobject thiz,
86             uint8_t *tsBuffer, int tsBufferSize, int timeout_ms);
87     int startTsPidFilter(JNIEnv *env, jobject thiz, int pid, int filterType);
88     void closeAllDvbPidFilter();
89     void setHasPendingTune(bool hasPendingTune);
90     int getDeliverySystemType(JNIEnv *env, jobject thiz);
91     int *getDeliverySystemTypes(JNIEnv *env, jobject thiz);
92     int getSignalStrength();
93 
94 private:
95     int tuneInternal(JNIEnv *env, jobject thiz,
96             const int deliverySystemType, const int frequency,
97             const char *modulationStr, int timeout_ms);
98     int openDvbFe(JNIEnv *env, jobject thiz);
99     int openDvbDvr(JNIEnv *env, jobject thiz);
100     void closePatFilter();
101     void closeDvbFe();
102     void closeDvbDvr();
103     void reset();
104     void resetExceptFe();
105     bool isFeLocked();
106     // Call to java method
107     int openDvbFeFromSystemApi(JNIEnv *env, jobject thiz);
108     int openDvbDemuxFromSystemApi(JNIEnv *env, jobject thiz);
109     int openDvbDvrFromSystemApi(JNIEnv *env, jobject thiz);
110 };
111 
112 #endif  // DVB_MANAGER_H_
113