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 "DemuxClient"
18  
19  #include "DemuxClient.h"
20  
21  #include <aidl/android/hardware/tv/tuner/Constant.h>
22  #include <aidl/android/hardware/tv/tuner/Constant64Bit.h>
23  #include <android-base/logging.h>
24  #include <utils/Log.h>
25  
26  using ::aidl::android::hardware::tv::tuner::Constant;
27  using ::aidl::android::hardware::tv::tuner::Constant64Bit;
28  
29  namespace android {
30  /////////////// DemuxClient ///////////////////////
DemuxClient(shared_ptr<ITunerDemux> tunerDemux)31  DemuxClient::DemuxClient(shared_ptr<ITunerDemux> tunerDemux) {
32      mTunerDemux = tunerDemux;
33  }
34  
~DemuxClient()35  DemuxClient::~DemuxClient() {
36      mTunerDemux = nullptr;
37  }
38  
setFrontendDataSource(sp<FrontendClient> frontendClient)39  Result DemuxClient::setFrontendDataSource(sp<FrontendClient> frontendClient) {
40      if (frontendClient == nullptr) {
41          return Result::INVALID_ARGUMENT;
42      }
43  
44      if (mTunerDemux != nullptr) {
45          Status s = mTunerDemux->setFrontendDataSource(frontendClient->getAidlFrontend());
46          return ClientHelper::getServiceSpecificErrorCode(s);
47      }
48  
49      return Result::INVALID_STATE;
50  }
51  
setFrontendDataSourceById(int frontendId)52  Result DemuxClient::setFrontendDataSourceById(int frontendId) {
53      if (mTunerDemux != nullptr) {
54          Status s = mTunerDemux->setFrontendDataSourceById(frontendId);
55          return ClientHelper::getServiceSpecificErrorCode(s);
56      }
57  
58      return Result::INVALID_STATE;
59  }
60  
openFilter(const DemuxFilterType & type,int32_t bufferSize,sp<FilterClientCallback> cb)61  sp<FilterClient> DemuxClient::openFilter(const DemuxFilterType& type, int32_t bufferSize,
62                                           sp<FilterClientCallback> cb) {
63      if (cb == nullptr) {
64          return nullptr;
65      }
66  
67      if (mTunerDemux != nullptr) {
68          shared_ptr<ITunerFilter> tunerFilter;
69          shared_ptr<TunerFilterCallback> callback =
70                  ::ndk::SharedRefBase::make<TunerFilterCallback>(cb);
71          Status s = mTunerDemux->openFilter(type, bufferSize, callback, &tunerFilter);
72          if (!s.isOk()) {
73              return nullptr;
74          }
75          return new FilterClient(type, tunerFilter);
76      }
77  
78      return nullptr;
79  }
80  
openTimeFilter()81  sp<TimeFilterClient> DemuxClient::openTimeFilter() {
82      if (mTunerDemux != nullptr) {
83          shared_ptr<ITunerTimeFilter> tunerTimeFilter;
84          Status s = mTunerDemux->openTimeFilter(&tunerTimeFilter);
85          if (!s.isOk()) {
86              return nullptr;
87          }
88          return new TimeFilterClient(tunerTimeFilter);
89      }
90  
91      return nullptr;
92  }
93  
getAvSyncHwId(sp<FilterClient> filterClient)94  int32_t DemuxClient::getAvSyncHwId(sp<FilterClient> filterClient) {
95      if (filterClient == nullptr) {
96          return static_cast<int32_t>(Constant::INVALID_AV_SYNC_ID);
97      }
98  
99      if (mTunerDemux != nullptr) {
100          int32_t hwId;
101          Status s = mTunerDemux->getAvSyncHwId(filterClient->getAidlFilter(), &hwId);
102          if (!s.isOk()) {
103              return static_cast<int32_t>(Constant::INVALID_AV_SYNC_ID);
104          }
105          return hwId;
106      }
107  
108      return static_cast<int32_t>(Constant::INVALID_AV_SYNC_ID);
109  }
110  
getAvSyncTime(int32_t avSyncHwId)111  int64_t DemuxClient::getAvSyncTime(int32_t avSyncHwId) {
112      if (mTunerDemux != nullptr) {
113          int64_t time;
114          Status s = mTunerDemux->getAvSyncTime(avSyncHwId, &time);
115          if (!s.isOk()) {
116              return static_cast<int64_t>(Constant64Bit::INVALID_PRESENTATION_TIME_STAMP);
117          }
118          return time;
119      }
120  
121      return static_cast<int64_t>(Constant64Bit::INVALID_PRESENTATION_TIME_STAMP);
122  }
123  
openDvr(DvrType dvbType,int32_t bufferSize,sp<DvrClientCallback> cb)124  sp<DvrClient> DemuxClient::openDvr(DvrType dvbType, int32_t bufferSize, sp<DvrClientCallback> cb) {
125      if (cb == nullptr) {
126          return nullptr;
127      }
128  
129      if (mTunerDemux != nullptr) {
130          shared_ptr<ITunerDvr> tunerDvr;
131          shared_ptr<TunerDvrCallback> callback =
132                  ::ndk::SharedRefBase::make<TunerDvrCallback>(cb);
133          Status s = mTunerDemux->openDvr(dvbType, bufferSize, callback, &tunerDvr);
134          if (!s.isOk()) {
135              return nullptr;
136          }
137          return new DvrClient(tunerDvr);
138      }
139  
140      return nullptr;
141  }
142  
connectCiCam(int32_t ciCamId)143  Result DemuxClient::connectCiCam(int32_t ciCamId) {
144      if (mTunerDemux != nullptr) {
145          Status s = mTunerDemux->connectCiCam(ciCamId);
146          return ClientHelper::getServiceSpecificErrorCode(s);
147      }
148  
149      return Result::INVALID_STATE;
150  }
151  
disconnectCiCam()152  Result DemuxClient::disconnectCiCam() {
153      if (mTunerDemux != nullptr) {
154          Status s = mTunerDemux->disconnectCiCam();
155          return ClientHelper::getServiceSpecificErrorCode(s);
156      }
157  
158      return Result::INVALID_STATE;
159  }
160  
close()161  Result DemuxClient::close() {
162      if (mTunerDemux != nullptr) {
163          Status s = mTunerDemux->close();
164          mTunerDemux = nullptr;
165          return ClientHelper::getServiceSpecificErrorCode(s);
166      }
167  
168      return Result::INVALID_STATE;
169  }
170  
171  }  // namespace android
172