• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020, 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_TUNERSERVICE_H
18 #define ANDROID_MEDIA_TUNERSERVICE_H
19 
20 #include <aidl/android/media/tv/tunerresourcemanager/ITunerResourceManager.h>
21 #include <aidl/android/media/tv/tuner/BnTunerService.h>
22 #include <android/hardware/tv/tuner/1.1/ITuner.h>
23 #include <fmq/AidlMessageQueue.h>
24 #include <fmq/EventFlag.h>
25 #include <fmq/MessageQueue.h>
26 
27 using ::aidl::android::hardware::common::fmq::GrantorDescriptor;
28 using ::aidl::android::hardware::common::fmq::MQDescriptor;
29 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
30 using ::aidl::android::media::tv::tuner::BnTunerService;
31 using ::aidl::android::media::tv::tuner::ITunerDemux;
32 using ::aidl::android::media::tv::tuner::ITunerDescrambler;
33 using ::aidl::android::media::tv::tuner::ITunerFrontend;
34 using ::aidl::android::media::tv::tuner::ITunerLnb;
35 using ::aidl::android::media::tv::tuner::TunerDemuxCapabilities;
36 using ::aidl::android::media::tv::tuner::TunerFrontendDtmbCapabilities;
37 using ::aidl::android::media::tv::tuner::TunerFrontendInfo;
38 using ::aidl::android::media::tv::tunerresourcemanager::ITunerResourceManager;
39 
40 using ::android::hardware::details::logError;
41 using ::android::hardware::hidl_vec;
42 using ::android::hardware::kSynchronizedReadWrite;
43 using ::android::hardware::EventFlag;
44 using ::android::hardware::MessageQueue;
45 using ::android::hardware::MQDescriptorSync;
46 using ::android::hardware::Return;
47 using ::android::hardware::Void;
48 using ::android::hardware::tv::tuner::V1_0::DemuxCapabilities;
49 using ::android::hardware::tv::tuner::V1_0::DemuxFilterAvSettings;
50 using ::android::hardware::tv::tuner::V1_0::DemuxFilterEvent;
51 using ::android::hardware::tv::tuner::V1_0::DemuxFilterMainType;
52 using ::android::hardware::tv::tuner::V1_0::DemuxFilterSettings;
53 using ::android::hardware::tv::tuner::V1_0::DemuxFilterStatus;
54 using ::android::hardware::tv::tuner::V1_0::DemuxFilterType;
55 using ::android::hardware::tv::tuner::V1_0::DemuxTsFilterSettings;
56 using ::android::hardware::tv::tuner::V1_0::DemuxTsFilterType;
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::IDemux;
60 using ::android::hardware::tv::tuner::V1_0::IDescrambler;
61 using ::android::hardware::tv::tuner::V1_0::IFilter;
62 using ::android::hardware::tv::tuner::V1_0::IFilterCallback;
63 using ::android::hardware::tv::tuner::V1_0::ITuner;
64 using ::android::hardware::tv::tuner::V1_0::Result;
65 
66 using Status = ::ndk::ScopedAStatus;
67 
68 using namespace std;
69 
70 namespace android {
71 
72 const static int TUNER_HAL_VERSION_UNKNOWN = 0;
73 const static int TUNER_HAL_VERSION_1_0 = 1 << 16;
74 const static int TUNER_HAL_VERSION_1_1 = (1 << 16) | 1;
75 // System Feature defined in PackageManager
76 static const ::android::String16 FEATURE_TUNER(::android::String16("android.hardware.tv.tuner"));
77 
78 typedef enum {
79     FRONTEND,
80     LNB,
81     DEMUX,
82     DESCRAMBLER,
83 } TunerResourceType;
84 
85 struct FilterCallback : public IFilterCallback {
~FilterCallbackFilterCallback86     ~FilterCallback() {}
onFilterEventFilterCallback87     Return<void> onFilterEvent(const DemuxFilterEvent&) {
88         return Void();
89     }
onFilterStatusFilterCallback90     Return<void> onFilterStatus(const DemuxFilterStatus) {
91         return Void();
92     }
93 };
94 
95 class TunerService : public BnTunerService {
96     typedef AidlMessageQueue<int8_t, SynchronizedReadWrite> AidlMessageQueue;
97     typedef MessageQueue<uint8_t, kSynchronizedReadWrite> HidlMessageQueue;
98     typedef MQDescriptor<int8_t, SynchronizedReadWrite> AidlMQDesc;
99 
100 public:
getServiceName()101     static char const *getServiceName() { return "media.tuner"; }
102     static binder_status_t instantiate();
103     TunerService();
104     virtual ~TunerService();
105 
106     Status getFrontendIds(vector<int32_t>* ids) override;
107     Status getFrontendInfo(int32_t id, TunerFrontendInfo* _aidl_return) override;
108     Status getFrontendDtmbCapabilities(
109             int32_t id, TunerFrontendDtmbCapabilities* _aidl_return) override;
110     Status openFrontend(
111             int32_t frontendHandle, shared_ptr<ITunerFrontend>* _aidl_return) override;
112     Status openLnb(int lnbHandle, shared_ptr<ITunerLnb>* _aidl_return) override;
113     Status openLnbByName(const string& lnbName, shared_ptr<ITunerLnb>* _aidl_return) override;
114     Status openDemux(int32_t demuxHandle, std::shared_ptr<ITunerDemux>* _aidl_return) override;
115     Status getDemuxCaps(TunerDemuxCapabilities* _aidl_return) override;
116     Status openDescrambler(int32_t descramblerHandle,
117             std::shared_ptr<ITunerDescrambler>* _aidl_return) override;
118     Status getTunerHalVersion(int* _aidl_return) override;
119 
120     // TODO: create a map between resource id and handles.
getResourceIdFromHandle(int resourceHandle,int)121     static int getResourceIdFromHandle(int resourceHandle, int /*type*/) {
122         return (resourceHandle & 0x00ff0000) >> 16;
123     }
124 
getResourceHandleFromId(int id,int resourceType)125     int getResourceHandleFromId(int id, int resourceType) {
126         // TODO: build up randomly generated id to handle mapping
127         return (resourceType & 0x000000ff) << 24
128                 | (id << 16)
129                 | (mResourceRequestCount++ & 0xffff);
130     }
131 
132 private:
133     bool hasITuner();
134     bool hasITuner_1_1();
135     void updateTunerResources();
136 
137     void updateFrontendResources();
138     void updateLnbResources();
139     Result getHidlFrontendIds(hidl_vec<FrontendId>& ids);
140     Result getHidlFrontendInfo(int id, FrontendInfo& info);
141     vector<int> getLnbHandles();
142 
143     TunerDemuxCapabilities getAidlDemuxCaps(DemuxCapabilities caps);
144     TunerFrontendInfo convertToAidlFrontendInfo(FrontendInfo halInfo);
145 
146     sp<ITuner> mTuner;
147     sp<::android::hardware::tv::tuner::V1_1::ITuner> mTuner_1_1;
148 
149     shared_ptr<ITunerResourceManager> mTunerResourceManager;
150     int mResourceRequestCount = 0;
151 
152     int mTunerVersion = TUNER_HAL_VERSION_UNKNOWN;
153 };
154 
155 } // namespace android
156 
157 #endif // ANDROID_MEDIA_TUNERSERVICE_H
158