blob: 50d92de4165e849dff978521f42beb8ad4292931 [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
151HidlDvrSettings TunerHidlDvr::getHidlDvrSettings(const DvrSettings& settings) {
152 HidlDvrSettings s;
153 switch (mType) {
154 case DvrType::PLAYBACK: {
155 s.playback({
156 .statusMask =
157 static_cast<uint8_t>(settings.get<DvrSettings::playback>().statusMask),
158 .lowThreshold =
159 static_cast<uint32_t>(settings.get<DvrSettings::playback>().lowThreshold),
160 .highThreshold =
161 static_cast<uint32_t>(settings.get<DvrSettings::playback>().highThreshold),
162 .dataFormat = static_cast<HidlDataFormat>(
163 settings.get<DvrSettings::playback>().dataFormat),
164 .packetSize =
165 static_cast<uint8_t>(settings.get<DvrSettings::playback>().packetSize),
166 });
167 return s;
168 }
169 case DvrType::RECORD: {
170 s.record({
171 .statusMask = static_cast<uint8_t>(settings.get<DvrSettings::record>().statusMask),
172 .lowThreshold =
173 static_cast<uint32_t>(settings.get<DvrSettings::record>().lowThreshold),
174 .highThreshold =
175 static_cast<uint32_t>(settings.get<DvrSettings::record>().highThreshold),
176 .dataFormat =
177 static_cast<HidlDataFormat>(settings.get<DvrSettings::record>().dataFormat),
178 .packetSize = static_cast<uint8_t>(settings.get<DvrSettings::record>().packetSize),
179 });
180 return s;
181 }
182 default:
183 break;
184 }
185 return s;
186}
187
188/////////////// IDvrCallback ///////////////////////
189Return<void> TunerHidlDvr::DvrCallback::onRecordStatus(const HidlRecordStatus status) {
190 if (mTunerDvrCallback != nullptr) {
191 mTunerDvrCallback->onRecordStatus(static_cast<RecordStatus>(status));
192 }
193 return Void();
194}
195
196Return<void> TunerHidlDvr::DvrCallback::onPlaybackStatus(const HidlPlaybackStatus status) {
197 if (mTunerDvrCallback != nullptr) {
198 mTunerDvrCallback->onPlaybackStatus(static_cast<PlaybackStatus>(status));
199 }
200 return Void();
201}
202
203} // namespace tuner
204} // namespace tv
205} // namespace media
206} // namespace android
207} // namespace aidl