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 MQDesc dvrMQDesc;
58 HidlResult res;
59 mDvr->getQueueDesc([&](HidlResult r, const MQDesc& desc) {
60 dvrMQDesc = desc;
61 res = r;
62 });
63 if (res != HidlResult::SUCCESS) {
64 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
65 }
66
67 AidlMQDesc aidlMQDesc;
68 unsafeHidlToAidlMQDescriptor<uint8_t, int8_t, SynchronizedReadWrite>(dvrMQDesc, &aidlMQDesc);
69 *_aidl_return = std::move(aidlMQDesc);
70 return ::ndk::ScopedAStatus::ok();
71 }
72
configure(const DvrSettings & in_settings)73 ::ndk::ScopedAStatus TunerHidlDvr::configure(const DvrSettings& in_settings) {
74 HidlResult res = mDvr->configure(getHidlDvrSettings(in_settings));
75 if (res != HidlResult::SUCCESS) {
76 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
77 }
78 return ::ndk::ScopedAStatus::ok();
79 }
80
attachFilter(const shared_ptr<ITunerFilter> & in_filter)81 ::ndk::ScopedAStatus TunerHidlDvr::attachFilter(const shared_ptr<ITunerFilter>& in_filter) {
82 if (in_filter == nullptr) {
83 return ::ndk::ScopedAStatus::fromServiceSpecificError(
84 static_cast<int32_t>(Result::INVALID_ARGUMENT));
85 }
86
87 sp<HidlIFilter> hidlFilter = static_cast<TunerHidlFilter*>(in_filter.get())->getHalFilter();
88 if (hidlFilter == nullptr) {
89 return ::ndk::ScopedAStatus::fromServiceSpecificError(
90 static_cast<int32_t>(Result::INVALID_ARGUMENT));
91 }
92
93 HidlResult res = mDvr->attachFilter(hidlFilter);
94 if (res != HidlResult::SUCCESS) {
95 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
96 }
97 return ::ndk::ScopedAStatus::ok();
98 }
99
detachFilter(const shared_ptr<ITunerFilter> & in_filter)100 ::ndk::ScopedAStatus TunerHidlDvr::detachFilter(const shared_ptr<ITunerFilter>& in_filter) {
101 if (in_filter == nullptr) {
102 return ::ndk::ScopedAStatus::fromServiceSpecificError(
103 static_cast<int32_t>(Result::INVALID_ARGUMENT));
104 }
105
106 sp<HidlIFilter> halFilter = (static_cast<TunerHidlFilter*>(in_filter.get()))->getHalFilter();
107 if (halFilter == nullptr) {
108 return ::ndk::ScopedAStatus::fromServiceSpecificError(
109 static_cast<int32_t>(Result::INVALID_ARGUMENT));
110 }
111
112 HidlResult res = mDvr->detachFilter(halFilter);
113 if (res != HidlResult::SUCCESS) {
114 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
115 }
116 return ::ndk::ScopedAStatus::ok();
117 }
118
start()119 ::ndk::ScopedAStatus TunerHidlDvr::start() {
120 HidlResult res = mDvr->start();
121 if (res != HidlResult::SUCCESS) {
122 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
123 }
124 return ::ndk::ScopedAStatus::ok();
125 }
126
stop()127 ::ndk::ScopedAStatus TunerHidlDvr::stop() {
128 HidlResult res = mDvr->stop();
129 if (res != HidlResult::SUCCESS) {
130 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
131 }
132 return ::ndk::ScopedAStatus::ok();
133 }
134
flush()135 ::ndk::ScopedAStatus TunerHidlDvr::flush() {
136 HidlResult res = mDvr->flush();
137 if (res != HidlResult::SUCCESS) {
138 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
139 }
140 return ::ndk::ScopedAStatus::ok();
141 }
142
close()143 ::ndk::ScopedAStatus TunerHidlDvr::close() {
144 HidlResult res = mDvr->close();
145 if (res != HidlResult::SUCCESS) {
146 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
147 }
148 return ::ndk::ScopedAStatus::ok();
149 }
150
setStatusCheckIntervalHint(int64_t)151 ::ndk::ScopedAStatus TunerHidlDvr::setStatusCheckIntervalHint(int64_t /* in_milliseconds */) {
152 HidlResult res = HidlResult::UNAVAILABLE;
153 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
154 }
155
getHidlDvrSettings(const DvrSettings & settings)156 HidlDvrSettings TunerHidlDvr::getHidlDvrSettings(const DvrSettings& settings) {
157 HidlDvrSettings s;
158 switch (mType) {
159 case DvrType::PLAYBACK: {
160 s.playback({
161 .statusMask =
162 static_cast<uint8_t>(settings.get<DvrSettings::playback>().statusMask),
163 .lowThreshold =
164 static_cast<uint32_t>(settings.get<DvrSettings::playback>().lowThreshold),
165 .highThreshold =
166 static_cast<uint32_t>(settings.get<DvrSettings::playback>().highThreshold),
167 .dataFormat = static_cast<HidlDataFormat>(
168 settings.get<DvrSettings::playback>().dataFormat),
169 .packetSize =
170 static_cast<uint8_t>(settings.get<DvrSettings::playback>().packetSize),
171 });
172 return s;
173 }
174 case DvrType::RECORD: {
175 s.record({
176 .statusMask = static_cast<uint8_t>(settings.get<DvrSettings::record>().statusMask),
177 .lowThreshold =
178 static_cast<uint32_t>(settings.get<DvrSettings::record>().lowThreshold),
179 .highThreshold =
180 static_cast<uint32_t>(settings.get<DvrSettings::record>().highThreshold),
181 .dataFormat =
182 static_cast<HidlDataFormat>(settings.get<DvrSettings::record>().dataFormat),
183 .packetSize = static_cast<uint8_t>(settings.get<DvrSettings::record>().packetSize),
184 });
185 return s;
186 }
187 default:
188 break;
189 }
190 return s;
191 }
192
193 /////////////// IDvrCallback ///////////////////////
onRecordStatus(const HidlRecordStatus status)194 Return<void> TunerHidlDvr::DvrCallback::onRecordStatus(const HidlRecordStatus status) {
195 if (mTunerDvrCallback != nullptr) {
196 mTunerDvrCallback->onRecordStatus(static_cast<RecordStatus>(status));
197 }
198 return Void();
199 }
200
onPlaybackStatus(const HidlPlaybackStatus status)201 Return<void> TunerHidlDvr::DvrCallback::onPlaybackStatus(const HidlPlaybackStatus status) {
202 if (mTunerDvrCallback != nullptr) {
203 mTunerDvrCallback->onPlaybackStatus(static_cast<PlaybackStatus>(status));
204 }
205 return Void();
206 }
207
208 } // namespace tuner
209 } // namespace tv
210 } // namespace media
211 } // namespace android
212 } // namespace aidl
213