1 /**
2  * Copyright (c) 2021, 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_TUNERHIDLSERVICE_H
18 #define ANDROID_MEDIA_TUNERHIDLSERVICE_H
19 
20 #include <aidl/android/hardware/tv/tuner/DemuxFilterEvent.h>
21 #include <aidl/android/hardware/tv/tuner/DemuxFilterStatus.h>
22 #include <aidl/android/media/tv/tuner/BnTunerService.h>
23 #include <aidl/android/media/tv/tunerresourcemanager/TunerFrontendInfo.h>
24 #include <android/hardware/tv/tuner/1.1/ITuner.h>
25 #include <utils/Mutex.h>
26 
27 #include <unordered_set>
28 
29 #include "TunerHelper.h"
30 #include "TunerHidlFilter.h"
31 #include "TunerHidlFrontend.h"
32 
33 using ::aidl::android::hardware::tv::tuner::DemuxCapabilities;
34 using ::aidl::android::hardware::tv::tuner::DemuxFilterEvent;
35 using ::aidl::android::hardware::tv::tuner::DemuxFilterStatus;
36 using ::aidl::android::hardware::tv::tuner::DemuxInfo;
37 using ::aidl::android::hardware::tv::tuner::FrontendInfo;
38 using ::aidl::android::hardware::tv::tuner::FrontendType;
39 using ::aidl::android::media::tv::tuner::ITunerDemux;
40 using ::aidl::android::media::tv::tuner::ITunerDescrambler;
41 using ::aidl::android::media::tv::tuner::ITunerFrontend;
42 using ::aidl::android::media::tv::tuner::ITunerLnb;
43 using ::aidl::android::media::tv::tunerresourcemanager::TunerFrontendInfo;
44 using ::android::Mutex;
45 using ::android::sp;
46 using ::android::hardware::hidl_vec;
47 using ::android::hardware::Return;
48 using ::android::hardware::Void;
49 using ::std::shared_ptr;
50 using ::std::string;
51 using ::std::vector;
52 
53 using HidlFrontendDtmbCapabilities = ::android::hardware::tv::tuner::V1_1::FrontendDtmbCapabilities;
54 using HidlDemuxFilterEvent = ::android::hardware::tv::tuner::V1_0::DemuxFilterEvent;
55 using HidlDemuxFilterStatus = ::android::hardware::tv::tuner::V1_0::DemuxFilterStatus;
56 using HidlDemuxCapabilities = ::android::hardware::tv::tuner::V1_0::DemuxCapabilities;
57 using HidlFrontendInfo = ::android::hardware::tv::tuner::V1_0::FrontendInfo;
58 using HidlITuner = ::android::hardware::tv::tuner::V1_0::ITuner;
59 using HidlResult = ::android::hardware::tv::tuner::V1_0::Result;
60 using HidlFrontendId = ::android::hardware::tv::tuner::V1_0::FrontendId;
61 
62 namespace aidl {
63 namespace android {
64 namespace media {
65 namespace tv {
66 namespace tuner {
67 
68 class TunerHidlService : public BnTunerService {
69 public:
getServiceName()70     static char const* getServiceName() { return "media.tuner"; }
71     static binder_status_t instantiate();
72     TunerHidlService();
73     virtual ~TunerHidlService();
74 
75     ::ndk::ScopedAStatus getFrontendIds(vector<int32_t>* out_ids) override;
76     ::ndk::ScopedAStatus getFrontendInfo(int32_t in_frontendHandle,
77                                          FrontendInfo* _aidl_return) override;
78     ::ndk::ScopedAStatus openFrontend(int32_t in_frontendHandle,
79                                       shared_ptr<ITunerFrontend>* _aidl_return) override;
80     ::ndk::ScopedAStatus openLnb(int32_t in_lnbHandle,
81                                  shared_ptr<ITunerLnb>* _aidl_return) override;
82     ::ndk::ScopedAStatus openLnbByName(const std::string& in_lnbName,
83                                        shared_ptr<ITunerLnb>* _aidl_return) override;
84     ::ndk::ScopedAStatus openDemux(int32_t in_demuxHandle,
85                                    shared_ptr<ITunerDemux>* _aidl_return) override;
86     ::ndk::ScopedAStatus getDemuxCaps(DemuxCapabilities* _aidl_return) override;
87     ::ndk::ScopedAStatus getDemuxInfo(int32_t in_demuxHandle, DemuxInfo* _aidl_return) override;
88     ::ndk::ScopedAStatus getDemuxInfoList(vector<DemuxInfo>* _aidl_return) override;
89     ::ndk::ScopedAStatus openDescrambler(int32_t in_descramblerHandle,
90                                          shared_ptr<ITunerDescrambler>* _aidl_return) override;
91     ::ndk::ScopedAStatus getTunerHalVersion(int32_t* _aidl_return) override;
92     ::ndk::ScopedAStatus openSharedFilter(const string& in_filterToken,
93                                           const shared_ptr<ITunerFilterCallback>& in_cb,
94                                           shared_ptr<ITunerFilter>* _aidl_return) override;
95     ::ndk::ScopedAStatus isLnaSupported(bool* _aidl_return) override;
96     ::ndk::ScopedAStatus setLna(bool in_bEnable) override;
97     ::ndk::ScopedAStatus setMaxNumberOfFrontends(FrontendType in_frontendType,
98                                                  int32_t in_maxNumber) override;
99     ::ndk::ScopedAStatus getMaxNumberOfFrontends(FrontendType in_frontendType,
100                                                  int32_t* _aidl_return) override;
101 
102     string addFilterToShared(const shared_ptr<TunerHidlFilter>& sharedFilter);
103     void removeSharedFilter(const shared_ptr<TunerHidlFilter>& sharedFilter);
104     void removeFrontend(const shared_ptr<TunerHidlFrontend>& frontend);
105 
106 private:
107     void updateTunerResources();
108     vector<TunerFrontendInfo> getTRMFrontendInfos();
109     vector<int32_t> getTRMLnbHandles();
110     HidlResult getHidlFrontendIds(hidl_vec<HidlFrontendId>& ids);
111     HidlResult getHidlFrontendInfo(const int id, HidlFrontendInfo& info);
112     DemuxCapabilities getAidlDemuxCaps(const HidlDemuxCapabilities& caps);
113     FrontendInfo getAidlFrontendInfo(const HidlFrontendInfo& halInfo,
114                                      const HidlFrontendDtmbCapabilities& dtmbCaps);
115 
116     sp<HidlITuner> mTuner;
117     sp<::android::hardware::tv::tuner::V1_1::ITuner> mTuner_1_1;
118     int mTunerVersion = TUNER_HAL_VERSION_UNKNOWN;
119     Mutex mSharedFiltersLock;
120     map<string, shared_ptr<TunerHidlFilter>> mSharedFilters;
121     Mutex mOpenedFrontendsLock;
122     unordered_set<shared_ptr<TunerHidlFrontend>> mOpenedFrontends;
123     int mLnaStatus = -1;
124 };
125 
126 }  // namespace tuner
127 }  // namespace tv
128 }  // namespace media
129 }  // namespace android
130 }  // namespace aidl
131 
132 #endif  // ANDROID_MEDIA_TUNERHIDLSERVICE_H
133