blob: 8083a6ed86691ae591a0b202d980d4b0838791b1 [file] [log] [blame]
Hongguang093c5f32021-08-09 19:46:34 -07001/**
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
27using ::aidl::android::hardware::tv::tuner::DataFormat;
28using ::aidl::android::hardware::tv::tuner::PlaybackStatus;
29using ::aidl::android::hardware::tv::tuner::RecordStatus;
30using ::aidl::android::hardware::tv::tuner::Result;
31using ::android::unsafeHidlToAidlMQDescriptor;
32using ::android::hardware::MessageQueue;
33using ::android::hardware::MQDescriptorSync;
34
35using HidlDataFormat = ::android::hardware::tv::tuner::V1_0::DataFormat;
36using HidlResult = ::android::hardware::tv::tuner::V1_0::Result;
37using MQDesc = MQDescriptorSync<uint8_t>;
38
39using namespace std;
40
41namespace aidl {
42namespace android {
43namespace media {
44namespace tv {
45namespace tuner {
46
47TunerHidlDvr::TunerHidlDvr(sp<HidlIDvr> dvr, DvrType type) {
48 mDvr = dvr;
49 mType = type;
50}
51
52TunerHidlDvr::~TunerHidlDvr() {
53 mDvr = nullptr;
54}
55
56::ndk::ScopedAStatus TunerHidlDvr::getQueueDesc(AidlMQDesc* _aidl_return) {
Hongguang093c5f32021-08-09 19:46:34 -070057 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 = move(aidlMQDesc);
70 return ::ndk::ScopedAStatus::ok();
71}
72
73::ndk::ScopedAStatus TunerHidlDvr::configure(const DvrSettings& in_settings) {
Hongguang093c5f32021-08-09 19:46:34 -070074 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
81::ndk::ScopedAStatus TunerHidlDvr::attachFilter(const shared_ptr<ITunerFilter>& in_filter) {
Hongguang093c5f32021-08-09 19:46:34 -070082 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
100::ndk::ScopedAStatus TunerHidlDvr::detachFilter(const shared_ptr<ITunerFilter>& in_filter) {
Hongguang093c5f32021-08-09 19:46:34 -0700101 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
119::ndk::ScopedAStatus TunerHidlDvr::start() {
Hongguang093c5f32021-08-09 19:46:34 -0700120 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
127::ndk::ScopedAStatus TunerHidlDvr::stop() {
Hongguang093c5f32021-08-09 19:46:34 -0700128 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
135::ndk::ScopedAStatus TunerHidlDvr::flush() {
Hongguang093c5f32021-08-09 19:46:34 -0700136 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
143::ndk::ScopedAStatus TunerHidlDvr::close() {
Hongguang093c5f32021-08-09 19:46:34 -0700144 HidlResult res = mDvr->close();
Hongguang093c5f32021-08-09 19:46:34 -0700145 if (res != HidlResult::SUCCESS) {
146 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
147 }
148 return ::ndk::ScopedAStatus::ok();
149}
150
Ray Chin82f49232022-09-15 14:29:29 +0800151::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
Hongguang093c5f32021-08-09 19:46:34 -0700156HidlDvrSettings 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 ///////////////////////
194Return<void> TunerHidlDvr::DvrCallback::onRecordStatus(const HidlRecordStatus status) {
195 if (mTunerDvrCallback != nullptr) {
196 mTunerDvrCallback->onRecordStatus(static_cast<RecordStatus>(status));
197 }
198 return Void();
199}
200
201Return<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