1 /* 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 #pragma once 18 19 #define TV_INPUT_AIDL_SERVICE_NAME "android.hardware.tv.input.ITvInput/default" 20 21 #include <aidl/android/hardware/tv/input/BnTvInputCallback.h> 22 #include <aidl/android/hardware/tv/input/CableConnectionStatus.h> 23 #include <aidl/android/hardware/tv/input/ITvInput.h> 24 #include <aidl/android/media/audio/common/AudioDevice.h> 25 #include <aidlcommonsupport/NativeHandle.h> 26 #include <android/binder_manager.h> 27 #include <fmq/AidlMessageQueue.h> 28 #include <nativehelper/JNIHelp.h> 29 #include <utils/Errors.h> 30 #include <utils/KeyedVector.h> 31 #include <utils/Log.h> 32 #include <utils/Looper.h> 33 #include <utils/NativeHandle.h> 34 35 #include <iomanip> 36 37 #include "BufferProducerThread.h" 38 #include "TvInputHal_hidl.h" 39 #include "android_os_MessageQueue.h" 40 #include "android_runtime/AndroidRuntime.h" 41 #include "android_runtime/android_view_Surface.h" 42 #include "tvinput/jstruct.h" 43 44 using ::android::AidlMessageQueue; 45 46 using ::aidl::android::hardware::common::fmq::MQDescriptor; 47 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite; 48 using ::aidl::android::hardware::tv::input::BnTvInputCallback; 49 using ::aidl::android::hardware::tv::input::CableConnectionStatus; 50 using ::aidl::android::hardware::tv::input::TvInputEventType; 51 using ::aidl::android::hardware::tv::input::TvInputType; 52 using ::aidl::android::hardware::tv::input::TvMessageEvent; 53 using ::aidl::android::hardware::tv::input::TvMessageEventType; 54 55 using AidlAudioDevice = ::aidl::android::media::audio::common::AudioDevice; 56 using AidlAudioDeviceAddress = ::aidl::android::media::audio::common::AudioDeviceAddress; 57 using AidlAudioDeviceType = ::aidl::android::media::audio::common::AudioDeviceType; 58 using AidlITvInput = ::aidl::android::hardware::tv::input::ITvInput; 59 using AidlNativeHandle = ::aidl::android::hardware::common::NativeHandle; 60 using AidlTvInputDeviceInfo = ::aidl::android::hardware::tv::input::TvInputDeviceInfo; 61 using AidlTvInputEvent = ::aidl::android::hardware::tv::input::TvInputEvent; 62 using AidlTvMessage = ::aidl::android::hardware::tv::input::TvMessage; 63 using AidlTvMessageEvent = ::aidl::android::hardware::tv::input::TvMessageEvent; 64 using AidlTvMessageEventType = ::aidl::android::hardware::tv::input::TvMessageEventType; 65 using AidlTvStreamConfig = ::aidl::android::hardware::tv::input::TvStreamConfig; 66 67 using AidlMessageQueueMap = std::unordered_map< 68 int, 69 std::unordered_map<int, std::shared_ptr<AidlMessageQueue<int8_t, SynchronizedReadWrite>>>>; 70 71 extern gBundleClassInfoType gBundleClassInfo; 72 extern gTvInputHalClassInfoType gTvInputHalClassInfo; 73 extern gTvStreamConfigClassInfoType gTvStreamConfigClassInfo; 74 extern gTvStreamConfigBuilderClassInfoType gTvStreamConfigBuilderClassInfo; 75 extern gTvInputHardwareInfoBuilderClassInfoType gTvInputHardwareInfoBuilderClassInfo; 76 77 namespace android { 78 79 class JTvInputHal { 80 public: 81 ~JTvInputHal(); 82 83 static JTvInputHal* createInstance(JNIEnv* env, jobject thiz, const sp<Looper>& looper); 84 85 int addOrUpdateStream(int deviceId, int streamId, const sp<Surface>& surface); 86 int setTvMessageEnabled(int deviceId, int streamId, int type, bool enabled); 87 int removeStream(int deviceId, int streamId); 88 const std::vector<AidlTvStreamConfig> getStreamConfigs(int deviceId); 89 90 private: 91 // Connection between a surface and a stream. 92 class Connection { 93 public: Connection()94 Connection() {} 95 96 sp<Surface> mSurface; 97 tv_stream_type_t mStreamType; 98 99 // Only valid when mStreamType == TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE 100 sp<NativeHandle> mSourceHandle; 101 // Only valid when mStreamType == TV_STREAM_TYPE_BUFFER_PRODUCER 102 sp<BufferProducerThread> mThread; 103 }; 104 105 class TvInputDeviceInfoWrapper { 106 public: TvInputDeviceInfoWrapper()107 TvInputDeviceInfoWrapper() {} 108 109 static TvInputDeviceInfoWrapper createDeviceInfoWrapper( 110 const AidlTvInputDeviceInfo& aidlTvInputDeviceInfo); 111 static TvInputDeviceInfoWrapper createDeviceInfoWrapper( 112 const HidlTvInputDeviceInfo& hidlTvInputDeviceInfo); 113 114 bool isHidl; 115 int deviceId; 116 TvInputType type; 117 int portId; 118 CableConnectionStatus cableConnectionStatus; 119 AidlAudioDevice aidlAudioDevice; 120 HidlAudioDevice hidlAudioType; 121 ::android::hardware::hidl_array<uint8_t, 32> hidlAudioAddress; 122 }; 123 124 class TvInputEventWrapper { 125 public: TvInputEventWrapper()126 TvInputEventWrapper() {} 127 128 static TvInputEventWrapper createEventWrapper(const AidlTvInputEvent& aidlTvInputEvent); 129 static TvInputEventWrapper createEventWrapper(const HidlTvInputEvent& hidlTvInputEvent); 130 131 TvInputEventType type; 132 TvInputDeviceInfoWrapper deviceInfo; 133 }; 134 135 class TvMessageEventWrapper { 136 public: TvMessageEventWrapper()137 TvMessageEventWrapper() {} 138 139 static TvMessageEventWrapper createEventWrapper( 140 const AidlTvMessageEvent& aidlTvMessageEvent); 141 142 int streamId; 143 int deviceId; 144 std::vector<AidlTvMessage> messages; 145 AidlTvMessageEventType type; 146 }; 147 148 class NotifyHandler : public MessageHandler { 149 public: 150 NotifyHandler(JTvInputHal* hal, const TvInputEventWrapper& event); 151 152 void handleMessage(const Message& message) override; 153 154 private: 155 TvInputEventWrapper mEvent; 156 JTvInputHal* mHal; 157 }; 158 159 class NotifyTvMessageHandler : public MessageHandler { 160 public: 161 NotifyTvMessageHandler(JTvInputHal* hal, const TvMessageEventWrapper& event); 162 163 void handleMessage(const Message& message) override; 164 165 private: 166 TvMessageEventWrapper mEvent; 167 JTvInputHal* mHal; 168 }; 169 170 class TvInputCallback : public HidlITvInputCallback, public BnTvInputCallback { 171 public: 172 explicit TvInputCallback(JTvInputHal* hal); 173 ::ndk::ScopedAStatus notify(const AidlTvInputEvent& event) override; 174 ::ndk::ScopedAStatus notifyTvMessageEvent(const AidlTvMessageEvent& event) override; 175 Return<void> notify(const HidlTvInputEvent& event) override; 176 177 private: 178 JTvInputHal* mHal; 179 }; 180 181 class ITvInputWrapper { 182 public: 183 ITvInputWrapper(std::shared_ptr<AidlITvInput>& aidlTvInput); 184 ITvInputWrapper(sp<HidlITvInput>& hidlTvInput); 185 186 ::ndk::ScopedAStatus setCallback(const std::shared_ptr<TvInputCallback>& in_callback); 187 ::ndk::ScopedAStatus getStreamConfigurations(int32_t in_deviceId, 188 std::vector<AidlTvStreamConfig>* _aidl_return); 189 ::ndk::ScopedAStatus openStream(int32_t in_deviceId, int32_t in_streamId, 190 AidlNativeHandle* _aidl_return); 191 ::ndk::ScopedAStatus closeStream(int32_t in_deviceId, int32_t in_streamId); 192 ::ndk::ScopedAStatus setTvMessageEnabled(int32_t deviceId, int32_t streamId, 193 TvMessageEventType in_type, bool enabled); 194 ::ndk::ScopedAStatus getTvMessageQueueDesc( 195 MQDescriptor<int8_t, SynchronizedReadWrite>* out_queue, int32_t in_deviceId, 196 int32_t in_streamId); 197 198 private: 199 ::ndk::ScopedAStatus hidlSetCallback(const std::shared_ptr<TvInputCallback>& in_callback); 200 ::ndk::ScopedAStatus hidlGetStreamConfigurations( 201 int32_t in_deviceId, std::vector<AidlTvStreamConfig>* _aidl_return); 202 ::ndk::ScopedAStatus hidlOpenStream(int32_t in_deviceId, int32_t in_streamId, 203 AidlNativeHandle* _aidl_return); 204 ::ndk::ScopedAStatus hidlCloseStream(int32_t in_deviceId, int32_t in_streamId); 205 206 bool mIsHidl; 207 sp<HidlITvInput> mHidlTvInput; 208 std::shared_ptr<AidlITvInput> mAidlTvInput; 209 }; 210 211 JTvInputHal(JNIEnv* env, jobject thiz, std::shared_ptr<ITvInputWrapper> tvInput, 212 const sp<Looper>& looper); 213 214 void hidlSetUpAudioInfo(JNIEnv* env, jobject& builder, const TvInputDeviceInfoWrapper& info); 215 void onDeviceAvailable(const TvInputDeviceInfoWrapper& info); 216 void onDeviceUnavailable(int deviceId); 217 void onStreamConfigurationsChanged(int deviceId, int cableConnectionStatus); 218 void onCaptured(int deviceId, int streamId, uint32_t seq, bool succeeded); 219 void onTvMessage(int deviceId, int streamId, AidlTvMessageEventType type, 220 AidlTvMessage& message, signed char data[], int dataLength); 221 222 Mutex mLock; 223 Mutex mStreamLock; 224 jweak mThiz; 225 sp<Looper> mLooper; 226 AidlMessageQueueMap mQueueMap; 227 228 KeyedVector<int, KeyedVector<int, Connection> > mConnections; 229 230 std::shared_ptr<ITvInputWrapper> mTvInput; 231 std::shared_ptr<TvInputCallback> mTvInputCallback; 232 }; 233 234 } // namespace android 235