blob: 5f1f7a24dd443be0d8d63ab719e157aa889b6105 [file] [log] [blame]
Yixiao Luoaaa52302022-08-11 18:44:50 -07001/*
2 * Copyright 2022 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-service.example"
18
19#include <utils/Log.h>
20
21#include "TvInput.h"
22
23namespace aidl {
24namespace android {
25namespace hardware {
26namespace tv {
27namespace input {
28
29TvInput::TvInput() {}
30
31void TvInput::init() {
32 // Set up TvInputDeviceInfo and TvStreamConfig
33 mDeviceInfos[0] = shared_ptr<TvInputDeviceInfoWrapper>(
34 new TvInputDeviceInfoWrapper(0, TvInputType::TUNER, true));
35 mDeviceInfos[1] = shared_ptr<TvInputDeviceInfoWrapper>(
36 new TvInputDeviceInfoWrapper(1, TvInputType::HDMI, true));
37 mDeviceInfos[3] = shared_ptr<TvInputDeviceInfoWrapper>(
38 new TvInputDeviceInfoWrapper(3, TvInputType::DISPLAY_PORT, true));
39
40 mStreamConfigs[0] = {
41 {1, shared_ptr<TvStreamConfigWrapper>(new TvStreamConfigWrapper(1, 720, 1080, false))}};
42 mStreamConfigs[1] = {{11, shared_ptr<TvStreamConfigWrapper>(
43 new TvStreamConfigWrapper(11, 360, 480, false))}};
44 mStreamConfigs[3] = {{5, shared_ptr<TvStreamConfigWrapper>(
45 new TvStreamConfigWrapper(5, 1080, 1920, false))}};
46}
47
48::ndk::ScopedAStatus TvInput::setCallback(const shared_ptr<ITvInputCallback>& in_callback) {
49 ALOGV("%s", __FUNCTION__);
50
51 mCallback = in_callback;
52
53 TvInputEvent event;
54 event.type = TvInputEventType::DEVICE_AVAILABLE;
55
56 event.deviceInfo = mDeviceInfos[0]->deviceInfo;
57 mCallback->notify(event);
58
59 event.deviceInfo = mDeviceInfos[1]->deviceInfo;
60 mCallback->notify(event);
61
62 event.deviceInfo = mDeviceInfos[3]->deviceInfo;
63 mCallback->notify(event);
64
65 return ::ndk::ScopedAStatus::ok();
66}
67
David Zhao6bdbc5e2023-01-12 16:51:03 -080068::ndk::ScopedAStatus TvInput::setTvMessageEnabled(int32_t deviceId, int32_t streamId,
69 TvMessageEventType in_type, bool enabled) {
70 ALOGV("%s", __FUNCTION__);
David Zhaoeb955ce2023-05-16 17:51:15 -070071
72 if (mStreamConfigs.count(deviceId) == 0) {
73 ALOGW("Device with id %d isn't available", deviceId);
74 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
75 }
76
77 mTvMessageEventEnabled[deviceId][streamId][in_type] = enabled;
David Zhao6bdbc5e2023-01-12 16:51:03 -080078 return ::ndk::ScopedAStatus::ok();
79}
80
81::ndk::ScopedAStatus TvInput::getTvMessageQueueDesc(
82 MQDescriptor<int8_t, SynchronizedReadWrite>* out_queue, int32_t in_deviceId,
83 int32_t in_streamId) {
84 ALOGV("%s", __FUNCTION__);
85 // TODO: Implement this
86 return ::ndk::ScopedAStatus::ok();
87}
88
Yixiao Luoaaa52302022-08-11 18:44:50 -070089::ndk::ScopedAStatus TvInput::getStreamConfigurations(int32_t in_deviceId,
90 vector<TvStreamConfig>* _aidl_return) {
91 ALOGV("%s", __FUNCTION__);
92
93 if (mStreamConfigs.count(in_deviceId) == 0) {
94 ALOGW("Device with id %d isn't available", in_deviceId);
95 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
96 }
97
98 for (auto const& iconfig : mStreamConfigs[in_deviceId]) {
99 _aidl_return->push_back(iconfig.second->streamConfig);
100 }
101
102 return ::ndk::ScopedAStatus::ok();
103}
104
105::ndk::ScopedAStatus TvInput::openStream(int32_t in_deviceId, int32_t in_streamId,
106 NativeHandle* _aidl_return) {
107 ALOGV("%s", __FUNCTION__);
108
109 if (mStreamConfigs.count(in_deviceId) == 0 ||
110 mStreamConfigs[in_deviceId].count(in_streamId) == 0) {
111 ALOGW("Stream with device id %d, stream id %d isn't available", in_deviceId, in_streamId);
112 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
113 }
114 if (mStreamConfigs[in_deviceId][in_streamId]->isOpen) {
115 ALOGW("Stream with device id %d, stream id %d is already opened", in_deviceId, in_streamId);
116 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_STATE);
117 }
118 mStreamConfigs[in_deviceId][in_streamId]->handle = createNativeHandle(in_streamId);
Yixiao Luoe1a73052022-10-18 16:40:14 -0700119 *_aidl_return = makeToAidl(mStreamConfigs[in_deviceId][in_streamId]->handle);
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700120 mStreamConfigs[in_deviceId][in_streamId]->isOpen = true;
Yixiao Luoaaa52302022-08-11 18:44:50 -0700121 return ::ndk::ScopedAStatus::ok();
122}
123
124::ndk::ScopedAStatus TvInput::closeStream(int32_t in_deviceId, int32_t in_streamId) {
125 ALOGV("%s", __FUNCTION__);
126
127 if (mStreamConfigs.count(in_deviceId) == 0 ||
128 mStreamConfigs[in_deviceId].count(in_streamId) == 0) {
129 ALOGW("Stream with device id %d, stream id %d isn't available", in_deviceId, in_streamId);
130 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
131 }
132 if (!mStreamConfigs[in_deviceId][in_streamId]->isOpen) {
133 ALOGW("Stream with device id %d, stream id %d is already closed", in_deviceId, in_streamId);
134 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_STATE);
135 }
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700136 native_handle_delete(mStreamConfigs[in_deviceId][in_streamId]->handle);
Yixiao Luoaaa52302022-08-11 18:44:50 -0700137 mStreamConfigs[in_deviceId][in_streamId]->handle = nullptr;
138 mStreamConfigs[in_deviceId][in_streamId]->isOpen = false;
139 return ::ndk::ScopedAStatus::ok();
140}
141
142native_handle_t* TvInput::createNativeHandle(int fd) {
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700143 native_handle_t* handle = native_handle_create(1, 1);
144 if (handle == nullptr) {
Yixiao Luoaaa52302022-08-11 18:44:50 -0700145 ALOGE("[TVInput] Failed to create native_handle %d", errno);
146 return nullptr;
147 }
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700148 handle->data[0] = dup(0);
149 handle->data[1] = fd;
150 return handle;
Yixiao Luoaaa52302022-08-11 18:44:50 -0700151}
152
153} // namespace input
154} // namespace tv
155} // namespace hardware
156} // namespace android
157} // namespace aidl