1 /*
2  * Copyright 2019 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 _ANDROID_MEDIA_TV_TUNER_H_
18 #define _ANDROID_MEDIA_TV_TUNER_H_
19 
20 #include <C2BlockInternal.h>
21 #include <C2HandleIonInternal.h>
22 #include <C2ParamDef.h>
23 #include <aidl/android/hardware/tv/tuner/DemuxFilterEvent.h>
24 #include <aidl/android/hardware/tv/tuner/DemuxFilterMonitorEvent.h>
25 #include <aidl/android/hardware/tv/tuner/DemuxFilterStatus.h>
26 #include <aidl/android/hardware/tv/tuner/DemuxFilterType.h>
27 #include <aidl/android/hardware/tv/tuner/DemuxPid.h>
28 #include <aidl/android/hardware/tv/tuner/DvrType.h>
29 #include <aidl/android/hardware/tv/tuner/FrontendCapabilities.h>
30 #include <aidl/android/hardware/tv/tuner/FrontendEventType.h>
31 #include <aidl/android/hardware/tv/tuner/FrontendInfo.h>
32 #include <aidl/android/hardware/tv/tuner/FrontendScanMessage.h>
33 #include <aidl/android/hardware/tv/tuner/FrontendScanMessageType.h>
34 #include <aidl/android/hardware/tv/tuner/FrontendScanType.h>
35 #include <aidl/android/hardware/tv/tuner/FrontendSettings.h>
36 #include <aidl/android/hardware/tv/tuner/LnbEventType.h>
37 #include <aidl/android/hardware/tv/tuner/PlaybackStatus.h>
38 #include <aidl/android/hardware/tv/tuner/RecordStatus.h>
39 #include <aidl/android/hardware/tv/tuner/Result.h>
40 #include <fmq/AidlMessageQueue.h>
41 #include <utils/Mutex.h>
42 #include <utils/RefBase.h>
43 
44 #include <fstream>
45 #include <string>
46 #include <unordered_map>
47 
48 #include "jni.h"
49 #include "tuner/DemuxClient.h"
50 #include "tuner/DescramblerClient.h"
51 #include "tuner/FilterClient.h"
52 #include "tuner/FilterClientCallback.h"
53 #include "tuner/FrontendClient.h"
54 #include "tuner/FrontendClientCallback.h"
55 #include "tuner/LnbClient.h"
56 #include "tuner/LnbClientCallback.h"
57 #include "tuner/TimeFilterClient.h"
58 #include "tuner/TunerClient.h"
59 
60 using ::aidl::android::hardware::common::fmq::MQDescriptor;
61 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
62 using ::aidl::android::hardware::tv::tuner::DemuxFilterEvent;
63 using ::aidl::android::hardware::tv::tuner::DemuxFilterMonitorEvent;
64 using ::aidl::android::hardware::tv::tuner::DemuxFilterStatus;
65 using ::aidl::android::hardware::tv::tuner::DemuxFilterType;
66 using ::aidl::android::hardware::tv::tuner::DemuxPid;
67 using ::aidl::android::hardware::tv::tuner::DvrType;
68 using ::aidl::android::hardware::tv::tuner::FrontendCapabilities;
69 using ::aidl::android::hardware::tv::tuner::FrontendEventType;
70 using ::aidl::android::hardware::tv::tuner::FrontendInfo;
71 using ::aidl::android::hardware::tv::tuner::FrontendScanMessage;
72 using ::aidl::android::hardware::tv::tuner::FrontendScanMessageType;
73 using ::aidl::android::hardware::tv::tuner::FrontendScanType;
74 using ::aidl::android::hardware::tv::tuner::FrontendSettings;
75 using ::aidl::android::hardware::tv::tuner::LnbEventType;
76 using ::aidl::android::hardware::tv::tuner::PlaybackStatus;
77 using ::aidl::android::hardware::tv::tuner::RecordStatus;
78 using ::aidl::android::hardware::tv::tuner::Result;
79 using ::android::hardware::EventFlag;
80 
81 using MQ = MQDescriptor<int8_t, SynchronizedReadWrite>;
82 
83 namespace android {
84 
85 struct LnbClientCallbackImpl : public LnbClientCallback {
86     ~LnbClientCallbackImpl();
87     virtual void onEvent(LnbEventType lnbEventType);
88     virtual void onDiseqcMessage(const vector<uint8_t>& diseqcMessage);
89 
90     void setLnb(jweak lnbObj);
91 private:
92     jweak mLnbObj;
93 };
94 
95 struct DvrClientCallbackImpl : public DvrClientCallback {
96     ~DvrClientCallbackImpl();
97     virtual void onRecordStatus(RecordStatus status);
98     virtual void onPlaybackStatus(PlaybackStatus status);
99 
100     void setDvr(jweak dvrObj);
101 private:
102     jweak mDvrObj;
103 };
104 
105 struct MediaEvent : public RefBase {
106     MediaEvent(sp<FilterClient> filterClient, native_handle_t* avHandle, int64_t dataId,
107                int64_t dataSize, jobject obj);
108     ~MediaEvent();
109     jobject getLinearBlock();
110     int64_t getAudioHandle();
111     void finalize();
112 
113     sp<FilterClient> mFilterClient;
114     native_handle_t* mAvHandle;
115     int64_t mDataId;
116     int64_t mDataSize;
117     uint8_t* mBuffer;
118     android::Mutex mLock;
119     int mDataIdRefCnt;
120     int mAvHandleRefCnt;
121     jweak mMediaEventObj;
122     jweak mLinearBlockObj;
123     C2HandleIon* mIonHandle;
124     std::weak_ptr<C2Buffer> mC2Buffer;
125 };
126 
127 struct FilterClientCallbackImpl : public FilterClientCallback {
128     FilterClientCallbackImpl();
129     ~FilterClientCallbackImpl();
130     virtual void onFilterEvent(const vector<DemuxFilterEvent>& events);
131     virtual void onFilterStatus(const DemuxFilterStatus status);
132 
133     void setFilter(jweak filterObj, sp<FilterClient> filterClient);
134     void setSharedFilter(jweak filterObj, sp<FilterClient> filterClient);
135 
136 private:
137     jweak mFilterObj;
138     sp<FilterClient> mFilterClient;
139     android::Mutex mLock;
140     jclass mEventClass;
141     jclass mSectionEventClass;
142     jclass mMediaEventClass;
143     jclass mAudioDescriptorClass;
144     jclass mPesEventClass;
145     jclass mTsRecordEventClass;
146     jclass mMmtpRecordEventClass;
147     jclass mDownloadEventClass;
148     jclass mIpPayloadEventClass;
149     jclass mTemiEventClass;
150     jclass mScramblingStatusEventClass;
151     jclass mIpCidChangeEventClass;
152     jclass mRestartEventClass;
153     jmethodID mSectionEventInitID;
154     jmethodID mMediaEventInitID;
155     jmethodID mAudioDescriptorInitID;
156     jmethodID mPesEventInitID;
157     jmethodID mTsRecordEventInitID;
158     jmethodID mMmtpRecordEventInitID;
159     jmethodID mDownloadEventInitID;
160     jmethodID mIpPayloadEventInitID;
161     jmethodID mTemiEventInitID;
162     jmethodID mScramblingStatusEventInitID;
163     jmethodID mIpCidChangeEventInitID;
164     jmethodID mRestartEventInitID;
165     jfieldID mMediaEventFieldContextID;
166     bool mSharedFilter;
167     void getSectionEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event);
168     void getMediaEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event);
169     void getPesEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event);
170     void getTsRecordEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event);
171     void getMmtpRecordEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event);
172     void getDownloadEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event);
173     void getIpPayloadEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event);
174     void getTemiEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event);
175     void getScramblingStatusEvent(const jobjectArray& arr, const int size,
176                                   const DemuxFilterEvent& event);
177     void getIpCidChangeEvent(const jobjectArray& arr, const int size,
178                              const DemuxFilterEvent& event);
179     void getRestartEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event);
180 };
181 
182 struct JTuner;
183 struct FrontendClientCallbackImpl : public FrontendClientCallback {
184     FrontendClientCallbackImpl(JTuner*, jweak);
185     ~FrontendClientCallbackImpl();
186     virtual void onEvent(FrontendEventType frontendEventType);
187     virtual void onScanMessage(
188             FrontendScanMessageType type, const FrontendScanMessage& message);
189 
190     void executeOnScanMessage(JNIEnv *env, const jclass& clazz, const jobject& frontend,
191                               FrontendScanMessageType type,
192                               const FrontendScanMessage& message);
193     void addCallbackListener(JTuner*, jweak obj);
194     void removeCallbackListener(JTuner* jtuner);
195     std::unordered_map<JTuner*, jweak> mListenersMap;
196     std::mutex mMutex;
197 };
198 
199 struct JTuner : public RefBase {
200     JTuner(JNIEnv *env, jobject thiz);
201     int getTunerVersion();
202     jobject getAvSyncHwId(sp<FilterClient> filter);
203     jobject getAvSyncTime(jint id);
204     int connectCiCam(jint id);
205     int linkCiCam(jint id);
206     int disconnectCiCam();
207     int unlinkCiCam(jint id);
208     jobject getFrontendIds();
209     jobject openFrontendByHandle(int feHandle);
210     int shareFrontend(int feId);
211     int unshareFrontend();
212     void registerFeCbListener(JTuner* jtuner);
213     void unregisterFeCbListener(JTuner* jtuner);
214     void updateFrontend(JTuner* jtuner);
215     jint closeFrontendById(int id);
216     jobject getFrontendInfo(int id);
217     int tune(const FrontendSettings& settings);
218     int stopTune();
219     int scan(const FrontendSettings& settings, FrontendScanType scanType);
220     int stopScan();
221     int setLnb(sp<LnbClient> lnbClient);
222     bool isLnaSupported();
223     int setLna(bool enable);
224     jobject openLnbByHandle(int handle);
225     jobject openLnbByName(jstring name);
226     jobject openFilter(DemuxFilterType type, int bufferSize);
227     jobject openTimeFilter();
228     jobject openDescrambler();
229     jobject openDvr(DvrType type, jlong bufferSize);
230     jobject getDemuxCaps();
231     jobject getDemuxInfo(int handle);
232     jobject getFrontendStatus(jintArray types);
233     Result openDemux(int handle);
234     jint close();
235     jint closeFrontend();
236     jint closeDemux();
237     Result getFrontendHardwareInfo(string& info);
238     jint setMaxNumberOfFrontends(int32_t frontendType, int32_t maxNumber);
239     int32_t getMaxNumberOfFrontends(int32_t frontendType);
240     jint removeOutputPid(int32_t pid);
241     jobjectArray getFrontendStatusReadiness(jintArray types);
242 
243     jweak getObject();
244 
245 protected:
246     virtual ~JTuner();
247 
248 private:
249     jclass mClass;
250     jweak mObject;
251     static sp<TunerClient> sTunerClient;
252     static std::mutex sTunerClientMutex;
253     sp<FrontendClient> mFeClient;
254     sp<FrontendClientCallbackImpl> mFeClientCb;
255     int mFeId;
256     int mSharedFeId;
257     sp<LnbClient> mLnbClient;
258     sp<DemuxClient> mDemuxClient;
259     static jobject getAnalogFrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
260     static jobject getAtsc3FrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
261     static jobject getAtscFrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
262     static jobject getDvbcFrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
263     static jobject getDvbsFrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
264     static jobject getDvbtFrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
265     static jobject getIsdbs3FrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
266     static jobject getIsdbsFrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
267     static jobject getIsdbtFrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
268     static jobject getDtmbFrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
269     static jobject getIptvFrontendCaps(JNIEnv* env, FrontendCapabilities& caps);
270 };
271 
272 class C2DataIdInfo : public C2Param {
273 public:
274     C2DataIdInfo(uint32_t index, uint64_t value);
275 private:
276     typedef C2GlobalParam<C2Info, C2Int64Value, 0> StubInfo;
277     StubInfo mInfo;
278     static const size_t kParamSize = sizeof(StubInfo);
279 };
280 
281 }  // namespace android
282 
283 #endif  // _ANDROID_MEDIA_TV_TUNER_H_
284