diff --git a/taoyao-client-openharmony/taoyao/AppScope/app.json5 b/taoyao-client-openharmony/taoyao/AppScope/app.json5 index 1c1cac2..8101b7c 100644 --- a/taoyao-client-openharmony/taoyao/AppScope/app.json5 +++ b/taoyao-client-openharmony/taoyao/AppScope/app.json5 @@ -1,10 +1,10 @@ { "app": { - "bundleName": "com.acgist.taoyao", + "icon" : "$media:app_icon", + "label" : "$string:app_name", "vendor": "acgist", + "bundleName" : "com.acgist.taoyao", "versionCode": 1000000, "versionName": "1.0.0", - "icon": "$media:app_icon", - "label": "$string:app_name" } } diff --git a/taoyao-client-openharmony/taoyao/AppScope/resources/base/element/string.json b/taoyao-client-openharmony/taoyao/AppScope/resources/base/element/string.json index d28be99..6b6c1b7 100644 --- a/taoyao-client-openharmony/taoyao/AppScope/resources/base/element/string.json +++ b/taoyao-client-openharmony/taoyao/AppScope/resources/base/element/string.json @@ -1,8 +1,8 @@ { "string": [ { - "name": "app_name", - "value": "taoyao" + "name" : "app_name", + "value": "桃夭" } ] } diff --git a/taoyao-client-openharmony/taoyao/AppScope/resources/base/media/app_icon.png b/taoyao-client-openharmony/taoyao/AppScope/resources/base/media/app_icon.png index ce307a8..002ccf8 100644 Binary files a/taoyao-client-openharmony/taoyao/AppScope/resources/base/media/app_icon.png and b/taoyao-client-openharmony/taoyao/AppScope/resources/base/media/app_icon.png differ diff --git a/taoyao-client-openharmony/taoyao/build-profile.json5 b/taoyao-client-openharmony/taoyao/build-profile.json5 index 77049ea..45df14a 100644 --- a/taoyao-client-openharmony/taoyao/build-profile.json5 +++ b/taoyao-client-openharmony/taoyao/build-profile.json5 @@ -13,13 +13,13 @@ "name": "default", "type": "HarmonyOS", "material": { - "certpath": "C:\\Users\\acgis\\.ohos\\config\\auto_debug_taoyao_com.acgist.taoyao_30086000001511570.cer", - "storePassword": "0000001B6B72744A79D44F625FF6D295CE824032D543A50BF93BD6985B89C7B657379C8735401C2A9700CC", + "signAlg" : "SHA256withECDSA", "keyAlias": "debugKey", - "keyPassword": "0000001B6120DA129145E921FE4068BC61E7D7B5F4BF9386D51C895374C65BF9FE5890AFCD0F992B21E509", - "profile": "C:\\Users\\acgis\\.ohos\\config\\auto_debug_taoyao_com.acgist.taoyao_30086000001511570.p7b", - "signAlg": "SHA256withECDSA", - "storeFile": "C:\\Users\\acgis\\.ohos\\config\\auto_debug_taoyao_com.acgist.taoyao_30086000001511570.p12" + "profile" : "C:\\Users\\acgis\\.ohos\\config\\auto_debug_taoyao_com.acgist.taoyao_30086000001511570.p7b", + "certpath" : "C:\\Users\\acgis\\.ohos\\config\\auto_debug_taoyao_com.acgist.taoyao_30086000001511570.cer", + "storeFile": "C:\\Users\\acgis\\.ohos\\config\\auto_debug_taoyao_com.acgist.taoyao_30086000001511570.p12", + "keyPassword" : "0000001B6120DA129145E921FE4068BC61E7D7B5F4BF9386D51C895374C65BF9FE5890AFCD0F992B21E509", + "storePassword": "0000001B6B72744A79D44F625FF6D295CE824032D543A50BF93BD6985B89C7B657379C8735401C2A9700CC", } } ], diff --git a/taoyao-client-openharmony/taoyao/media/build-profile.json5 b/taoyao-client-openharmony/taoyao/media/build-profile.json5 index e09100b..e59e271 100644 --- a/taoyao-client-openharmony/taoyao/media/build-profile.json5 +++ b/taoyao-client-openharmony/taoyao/media/build-profile.json5 @@ -1,11 +1,10 @@ { - "apiType": 'stageMode', + "apiType": "stageMode", "buildOption": { "externalNativeOptions": { "path": "./src/main/cpp/CMakeLists.txt", - "arguments": "", -// "cFlags": "-std=c17", - "cppFlags": "-std=c++17", + "cppFlags" : "-std=c++17", + "arguments" : "", "abiFilters": [ "arm64-v8a" ] } }, diff --git a/taoyao-client-openharmony/taoyao/media/oh-package.json5 b/taoyao-client-openharmony/taoyao/media/oh-package.json5 index 4037289..7fd3345 100644 --- a/taoyao-client-openharmony/taoyao/media/oh-package.json5 +++ b/taoyao-client-openharmony/taoyao/media/oh-package.json5 @@ -1,10 +1,10 @@ { - "name": "media", - "main": "", + "name" : "media", + "main" : "", "author": "acgist", - "description": "桃夭媒体终端", "license": "Apache-2.0", "version": "1.0.0", + "description" : "桃夭媒体终端", "dependencies": {}, "devDependencies": { "@types/libtaoyao.so": "file:./src/main/cpp/types/libtaoyao" diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/CMakeLists.txt b/taoyao-client-openharmony/taoyao/media/src/main/cpp/CMakeLists.txt index 7601413..f64ca95 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/CMakeLists.txt +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/CMakeLists.txt @@ -23,7 +23,16 @@ set(LIBWEBRTC_BINARY_PATH "${CMAKE_CURRENT_SOURCE_DIR}/deps/webrtc/lib/arm64-v8 add_subdirectory("./deps/libmediasoupclient") -add_library(${PROJECT_NAME} SHARED hello.cpp) +add_library( + ${PROJECT_NAME} SHARED + bind.cpp + media/Room.cpp + media/Client.cpp + media/LocalClient.cpp + media/RemoteClient.cpp + media/MediaManager.cpp + media/SessionClient.cpp +) target_include_directories( ${PROJECT_NAME} PUBLIC diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/bind.cpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/bind.cpp new file mode 100644 index 0000000..fb159b2 --- /dev/null +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/bind.cpp @@ -0,0 +1,86 @@ +/** + * 方法绑定 + */ + +#include +#include + +#include "hilog/log.h" +#include "napi/native_api.h" + +#include "./include/Room.hpp" +#include "./include/MediaManager.hpp" +#include "./include/SessionClient.hpp" + +namespace acgist { + +acgist::MediaManager* mediaManager; +std::map roomMap; +std::map sessionMap; + +static void init() { +// OH_LOG_INFO(LOG_APP, "加载bind"); +} + +static napi_value sessionCall(napi_env env, napi_callback_info info) { return 0; } +static napi_value sessionPause(napi_env env, napi_callback_info info) { return 0; } +static napi_value sessionResume(napi_env env, napi_callback_info info) { return 0; } +static napi_value sessionExchange(napi_env env, napi_callback_info info) { return 0; } +static napi_value sessionClose(napi_env env, napi_callback_info info) { return 0; } +static napi_value mediaConsume(napi_env env, napi_callback_info info) { return 0; } +static napi_value mediaConsumerClose(napi_env env, napi_callback_info info) { return 0; } +static napi_value mediaConsumerPause(napi_env env, napi_callback_info info) { return 0; } +static napi_value mediaConsumerResume(napi_env env, napi_callback_info info) { return 0; } +static napi_value mediaProducerClose(napi_env env, napi_callback_info info) { return 0; } +static napi_value mediaProducerPause(napi_env env, napi_callback_info info) { return 0; } +static napi_value mediaProducerResume(napi_env env, napi_callback_info info) { return 0; } +static napi_value roomClientList(napi_env env, napi_callback_info info) { return 0; } +static napi_value roomClose(napi_env env, napi_callback_info info) { return 0; } +static napi_value roomEnter(napi_env env, napi_callback_info info) { return 0; } +static napi_value roomExpel(napi_env env, napi_callback_info info) { return 0; } +static napi_value roomInvite(napi_env env, napi_callback_info info) { return 0; } +static napi_value roomLeave(napi_env env, napi_callback_info info) { return 0; } + +} + +EXTERN_C_START +static napi_value Init(napi_env env, napi_value exports) { + napi_property_descriptor desc[] = { + { "sessionCall", nullptr, acgist::sessionCall, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "sessionPause", nullptr, acgist::sessionPause, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "sessionResume", nullptr, acgist::sessionResume, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "sessionExchange", nullptr, acgist::sessionExchange, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "sessionClose", nullptr, acgist::sessionClose, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "mediaConsume", nullptr, acgist::mediaConsume, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "mediaConsumerClose", nullptr, acgist::mediaConsumerClose, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "mediaConsumerPause", nullptr, acgist::mediaConsumerPause, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "mediaConsumerResume", nullptr, acgist::mediaConsumerResume, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "mediaProducerClose", nullptr, acgist::mediaProducerClose, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "mediaProducerPause", nullptr, acgist::mediaProducerPause, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "mediaProducerResume", nullptr, acgist::mediaProducerResume, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "roomClientList", nullptr, acgist::roomClientList, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "roomClose", nullptr, acgist::roomClose, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "roomEnter", nullptr, acgist::roomEnter, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "roomExpel", nullptr, acgist::roomExpel, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "roomInvite", nullptr, acgist::roomInvite, nullptr, nullptr, nullptr, napi_default, nullptr }, + { "roomLeave", nullptr, acgist::roomLeave, nullptr, nullptr, nullptr, napi_default, nullptr }, + }; + napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc); + return exports; +} +EXTERN_C_END + +static napi_module demoModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = Init, + .nm_modname = "taoyao", + .nm_priv = ((void*) 0), + .reserved = { 0 }, +}; + +extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { + napi_module_register(&demoModule); + acgist::init(); +} diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/hello.cpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/hello.cpp deleted file mode 100644 index 330a2c1..0000000 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/hello.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "napi/native_api.h" - -static napi_value Add(napi_env env, napi_callback_info info) -{ - size_t requireArgc = 2; - size_t argc = 2; - napi_value args[2] = {nullptr}; - - napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); - - napi_valuetype valuetype0; - napi_typeof(env, args[0], &valuetype0); - - napi_valuetype valuetype1; - napi_typeof(env, args[1], &valuetype1); - - double value0; - napi_get_value_double(env, args[0], &value0); - - double value1; - napi_get_value_double(env, args[1], &value1); - - napi_value sum; - napi_create_double(env, value0 + value1, &sum); - - return sum; - -} - -EXTERN_C_START -static napi_value Init(napi_env env, napi_value exports) -{ - napi_property_descriptor desc[] = { - { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr } - }; - napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); - return exports; -} -EXTERN_C_END - -static napi_module demoModule = { - .nm_version = 1, - .nm_flags = 0, - .nm_filename = nullptr, - .nm_register_func = Init, - .nm_modname = "taoyao", - .nm_priv = ((void*)0), - .reserved = { 0 }, -}; - -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) -{ - napi_module_register(&demoModule); -} diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Client.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Client.hpp index 8091a8c..4c63735 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Client.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Client.hpp @@ -16,4 +16,4 @@ public: } -#endif taoyao_Client_HPP +#endif // taoyao_Client_HPP diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/LocalClient.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/LocalClient.hpp index 96b0ac5..f9d734c 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/LocalClient.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/LocalClient.hpp @@ -7,4 +7,4 @@ #ifndef taoyao_LocalClient_HPP #define taoyao_LocalClient_HPP -#endif taoyao_LocalClient_HPP +#endif // taoyao_LocalClient_HPP diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/MediaManager.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/MediaManager.hpp index daed635..1650abc 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/MediaManager.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/MediaManager.hpp @@ -26,4 +26,4 @@ public: } -#endif taoyao_MediaManager_HPP +#endif // taoyao_MediaManager_HPP diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/RemoteClient.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/RemoteClient.hpp index a35447d..ee18992 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/RemoteClient.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/RemoteClient.hpp @@ -7,4 +7,4 @@ #ifndef taoyao_RemoteClient_HPP #define taoyao_RemoteClient_HPP -#endif taoyao_RemoteClient_HPP +#endif // taoyao_RemoteClient_HPP diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Room.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Room.hpp index b42f2ff..5316e03 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Room.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/Room.hpp @@ -4,7 +4,15 @@ * @author acgist */ -#ifndef taoyao_Room.cpp_HPP -#define taoyao_Room.cpp_HPP +#ifndef taoyao_Room_HPP +#define taoyao_Room_HPP -#endif taoyao_Room.cpp_HPP +namespace acgist { + +class Room { + +}; + +} + +#endif // taoyao_Room_HPP diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/RoomClient.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/RoomClient.hpp index f6c51dd..415c970 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/RoomClient.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/RoomClient.hpp @@ -7,4 +7,4 @@ #ifndef taoyao_RoomClient_HPP #define taoyao_RoomClient_HPP -#endif taoyao_RoomClient_HPP +#endif // taoyao_RoomClient_HPP diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/SessionClient.hpp b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/SessionClient.hpp index 91b542c..c7e37db 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/SessionClient.hpp +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/include/SessionClient.hpp @@ -7,4 +7,12 @@ #ifndef taoyao_SessionClient_HPP #define taoyao_SessionClient_HPP -#endif taoyao_SessionClient_HPP +namespace acgist { + +class SessionClient { + +}; + +} + +#endif // taoyao_SessionClient_HPP diff --git a/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/index.d.ts b/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/index.d.ts index e44f361..4615052 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/index.d.ts +++ b/taoyao-client-openharmony/taoyao/media/src/main/cpp/types/libtaoyao/index.d.ts @@ -1 +1,18 @@ -export const add: (a: number, b: number) => number; \ No newline at end of file +export const sessionCall: () => void; +export const sessionPause: () => void; +export const sessionResume: () => void; +export const sessionExchange: () => void; +export const sessionClose: () => void; +export const mediaConsume: () => void; +export const mediaConsumerClose: () => void; +export const mediaConsumerPause: () => void; +export const mediaConsumerResume: () => void; +export const mediaProducerClose: () => void; +export const mediaProducerPause: () => void; +export const mediaProducerResume: () => void; +export const roomClientList: () => void; +export const roomClose: () => void; +export const roomEnter: () => void; +export const roomExpel: () => void; +export const roomInvite: () => void; +export const roomLeave: () => void; diff --git a/taoyao-client-openharmony/taoyao/media/src/main/ets/entryability/EntryAbility.ts b/taoyao-client-openharmony/taoyao/media/src/main/ets/entryability/EntryAbility.ts index 1d53c83..7bb3d5c 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/ets/entryability/EntryAbility.ts +++ b/taoyao-client-openharmony/taoyao/media/src/main/ets/entryability/EntryAbility.ts @@ -1,41 +1,33 @@ -import UIAbility from '@ohos.app.ability.UIAbility'; import hilog from '@ohos.hilog'; import window from '@ohos.window'; +import UIAbility from '@ohos.app.ability.UIAbility'; export default class EntryAbility extends UIAbility { + onCreate(want, launchParam) { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); + hilog.info(0x0000, 'EntryAbility', 'onCreate'); } onDestroy() { - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); + hilog.info(0x0000, 'EntryAbility', 'onDestroy'); } onWindowStageCreate(windowStage: window.WindowStage) { - // Main window is created, set main page for this ability - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - + hilog.info(0x0000, 'EntryAbility', 'onWindowStageCreate'); windowStage.loadContent('pages/Index', (err, data) => { - if (err.code) { - hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); - return; - } - hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); }); } onWindowStageDestroy() { - // Main window is destroyed, release UI related resources - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + hilog.info(0x0000, 'EntryAbility', 'onWindowStageDestroy'); } onForeground() { - // Ability has brought to foreground - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); + hilog.info(0x0000, 'EntryAbility', 'onForeground'); } onBackground() { - // Ability has back to background - hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); + hilog.info(0x0000, 'EntryAbility', 'onBackground'); } + }; diff --git a/taoyao-client-openharmony/taoyao/media/src/main/ets/pages/Index.ets b/taoyao-client-openharmony/taoyao/media/src/main/ets/pages/Index.ets index 3093b4c..86b2498 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/ets/pages/Index.ets +++ b/taoyao-client-openharmony/taoyao/media/src/main/ets/pages/Index.ets @@ -1,25 +1,31 @@ -import hilog from '@ohos.hilog'; -// import testNapi from 'libtaoyao.so' +import { signal } from "../taoyao/TaoyaoSignal"; @Entry @Component struct Index { - @State message: string = 'Hello World' - build() { Row() { Column() { - Text(this.message) - .fontSize(50) + Button("连接信令") + .fontSize(20) .fontWeight(FontWeight.Bold) .onClick(() => { - // hilog.info(0x0000, 'testTag', 'Test NAPI 2 + 3 = %{public}d', testNapi.add(2, 3)); - }) + signal.connect(); + }); } - .width('100%') + .width('50%'); + Column() { + Button("断开信令") + .fontSize(20) + .fontWeight(FontWeight.Bold) + .onClick(() => { + signal.close(); + }); + } + .width('50%'); } - .height('100%') + .height('100%'); } } diff --git a/taoyao-client-openharmony/taoyao/media/src/main/ets/signal/Signal.ets b/taoyao-client-openharmony/taoyao/media/src/main/ets/signal/Signal.ets deleted file mode 100644 index 44ebcfd..0000000 --- a/taoyao-client-openharmony/taoyao/media/src/main/ets/signal/Signal.ets +++ /dev/null @@ -1,18 +0,0 @@ -/** - * 信令连接 - * - * @author acgist - */ - -// import { BusinessError } from '@ohos.base'; -import webSocket from '@ohos.net.webSocket'; - -export default class TaoyaoSignal { - - // const socket; - // - // void connect(address) { - // - // } - -} diff --git a/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/MediaManager.ets b/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/MediaManager.ets new file mode 100644 index 0000000..e69de29 diff --git a/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/Setting.ets b/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/Setting.ets new file mode 100644 index 0000000..cae4582 --- /dev/null +++ b/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/Setting.ets @@ -0,0 +1,35 @@ +/** + * 配置 + * + * @author acgist + */ + +class Signal { + // 终端名称 + name: string = "鸿蒙"; + // 终端ID + clientId: string = "harmony"; + // 终端类型 + clientType: string = "MOBILE"; + // 信令账号 + username: string = "taoyao"; + // 信令密码 + password: string = "taoyao"; +}; + +class Setting { + + // 信令地址 + signalAddress: string = "wss://192.168.8.204:8888/websocket.signal"; + // 信令版本 + version: string = "1.0.0"; + // 信令配置 + signal: Signal = new Signal(); + +}; + +const setting = new Setting(); + +export { + setting +} diff --git a/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/TaoyaoSignal.ets b/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/TaoyaoSignal.ets new file mode 100644 index 0000000..6989f46 --- /dev/null +++ b/taoyao-client-openharmony/taoyao/media/src/main/ets/taoyao/TaoyaoSignal.ets @@ -0,0 +1,210 @@ +/** + * 信令连接 + * + * @author acgist + */ + +import hilog from '@ohos.hilog'; +import webSocket from '@ohos.net.webSocket'; + +import { setting } from './Setting'; + +interface BusinessError extends Error { + code: number; + data?: T; +} + +class TaoyaoSignal { + + // WebSocket信令连接 + socket: webSocket.WebSocket; + // 是否关闭 + closed: boolean = false; + // 是否连接成功 + connected: boolean = false; + // 心跳定时 + heartbeatTimer: number; + // 同步请求 + callbackMapping = new Map(); + // 当前消息索引 + index: number = 0; + // 最大消息索引 + maxIndex: number = 999; + // 当前终端索引 + clientIndex: number = 99999; + + connect() { + if(this.socket) { + this.socket.close(); + } + this.connected = false; + hilog.info(0x0000, 'TaoyaoSignal', '连接信令:%s', setting.signalAddress); + this.socket = webSocket.createWebSocket(); + this.socket.on('open', (err: BusinessError, value: Object) => { + hilog.info(0x0000, 'TaoyaoSignal', '打开信令:%s', setting.signalAddress); + this.register(); + this.connected = true; + }); + this.socket.on('message', (err: BusinessError, value: string | ArrayBuffer) => { + hilog.debug(0x0000, 'TaoyaoSignal', '信令消息:%s', value); + try { + this.onMessage(value); + } catch (error) { + hilog.error(0x0000, 'TaoyaoSignal', '处理信令消息异常:%s', value, error); + } + }); + this.socket.on('close', (err: BusinessError, value: Object) => { + hilog.error(0x0000, 'TaoyaoSignal', '关闭信令:%s', setting.signalAddress, err); + this.reconnect(); + }); + this.socket.on('error', (err: BusinessError) => { + hilog.error(0x0000, 'TaoyaoSignal', '信令异常:%s', setting.signalAddress, err); + this.reconnect(); + }); + this.socket.connect(setting.signalAddress, (err: BusinessError, value: boolean) => { + hilog.info(0x0000, 'TaoyaoSignal', '信令连接成功:%s', setting.signalAddress); + }); + }; + + reconnect() { + if(!this.closed) { + setTimeout(() => this.connect(), 5000); + } + } + + close() { + hilog.info(0x0000, 'TaoyaoSignal', '关闭信令:%s', setting.signalAddress); + this.closed = true; + this.connected = false; + if(this.socket) { + this.socket.close(); + } + } + + async register() { + const response: any = await this.request("client::register", { + name : setting.signal.name, + clientId : setting.signal.clientId, + clientType: setting.signal.clientType, + username : setting.signal.username, + password : setting.signal.password, + battery : 100, + charging : true, + }); + const { body } = response; + const { index } = body; + this.clientIndex = index; + hilog.info(0x0000, "TaoyaoSignal", "信令注册成功:%d", index); + this.heartbeat(); + } + + heartbeat() { + if(this.heartbeatTimer) { + clearInterval(this.heartbeatTimer); + } + this.heartbeatTimer = setInterval(() => { + this.send("client::heartbeat", { + battery : 100, + charging: true, + }); + }, 30 * 1000); + }; + + buildId() { + if (++this.index > this.maxIndex) { + this.index = 0; + } + const date = new Date(); + return ( + 100000000000000 * date.getDate() + + 1000000000000 * date.getHours() + + 10000000000 * date.getMinutes() + + 100000000 * date.getSeconds() + + 1000 * this.clientIndex + + this.index + ); + } + + send(signal, body) { + const header = { + v: setting.version, + id: this.buildId(), + signal + }; + const message = { + header, + body + }; + try { + this.socket.send(JSON.stringify(message)); + } catch (error) { + hilog.error(0x0000, 'TaoyaoSignal', '发送消息异常:%o', message, error); + } + } + + async request(signal, body) { + const id = this.buildId(); + return new Promise((resolve, reject) => { + const header = { + v: setting.version, + id, + signal + }; + const message = { + header, + body + }; + // 设置超时 + const rejectTimeout = setTimeout(() => { + this.callbackMapping.delete(id); + reject("请求超时"); + }, 5000); + // 请求回调 + this.callbackMapping.set(id, (response) => { + resolve(response); + clearTimeout(rejectTimeout); + // 默认不用继续处理 + return true; + }); + // 发送消息 + try { + this.socket.send(JSON.stringify(message)); + } catch (error) { + hilog.error(0x0000, 'TaoyaoSignal', '发送消息异常:%o', message, error); + reject(error); + } + }); + } + + onMessage(message) { + const json = JSON.parse(message); + const { + header, + body, + } = json; + const { + id + } = header; + if (this.callbackMapping.has(id)) { + hilog.info(0x0000, "TaoyaoSignal", "处理同步消息:%s", message); + try { + if( + this.callbackMapping.get(id)(json) + ) { + return; + } + } finally { + this.callbackMapping.delete(id); + } + } + hilog.info(0x0000, "TaoyaoSignal", "处理异步消息:%s", message); + // TODO + } + +} + +const signal = new TaoyaoSignal(); + +export { + signal +} diff --git a/taoyao-client-openharmony/taoyao/media/src/main/module.json5 b/taoyao-client-openharmony/taoyao/media/src/main/module.json5 index f12f0f2..8f5afd9 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/module.json5 +++ b/taoyao-client-openharmony/taoyao/media/src/main/module.json5 @@ -1,11 +1,11 @@ { "module": { - "name": "media", - "type": "entry", + "name" : "media", + "type" : "entry", "pages": "$profile:main_pages", "description": "$string:module_desc", "mainElement": "EntryAbility", - "installationFree": false, + "installationFree" : false, "deliveryWithInstall": true, "deviceTypes": [ "phone", @@ -13,8 +13,8 @@ ], "abilities": [ { - "name": "EntryAbility", - "icon": "$media:icon", + "name" : "EntryAbility", + "icon" : "$media:icon", "label": "$string:EntryAbility_label", "exported": true, "srcEntry": "./ets/entryability/EntryAbility.ts", @@ -32,6 +32,26 @@ } ] } + ], + "requestPermissions": [ + { + "name": "ohos.permission.CAMERA", + "usedScene": { + "when": "always" + } + }, + { + "name": "ohos.permission.INTERNET", + "usedScene": { + "when": "always" + } + }, + { + "name": "ohos.permission.MICROPHONE", + "usedScene": { + "when": "always" + } + } ] } } diff --git a/taoyao-client-openharmony/taoyao/media/src/main/resources/base/element/color.json b/taoyao-client-openharmony/taoyao/media/src/main/resources/base/element/color.json index 3c71296..162a7b6 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/resources/base/element/color.json +++ b/taoyao-client-openharmony/taoyao/media/src/main/resources/base/element/color.json @@ -5,4 +5,4 @@ "value": "#FFFFFF" } ] -} \ No newline at end of file +} diff --git a/taoyao-client-openharmony/taoyao/media/src/main/resources/base/element/string.json b/taoyao-client-openharmony/taoyao/media/src/main/resources/base/element/string.json index 1ce3ce9..9caab5d 100644 --- a/taoyao-client-openharmony/taoyao/media/src/main/resources/base/element/string.json +++ b/taoyao-client-openharmony/taoyao/media/src/main/resources/base/element/string.json @@ -6,11 +6,11 @@ }, { "name": "EntryAbility_desc", - "value": "主界面" + "value": "桃夭媒体服务" }, { "name": "EntryAbility_label", - "value": "主界面" + "value": "桃夭" } ] -} \ No newline at end of file +} diff --git a/taoyao-client-openharmony/taoyao/media/src/main/resources/base/media/icon.png b/taoyao-client-openharmony/taoyao/media/src/main/resources/base/media/icon.png index ce307a8..002ccf8 100644 Binary files a/taoyao-client-openharmony/taoyao/media/src/main/resources/base/media/icon.png and b/taoyao-client-openharmony/taoyao/media/src/main/resources/base/media/icon.png differ diff --git a/taoyao-client-openharmony/taoyao/oh-package.json5 b/taoyao-client-openharmony/taoyao/oh-package.json5 index d2f79e4..d22cbfd 100644 --- a/taoyao-client-openharmony/taoyao/oh-package.json5 +++ b/taoyao-client-openharmony/taoyao/oh-package.json5 @@ -1,10 +1,10 @@ { - "name": "taoyao", - "main": "", + "name" : "taoyao", + "main" : "", "author": "acgist", - "description": "桃夭鸿蒙终端", "version": "1.0.0", "license": "Apache-2.0", + "description" : "桃夭鸿蒙终端", "dependencies": {}, "devDependencies": { "@ohos/hypium": "1.0.6"