• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <android/hardware/tv/tuner/1.1/types.h>
21 
22 #include <C2BlockInternal.h>
23 #include <C2HandleIonInternal.h>
24 #include <C2ParamDef.h>
25 #include <fmq/MessageQueue.h>
26 #include <fstream>
27 #include <string>
28 #include <unordered_map>
29 #include <utils/Mutex.h>
30 #include <utils/RefBase.h>
31 
32 #include "tuner/DemuxClient.h"
33 #include "tuner/DescramblerClient.h"
34 #include "tuner/FilterClient.h"
35 #include "tuner/FilterClientCallback.h"
36 #include "tuner/FrontendClient.h"
37 #include "tuner/FrontendClientCallback.h"
38 #include "tuner/LnbClient.h"
39 #include "tuner/LnbClientCallback.h"
40 #include "tuner/TimeFilterClient.h"
41 #include "tuner/TunerClient.h"
42 #include "jni.h"
43 
44 using ::android::hardware::EventFlag;
45 using ::android::hardware::MQDescriptorSync;
46 using ::android::hardware::MessageQueue;
47 using ::android::hardware::Return;
48 using ::android::hardware::hidl_handle;
49 using ::android::hardware::hidl_vec;
50 using ::android::hardware::kSynchronizedReadWrite;
51 using ::android::hardware::tv::tuner::V1_0::DemuxFilterEvent;
52 using ::android::hardware::tv::tuner::V1_0::DemuxFilterStatus;
53 using ::android::hardware::tv::tuner::V1_0::DemuxFilterType;
54 using ::android::hardware::tv::tuner::V1_0::DemuxPid;
55 using ::android::hardware::tv::tuner::V1_0::DvrType;
56 using ::android::hardware::tv::tuner::V1_0::FrontendEventType;
57 using ::android::hardware::tv::tuner::V1_0::FrontendId;
58 using ::android::hardware::tv::tuner::V1_0::FrontendInfo;
59 using ::android::hardware::tv::tuner::V1_0::FrontendScanMessage;
60 using ::android::hardware::tv::tuner::V1_0::FrontendScanMessageType;
61 using ::android::hardware::tv::tuner::V1_0::FrontendScanType;
62 using ::android::hardware::tv::tuner::V1_0::FrontendSettings;
63 using ::android::hardware::tv::tuner::V1_1::FrontendSettingsExt1_1;
64 using ::android::hardware::tv::tuner::V1_0::LnbEventType;
65 using ::android::hardware::tv::tuner::V1_0::LnbId;
66 using ::android::hardware::tv::tuner::V1_0::PlaybackStatus;
67 using ::android::hardware::tv::tuner::V1_0::RecordStatus;
68 using ::android::hardware::tv::tuner::V1_0::Result;
69 using ::android::hardware::tv::tuner::V1_1::DemuxFilterEventExt;
70 using ::android::hardware::tv::tuner::V1_1::DemuxFilterMonitorEvent;
71 using ::android::hardware::tv::tuner::V1_1::FrontendScanMessageExt1_1;
72 using ::android::hardware::tv::tuner::V1_1::FrontendScanMessageTypeExt1_1;
73 
74 using MQ = MessageQueue<uint8_t, kSynchronizedReadWrite>;
75 
76 const static int TUNER_VERSION_1_1 = ((1 << 16) | 1);
77 
78 namespace android {
79 
80 struct LnbClientCallbackImpl : public LnbClientCallback {
81     ~LnbClientCallbackImpl();
82     virtual void onEvent(LnbEventType lnbEventType);
83     virtual void onDiseqcMessage(const hidl_vec<uint8_t>& diseqcMessage);
84 
85     void setLnb(jweak lnbObj);
86 private:
87     jweak mLnbObj;
88 };
89 
90 struct DvrClientCallbackImpl : public DvrClientCallback {
91     ~DvrClientCallbackImpl();
92     virtual void onRecordStatus(RecordStatus status);
93     virtual void onPlaybackStatus(PlaybackStatus status);
94 
95     void setDvr(jweak dvrObj);
96 private:
97     jweak mDvrObj;
98 };
99 
100 struct MediaEvent : public RefBase {
101     MediaEvent(sp<FilterClient> filterClient, hidl_handle avHandle, uint64_t dataId,
102         uint64_t dataSize, jobject obj);
103     ~MediaEvent();
104     jobject getLinearBlock();
105     uint64_t getAudioHandle();
106     void finalize();
107 
108     sp<FilterClient> mFilterClient;
109     native_handle_t* mAvHandle;
110     uint64_t mDataId;
111     uint64_t mDataSize;
112     uint8_t* mBuffer;
113     android::Mutex mLock;
114     int mDataIdRefCnt;
115     int mAvHandleRefCnt;
116     jweak mMediaEventObj;
117     jweak mLinearBlockObj;
118     C2HandleIon* mIonHandle;
119     std::weak_ptr<C2Buffer> mC2Buffer;
120 };
121 
122 struct FilterClientCallbackImpl : public FilterClientCallback {
123     ~FilterClientCallbackImpl();
124     virtual void onFilterEvent_1_1(const DemuxFilterEvent& filterEvent,
125             const DemuxFilterEventExt& filterEventExt);
126     virtual void onFilterEvent(const DemuxFilterEvent& filterEvent);
127     virtual void onFilterStatus(const DemuxFilterStatus status);
128 
129     void setFilter(jweak filterObj, sp<FilterClient> filterClient);
130 private:
131     jweak mFilterObj;
132     sp<FilterClient> mFilterClient;
133     jobjectArray getSectionEvent(
134             jobjectArray& arr, const std::vector<DemuxFilterEvent::Event>& events);
135     jobjectArray getMediaEvent(
136             jobjectArray& arr, const std::vector<DemuxFilterEvent::Event>& events);
137     jobjectArray getPesEvent(
138             jobjectArray& arr, const std::vector<DemuxFilterEvent::Event>& events);
139     jobjectArray getTsRecordEvent(
140             jobjectArray& arr, const std::vector<DemuxFilterEvent::Event>&events,
141                     const std::vector<DemuxFilterEventExt::Event>& eventsExt);
142     jobjectArray getMmtpRecordEvent(
143             jobjectArray& arr, const std::vector<DemuxFilterEvent::Event>&events,
144                     const std::vector<DemuxFilterEventExt::Event>& eventsExt);
145     jobjectArray getDownloadEvent(
146             jobjectArray& arr, const std::vector<DemuxFilterEvent::Event>& events);
147     jobjectArray getIpPayloadEvent(
148             jobjectArray& arr, const std::vector<DemuxFilterEvent::Event>& events);
149     jobjectArray getTemiEvent(
150             jobjectArray& arr, const std::vector<DemuxFilterEvent::Event>& events);
151     jobjectArray getScramblingStatusEvent(
152             jobjectArray& arr, const std::vector<DemuxFilterEventExt::Event>& eventsExt);
153     jobjectArray getIpCidChangeEvent(
154             jobjectArray& arr, const std::vector<DemuxFilterEventExt::Event>& eventsExt);
155     jobjectArray getRestartEvent(
156             jobjectArray& arr, const std::vector<DemuxFilterEventExt::Event>& eventsExt);
157 };
158 
159 struct FrontendClientCallbackImpl : public FrontendClientCallback {
160     FrontendClientCallbackImpl(jweak tunerObj);
161     ~FrontendClientCallbackImpl();
162     virtual void onEvent(FrontendEventType frontendEventType);
163     virtual void onScanMessage(
164             FrontendScanMessageType type, const FrontendScanMessage& message);
165     virtual void onScanMessageExt1_1(
166             FrontendScanMessageTypeExt1_1 type, const FrontendScanMessageExt1_1& messageExt);
167 
168     jweak mObject;
169 };
170 
171 struct JTuner : public RefBase {
172     JTuner(JNIEnv *env, jobject thiz);
173     int getTunerVersion();
174     jobject getAvSyncHwId(sp<FilterClient> filter);
175     jobject getAvSyncTime(jint id);
176     int connectCiCam(jint id);
177     int linkCiCam(jint id);
178     int disconnectCiCam();
179     int unlinkCiCam(jint id);
180     jobject getFrontendIds();
181     jobject openFrontendByHandle(int feHandle);
182     jint closeFrontendById(int id);
183     jobject getFrontendInfo(int id);
184     int tune(const FrontendSettings& settings, const FrontendSettingsExt1_1& settingsExt1_1);
185     int stopTune();
186     int scan(const FrontendSettings& settings, FrontendScanType scanType,
187             const FrontendSettingsExt1_1& settingsExt1_1);
188     int stopScan();
189     int setLnb(sp<LnbClient> lnbClient);
190     int setLna(bool enable);
191     jobject openLnbByHandle(int handle);
192     jobject openLnbByName(jstring name);
193     jobject openFilter(DemuxFilterType type, int bufferSize);
194     jobject openTimeFilter();
195     jobject openDescrambler();
196     jobject openDvr(DvrType type, jlong bufferSize);
197     jobject getDemuxCaps();
198     jobject getFrontendStatus(jintArray types);
199     Result openDemux(int handle);
200     jint close();
201     jint closeFrontend();
202     jint closeDemux();
203 
204 protected:
205     virtual ~JTuner();
206 
207 private:
208     jclass mClass;
209     jweak mObject;
210     static sp<TunerClient> mTunerClient;
211     sp<FrontendClient> mFeClient;
212     int mFeId;
213     sp<LnbClient> mLnbClient;
214     sp<DemuxClient> mDemuxClient;
215     static jobject getAnalogFrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps);
216     static jobject getAtsc3FrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps);
217     static jobject getAtscFrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps);
218     static jobject getDvbcFrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps);
219     static jobject getDvbsFrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps);
220     static jobject getDvbtFrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps);
221     static jobject getIsdbs3FrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps);
222     static jobject getIsdbsFrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps);
223     static jobject getIsdbtFrontendCaps(JNIEnv *env, FrontendInfo::FrontendCapabilities& caps);
224     static jobject getDtmbFrontendCaps(JNIEnv *env, int id);
225 
226     bool isV1_1ExtendedStatusType(jint type);
227 };
228 
229 class C2DataIdInfo : public C2Param {
230 public:
231     C2DataIdInfo(uint32_t index, uint64_t value);
232 private:
233     typedef C2GlobalParam<C2Info, C2Int64Value, 0> StubInfo;
234     StubInfo mInfo;
235     static const size_t kParamSize = sizeof(StubInfo);
236 };
237 
238 }  // namespace android
239 
240 #endif  // _ANDROID_MEDIA_TV_TUNER_H_
241