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