1 /**
2 * Copyright 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 #define LOG_TAG "TunerHidlDemux"
18
19 #include "TunerHidlDemux.h"
20
21 #include "TunerHidlDvr.h"
22 #include "TunerHidlFilter.h"
23 #include "TunerHidlService.h"
24 #include "TunerHidlTimeFilter.h"
25
26 using ::aidl::android::hardware::tv::tuner::DemuxFilterSubType;
27
28 using HidlDemuxAlpFilterType = ::android::hardware::tv::tuner::V1_0::DemuxAlpFilterType;
29 using HidlDemuxFilterMainType = ::android::hardware::tv::tuner::V1_0::DemuxFilterMainType;
30 using HidlDemuxFilterType = ::android::hardware::tv::tuner::V1_0::DemuxFilterType;
31 using HidlDemuxIpFilterType = ::android::hardware::tv::tuner::V1_0::DemuxIpFilterType;
32 using HidlDemuxMmtpFilterType = ::android::hardware::tv::tuner::V1_0::DemuxMmtpFilterType;
33 using HidlDemuxTlvFilterType = ::android::hardware::tv::tuner::V1_0::DemuxTlvFilterType;
34 using HidlDemuxTsFilterType = ::android::hardware::tv::tuner::V1_0::DemuxTsFilterType;
35 using HidlDvrType = ::android::hardware::tv::tuner::V1_0::DvrType;
36 using HidlResult = ::android::hardware::tv::tuner::V1_0::Result;
37
38 using namespace std;
39
40 namespace aidl {
41 namespace android {
42 namespace media {
43 namespace tv {
44 namespace tuner {
45
TunerHidlDemux(const sp<IDemux> demux,const int id,const shared_ptr<TunerHidlService> tuner)46 TunerHidlDemux::TunerHidlDemux(const sp<IDemux> demux, const int id,
47 const shared_ptr<TunerHidlService> tuner) {
48 mDemux = demux;
49 mDemuxId = id;
50 mTunerService = tuner;
51 }
52
~TunerHidlDemux()53 TunerHidlDemux::~TunerHidlDemux() {
54 mDemux = nullptr;
55 mTunerService = nullptr;
56 }
57
setFrontendDataSource(const shared_ptr<ITunerFrontend> & in_frontend)58 ::ndk::ScopedAStatus TunerHidlDemux::setFrontendDataSource(
59 const shared_ptr<ITunerFrontend>& in_frontend) {
60 int frontendId;
61 in_frontend->getFrontendId(&frontendId);
62 HidlResult res = mDemux->setFrontendDataSource(frontendId);
63 if (res != HidlResult::SUCCESS) {
64 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
65 }
66 return ::ndk::ScopedAStatus::ok();
67 }
68
setFrontendDataSourceById(int frontendId)69 ::ndk::ScopedAStatus TunerHidlDemux::setFrontendDataSourceById(int frontendId) {
70 HidlResult res = mDemux->setFrontendDataSource(frontendId);
71 if (res != HidlResult::SUCCESS) {
72 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
73 }
74 return ::ndk::ScopedAStatus::ok();
75 }
76
openFilter(const DemuxFilterType & in_type,int32_t in_bufferSize,const shared_ptr<ITunerFilterCallback> & in_cb,shared_ptr<ITunerFilter> * _aidl_return)77 ::ndk::ScopedAStatus TunerHidlDemux::openFilter(const DemuxFilterType& in_type,
78 int32_t in_bufferSize,
79 const shared_ptr<ITunerFilterCallback>& in_cb,
80 shared_ptr<ITunerFilter>* _aidl_return) {
81 HidlDemuxFilterMainType mainType = static_cast<HidlDemuxFilterMainType>(in_type.mainType);
82 HidlDemuxFilterType filterType{
83 .mainType = mainType,
84 };
85
86 switch (mainType) {
87 case HidlDemuxFilterMainType::TS:
88 filterType.subType.tsFilterType(static_cast<HidlDemuxTsFilterType>(
89 in_type.subType.get<DemuxFilterSubType::Tag::tsFilterType>()));
90 break;
91 case HidlDemuxFilterMainType::MMTP:
92 filterType.subType.mmtpFilterType(static_cast<HidlDemuxMmtpFilterType>(
93 in_type.subType.get<DemuxFilterSubType::Tag::mmtpFilterType>()));
94 break;
95 case HidlDemuxFilterMainType::IP:
96 filterType.subType.ipFilterType(static_cast<HidlDemuxIpFilterType>(
97 in_type.subType.get<DemuxFilterSubType::Tag::ipFilterType>()));
98 break;
99 case HidlDemuxFilterMainType::TLV:
100 filterType.subType.tlvFilterType(static_cast<HidlDemuxTlvFilterType>(
101 in_type.subType.get<DemuxFilterSubType::Tag::tlvFilterType>()));
102 break;
103 case HidlDemuxFilterMainType::ALP:
104 filterType.subType.alpFilterType(static_cast<HidlDemuxAlpFilterType>(
105 in_type.subType.get<DemuxFilterSubType::Tag::alpFilterType>()));
106 break;
107 }
108 HidlResult status;
109 sp<HidlIFilter> filterSp;
110 sp<TunerHidlFilter::FilterCallback> filterCb = new TunerHidlFilter::FilterCallback(in_cb);
111 sp<::android::hardware::tv::tuner::V1_0::IFilterCallback> cbSp = filterCb;
112 mDemux->openFilter(filterType, static_cast<uint32_t>(in_bufferSize), cbSp,
113 [&](HidlResult r, const sp<HidlIFilter>& filter) {
114 filterSp = filter;
115 status = r;
116 });
117 if (status != HidlResult::SUCCESS) {
118 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(status));
119 }
120
121 *_aidl_return =
122 ::ndk::SharedRefBase::make<TunerHidlFilter>(filterSp, filterCb, in_type, mTunerService);
123 return ::ndk::ScopedAStatus::ok();
124 }
125
openTimeFilter(shared_ptr<ITunerTimeFilter> * _aidl_return)126 ::ndk::ScopedAStatus TunerHidlDemux::openTimeFilter(shared_ptr<ITunerTimeFilter>* _aidl_return) {
127 HidlResult status;
128 sp<HidlITimeFilter> filterSp;
129 mDemux->openTimeFilter([&](HidlResult r, const sp<HidlITimeFilter>& filter) {
130 filterSp = filter;
131 status = r;
132 });
133 if (status != HidlResult::SUCCESS) {
134 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(status));
135 }
136
137 *_aidl_return = ::ndk::SharedRefBase::make<TunerHidlTimeFilter>(filterSp);
138 return ::ndk::ScopedAStatus::ok();
139 }
140
getAvSyncHwId(const shared_ptr<ITunerFilter> & tunerFilter,int32_t * _aidl_return)141 ::ndk::ScopedAStatus TunerHidlDemux::getAvSyncHwId(const shared_ptr<ITunerFilter>& tunerFilter,
142 int32_t* _aidl_return) {
143 uint32_t avSyncHwId;
144 HidlResult res;
145 sp<HidlIFilter> halFilter = static_cast<TunerHidlFilter*>(tunerFilter.get())->getHalFilter();
146 mDemux->getAvSyncHwId(halFilter, [&](HidlResult r, uint32_t id) {
147 res = r;
148 avSyncHwId = id;
149 });
150 if (res != HidlResult::SUCCESS) {
151 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
152 }
153
154 *_aidl_return = (int)avSyncHwId;
155 return ::ndk::ScopedAStatus::ok();
156 }
157
getAvSyncTime(int32_t avSyncHwId,int64_t * _aidl_return)158 ::ndk::ScopedAStatus TunerHidlDemux::getAvSyncTime(int32_t avSyncHwId, int64_t* _aidl_return) {
159 uint64_t time;
160 HidlResult res;
161 mDemux->getAvSyncTime(static_cast<uint32_t>(avSyncHwId), [&](HidlResult r, uint64_t ts) {
162 res = r;
163 time = ts;
164 });
165 if (res != HidlResult::SUCCESS) {
166 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
167 }
168
169 *_aidl_return = (int64_t)time;
170 return ::ndk::ScopedAStatus::ok();
171 }
172
openDvr(DvrType in_dvbType,int32_t in_bufferSize,const shared_ptr<ITunerDvrCallback> & in_cb,shared_ptr<ITunerDvr> * _aidl_return)173 ::ndk::ScopedAStatus TunerHidlDemux::openDvr(DvrType in_dvbType, int32_t in_bufferSize,
174 const shared_ptr<ITunerDvrCallback>& in_cb,
175 shared_ptr<ITunerDvr>* _aidl_return) {
176 HidlResult res;
177 sp<HidlIDvrCallback> callback = new TunerHidlDvr::DvrCallback(in_cb);
178 sp<HidlIDvr> hidlDvr;
179 mDemux->openDvr(static_cast<HidlDvrType>(in_dvbType), in_bufferSize, callback,
180 [&](HidlResult r, const sp<HidlIDvr>& dvr) {
181 hidlDvr = dvr;
182 res = r;
183 });
184 if (res != HidlResult::SUCCESS) {
185 *_aidl_return = nullptr;
186 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
187 }
188
189 *_aidl_return = ::ndk::SharedRefBase::make<TunerHidlDvr>(hidlDvr, in_dvbType);
190 return ::ndk::ScopedAStatus::ok();
191 }
192
connectCiCam(int32_t ciCamId)193 ::ndk::ScopedAStatus TunerHidlDemux::connectCiCam(int32_t ciCamId) {
194 HidlResult res = mDemux->connectCiCam(static_cast<uint32_t>(ciCamId));
195 if (res != HidlResult::SUCCESS) {
196 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
197 }
198 return ::ndk::ScopedAStatus::ok();
199 }
200
disconnectCiCam()201 ::ndk::ScopedAStatus TunerHidlDemux::disconnectCiCam() {
202 HidlResult res = mDemux->disconnectCiCam();
203 if (res != HidlResult::SUCCESS) {
204 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
205 }
206 return ::ndk::ScopedAStatus::ok();
207 }
208
close()209 ::ndk::ScopedAStatus TunerHidlDemux::close() {
210 HidlResult res = mDemux->close();
211 if (res != HidlResult::SUCCESS) {
212 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
213 }
214 return ::ndk::ScopedAStatus::ok();
215 }
216
217 } // namespace tuner
218 } // namespace tv
219 } // namespace media
220 } // namespace android
221 } // namespace aidl
222