blob: db4e07be9337beb46dff28cbc554a932c29c7c9f [file] [log] [blame]
Amy Zhangada92d72021-01-22 16:45:53 -08001/**
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 "TunerDvr"
18
19#include <fmq/ConvertMQDescriptors.h>
20#include "TunerDvr.h"
21#include "TunerFilter.h"
22
23using ::android::hardware::tv::tuner::V1_0::DataFormat;
24using ::android::hardware::tv::tuner::V1_0::Result;
25
26namespace android {
27
28TunerDvr::TunerDvr(sp<IDvr> dvr, int type) {
29 mDvr = dvr;
30 mType = static_cast<DvrType>(type);
31}
32
33TunerDvr::~TunerDvr() {
34 mDvr = NULL;
35}
36
37Status TunerDvr::getQueueDesc(AidlMQDesc* _aidl_return) {
38 if (mDvr == NULL) {
39 ALOGE("IDvr is not initialized");
40 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
41 }
42
43 MQDesc dvrMQDesc;
44 Result res;
45 mDvr->getQueueDesc([&](Result r, const MQDesc& desc) {
46 dvrMQDesc = desc;
47 res = r;
48 });
49 if (res != Result::SUCCESS) {
50 return Status::fromServiceSpecificError(static_cast<int32_t>(res));
51 }
52
53 AidlMQDesc aidlMQDesc;
54 unsafeHidlToAidlMQDescriptor<uint8_t, int8_t, SynchronizedReadWrite>(
55 dvrMQDesc, &aidlMQDesc);
56 *_aidl_return = move(aidlMQDesc);
57 return Status::ok();
58}
59
60Status TunerDvr::configure(const TunerDvrSettings& settings) {
61 if (mDvr == NULL) {
62 ALOGE("IDvr is not initialized");
63 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
64 }
65
66 Result res = mDvr->configure(getHidlDvrSettingsFromAidl(settings));
67 if (res != Result::SUCCESS) {
68 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
69 }
70 return Status::ok();
71}
72
73Status TunerDvr::attachFilter(const shared_ptr<ITunerFilter>& filter) {
74 if (mDvr == NULL) {
75 ALOGE("IDvr is not initialized");
76 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
77 }
78
79 ITunerFilter* tunerFilter = filter.get();
80 sp<IFilter> hidlFilter = static_cast<TunerFilter*>(tunerFilter)->getHalFilter();
81 if (hidlFilter == NULL) {
82 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::INVALID_ARGUMENT));
83 }
84
85 Result res = mDvr->attachFilter(hidlFilter);
86 if (res != Result::SUCCESS) {
87 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
88 }
89 return Status::ok();
90}
91
92Status TunerDvr::detachFilter(const shared_ptr<ITunerFilter>& filter) {
93 if (mDvr == NULL) {
94 ALOGE("IDvr is not initialized");
95 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
96 }
97
98 ITunerFilter* tunerFilter = filter.get();
99 sp<IFilter> hidlFilter = static_cast<TunerFilter*>(tunerFilter)->getHalFilter();
100 if (hidlFilter == NULL) {
101 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::INVALID_ARGUMENT));
102 }
103
104 Result res = mDvr->detachFilter(hidlFilter);
105 if (res != Result::SUCCESS) {
106 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
107 }
108 return Status::ok();
109}
110
111Status TunerDvr::start() {
112 if (mDvr == NULL) {
113 ALOGE("IDvr is not initialized");
114 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
115 }
116
117 Result res = mDvr->start();
118 if (res != Result::SUCCESS) {
119 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
120 }
121 return Status::ok();
122}
123
124Status TunerDvr::stop() {
125 if (mDvr == NULL) {
126 ALOGE("IDvr is not initialized");
127 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
128 }
129
130 Result res = mDvr->stop();
131 if (res != Result::SUCCESS) {
132 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
133 }
134 return Status::ok();
135}
136
137Status TunerDvr::flush() {
138 if (mDvr == NULL) {
139 ALOGE("IDvr is not initialized");
140 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
141 }
142
143 Result res = mDvr->flush();
144 if (res != Result::SUCCESS) {
145 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
146 }
147 return Status::ok();
148}
149
150Status TunerDvr::close() {
151 if (mDvr == NULL) {
152 ALOGE("IDvr is not initialized");
153 return Status::fromServiceSpecificError(static_cast<int32_t>(Result::UNAVAILABLE));
154 }
155
156 Result res = mDvr->close();
Amy Zhang14a60e82021-02-11 15:22:52 -0800157 mDvr = NULL;
158
Amy Zhangada92d72021-01-22 16:45:53 -0800159 if (res != Result::SUCCESS) {
160 return ::ndk::ScopedAStatus::fromServiceSpecificError(static_cast<int32_t>(res));
161 }
162 return Status::ok();
163}
164
165DvrSettings TunerDvr::getHidlDvrSettingsFromAidl(TunerDvrSettings settings) {
166 DvrSettings s;
167 switch (mType) {
168 case DvrType::PLAYBACK: {
169 s.playback({
170 .statusMask = static_cast<uint8_t>(settings.statusMask),
171 .lowThreshold = static_cast<uint32_t>(settings.lowThreshold),
172 .highThreshold = static_cast<uint32_t>(settings.highThreshold),
173 .dataFormat = static_cast<DataFormat>(settings.dataFormat),
174 .packetSize = static_cast<uint8_t>(settings.packetSize),
175 });
176 return s;
177 }
178 case DvrType::RECORD: {
179 s.record({
180 .statusMask = static_cast<uint8_t>(settings.statusMask),
181 .lowThreshold = static_cast<uint32_t>(settings.lowThreshold),
182 .highThreshold = static_cast<uint32_t>(settings.highThreshold),
183 .dataFormat = static_cast<DataFormat>(settings.dataFormat),
184 .packetSize = static_cast<uint8_t>(settings.packetSize),
185 });
186 return s;
187 }
188 default:
189 break;
190 }
191 return s;
192}
193
194/////////////// IDvrCallback ///////////////////////
195
196Return<void> TunerDvr::DvrCallback::onRecordStatus(const RecordStatus status) {
197 if (mTunerDvrCallback != NULL) {
198 mTunerDvrCallback->onRecordStatus(static_cast<int>(status));
199 }
200 return Void();
201}
202
203Return<void> TunerDvr::DvrCallback::onPlaybackStatus(const PlaybackStatus status) {
204 if (mTunerDvrCallback != NULL) {
205 mTunerDvrCallback->onPlaybackStatus(static_cast<int>(status));
206 }
207 return Void();
208}
209} // namespace android