[*] 信令回调
This commit is contained in:
@@ -648,7 +648,7 @@ public final class Taoyao implements ITaoyao {
|
|||||||
case "room::close" -> this.roomClose(message, message.body());
|
case "room::close" -> this.roomClose(message, message.body());
|
||||||
case "room::enter" -> this.roomEnter(message, message.body());
|
case "room::enter" -> this.roomEnter(message, message.body());
|
||||||
case "room::expel" -> this.roomExpel(message, message.body());
|
case "room::expel" -> this.roomExpel(message, message.body());
|
||||||
case "room::invite" -> this.roomInivte(message, message.body());
|
case "room::invite" -> this.roomInvite(message, message.body());
|
||||||
case "room::leave" -> this.roomLeave(message, message.body());
|
case "room::leave" -> this.roomLeave(message, message.body());
|
||||||
case "session::call" -> this.sessionCall(message, message.body());
|
case "session::call" -> this.sessionCall(message, message.body());
|
||||||
case "session::close" -> this.sessionClose(message, message.body());
|
case "session::close" -> this.sessionClose(message, message.body());
|
||||||
@@ -1062,7 +1062,7 @@ public final class Taoyao implements ITaoyao {
|
|||||||
* @param message 信令消息
|
* @param message 信令消息
|
||||||
* @param body 信令主体
|
* @param body 信令主体
|
||||||
*/
|
*/
|
||||||
private void roomInivte(Message message, Map<String, Object> body) {
|
private void roomInvite(Message message, Map<String, Object> body) {
|
||||||
final String roomId = MapUtils.get(body, "roomId");
|
final String roomId = MapUtils.get(body, "roomId");
|
||||||
final String password = MapUtils.get(body, "password");
|
final String password = MapUtils.get(body, "password");
|
||||||
// 安卓可以同时进入多个房间
|
// 安卓可以同时进入多个房间
|
||||||
|
|||||||
@@ -54,7 +54,6 @@ public abstract class CloseableClient implements Closeable {
|
|||||||
this.init = true;
|
this.init = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
this.close = true;
|
this.close = true;
|
||||||
|
|||||||
@@ -18,9 +18,9 @@ set(CMAKE_CXX_STANDARD 17)
|
|||||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++17 -O0 -g")
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -std=c++17 -O0 -g")
|
||||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++17 -O3")
|
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -std=c++17 -O3")
|
||||||
|
|
||||||
set(MEDIASOUPCLIENT_LOG_DEV OFF)
|
option(MEDIASOUPCLIENT_LOG_DEV OFF)
|
||||||
set(MEDIASOUPCLIENT_LOG_TRACE OFF)
|
option(MEDIASOUPCLIENT_LOG_TRACE OFF)
|
||||||
set(MEDIASOUPCLIENT_BUILD_TESTS OFF)
|
option(MEDIASOUPCLIENT_BUILD_TESTS OFF)
|
||||||
set(LIBWEBRTC_BINARY_PATH "${CMAKE_CURRENT_SOURCE_DIR}/deps/webrtc/lib/")
|
set(LIBWEBRTC_BINARY_PATH "${CMAKE_CURRENT_SOURCE_DIR}/deps/webrtc/lib/")
|
||||||
set(LIBWEBRTC_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/deps/webrtc/src/")
|
set(LIBWEBRTC_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/deps/webrtc/src/")
|
||||||
|
|
||||||
@@ -54,6 +54,23 @@ target_include_directories(
|
|||||||
target_link_libraries(
|
target_link_libraries(
|
||||||
${PROJECT_NAME} PUBLIC
|
${PROJECT_NAME} PUBLIC
|
||||||
mediasoupclient
|
mediasoupclient
|
||||||
|
${LIBWEBRTC_BINARY_PATH}/libwebrtc.a
|
||||||
|
# NAPI
|
||||||
libace_napi.z.so
|
libace_napi.z.so
|
||||||
|
# LOG
|
||||||
libhilog_ndk.z.so
|
libhilog_ndk.z.so
|
||||||
|
# 媒体
|
||||||
|
libohaudio.so
|
||||||
|
libnative_media_aenc.so
|
||||||
|
libnative_media_venc.so
|
||||||
|
libnative_media_core.so
|
||||||
|
libnative_media_codecbase.so
|
||||||
|
)
|
||||||
|
|
||||||
|
target_compile_definitions(${PROJECT_NAME} PUBLIC
|
||||||
|
$<$<NOT:$<PLATFORM_ID:Windows>>:WEBRTC_POSIX>
|
||||||
|
$<$<PLATFORM_ID:Windows>:WEBRTC_WIN>
|
||||||
|
$<$<PLATFORM_ID:Windows>:NOMINMAX>
|
||||||
|
$<$<PLATFORM_ID:Windows>:WIN32_LEAN_AND_MEAN>
|
||||||
|
$<$<PLATFORM_ID:Darwin>:WEBRTC_MAC>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -3,53 +3,194 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "hilog/log.h"
|
#include "hilog/log.h"
|
||||||
#include "napi/native_api.h"
|
#include "napi/native_api.h"
|
||||||
|
|
||||||
|
#include "mediasoupclient.hpp"
|
||||||
|
|
||||||
#include "./include/Room.hpp"
|
#include "./include/Room.hpp"
|
||||||
#include "./include/MediaManager.hpp"
|
#include "./include/MediaManager.hpp"
|
||||||
|
|
||||||
|
#include <multimedia/player_framework/native_avcapability.h>
|
||||||
|
#include <multimedia/player_framework/native_avcodec_base.h>
|
||||||
|
|
||||||
namespace acgist {
|
namespace acgist {
|
||||||
|
|
||||||
acgist::MediaManager* mediaManager = nullptr;
|
static std::mutex roomMutex;
|
||||||
std::map<std::string, acgist::Room*> roomMap;
|
|
||||||
|
|
||||||
|
// JS环境
|
||||||
|
static napi_env env = nullptr;
|
||||||
|
// 是否加载
|
||||||
|
static bool initTaoyao = false;
|
||||||
|
// SEND方法引用
|
||||||
|
static napi_ref sendRef = nullptr;
|
||||||
|
// REQUEST方法引用
|
||||||
|
static napi_ref requestRef = nullptr;
|
||||||
|
// 媒体功能
|
||||||
|
static acgist::MediaManager* mediaManager = nullptr;
|
||||||
|
// 房间管理
|
||||||
|
static std::map<std::string, acgist::Room*> roomMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持的编解码
|
||||||
|
*/
|
||||||
|
static void printSupportCodec() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 加载系统
|
||||||
|
*/
|
||||||
static void init() {
|
static void init() {
|
||||||
|
if(initTaoyao) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
initTaoyao = true;
|
||||||
OH_LOG_INFO(LOG_APP, "加载libtaoyao");
|
OH_LOG_INFO(LOG_APP, "加载libtaoyao");
|
||||||
// TODO:输出编码能力
|
// 编码能力
|
||||||
|
printSupportCodec();
|
||||||
|
std::string version = mediasoupclient::Version();
|
||||||
|
OH_LOG_INFO(LOG_APP, "加载MediasoupClient:%s", version.data());
|
||||||
|
mediasoupclient::Initialize();
|
||||||
|
OH_LOG_INFO(LOG_APP, "加载媒体功能");
|
||||||
mediaManager = new MediaManager();
|
mediaManager = new MediaManager();
|
||||||
mediaManager->initPeerConnectionFactory();
|
mediaManager->initPeerConnectionFactory();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void shutdown() {
|
/**
|
||||||
|
* 卸载系统
|
||||||
|
*/
|
||||||
|
static napi_value shutdown(napi_env env, napi_callback_info info) {
|
||||||
|
if(!initTaoyao) {
|
||||||
|
OH_LOG_INFO(LOG_APP, "已经卸载libtaoyao");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
initTaoyao = false;
|
||||||
OH_LOG_INFO(LOG_APP, "卸载libtaoyao");
|
OH_LOG_INFO(LOG_APP, "卸载libtaoyao");
|
||||||
|
OH_LOG_INFO(LOG_APP, "释放mediasoupclient");
|
||||||
|
mediasoupclient::Cleanup();
|
||||||
|
// this->roomMap
|
||||||
if (mediaManager != nullptr) {
|
if (mediaManager != nullptr) {
|
||||||
delete mediaManager;
|
delete mediaManager;
|
||||||
mediaManager = nullptr;
|
mediaManager = nullptr;
|
||||||
}
|
}
|
||||||
// this->roomMap
|
// napi_delete_reference(env, acgist::sendRef);
|
||||||
|
// napi_delete_reference(env, acgist::requestRef);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void send(const std::string& signal, const std::string& body) {
|
||||||
|
napi_value ret;
|
||||||
|
napi_value callback = nullptr;
|
||||||
|
napi_get_reference_value(env, acgist::sendRef, &callback);
|
||||||
|
napi_value data[2];
|
||||||
|
napi_create_string_utf8(acgist::env, signal.c_str(), NAPI_AUTO_LENGTH, &data[0]);
|
||||||
|
napi_create_string_utf8(acgist::env, body.c_str(), NAPI_AUTO_LENGTH, &data[1]);
|
||||||
|
napi_call_function(acgist::env, nullptr, callback, 2, data, &ret);
|
||||||
|
// napi_get_undefined(acgist::env, &ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string request(const std::string& signal, const std::string& body) {
|
||||||
|
napi_value ret;
|
||||||
|
napi_value callback = nullptr;
|
||||||
|
napi_get_reference_value(env, acgist::requestRef, &callback);
|
||||||
|
napi_value data[2];
|
||||||
|
napi_create_string_utf8(acgist::env, signal.c_str(), NAPI_AUTO_LENGTH, &data[0]);
|
||||||
|
napi_create_string_utf8(acgist::env, body.c_str(), NAPI_AUTO_LENGTH, &data[1]);
|
||||||
|
napi_call_function(acgist::env, nullptr, callback, 2, data, &ret);
|
||||||
|
char chars[2048];
|
||||||
|
size_t length;
|
||||||
|
napi_get_value_string_utf8(env, ret, chars, sizeof(chars), &length);
|
||||||
|
return chars;
|
||||||
}
|
}
|
||||||
|
|
||||||
static napi_value mediaConsume(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 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 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 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 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 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 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 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 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 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 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 roomInvite(napi_env env, napi_callback_info info) {
|
||||||
|
napi_value ret;
|
||||||
|
size_t argc = 1;
|
||||||
|
napi_value args[1] = { nullptr };
|
||||||
|
// TODO: 是否需要释放
|
||||||
|
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||||
|
char chars[2048];
|
||||||
|
size_t length;
|
||||||
|
// TODO: 是否需要释放
|
||||||
|
napi_get_value_string_utf8(env, args[0], chars, sizeof(chars), &length);
|
||||||
|
nlohmann::json json = nlohmann::json::parse(chars);
|
||||||
|
nlohmann::json body = json["body"];
|
||||||
|
std::string roomId = body["roomId"];
|
||||||
|
std::string password = body["password"];
|
||||||
|
std::lock_guard<std::mutex> guard(roomMutex);
|
||||||
|
auto iterator = roomMap.find(roomId);
|
||||||
|
if(iterator == roomMap.end()) {
|
||||||
|
OH_LOG_INFO(LOG_APP, "进入房间:%s", roomId.c_str());
|
||||||
|
auto room = new acgist::Room(roomId, mediaManager);
|
||||||
|
roomMap[roomId] = room;
|
||||||
|
int enterRet = room->enter(password);
|
||||||
|
napi_create_int32(env, enterRet, &ret);
|
||||||
|
} else {
|
||||||
|
OH_LOG_INFO(LOG_APP, "已经进入房间:%s", roomId.c_str());
|
||||||
|
napi_create_int32(env, -1, &ret);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
static napi_value roomLeave(napi_env env, napi_callback_info info) { return 0; }
|
static napi_value roomLeave(napi_env env, napi_callback_info info) { return 0; }
|
||||||
|
|
||||||
|
static napi_value registerSend(napi_env env, napi_callback_info info) {
|
||||||
|
size_t argc = 1;
|
||||||
|
napi_value args[1] = { nullptr };
|
||||||
|
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||||
|
napi_create_reference(env, args[0], 1, &acgist::sendRef);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static napi_value registerRequest(napi_env env, napi_callback_info info) {
|
||||||
|
size_t argc = 1;
|
||||||
|
napi_value args[1] = { nullptr };
|
||||||
|
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
|
||||||
|
napi_create_reference(env, args[0], 1, &acgist::requestRef);
|
||||||
|
// napi_create_promise
|
||||||
|
// napi_resolve_deferred
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EXTERN_C_START
|
EXTERN_C_START
|
||||||
static napi_value Init(napi_env env, napi_value exports) {
|
static napi_value Init(napi_env env, napi_value exports) {
|
||||||
|
acgist::env = env;
|
||||||
napi_property_descriptor desc[] = {
|
napi_property_descriptor desc[] = {
|
||||||
{ "mediaConsume", nullptr, acgist::mediaConsume, 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 },
|
{ "mediaConsumerClose", nullptr, acgist::mediaConsumerClose, nullptr, nullptr, nullptr, napi_default, nullptr },
|
||||||
@@ -64,6 +205,9 @@ static napi_value Init(napi_env env, napi_value exports) {
|
|||||||
{ "roomExpel", nullptr, acgist::roomExpel, 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 },
|
{ "roomInvite", nullptr, acgist::roomInvite, nullptr, nullptr, nullptr, napi_default, nullptr },
|
||||||
{ "roomLeave", nullptr, acgist::roomLeave, nullptr, nullptr, nullptr, napi_default, nullptr },
|
{ "roomLeave", nullptr, acgist::roomLeave, nullptr, nullptr, nullptr, napi_default, nullptr },
|
||||||
|
{ "shutdown", nullptr, acgist::shutdown, nullptr, nullptr, nullptr, napi_default, nullptr },
|
||||||
|
{ "registerSend", nullptr, acgist::registerSend, nullptr, nullptr, nullptr, napi_default, nullptr },
|
||||||
|
{ "registerRequest", nullptr, acgist::registerRequest, nullptr, nullptr, nullptr, napi_default, nullptr },
|
||||||
};
|
};
|
||||||
napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc);
|
napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc);
|
||||||
return exports;
|
return exports;
|
||||||
|
|||||||
@@ -2,6 +2,13 @@
|
|||||||
* 采集器
|
* 采集器
|
||||||
*
|
*
|
||||||
* @author acgist
|
* @author acgist
|
||||||
|
*
|
||||||
|
* https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/media/audio-encoding.md
|
||||||
|
* https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/media/video-encoding.md
|
||||||
|
* https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/media/obtain-supported-codecs.md
|
||||||
|
* https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/media/using-ohaudio-for-recording.md
|
||||||
|
* https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/reference/native-lib/third_party_opengl/opengles.md
|
||||||
|
* https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/reference/native-lib/third_party_opensles/opensles.md
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef taoyao_Capturer_HPP
|
#ifndef taoyao_Capturer_HPP
|
||||||
|
|||||||
@@ -7,18 +7,36 @@
|
|||||||
#ifndef taoyao_Client_HPP
|
#ifndef taoyao_Client_HPP
|
||||||
#define taoyao_Client_HPP
|
#define taoyao_Client_HPP
|
||||||
|
|
||||||
|
#include "MediaManager.hpp"
|
||||||
|
|
||||||
namespace acgist {
|
namespace acgist {
|
||||||
|
|
||||||
class Client {
|
class Client {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
/**
|
||||||
|
* 资源释放
|
||||||
|
*
|
||||||
|
* @return 是否成功
|
||||||
|
*/
|
||||||
|
virtual bool release() = 0;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class RoomClient {};
|
class RoomClient : public Client {
|
||||||
|
|
||||||
class LocalClient {};
|
public:
|
||||||
|
acgist::MediaManager* mediaManager;
|
||||||
|
|
||||||
class RemoteClient {};
|
};
|
||||||
|
|
||||||
|
class LocalClient : public RoomClient {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class RemoteClient : public RoomClient {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,9 @@
|
|||||||
#ifndef taoyao_MediaManager_HPP
|
#ifndef taoyao_MediaManager_HPP
|
||||||
#define taoyao_MediaManager_HPP
|
#define taoyao_MediaManager_HPP
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "pc/peer_connection.h"
|
#include "pc/peer_connection.h"
|
||||||
#include "pc/peer_connection_factory.h"
|
#include "pc/peer_connection_factory.h"
|
||||||
|
|
||||||
@@ -20,13 +23,24 @@ public:
|
|||||||
MediaManager();
|
MediaManager();
|
||||||
~MediaManager();
|
~MediaManager();
|
||||||
public:
|
public:
|
||||||
webrtc::PeerConnectionFactory* peerConnectionFactoryPtr;
|
int localClientRef = 0;
|
||||||
|
std::unique_ptr<rtc::Thread> networkThread = nullptr;
|
||||||
|
std::unique_ptr<rtc::Thread> signalingThread = nullptr;
|
||||||
|
std::unique_ptr<rtc::Thread> workerThread = nullptr;
|
||||||
|
rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> peerConnectionFactory = nullptr;
|
||||||
public:
|
public:
|
||||||
void initPeerConnectionFactory();
|
// 加载PC工厂
|
||||||
webrtc::PeerConnection* buildPeerConnection();
|
bool initPeerConnectionFactory();
|
||||||
|
// 新增本地终端
|
||||||
|
int newLocalClient();
|
||||||
|
// 释放本地终端
|
||||||
|
int releaseLocalClient();
|
||||||
void startCapture();
|
void startCapture();
|
||||||
void startAudioCapture();
|
void startAudioCapture();
|
||||||
void startVideoCapture();
|
void startVideoCapture();
|
||||||
|
void stopCapture();
|
||||||
|
void stopAudioCapture();
|
||||||
|
void stopVideoCapture();
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
/**
|
/**
|
||||||
* 播放器
|
* 播放器
|
||||||
|
*
|
||||||
|
* https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/media/using-ohaudio-for-playback.md
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef TAOYAO_PALYER_H
|
#ifndef TAOYAO_PALYER_H
|
||||||
|
|||||||
@@ -7,10 +7,45 @@
|
|||||||
#ifndef taoyao_Room_HPP
|
#ifndef taoyao_Room_HPP
|
||||||
#define taoyao_Room_HPP
|
#define taoyao_Room_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "./Client.hpp"
|
||||||
|
#include "./MediaManager.hpp"
|
||||||
|
|
||||||
namespace acgist {
|
namespace acgist {
|
||||||
|
|
||||||
class Room {
|
class Room {
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool audioProduce = true;
|
||||||
|
bool videoProduce = true;
|
||||||
|
bool audioConsume = true;
|
||||||
|
bool videoConsume = true;
|
||||||
|
std::string roomId = "";
|
||||||
|
// 本地终端
|
||||||
|
acgist::LocalClient* client = nullptr;
|
||||||
|
// 远程终端
|
||||||
|
std::vector<acgist::RemoteClient*> clients;
|
||||||
|
// 媒体管理
|
||||||
|
acgist::MediaManager* mediaManager = nullptr;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Room(const std::string& roomId, acgist::MediaManager* mediaManager);
|
||||||
|
virtual ~Room();
|
||||||
|
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* 进入房间
|
||||||
|
*
|
||||||
|
* @param password 密码
|
||||||
|
*
|
||||||
|
* @return 是否成功
|
||||||
|
*/
|
||||||
|
int enter(const std::string& password);
|
||||||
|
int close();
|
||||||
|
int newRemoteClient();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,56 @@
|
|||||||
#include "../include/MediaManager.hpp"
|
#include "../include/MediaManager.hpp"
|
||||||
|
|
||||||
#include "api/peer_connection_interface.h"
|
#include "hilog/log.h"
|
||||||
|
|
||||||
|
#include "api/create_peerconnection_factory.h"
|
||||||
|
#include "api/audio_codecs/audio_decoder_factory.h"
|
||||||
|
#include "api/audio_codecs/audio_encoder_factory.h"
|
||||||
|
#include "api/video_codecs/video_decoder_factory.h"
|
||||||
|
#include "api/video_codecs/video_encoder_factory.h"
|
||||||
|
#include <api/audio_codecs/builtin_audio_decoder_factory.h>
|
||||||
|
#include <api/audio_codecs/builtin_audio_encoder_factory.h>
|
||||||
|
#include <api/video_codecs/builtin_video_decoder_factory.h>
|
||||||
|
#include <api/video_codecs/builtin_video_encoder_factory.h>
|
||||||
|
|
||||||
acgist::MediaManager::MediaManager() {
|
acgist::MediaManager::MediaManager() {
|
||||||
this->peerConnectionFactoryPtr = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
acgist::MediaManager::~MediaManager() {
|
acgist::MediaManager::~MediaManager() {
|
||||||
if(this->peerConnectionFactoryPtr != nullptr) {
|
// TODO:验证是否需要释放线程和工厂
|
||||||
// delete this->peerConnectionFactoryPtr;
|
|
||||||
this->peerConnectionFactoryPtr->Release();
|
|
||||||
this->peerConnectionFactoryPtr = nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void acgist::MediaManager::initPeerConnectionFactory() {
|
bool acgist::MediaManager::initPeerConnectionFactory() {
|
||||||
webrtc::PeerConnectionFactoryDependencies dependencies;
|
OH_LOG_INFO(LOG_APP, "加载PeerConnectionFactory");
|
||||||
// webrtc::PeerConnectionFactory::Create(dependencies);
|
this->networkThread = rtc::Thread::CreateWithSocketServer();
|
||||||
|
this->signalingThread = rtc::Thread::Create();
|
||||||
|
this->workerThread = rtc::Thread::Create();
|
||||||
|
this->networkThread->SetName("network_thread", nullptr);
|
||||||
|
this->signalingThread->SetName("signaling_thread", nullptr);
|
||||||
|
this->workerThread->SetName("worker_thread", nullptr);
|
||||||
|
if (!this->networkThread->Start() || !this->signalingThread->Start() || !this->workerThread->Start()) {
|
||||||
|
OH_LOG_WARN(LOG_APP, "WebRTC线程启动失败");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
this->peerConnectionFactory = webrtc::CreatePeerConnectionFactory(
|
||||||
|
this->networkThread.get(),
|
||||||
|
// worker和signaling使用相同线程
|
||||||
|
this->workerThread.get(),
|
||||||
|
// this->signalingThread.get(),
|
||||||
|
this->signalingThread.get(),
|
||||||
|
nullptr /* default_adm */,
|
||||||
|
webrtc::CreateBuiltinAudioEncoderFactory(),
|
||||||
|
webrtc::CreateBuiltinAudioDecoderFactory(),
|
||||||
|
nullptr,
|
||||||
|
nullptr,
|
||||||
|
// TODO: 视频工厂
|
||||||
|
// webrtc::CreateBuiltinVideoEncoderFactory(),
|
||||||
|
// webrtc::CreateBuiltinVideoDecoderFactory(),
|
||||||
|
nullptr /* audio_mixer */,
|
||||||
|
nullptr /* audio_processing */
|
||||||
|
);
|
||||||
|
return this->peerConnectionFactory != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
int acgist::MediaManager::newLocalClient() {
|
||||||
|
this->localClientRef++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#include "../include/Room.hpp"
|
||||||
|
#include "Client.hpp"
|
||||||
|
|
||||||
|
acgist::Room::Room(const std::string& roomId, acgist::MediaManager* mediaManager) : roomId(roomId), mediaManager(mediaManager) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
acgist::Room::~Room() {
|
||||||
|
if(this->client != nullptr) {
|
||||||
|
this->client->release();
|
||||||
|
delete this->client;
|
||||||
|
this->client = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int acgist::Room::enter(const std::string& password) {
|
||||||
|
this->mediaManager->newLocalClient();
|
||||||
|
// this->client = new LocalClient();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,5 +9,8 @@ export const roomClientList : () => number;
|
|||||||
export const roomClose : () => number;
|
export const roomClose : () => number;
|
||||||
export const roomEnter : () => number;
|
export const roomEnter : () => number;
|
||||||
export const roomExpel : () => number;
|
export const roomExpel : () => number;
|
||||||
export const roomInvite: () => number;
|
export const roomInvite: (json: string) => number;
|
||||||
export const roomLeave : () => number;
|
export const roomLeave : () => number;
|
||||||
|
export const shutdown : () => number;
|
||||||
|
export const registerSend : (callback: (signal: string, body: string) => void) => number;
|
||||||
|
export const registerRequest: (callback: (signal: string, body: string) => string) => number;
|
||||||
|
|||||||
@@ -47,12 +47,12 @@ class TaoyaoSignal {
|
|||||||
hilog.info(0x0000, "TaoyaoSignal", "连接信令:%s", setting.signalAddress);
|
hilog.info(0x0000, "TaoyaoSignal", "连接信令:%s", setting.signalAddress);
|
||||||
this.socket = webSocket.createWebSocket();
|
this.socket = webSocket.createWebSocket();
|
||||||
this.socket.on("open", (err: BusinessError, value: Object) => {
|
this.socket.on("open", (err: BusinessError, value: Object) => {
|
||||||
hilog.info(0x0000, "TaoyaoSignal", "打开信令:%s", setting.signalAddress);
|
hilog.info(0x0000, "TaoyaoSignal", "打开信令:%s", setting.signalAddress, value, err);
|
||||||
this.register();
|
this.register();
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
});
|
});
|
||||||
this.socket.on("message", (err: BusinessError, value: string | ArrayBuffer) => {
|
this.socket.on("message", (err: BusinessError, value: string | ArrayBuffer) => {
|
||||||
hilog.debug(0x0000, "TaoyaoSignal", "信令消息:%s", value);
|
hilog.debug(0x0000, "TaoyaoSignal", "信令消息:%s", value, err);
|
||||||
try {
|
try {
|
||||||
this.onMessage(value.toString());
|
this.onMessage(value.toString());
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -60,7 +60,7 @@ class TaoyaoSignal {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
this.socket.on("close", (err: BusinessError, value: Object) => {
|
this.socket.on("close", (err: BusinessError, value: Object) => {
|
||||||
hilog.error(0x0000, "TaoyaoSignal", "关闭信令:%s", setting.signalAddress, err);
|
hilog.error(0x0000, "TaoyaoSignal", "关闭信令:%s", setting.signalAddress, value, err);
|
||||||
this.reconnect();
|
this.reconnect();
|
||||||
});
|
});
|
||||||
this.socket.on("error", (err: BusinessError) => {
|
this.socket.on("error", (err: BusinessError) => {
|
||||||
@@ -68,7 +68,7 @@ class TaoyaoSignal {
|
|||||||
this.reconnect();
|
this.reconnect();
|
||||||
});
|
});
|
||||||
this.socket.connect(setting.signalAddress, (err: BusinessError, value: boolean) => {
|
this.socket.connect(setting.signalAddress, (err: BusinessError, value: boolean) => {
|
||||||
hilog.info(0x0000, "TaoyaoSignal", "信令连接成功:%s", setting.signalAddress);
|
hilog.info(0x0000, "TaoyaoSignal", "信令连接成功:%s", setting.signalAddress, value, err);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -224,6 +224,7 @@ class TaoyaoSignal {
|
|||||||
const header: Record<string, Object> = json.header as Record<string, Object>;
|
const header: Record<string, Object> = json.header as Record<string, Object>;
|
||||||
const body : Record<string, Object> = json.body as Record<string, Object>;
|
const body : Record<string, Object> = json.body as Record<string, Object>;
|
||||||
const id : number = header.id as number;
|
const id : number = header.id as number;
|
||||||
|
const signal: string = header.signal as string;
|
||||||
if (this.callbackMapping.has(id)) {
|
if (this.callbackMapping.has(id)) {
|
||||||
hilog.debug(0x0000, "TaoyaoSignal", "处理同步消息:%s", message);
|
hilog.debug(0x0000, "TaoyaoSignal", "处理同步消息:%s", message);
|
||||||
try {
|
try {
|
||||||
@@ -236,13 +237,33 @@ class TaoyaoSignal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
hilog.debug(0x0000, "TaoyaoSignal", "处理异步消息:%s", message);
|
hilog.debug(0x0000, "TaoyaoSignal", "处理异步消息:%s", message);
|
||||||
// TODO
|
switch (signal) {
|
||||||
|
case "room::invite":
|
||||||
|
taoyaoModule.roomInvite(message);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
hilog.error(0x0000, "TaoyaoSignal", "没有适配信令:%s", signal);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nativeSend(signal: string, body: string) {
|
||||||
|
this.send(signal, JSON.parse(body));
|
||||||
|
}
|
||||||
|
|
||||||
|
nativeRequest(signal: string, body: string): string {
|
||||||
|
const response = this.request(signal, JSON.parse(body));
|
||||||
|
return JSON.stringify(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const signal = new TaoyaoSignal();
|
const signal = new TaoyaoSignal();
|
||||||
|
|
||||||
|
// 注册回调
|
||||||
|
taoyaoModule.registerSend(signal.nativeSend);
|
||||||
|
taoyaoModule.registerRequest(signal.nativeRequest);
|
||||||
|
|
||||||
export {
|
export {
|
||||||
signal
|
signal
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user