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 "TunerHidlDvr"
18
19 #include "TunerHidlDvr.h"
20
21 #include <aidl/android/hardware/tv/tuner/DataFormat.h>
22 #include <aidl/android/hardware/tv/tuner/PlaybackStatus.h>
23 #include <aidl/android/hardware/tv/tuner/RecordStatus.h>
24 #include <aidl/android/hardware/tv/tuner/Result.h>
25 #include <fmq/ConvertMQDescriptors.h>
26
27 using ::aidl::android::hardware::tv::tuner::DataFormat;
28 using ::aidl::android::hardware::tv::tuner::PlaybackStatus;
29 using ::aidl::android::hardware::tv::tuner::RecordStatus;
30 using ::aidl::android::hardware::tv::tuner::Result;
31 using ::android::unsafeHidlToAidlMQDescriptor;
32 using ::android::hardware::MessageQueue;
33 using ::android::hardware::MQDescriptorSync;
34
35 using HidlDataFormat = ::android::hardware::tv::tuner::V1_0::DataFormat;
36 using HidlResult = ::android::hardware::tv::tuner::V1_0::Result;
37 using MQDesc = MQDescriptorSync<uint8_t>;
38
39 using namespace std;
40
41 namespace aidl {
42 namespace android {
43 namespace media {
44 namespace tv {
45 namespace tuner {
46
TunerHidlDvr(sp<HidlIDvr> dvr,DvrType type)47 TunerHidlDvr::TunerHidlDvr(sp<HidlIDvr> dvr, DvrType type) {
48 mDvr = dvr;
49 mType = type;
50 }
51
~TunerHidlDvr()52 TunerHidlDvr::~TunerHidlDvr() {
53 mDvr = nullptr;
54 }
55
getQueueDesc(AidlMQDesc * _aidl_return)56 ::ndk::ScopedAStatus TunerHidlDvr::getQueueDesc(AidlMQDesc* _aidl_return) {
57 if (mDvr == nullptr) {
58 ALOGE("IDvr is not initialized");
59 return ::ndk::ScopedAStatus::fromServiceSpecificError(
60 static_cast<int32_t>(Result::UNAVAILABLE));
61 }
62
63 MQDesc dvrMQDesc;
64 HidlResult res;
65 mDvr->getQueueDesc([&](HidlResult r, const MQDesc& desc) {
66 dvrMQDesc = desc;
67 res = r;
68 });
69 if (res != HidlResult::SUCCESS) {
70 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
71 }
72
73 AidlMQDesc aidlMQDesc;
74 unsafeHidlToAidlMQDescriptor<uint8_t, int8_t, SynchronizedReadWrite>(dvrMQDesc, &aidlMQDesc);
75 *_aidl_return = move(aidlMQDesc);
76 return ::ndk::ScopedAStatus::ok();
77 }
78
configure(const DvrSettings & in_settings)79 ::ndk::ScopedAStatus TunerHidlDvr::configure(const DvrSettings& in_settings) {
80 if (mDvr == nullptr) {
81 ALOGE("IDvr is not initialized");
82 return ::ndk::ScopedAStatus::fromServiceSpecificError(
83 static_cast<int32_t>(Result::UNAVAILABLE));
84 }
85
86 HidlResult res = mDvr->configure(getHidlDvrSettings(in_settings));
87 if (res != HidlResult::SUCCESS) {
88 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
89 }
90 return ::ndk::ScopedAStatus::ok();
91 }
92
attachFilter(const shared_ptr<ITunerFilter> & in_filter)93 ::ndk::ScopedAStatus TunerHidlDvr::attachFilter(const shared_ptr<ITunerFilter>& in_filter) {
94 if (mDvr == nullptr) {
95 ALOGE("IDvr is not initialized");
96 return ::ndk::ScopedAStatus::fromServiceSpecificError(
97 static_cast<int32_t>(Result::UNAVAILABLE));
98 }
99
100 if (in_filter == nullptr) {
101 return ::ndk::ScopedAStatus::fromServiceSpecificError(
102 static_cast<int32_t>(Result::INVALID_ARGUMENT));
103 }
104
105 sp<HidlIFilter> hidlFilter = static_cast<TunerHidlFilter*>(in_filter.get())->getHalFilter();
106 if (hidlFilter == nullptr) {
107 return ::ndk::ScopedAStatus::fromServiceSpecificError(
108 static_cast<int32_t>(Result::INVALID_ARGUMENT));
109 }
110
111 HidlResult res = mDvr->attachFilter(hidlFilter);
112 if (res != HidlResult::SUCCESS) {
113 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
114 }
115 return ::ndk::ScopedAStatus::ok();
116 }
117
detachFilter(const shared_ptr<ITunerFilter> & in_filter)118 ::ndk::ScopedAStatus TunerHidlDvr::detachFilter(const shared_ptr<ITunerFilter>& in_filter) {
119 if (mDvr == nullptr) {
120 ALOGE("IDvr is not initialized");
121 return ::ndk::ScopedAStatus::fromServiceSpecificError(
122 static_cast<int32_t>(Result::UNAVAILABLE));
123 }
124
125 if (in_filter == nullptr) {
126 return ::ndk::ScopedAStatus::fromServiceSpecificError(
127 static_cast<int32_t>(Result::INVALID_ARGUMENT));
128 }
129
130 sp<HidlIFilter> halFilter = (static_cast<TunerHidlFilter*>(in_filter.get()))->getHalFilter();
131 if (halFilter == nullptr) {
132 return ::ndk::ScopedAStatus::fromServiceSpecificError(
133 static_cast<int32_t>(Result::INVALID_ARGUMENT));
134 }
135
136 HidlResult res = mDvr->detachFilter(halFilter);
137 if (res != HidlResult::SUCCESS) {
138 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
139 }
140 return ::ndk::ScopedAStatus::ok();
141 }
142
start()143 ::ndk::ScopedAStatus TunerHidlDvr::start() {
144 if (mDvr == nullptr) {
145 ALOGE("IDvr is not initialized");
146 return ::ndk::ScopedAStatus::fromServiceSpecificError(
147 static_cast<int32_t>(Result::UNAVAILABLE));
148 }
149
150 HidlResult res = mDvr->start();
151 if (res != HidlResult::SUCCESS) {
152 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
153 }
154 return ::ndk::ScopedAStatus::ok();
155 }
156
stop()157 ::ndk::ScopedAStatus TunerHidlDvr::stop() {
158 if (mDvr == nullptr) {
159 ALOGE("IDvr is not initialized");
160 return ::ndk::ScopedAStatus::fromServiceSpecificError(
161 static_cast<int32_t>(Result::UNAVAILABLE));
162 }
163
164 HidlResult res = mDvr->stop();
165 if (res != HidlResult::SUCCESS) {
166 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
167 }
168 return ::ndk::ScopedAStatus::ok();
169 }
170
flush()171 ::ndk::ScopedAStatus TunerHidlDvr::flush() {
172 if (mDvr == nullptr) {
173 ALOGE("IDvr is not initialized");
174 return ::ndk::ScopedAStatus::fromServiceSpecificError(
175 static_cast<int32_t>(Result::UNAVAILABLE));
176 }
177
178 HidlResult res = mDvr->flush();
179 if (res != HidlResult::SUCCESS) {
180 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
181 }
182 return ::ndk::ScopedAStatus::ok();
183 }
184
close()185 ::ndk::ScopedAStatus TunerHidlDvr::close() {
186 if (mDvr == nullptr) {
187 ALOGE("IDvr is not initialized");
188 return ::ndk::ScopedAStatus::fromServiceSpecificError(
189 static_cast<int32_t>(Result::UNAVAILABLE));
190 }
191
192 HidlResult res = mDvr->close();
193 mDvr = nullptr;
194
195 if (res != HidlResult::SUCCESS) {
196 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
197 }
198 return ::ndk::ScopedAStatus::ok();
199 }
200
getHidlDvrSettings(const DvrSettings & settings)201 HidlDvrSettings TunerHidlDvr::getHidlDvrSettings(const DvrSettings& settings) {
202 HidlDvrSettings s;
203 switch (mType) {
204 case DvrType::PLAYBACK: {
205 s.playback({
206 .statusMask =
207 static_cast<uint8_t>(settings.get<DvrSettings::playback>().statusMask),
208 .lowThreshold =
209 static_cast<uint32_t>(settings.get<DvrSettings::playback>().lowThreshold),
210 .highThreshold =
211 static_cast<uint32_t>(settings.get<DvrSettings::playback>().highThreshold),
212 .dataFormat = static_cast<HidlDataFormat>(
213 settings.get<DvrSettings::playback>().dataFormat),
214 .packetSize =
215 static_cast<uint8_t>(settings.get<DvrSettings::playback>().packetSize),
216 });
217 return s;
218 }
219 case DvrType::RECORD: {
220 s.record({
221 .statusMask = static_cast<uint8_t>(settings.get<DvrSettings::record>().statusMask),
222 .lowThreshold =
223 static_cast<uint32_t>(settings.get<DvrSettings::record>().lowThreshold),
224 .highThreshold =
225 static_cast<uint32_t>(settings.get<DvrSettings::record>().highThreshold),
226 .dataFormat =
227 static_cast<HidlDataFormat>(settings.get<DvrSettings::record>().dataFormat),
228 .packetSize = static_cast<uint8_t>(settings.get<DvrSettings::record>().packetSize),
229 });
230 return s;
231 }
232 default:
233 break;
234 }
235 return s;
236 }
237
238 /////////////// IDvrCallback ///////////////////////
onRecordStatus(const HidlRecordStatus status)239 Return<void> TunerHidlDvr::DvrCallback::onRecordStatus(const HidlRecordStatus status) {
240 if (mTunerDvrCallback != nullptr) {
241 mTunerDvrCallback->onRecordStatus(static_cast<RecordStatus>(status));
242 }
243 return Void();
244 }
245
onPlaybackStatus(const HidlPlaybackStatus status)246 Return<void> TunerHidlDvr::DvrCallback::onPlaybackStatus(const HidlPlaybackStatus status) {
247 if (mTunerDvrCallback != nullptr) {
248 mTunerDvrCallback->onPlaybackStatus(static_cast<PlaybackStatus>(status));
249 }
250 return Void();
251 }
252
253 } // namespace tuner
254 } // namespace tv
255 } // namespace media
256 } // namespace android
257 } // namespace aidl
258