Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "tango_api.h" | |
| 6 | |
| 7 #include <dlfcn.h> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace media { | |
| 12 | |
| 13 // static | |
| 14 std::unique_ptr<TangoApi> TangoApi::loadAndCreate() { | |
|
aleksandar.stojiljkovic
2017/07/24 08:16:58
Note that WebAR code (I copied the native Tango in
| |
| 15 void* lib_ = dlopen( | |
| 16 "/data/data/com.google.tango/libfiles/armeabi-v7a/" | |
| 17 "libtango_client_api.so", | |
| 18 RTLD_NOW); | |
| 19 if (!lib_) { | |
| 20 LOG(ERROR) << "Could not dlopen(\"libtango_client_api\"):" << dlerror(); | |
| 21 return nullptr; | |
| 22 } | |
| 23 return std::unique_ptr<TangoApi>(new TangoApi(lib_)); | |
| 24 } | |
| 25 | |
| 26 TangoApi::TangoApi(void* lib_client_api) : lib_(lib_client_api) {} | |
| 27 | |
| 28 // An example of expanded code for TANGO_METHOD: | |
| 29 // TANGO_METHOD(TangoErrorType, TangoService_setBinder, TANGO_ERROR, | |
| 30 // void* jni_env, void* iBinder) | |
| 31 // expands to: | |
| 32 // TangoErrorType TangoService_setBinder(void* jni_env, void* iBinder) { | |
| 33 // using TangoService_setBinderPtr = TangoErrorType(*)(void* jni_env, | |
| 34 // void* iBinder); | |
| 35 // static TangoService_setBinderPtr func = | |
| 36 // reinterpret_cast<TangoService_setBinderPtr>(dlsym(lib_, __func__)); | |
| 37 // if (!func) { | |
| 38 // LOG(ERROR) << "dlsym failed on"<< __PRETTY_FUNCTION__ << dlerror(); | |
| 39 // return TANGO_ERROR; | |
| 40 // } | |
| 41 #define TANGO_METHOD(ret, name, return_on_error, params, ...) \ | |
| 42 ret TangoApi::name(__VA_ARGS__) { \ | |
| 43 using name##Ptr = ret (*)(__VA_ARGS__); \ | |
| 44 static name##Ptr func = \ | |
| 45 reinterpret_cast<name##Ptr>(dlsym(lib_, __func__)); \ | |
| 46 if (!func) { \ | |
| 47 LOG(ERROR) << "dlsym failed on" << __func__ << dlerror(); \ | |
| 48 return return_on_error; \ | |
| 49 } \ | |
| 50 return params; \ | |
| 51 } | |
| 52 | |
| 53 TANGO_METHOD(TangoApi::TangoErrorType, | |
| 54 TangoService_setBinder, | |
| 55 TANGO_METHOD_NOT_FOUND, | |
| 56 func(jni_env, iBinder), | |
| 57 void* jni_env, | |
| 58 void* iBinder) | |
| 59 | |
| 60 TANGO_METHOD(TangoApi::TangoConfig, | |
| 61 TangoService_getConfig, | |
| 62 nullptr, | |
| 63 func(config_type), | |
| 64 TangoConfigType config_type) | |
| 65 | |
| 66 TANGO_METHOD(TangoApi::TangoErrorType, | |
| 67 TangoConfig_setBool, | |
| 68 TANGO_METHOD_NOT_FOUND, | |
| 69 func(config, key, value), | |
| 70 TangoConfig config, | |
| 71 const char* key, | |
| 72 bool value) | |
| 73 | |
| 74 TANGO_METHOD(TangoApi::TangoErrorType, | |
| 75 TangoConfig_setInt32, | |
| 76 TANGO_METHOD_NOT_FOUND, | |
| 77 func(config, key, value), | |
| 78 TangoConfig config, | |
| 79 const char* key, | |
| 80 int32_t value) | |
| 81 | |
| 82 TANGO_METHOD(TangoApi::TangoErrorType, | |
| 83 TangoService_connect, | |
| 84 TANGO_METHOD_NOT_FOUND, | |
| 85 func(context, config), | |
| 86 void* context, | |
| 87 TangoConfig config) | |
| 88 | |
| 89 TANGO_METHOD(TangoApi::TangoErrorType, | |
| 90 TangoService_getCameraIntrinsics, | |
| 91 TANGO_METHOD_NOT_FOUND, | |
| 92 func(camera_id, intrinsics), | |
| 93 TangoCameraId camera_id, | |
| 94 TangoCameraIntrinsics* intrinsics) | |
| 95 | |
| 96 TANGO_METHOD(void, TangoService_disconnect, void(), func()) | |
| 97 | |
| 98 TangoApi::TangoErrorType TangoApi::TangoService_connectOnPointCloudAvailable( | |
| 99 void (*TangoService_onPointCloudAvailable)(void* context, | |
| 100 const TangoPointCloud* cloud), | |
| 101 ...) { | |
| 102 using TangoService_connectOnPointCloudAvailablePtr = | |
| 103 TangoApi::TangoErrorType (*)( | |
| 104 void (*TangoService_onPointCloudAvailable)( | |
| 105 void* context, const TangoPointCloud* cloud), | |
| 106 ...); | |
| 107 static TangoService_connectOnPointCloudAvailablePtr func = | |
| 108 reinterpret_cast<TangoService_connectOnPointCloudAvailablePtr>( | |
| 109 dlsym(lib_, __func__)); | |
| 110 if (!func) { | |
| 111 LOG(ERROR) << "dlsym failed on" << __PRETTY_FUNCTION__ << dlerror(); | |
| 112 return TANGO_METHOD_NOT_FOUND; | |
| 113 } | |
| 114 va_list args; | |
| 115 va_start(args, TangoService_onPointCloudAvailable); | |
| 116 TangoApi::TangoErrorType result = | |
| 117 func(TangoService_onPointCloudAvailable, args); | |
| 118 va_end(args); | |
| 119 return result; | |
| 120 } | |
| 121 | |
| 122 } // namespace media | |
| OLD | NEW |