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 "TunerDvr"
18 
19 #include "TunerDvr.h"
20 
21 #include <aidl/android/hardware/tv/tuner/Result.h>
22 #include <utils/Log.h>
23 
24 #include "TunerFilter.h"
25 
26 using ::aidl::android::hardware::tv::tuner::Result;
27 
28 namespace aidl {
29 namespace android {
30 namespace media {
31 namespace tv {
32 namespace tuner {
33 
TunerDvr(shared_ptr<IDvr> dvr,DvrType type)34 TunerDvr::TunerDvr(shared_ptr<IDvr> dvr, DvrType type) {
35     mDvr = dvr;
36     mType = type;
37 }
38 
~TunerDvr()39 TunerDvr::~TunerDvr() {
40     if (!isClosed) {
41         close();
42     }
43     mDvr = nullptr;
44 }
45 
getQueueDesc(AidlMQDesc * _aidl_return)46 ::ndk::ScopedAStatus TunerDvr::getQueueDesc(AidlMQDesc* _aidl_return) {
47     return mDvr->getQueueDesc(_aidl_return);
48 }
49 
configure(const DvrSettings & in_settings)50 ::ndk::ScopedAStatus TunerDvr::configure(const DvrSettings& in_settings) {
51     return mDvr->configure(in_settings);
52 }
53 
attachFilter(const shared_ptr<ITunerFilter> & in_filter)54 ::ndk::ScopedAStatus TunerDvr::attachFilter(const shared_ptr<ITunerFilter>& in_filter) {
55     if (in_filter == nullptr) {
56         return ::ndk::ScopedAStatus::fromServiceSpecificError(
57                 static_cast<int32_t>(Result::INVALID_ARGUMENT));
58     }
59 
60     shared_ptr<IFilter> halFilter = (static_cast<TunerFilter*>(in_filter.get()))->getHalFilter();
61     if (halFilter == nullptr) {
62         return ::ndk::ScopedAStatus::fromServiceSpecificError(
63                 static_cast<int32_t>(Result::INVALID_ARGUMENT));
64     }
65 
66     return mDvr->attachFilter(halFilter);
67 }
68 
detachFilter(const shared_ptr<ITunerFilter> & in_filter)69 ::ndk::ScopedAStatus TunerDvr::detachFilter(const shared_ptr<ITunerFilter>& in_filter) {
70     if (in_filter == nullptr) {
71         return ::ndk::ScopedAStatus::fromServiceSpecificError(
72                 static_cast<int32_t>(Result::INVALID_ARGUMENT));
73     }
74 
75     shared_ptr<IFilter> halFilter = (static_cast<TunerFilter*>(in_filter.get()))->getHalFilter();
76     if (halFilter == nullptr) {
77         return ::ndk::ScopedAStatus::fromServiceSpecificError(
78                 static_cast<int32_t>(Result::INVALID_ARGUMENT));
79     }
80 
81     return mDvr->detachFilter(halFilter);
82 }
83 
start()84 ::ndk::ScopedAStatus TunerDvr::start() {
85     return mDvr->start();
86 }
87 
stop()88 ::ndk::ScopedAStatus TunerDvr::stop() {
89     return mDvr->stop();
90 }
91 
flush()92 ::ndk::ScopedAStatus TunerDvr::flush() {
93     return mDvr->flush();
94 }
95 
close()96 ::ndk::ScopedAStatus TunerDvr::close() {
97     isClosed = true;
98     return mDvr->close();
99 }
100 
setStatusCheckIntervalHint(const int64_t milliseconds)101 ::ndk::ScopedAStatus TunerDvr::setStatusCheckIntervalHint(const int64_t milliseconds) {
102     if (milliseconds < 0L) {
103         return ::ndk::ScopedAStatus::fromServiceSpecificError(
104                 static_cast<int32_t>(Result::INVALID_ARGUMENT));
105     }
106 
107     ::ndk::ScopedAStatus s = mDvr->setStatusCheckIntervalHint(milliseconds);
108     if (s.getStatus() == STATUS_UNKNOWN_TRANSACTION) {
109         return ::ndk::ScopedAStatus::fromServiceSpecificError(
110                 static_cast<int32_t>(Result::UNAVAILABLE));
111     }
112 
113     return s;
114 }
115 
116 /////////////// IDvrCallback ///////////////////////
onRecordStatus(const RecordStatus status)117 ::ndk::ScopedAStatus TunerDvr::DvrCallback::onRecordStatus(const RecordStatus status) {
118     if (mTunerDvrCallback != nullptr) {
119         mTunerDvrCallback->onRecordStatus(status);
120     }
121     return ndk::ScopedAStatus::ok();
122 }
123 
onPlaybackStatus(const PlaybackStatus status)124 ::ndk::ScopedAStatus TunerDvr::DvrCallback::onPlaybackStatus(const PlaybackStatus status) {
125     if (mTunerDvrCallback != nullptr) {
126         mTunerDvrCallback->onPlaybackStatus(status);
127     }
128     return ndk::ScopedAStatus::ok();
129 }
130 
131 }  // namespace tuner
132 }  // namespace tv
133 }  // namespace media
134 }  // namespace android
135 }  // namespace aidl
136