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