1 /*
2 * Copyright (C) 2016 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 "android.hardware.tv.input@1.0-service"
18 #include <android-base/logging.h>
19
20 #include "TvInput.h"
21
22 namespace android {
23 namespace hardware {
24 namespace tv {
25 namespace input {
26 namespace V1_0 {
27 namespace implementation {
28
29 static_assert(TV_INPUT_TYPE_OTHER_HARDWARE == static_cast<int>(TvInputType::OTHER),
30 "TvInputType::OTHER must match legacy value.");
31 static_assert(TV_INPUT_TYPE_TUNER == static_cast<int>(TvInputType::TUNER),
32 "TvInputType::TUNER must match legacy value.");
33 static_assert(TV_INPUT_TYPE_COMPOSITE == static_cast<int>(TvInputType::COMPOSITE),
34 "TvInputType::COMPOSITE must match legacy value.");
35 static_assert(TV_INPUT_TYPE_SVIDEO == static_cast<int>(TvInputType::SVIDEO),
36 "TvInputType::SVIDEO must match legacy value.");
37 static_assert(TV_INPUT_TYPE_SCART == static_cast<int>(TvInputType::SCART),
38 "TvInputType::SCART must match legacy value.");
39 static_assert(TV_INPUT_TYPE_COMPONENT == static_cast<int>(TvInputType::COMPONENT),
40 "TvInputType::COMPONENT must match legacy value.");
41 static_assert(TV_INPUT_TYPE_VGA == static_cast<int>(TvInputType::VGA),
42 "TvInputType::VGA must match legacy value.");
43 static_assert(TV_INPUT_TYPE_DVI == static_cast<int>(TvInputType::DVI),
44 "TvInputType::DVI must match legacy value.");
45 static_assert(TV_INPUT_TYPE_HDMI == static_cast<int>(TvInputType::HDMI),
46 "TvInputType::HDMI must match legacy value.");
47 static_assert(TV_INPUT_TYPE_DISPLAY_PORT == static_cast<int>(TvInputType::DISPLAY_PORT),
48 "TvInputType::DISPLAY_PORT must match legacy value.");
49
50 static_assert(TV_INPUT_EVENT_DEVICE_AVAILABLE == static_cast<int>(
51 TvInputEventType::DEVICE_AVAILABLE),
52 "TvInputEventType::DEVICE_AVAILABLE must match legacy value.");
53 static_assert(TV_INPUT_EVENT_DEVICE_UNAVAILABLE == static_cast<int>(
54 TvInputEventType::DEVICE_UNAVAILABLE),
55 "TvInputEventType::DEVICE_UNAVAILABLE must match legacy value.");
56 static_assert(TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED == static_cast<int>(
57 TvInputEventType::STREAM_CONFIGURATIONS_CHANGED),
58 "TvInputEventType::STREAM_CONFIGURATIONS_CHANGED must match legacy value.");
59
60 sp<ITvInputCallback> TvInput::mCallback = nullptr;
61
TvInput(tv_input_device_t * device)62 TvInput::TvInput(tv_input_device_t* device) : mDevice(device) {
63 mCallbackOps.notify = &TvInput::notify;
64 }
65
~TvInput()66 TvInput::~TvInput() {
67 if (mDevice != nullptr) {
68 free(mDevice);
69 }
70 }
71
72 // Methods from ::android::hardware::tv_input::V1_0::ITvInput follow.
setCallback(const sp<ITvInputCallback> & callback)73 Return<void> TvInput::setCallback(const sp<ITvInputCallback>& callback) {
74 mCallback = callback;
75 if (mCallback != nullptr) {
76 mDevice->initialize(mDevice, &mCallbackOps, nullptr);
77 }
78 return Void();
79 }
80
getStreamConfigurations(int32_t deviceId,getStreamConfigurations_cb cb)81 Return<void> TvInput::getStreamConfigurations(int32_t deviceId, getStreamConfigurations_cb cb) {
82 int32_t configCount = 0;
83 const tv_stream_config_t* configs = nullptr;
84 int ret = mDevice->get_stream_configurations(mDevice, deviceId, &configCount, &configs);
85 Result res = Result::UNKNOWN;
86 hidl_vec<TvStreamConfig> tvStreamConfigs;
87 if (ret == 0) {
88 res = Result::OK;
89 tvStreamConfigs.resize(getSupportedConfigCount(configCount, configs));
90 int32_t pos = 0;
91 for (int32_t i = 0; i < configCount; ++i) {
92 if (isSupportedStreamType(configs[i].type)) {
93 tvStreamConfigs[pos].streamId = configs[i].stream_id;
94 tvStreamConfigs[pos].maxVideoWidth = configs[i].max_video_width;
95 tvStreamConfigs[pos].maxVideoHeight = configs[i].max_video_height;
96 ++pos;
97 }
98 }
99 } else if (ret == -EINVAL) {
100 res = Result::INVALID_ARGUMENTS;
101 }
102 cb(res, tvStreamConfigs);
103 return Void();
104 }
105
openStream(int32_t deviceId,int32_t streamId,openStream_cb cb)106 Return<void> TvInput::openStream(int32_t deviceId, int32_t streamId, openStream_cb cb) {
107 tv_stream_t stream;
108 stream.stream_id = streamId;
109 int ret = mDevice->open_stream(mDevice, deviceId, &stream);
110 Result res = Result::UNKNOWN;
111 native_handle_t* sidebandStream = nullptr;
112 if (ret == 0) {
113 if (isSupportedStreamType(stream.type)) {
114 res = Result::OK;
115 sidebandStream = stream.sideband_stream_source_handle;
116 }
117 } else {
118 if (ret == -EBUSY) {
119 res = Result::NO_RESOURCE;
120 } else if (ret == -EEXIST) {
121 res = Result::INVALID_STATE;
122 } else if (ret == -EINVAL) {
123 res = Result::INVALID_ARGUMENTS;
124 }
125 }
126 cb(res, sidebandStream);
127 return Void();
128 }
129
closeStream(int32_t deviceId,int32_t streamId)130 Return<Result> TvInput::closeStream(int32_t deviceId, int32_t streamId) {
131 int ret = mDevice->close_stream(mDevice, deviceId, streamId);
132 Result res = Result::UNKNOWN;
133 if (ret == 0) {
134 res = Result::OK;
135 } else if (ret == -ENOENT) {
136 res = Result::INVALID_STATE;
137 } else if (ret == -EINVAL) {
138 res = Result::INVALID_ARGUMENTS;
139 }
140 return res;
141 }
142
143 // static
notify(struct tv_input_device * __unused,tv_input_event_t * event,void * optionalStatus)144 void TvInput::notify(struct tv_input_device* __unused, tv_input_event_t* event,
145 void* optionalStatus) {
146 if (mCallback != nullptr && event != nullptr) {
147 // Capturing is no longer supported.
148 if (event->type >= TV_INPUT_EVENT_CAPTURE_SUCCEEDED) {
149 return;
150 }
151 TvInputEvent tvInputEvent;
152 tvInputEvent.type = static_cast<TvInputEventType>(event->type);
153 tvInputEvent.deviceInfo.deviceId = event->device_info.device_id;
154 tvInputEvent.deviceInfo.type = static_cast<TvInputType>(
155 event->device_info.type);
156 tvInputEvent.deviceInfo.portId = event->device_info.hdmi.port_id;
157 CableConnectionStatus connectionStatus = CableConnectionStatus::UNKNOWN;
158 if (optionalStatus != nullptr &&
159 ((event->type == TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED) ||
160 (event->type == TV_INPUT_EVENT_DEVICE_AVAILABLE))) {
161 int newStatus = *reinterpret_cast<int*>(optionalStatus);
162 if (newStatus <= static_cast<int>(CableConnectionStatus::DISCONNECTED) &&
163 newStatus >= static_cast<int>(CableConnectionStatus::UNKNOWN)) {
164 connectionStatus = static_cast<CableConnectionStatus>(newStatus);
165 }
166 }
167 tvInputEvent.deviceInfo.cableConnectionStatus = connectionStatus;
168 // TODO: Ensure the legacy audio type code is the same once audio HAL default
169 // implementation is ready.
170 tvInputEvent.deviceInfo.audioType = static_cast<AudioDevice>(
171 event->device_info.audio_type);
172 memset(tvInputEvent.deviceInfo.audioAddress.data(), 0,
173 tvInputEvent.deviceInfo.audioAddress.size());
174 const char* address = event->device_info.audio_address;
175 if (address != nullptr) {
176 size_t size = strlen(address);
177 if (size > tvInputEvent.deviceInfo.audioAddress.size()) {
178 LOG(ERROR) << "Audio address is too long. Address:" << address << "";
179 return;
180 }
181 for (size_t i = 0; i < size; ++i) {
182 tvInputEvent.deviceInfo.audioAddress[i] =
183 static_cast<uint8_t>(event->device_info.audio_address[i]);
184 }
185 }
186 mCallback->notify(tvInputEvent);
187 }
188 }
189
190 // static
getSupportedConfigCount(uint32_t configCount,const tv_stream_config_t * configs)191 uint32_t TvInput::getSupportedConfigCount(uint32_t configCount,
192 const tv_stream_config_t* configs) {
193 uint32_t supportedConfigCount = 0;
194 for (uint32_t i = 0; i < configCount; ++i) {
195 if (isSupportedStreamType(configs[i].type)) {
196 supportedConfigCount++;
197 }
198 }
199 return supportedConfigCount;
200 }
201
202 // static
isSupportedStreamType(int type)203 bool TvInput::isSupportedStreamType(int type) {
204 // Buffer producer type is no longer supported.
205 return type != TV_STREAM_TYPE_BUFFER_PRODUCER;
206 }
207
HIDL_FETCH_ITvInput(const char *)208 ITvInput* HIDL_FETCH_ITvInput(const char* /* name */) {
209 int ret = 0;
210 const hw_module_t* hw_module = nullptr;
211 tv_input_device_t* input_device;
212 ret = hw_get_module(TV_INPUT_HARDWARE_MODULE_ID, &hw_module);
213 if (ret == 0 && hw_module->methods->open != nullptr) {
214 ret = hw_module->methods->open(hw_module, TV_INPUT_DEFAULT_DEVICE,
215 reinterpret_cast<hw_device_t**>(&input_device));
216 if (ret == 0) {
217 return new TvInput(input_device);
218 }
219 else {
220 LOG(ERROR) << "Passthrough failed to load legacy HAL.";
221 return nullptr;
222 }
223 }
224 else {
225 LOG(ERROR) << "hw_get_module " << TV_INPUT_HARDWARE_MODULE_ID
226 << " failed: " << ret;
227 return nullptr;
228 }
229 }
230
231 } // namespace implementation
232 } // namespace V1_0
233 } // namespace input
234 } // namespace tv
235 } // namespace hardware
236 } // namespace android
237