blob: 47f8be5e398c3eee9716a7bedff5d23f0c9284ed [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__);
71 // TODO: Implement this
72 return ::ndk::ScopedAStatus::ok();
73}
74
75::ndk::ScopedAStatus TvInput::getTvMessageQueueDesc(
76 MQDescriptor<int8_t, SynchronizedReadWrite>* out_queue, int32_t in_deviceId,
77 int32_t in_streamId) {
78 ALOGV("%s", __FUNCTION__);
79 // TODO: Implement this
80 return ::ndk::ScopedAStatus::ok();
81}
82
Yixiao Luoaaa52302022-08-11 18:44:50 -070083::ndk::ScopedAStatus TvInput::getStreamConfigurations(int32_t in_deviceId,
84 vector<TvStreamConfig>* _aidl_return) {
85 ALOGV("%s", __FUNCTION__);
86
87 if (mStreamConfigs.count(in_deviceId) == 0) {
88 ALOGW("Device with id %d isn't available", in_deviceId);
89 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
90 }
91
92 for (auto const& iconfig : mStreamConfigs[in_deviceId]) {
93 _aidl_return->push_back(iconfig.second->streamConfig);
94 }
95
96 return ::ndk::ScopedAStatus::ok();
97}
98
99::ndk::ScopedAStatus TvInput::openStream(int32_t in_deviceId, int32_t in_streamId,
100 NativeHandle* _aidl_return) {
101 ALOGV("%s", __FUNCTION__);
102
103 if (mStreamConfigs.count(in_deviceId) == 0 ||
104 mStreamConfigs[in_deviceId].count(in_streamId) == 0) {
105 ALOGW("Stream with device id %d, stream id %d isn't available", in_deviceId, in_streamId);
106 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
107 }
108 if (mStreamConfigs[in_deviceId][in_streamId]->isOpen) {
109 ALOGW("Stream with device id %d, stream id %d is already opened", in_deviceId, in_streamId);
110 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_STATE);
111 }
112 mStreamConfigs[in_deviceId][in_streamId]->handle = createNativeHandle(in_streamId);
113 mStreamConfigs[in_deviceId][in_streamId]->isOpen = true;
Yixiao Luoe1a73052022-10-18 16:40:14 -0700114 *_aidl_return = makeToAidl(mStreamConfigs[in_deviceId][in_streamId]->handle);
Yixiao Luoaaa52302022-08-11 18:44:50 -0700115 return ::ndk::ScopedAStatus::ok();
116}
117
118::ndk::ScopedAStatus TvInput::closeStream(int32_t in_deviceId, int32_t in_streamId) {
119 ALOGV("%s", __FUNCTION__);
120
121 if (mStreamConfigs.count(in_deviceId) == 0 ||
122 mStreamConfigs[in_deviceId].count(in_streamId) == 0) {
123 ALOGW("Stream with device id %d, stream id %d isn't available", in_deviceId, in_streamId);
124 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
125 }
126 if (!mStreamConfigs[in_deviceId][in_streamId]->isOpen) {
127 ALOGW("Stream with device id %d, stream id %d is already closed", in_deviceId, in_streamId);
128 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_STATE);
129 }
130 releaseNativeHandle(mStreamConfigs[in_deviceId][in_streamId]->handle);
131 mStreamConfigs[in_deviceId][in_streamId]->handle = nullptr;
132 mStreamConfigs[in_deviceId][in_streamId]->isOpen = false;
133 return ::ndk::ScopedAStatus::ok();
134}
135
136native_handle_t* TvInput::createNativeHandle(int fd) {
137 native_handle_t* nativeHandle = native_handle_create(1, 0);
138 if (nativeHandle == nullptr) {
139 ALOGE("[TVInput] Failed to create native_handle %d", errno);
140 return nullptr;
141 }
142 if (nativeHandle->numFds > 0) {
143 nativeHandle->data[0] = dup(fd);
144 }
145 return nativeHandle;
146}
147
148void TvInput::releaseNativeHandle(native_handle_t* handle) {
149 native_handle_close(handle);
150 native_handle_delete(handle);
151}
152
153} // namespace input
154} // namespace tv
155} // namespace hardware
156} // namespace android
157} // namespace aidl