blob: 6fcb2e5c0a89ce07af131f9d03efd9f5cc6bad8c [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 }
Shubang8324f702017-01-30 10:37:59 -0800101 } else if (ret == -EINVAL) {
102 res = Result::INVALID_ARGUMENTS;
Dongwon Kang74088492016-10-13 16:17:01 -0700103 }
104 cb(res, tvStreamConfigs);
105 return Void();
106}
107
108Return<void> TvInput::openStream(int32_t deviceId, int32_t streamId, openStream_cb cb) {
109 tv_stream_t stream;
110 stream.stream_id = streamId;
111 int ret = mDevice->open_stream(mDevice, deviceId, &stream);
112 Result res = Result::UNKNOWN;
113 native_handle_t* sidebandStream = nullptr;
114 if (ret == 0) {
115 if (isSupportedStreamType(stream.type)) {
116 res = Result::OK;
117 sidebandStream = stream.sideband_stream_source_handle;
118 }
119 } else {
120 // TODO(b/30814137)
121 sidebandStream = const_cast<native_handle_t*>(&kNullNativeHandle);
122 if (ret == -EBUSY) {
123 res = Result::NO_RESOURCE;
124 } else if (ret == -EEXIST) {
125 res = Result::INVALID_STATE;
126 } else if (ret == -EINVAL) {
127 res = Result::INVALID_ARGUMENTS;
128 }
129 }
130 cb(res, sidebandStream);
131 return Void();
132}
133
134Return<Result> TvInput::closeStream(int32_t deviceId, int32_t streamId) {
135 int ret = mDevice->close_stream(mDevice, deviceId, streamId);
136 Result res = Result::UNKNOWN;
137 if (ret == 0) {
138 res = Result::OK;
139 } else if (ret == -ENOENT) {
140 res = Result::INVALID_STATE;
141 } else if (ret == -EINVAL) {
142 res = Result::INVALID_ARGUMENTS;
143 }
144 return res;
145}
146
147// static
148void TvInput::notify(struct tv_input_device* __unused, tv_input_event_t* event,
149 void* __unused) {
150 if (mCallback != nullptr && event != nullptr) {
151 // Capturing is no longer supported.
152 if (event->type >= TV_INPUT_EVENT_CAPTURE_SUCCEEDED) {
153 return;
154 }
155 TvInputEvent tvInputEvent;
156 tvInputEvent.type = static_cast<TvInputEventType>(event->type);
157 tvInputEvent.deviceInfo.deviceId = event->device_info.device_id;
158 tvInputEvent.deviceInfo.type = static_cast<TvInputType>(
159 event->device_info.type);
160 tvInputEvent.deviceInfo.portId = event->device_info.hdmi.port_id;
161 // TODO: Ensure the legacy audio type code is the same once audio HAL default
162 // implementation is ready.
163 tvInputEvent.deviceInfo.audioType = static_cast<AudioDevice>(
164 event->device_info.audio_type);
165 memset(tvInputEvent.deviceInfo.audioAddress.data(), 0,
166 tvInputEvent.deviceInfo.audioAddress.size());
167 const char* address = event->device_info.audio_address;
168 if (address != nullptr) {
169 size_t size = strlen(address);
170 if (size > tvInputEvent.deviceInfo.audioAddress.size()) {
171 LOG(ERROR) << "Audio address is too long. Address:" << address << "";
172 return;
173 }
174 for (size_t i = 0; i < size; ++i) {
175 tvInputEvent.deviceInfo.audioAddress[i] =
176 static_cast<uint8_t>(event->device_info.audio_address[i]);
177 }
178 }
179 mCallback->notify(tvInputEvent);
180 }
181}
182
183// static
184uint32_t TvInput::getSupportedConfigCount(uint32_t configCount,
185 const tv_stream_config_t* configs) {
186 uint32_t supportedConfigCount = 0;
187 for (uint32_t i = 0; i < configCount; ++i) {
188 if (isSupportedStreamType(configs[i].type)) {
189 supportedConfigCount++;
190 }
191 }
192 return supportedConfigCount;
193}
194
195// static
196bool TvInput::isSupportedStreamType(int type) {
197 // Buffer producer type is no longer supported.
198 return type != TV_STREAM_TYPE_BUFFER_PRODUCER;
199}
200
Chris Phoenix7b1af272017-01-18 18:53:15 -0800201ITvInput* HIDL_FETCH_ITvInput(const char* /* name */) {
Dongwon Kang74088492016-10-13 16:17:01 -0700202 int ret = 0;
203 const hw_module_t* hw_module = nullptr;
204 tv_input_device_t* input_device;
205 ret = hw_get_module(TV_INPUT_HARDWARE_MODULE_ID, &hw_module);
206 if (ret == 0 && hw_module->methods->open != nullptr) {
207 ret = hw_module->methods->open(hw_module, TV_INPUT_DEFAULT_DEVICE,
208 reinterpret_cast<hw_device_t**>(&input_device));
209 if (ret == 0) {
210 return new TvInput(input_device);
211 }
212 else {
213 LOG(ERROR) << "Passthrough failed to load legacy HAL.";
214 return nullptr;
215 }
216 }
217 else {
Chris Phoenix7b1af272017-01-18 18:53:15 -0800218 LOG(ERROR) << "hw_get_module " << TV_INPUT_HARDWARE_MODULE_ID
219 << " failed: " << ret;
Dongwon Kang74088492016-10-13 16:17:01 -0700220 return nullptr;
221 }
222}
223
224} // namespace implementation
225} // namespace V1_0
226} // namespace input
227} // namespace tv
228} // namespace hardware
229} // namespace android