blob: 255bef70ee4d044765f6b1dbac51785752b8e96d [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
68::ndk::ScopedAStatus TvInput::getStreamConfigurations(int32_t in_deviceId,
69 vector<TvStreamConfig>* _aidl_return) {
70 ALOGV("%s", __FUNCTION__);
71
72 if (mStreamConfigs.count(in_deviceId) == 0) {
73 ALOGW("Device with id %d isn't available", in_deviceId);
74 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
75 }
76
77 for (auto const& iconfig : mStreamConfigs[in_deviceId]) {
78 _aidl_return->push_back(iconfig.second->streamConfig);
79 }
80
81 return ::ndk::ScopedAStatus::ok();
82}
83
84::ndk::ScopedAStatus TvInput::openStream(int32_t in_deviceId, int32_t in_streamId,
85 NativeHandle* _aidl_return) {
86 ALOGV("%s", __FUNCTION__);
87
88 if (mStreamConfigs.count(in_deviceId) == 0 ||
89 mStreamConfigs[in_deviceId].count(in_streamId) == 0) {
90 ALOGW("Stream with device id %d, stream id %d isn't available", in_deviceId, in_streamId);
91 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
92 }
93 if (mStreamConfigs[in_deviceId][in_streamId]->isOpen) {
94 ALOGW("Stream with device id %d, stream id %d is already opened", in_deviceId, in_streamId);
95 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_STATE);
96 }
97 mStreamConfigs[in_deviceId][in_streamId]->handle = createNativeHandle(in_streamId);
Yixiao Luoe1a73052022-10-18 16:40:14 -070098 *_aidl_return = makeToAidl(mStreamConfigs[in_deviceId][in_streamId]->handle);
Yixiao Luo4cd52a92022-10-28 15:51:48 -070099 mStreamConfigs[in_deviceId][in_streamId]->isOpen = true;
Yixiao Luoaaa52302022-08-11 18:44:50 -0700100 return ::ndk::ScopedAStatus::ok();
101}
102
103::ndk::ScopedAStatus TvInput::closeStream(int32_t in_deviceId, int32_t in_streamId) {
104 ALOGV("%s", __FUNCTION__);
105
106 if (mStreamConfigs.count(in_deviceId) == 0 ||
107 mStreamConfigs[in_deviceId].count(in_streamId) == 0) {
108 ALOGW("Stream with device id %d, stream id %d isn't available", in_deviceId, in_streamId);
109 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_ARGUMENTS);
110 }
111 if (!mStreamConfigs[in_deviceId][in_streamId]->isOpen) {
112 ALOGW("Stream with device id %d, stream id %d is already closed", in_deviceId, in_streamId);
113 return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_STATE);
114 }
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700115 native_handle_delete(mStreamConfigs[in_deviceId][in_streamId]->handle);
Yixiao Luoaaa52302022-08-11 18:44:50 -0700116 mStreamConfigs[in_deviceId][in_streamId]->handle = nullptr;
117 mStreamConfigs[in_deviceId][in_streamId]->isOpen = false;
118 return ::ndk::ScopedAStatus::ok();
119}
120
121native_handle_t* TvInput::createNativeHandle(int fd) {
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700122 native_handle_t* handle = native_handle_create(1, 1);
123 if (handle == nullptr) {
Yixiao Luoaaa52302022-08-11 18:44:50 -0700124 ALOGE("[TVInput] Failed to create native_handle %d", errno);
125 return nullptr;
126 }
Yixiao Luo4cd52a92022-10-28 15:51:48 -0700127 handle->data[0] = dup(0);
128 handle->data[1] = fd;
129 return handle;
Yixiao Luoaaa52302022-08-11 18:44:50 -0700130}
131
132} // namespace input
133} // namespace tv
134} // namespace hardware
135} // namespace android
136} // namespace aidl