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 jclass mEventClass; 140 jclass mSectionEventClass; 141 jclass mMediaEventClass; 142 jclass mAudioDescriptorClass; 143 jclass mPesEventClass; 144 jclass mTsRecordEventClass; 145 jclass mMmtpRecordEventClass; 146 jclass mDownloadEventClass; 147 jclass mIpPayloadEventClass; 148 jclass mTemiEventClass; 149 jclass mScramblingStatusEventClass; 150 jclass mIpCidChangeEventClass; 151 jclass mRestartEventClass; 152 jmethodID mSectionEventInitID; 153 jmethodID mMediaEventInitID; 154 jmethodID mAudioDescriptorInitID; 155 jmethodID mPesEventInitID; 156 jmethodID mTsRecordEventInitID; 157 jmethodID mMmtpRecordEventInitID; 158 jmethodID mDownloadEventInitID; 159 jmethodID mIpPayloadEventInitID; 160 jmethodID mTemiEventInitID; 161 jmethodID mScramblingStatusEventInitID; 162 jmethodID mIpCidChangeEventInitID; 163 jmethodID mRestartEventInitID; 164 jfieldID mMediaEventFieldContextID; 165 bool mSharedFilter; 166 void getSectionEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event); 167 void getMediaEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event); 168 void getPesEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event); 169 void getTsRecordEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event); 170 void getMmtpRecordEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event); 171 void getDownloadEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event); 172 void getIpPayloadEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event); 173 void getTemiEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event); 174 void getScramblingStatusEvent(const jobjectArray& arr, const int size, 175 const DemuxFilterEvent& event); 176 void getIpCidChangeEvent(const jobjectArray& arr, const int size, 177 const DemuxFilterEvent& event); 178 void getRestartEvent(const jobjectArray& arr, const int size, const DemuxFilterEvent& event); 179 }; 180 181 struct JTuner; 182 struct FrontendClientCallbackImpl : public FrontendClientCallback { 183 FrontendClientCallbackImpl(JTuner*, jweak); 184 ~FrontendClientCallbackImpl(); 185 virtual void onEvent(FrontendEventType frontendEventType); 186 virtual void onScanMessage( 187 FrontendScanMessageType type, const FrontendScanMessage& message); 188 189 void executeOnScanMessage(JNIEnv *env, const jclass& clazz, const jobject& frontend, 190 FrontendScanMessageType type, 191 const FrontendScanMessage& message); 192 void addCallbackListener(JTuner*, jweak obj); 193 void removeCallbackListener(JTuner* jtuner); 194 std::unordered_map<JTuner*, jweak> mListenersMap; 195 std::mutex mMutex; 196 }; 197 198 struct JTuner : public RefBase { 199 JTuner(JNIEnv *env, jobject thiz); 200 int getTunerVersion(); 201 jobject getAvSyncHwId(sp<FilterClient> filter); 202 jobject getAvSyncTime(jint id); 203 int connectCiCam(jint id); 204 int linkCiCam(jint id); 205 int disconnectCiCam(); 206 int unlinkCiCam(jint id); 207 jobject getFrontendIds(); 208 jobject openFrontendByHandle(int feHandle); 209 int shareFrontend(int feId); 210 int unshareFrontend(); 211 void registerFeCbListener(JTuner* jtuner); 212 void unregisterFeCbListener(JTuner* jtuner); 213 void updateFrontend(JTuner* jtuner); 214 jint closeFrontendById(int id); 215 jobject getFrontendInfo(int id); 216 int tune(const FrontendSettings& settings); 217 int stopTune(); 218 int scan(const FrontendSettings& settings, FrontendScanType scanType); 219 int stopScan(); 220 int setLnb(sp<LnbClient> lnbClient); 221 bool isLnaSupported(); 222 int setLna(bool enable); 223 jobject openLnbByHandle(int handle); 224 jobject openLnbByName(jstring name); 225 jobject openFilter(DemuxFilterType type, int bufferSize); 226 jobject openTimeFilter(); 227 jobject openDescrambler(); 228 jobject openDvr(DvrType type, jlong bufferSize); 229 jobject getDemuxCaps(); 230 jobject getDemuxInfo(int handle); 231 jobject getFrontendStatus(jintArray types); 232 Result openDemux(int handle); 233 jint close(); 234 jint closeFrontend(); 235 jint closeDemux(); 236 Result getFrontendHardwareInfo(string& info); 237 jint setMaxNumberOfFrontends(int32_t frontendType, int32_t maxNumber); 238 int32_t getMaxNumberOfFrontends(int32_t frontendType); 239 jint removeOutputPid(int32_t pid); 240 jobjectArray getFrontendStatusReadiness(jintArray types); 241 242 jweak getObject(); 243 244 protected: 245 virtual ~JTuner(); 246 247 private: 248 jclass mClass; 249 jweak mObject; 250 static sp<TunerClient> sTunerClient; 251 static std::mutex sTunerClientMutex; 252 sp<FrontendClient> mFeClient; 253 sp<FrontendClientCallbackImpl> mFeClientCb; 254 int mFeId; 255 int mSharedFeId; 256 sp<LnbClient> mLnbClient; 257 sp<DemuxClient> mDemuxClient; 258 static jobject getAnalogFrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 259 static jobject getAtsc3FrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 260 static jobject getAtscFrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 261 static jobject getDvbcFrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 262 static jobject getDvbsFrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 263 static jobject getDvbtFrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 264 static jobject getIsdbs3FrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 265 static jobject getIsdbsFrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 266 static jobject getIsdbtFrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 267 static jobject getDtmbFrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 268 static jobject getIptvFrontendCaps(JNIEnv* env, FrontendCapabilities& caps); 269 }; 270 271 class C2DataIdInfo : public C2Param { 272 public: 273 C2DataIdInfo(uint32_t index, uint64_t value); 274 private: 275 typedef C2GlobalParam<C2Info, C2Int64Value, 0> StubInfo; 276 StubInfo mInfo; 277 static const size_t kParamSize = sizeof(StubInfo); 278 }; 279 280 } // namespace android 281 282 #endif // _ANDROID_MEDIA_TV_TUNER_H_ 283