[*] 细节调整

This commit is contained in:
acgist
2023-04-25 08:37:22 +08:00
parent c6a5f1d1e0
commit 4cda1ef114
11 changed files with 109 additions and 53 deletions

View File

@@ -182,7 +182,7 @@ public class MainActivity extends AppCompatActivity implements Serializable {
} }
this.threadHandler.post(() -> { this.threadHandler.post(() -> {
// 进入房间 // 进入房间
Taoyao.taoyao.roomEnter("4ca4b920-a422-473d-9954-660df424270f", null); Taoyao.taoyao.roomEnter("53a5d97d-2860-4659-9531-095bdecf3745", null);
// Taoyao.taoyao.sessionCall("taoyao"); // Taoyao.taoyao.sessionCall("taoyao");
}); });
} }

View File

@@ -8,4 +8,12 @@
namespace acgist { namespace acgist {
#ifndef TAOYAO_JAVA_VM
#define TAOYAO_JAVA_VM
/**
* 全局JavaVM指针
*/
extern JavaVM* taoyaoJavaVM;
#endif
} }

View File

@@ -7,6 +7,7 @@
#include "sdk/android/native_api/jni/scoped_java_ref.h" #include "sdk/android/native_api/jni/scoped_java_ref.h"
#include "Log.hpp" #include "Log.hpp"
#include "MediaManager.hpp"
#include "RouterCallback.hpp" #include "RouterCallback.hpp"
namespace acgist { namespace acgist {
@@ -26,7 +27,7 @@ namespace acgist {
mediasoupclient::Consumer::Listener* consumerListener; mediasoupclient::Consumer::Listener* consumerListener;
std::map<std::string, mediasoupclient::Consumer*> consumers; std::map<std::string, mediasoupclient::Consumer*> consumers;
public: public:
Room(JavaVM* javaVM, const std::string& roomId, const jobject& routerCallback); Room(const std::string& roomId, const jobject& routerCallback);
virtual ~Room(); virtual ~Room();
public: public:
void enterRoom(JNIEnv* env, const std::string& rtpCapabilities, webrtc::PeerConnectionFactoryInterface* factory, webrtc::PeerConnectionInterface::RTCConfiguration& rtcConfiguration); void enterRoom(JNIEnv* env, const std::string& rtpCapabilities, webrtc::PeerConnectionFactoryInterface* factory, webrtc::PeerConnectionInterface::RTCConfiguration& rtcConfiguration);

View File

@@ -9,7 +9,6 @@ namespace acgist {
class RouterCallback { class RouterCallback {
public: public:
JavaVM* javaVM;
jobject routerCallback; jobject routerCallback;
public: public:
void enterRoomCallback(JNIEnv* env, std::string rtpCapabilities, std::string sctpCapabilities); void enterRoomCallback(JNIEnv* env, std::string rtpCapabilities, std::string sctpCapabilities);

View File

@@ -2,21 +2,19 @@
namespace acgist { namespace acgist {
JavaVM* taoyaoJavaVM;
/** /**
* 非常重要 * 非常重要
* 如果没有配置很多方法莫名其妙报错
*
* @param env JNIEnv
*/ */
void initWebrtcJni(JNIEnv* env) { extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* javaVM, void* reserved) {
JavaVM* javaVM; taoyaoJavaVM = javaVM;
env->GetJavaVM(&javaVM);
webrtc::jni::InitGlobalJniVariables(javaVM); webrtc::jni::InitGlobalJniVariables(javaVM);
return JNI_VERSION_1_6;
} }
extern "C" JNIEXPORT void JNICALL extern "C" JNIEXPORT void JNICALL
Java_com_acgist_taoyao_media_MediaManager_nativeInit(JNIEnv* env, jobject me) { Java_com_acgist_taoyao_media_MediaManager_nativeInit(JNIEnv* env, jobject me) {
initWebrtcJni(env);
std::string version = mediasoupclient::Version(); std::string version = mediasoupclient::Version();
LOG_I("加载MediasoupClient", version.data()); LOG_I("加载MediasoupClient", version.data());
mediasoupclient::Initialize(); mediasoupclient::Initialize();

View File

@@ -14,13 +14,22 @@ namespace acgist {
public: public:
std::future<void> OnConnect(mediasoupclient::Transport* transport, const nlohmann::json& dtlsParameters) override { std::future<void> OnConnect(mediasoupclient::Transport* transport, const nlohmann::json& dtlsParameters) override {
const std::string& cTransportId = transport->GetId(); const std::string& cTransportId = transport->GetId();
std::string cDtlsParameters = dtlsParameters.dump(); const std::string cDtlsParameters = dtlsParameters.dump();
JNIEnv* env; JNIEnv* env;
this->room->javaVM->AttachCurrentThread(&env, nullptr); if(taoyaoJavaVM->GetEnv((void**) &env, JNI_VERSION_1_6) == JNI_OK) {
this->room->sendTransportConnectCallback(env, cTransportId, cDtlsParameters); this->room->sendTransportConnectCallback(env, cTransportId, cDtlsParameters);
this->room->javaVM->DetachCurrentThread(); } else {
return std::future<void>(); JavaVMAttachArgs args;
// args.version = JNI_VERSION_1_6;
// args.name = "name";
taoyaoJavaVM->AttachCurrentThreadAsDaemon(&env, &args);
this->room->sendTransportConnectCallback(env, cTransportId, cDtlsParameters);
taoyaoJavaVM->DetachCurrentThread();
}
std::promise <void> promise;
promise.set_value();
return promise.get_future();
} }
void OnConnectionStateChange(mediasoupclient::Transport* transport, const std::string& connectionState) override { void OnConnectionStateChange(mediasoupclient::Transport* transport, const std::string& connectionState) override {
@@ -28,19 +37,26 @@ namespace acgist {
} }
std::future<std::string> OnProduce(mediasoupclient::SendTransport* transport, const std::string& kind, nlohmann::json rtpParameters, const nlohmann::json& appData) override { std::future<std::string> OnProduce(mediasoupclient::SendTransport* transport, const std::string& kind, nlohmann::json rtpParameters, const nlohmann::json& appData) override {
const std::string& cTransportId = transport->GetId(); const std::string& cTransportId = transport->GetId();
const std::string cRtpParameters = rtpParameters.dump(); const std::string cRtpParameters = rtpParameters.dump();
JNIEnv* env; JNIEnv* env;
this->room->javaVM->AttachCurrentThread(&env, nullptr); std::string result;
std::string result = this->room->sendTransportProduceCallback(env, kind, cTransportId, cRtpParameters); if(taoyaoJavaVM->GetEnv((void**) &env, JNI_VERSION_1_6) == JNI_OK) {
this->room->javaVM->DetachCurrentThread(); result = this->room->sendTransportProduceCallback(env, kind, cTransportId, cRtpParameters);
} else {
taoyaoJavaVM->AttachCurrentThreadAsDaemon(&env, nullptr);
result = this->room->sendTransportProduceCallback(env, kind, cTransportId, cRtpParameters);
taoyaoJavaVM->DetachCurrentThread();
}
std::promise <std::string> promise; std::promise <std::string> promise;
promise.set_value(result); promise.set_value(result);
return promise.get_future(); return promise.get_future();
} }
std::future<std::string> OnProduceData(mediasoupclient::SendTransport* transport, const nlohmann::json& sctpStreamParameters, const std::string& label, const std::string& protocol, const nlohmann::json& appData) override { std::future<std::string> OnProduceData(mediasoupclient::SendTransport* transport, const nlohmann::json& sctpStreamParameters, const std::string& label, const std::string& protocol, const nlohmann::json& appData) override {
return std::future<std::string>(); std::promise <std::string> promise;
// TODO实现
return promise.get_future();
} }
}; };
@@ -59,10 +75,16 @@ namespace acgist {
const std::string& cTransportId = transport->GetId(); const std::string& cTransportId = transport->GetId();
const std::string& cDtlsParameters = dtlsParameters.dump(); const std::string& cDtlsParameters = dtlsParameters.dump();
JNIEnv* env; JNIEnv* env;
this->room->javaVM->AttachCurrentThread(&env, nullptr); if(taoyaoJavaVM->GetEnv((void**) &env, JNI_VERSION_1_6) == JNI_OK) {
this->room->recvTransportConnectCallback(env, cTransportId, cDtlsParameters); this->room->recvTransportConnectCallback(env, cTransportId, cDtlsParameters);
this->room->javaVM->DetachCurrentThread(); } else {
return std::future<void>(); taoyaoJavaVM->AttachCurrentThreadAsDaemon(&env, nullptr);
this->room->recvTransportConnectCallback(env, cTransportId, cDtlsParameters);
taoyaoJavaVM->DetachCurrentThread();
}
std::promise <void> promise;
promise.set_value();
return promise.get_future();
} }
void OnConnectionStateChange(mediasoupclient::Transport* transport, const std::string& connectionState) override { void OnConnectionStateChange(mediasoupclient::Transport* transport, const std::string& connectionState) override {
@@ -83,9 +105,13 @@ namespace acgist {
void OnTransportClose(mediasoupclient::Producer* producer) override { void OnTransportClose(mediasoupclient::Producer* producer) override {
producer->Close(); producer->Close();
JNIEnv* env; JNIEnv* env;
this->room->javaVM->AttachCurrentThread(&env, nullptr); if(taoyaoJavaVM->GetEnv((void**) &env, JNI_VERSION_1_6) == JNI_OK) {
this->room->producerCloseCallback(env, producer->GetId()); this->room->producerCloseCallback(env, producer->GetId());
this->room->javaVM->DetachCurrentThread(); } else {
taoyaoJavaVM->AttachCurrentThreadAsDaemon(&env, nullptr);
this->room->producerCloseCallback(env, producer->GetId());
taoyaoJavaVM->DetachCurrentThread();
}
} }
}; };
@@ -103,24 +129,31 @@ namespace acgist {
void OnTransportClose(mediasoupclient::Consumer* consumer) override { void OnTransportClose(mediasoupclient::Consumer* consumer) override {
consumer->Close(); consumer->Close();
JNIEnv* env; JNIEnv* env;
this->room->javaVM->AttachCurrentThread(&env, nullptr); if(taoyaoJavaVM->GetEnv((void**) &env, JNI_VERSION_1_6) == JNI_OK) {
this->room->consumerCloseCallback(env, consumer->GetId()); this->room->consumerCloseCallback(env, consumer->GetId());
this->room->javaVM->DetachCurrentThread(); } else {
taoyaoJavaVM->AttachCurrentThreadAsDaemon(&env, nullptr);
this->room->consumerCloseCallback(env, consumer->GetId());
taoyaoJavaVM->DetachCurrentThread();
}
} }
}; };
Room::Room( Room::Room(
JavaVM* javaVM,
const std::string& roomId, const std::string& roomId,
const jobject& routerCallback const jobject& routerCallback
) { ) {
this->roomId = roomId;
this->javaVM = javaVM;
this->routerCallback = routerCallback; this->routerCallback = routerCallback;
this->roomId = roomId;
this->factory = nullptr;
this->device = new mediasoupclient::Device(); this->device = new mediasoupclient::Device();
this->sendTransport = nullptr;
this->recvTransport = nullptr;
this->sendListener = new SendListener(this); this->sendListener = new SendListener(this);
this->recvListener = new RecvListener(this); this->recvListener = new RecvListener(this);
this->audioProducer = nullptr;
this->videoProducer = nullptr;
this->producerListener = new ProducerListener(this); this->producerListener = new ProducerListener(this);
this->consumerListener = new ConsumerListener(this); this->consumerListener = new ConsumerListener(this);
} }
@@ -136,10 +169,13 @@ namespace acgist {
delete this->producerListener; delete this->producerListener;
delete this->consumerListener; delete this->consumerListener;
JNIEnv* env; JNIEnv* env;
this->javaVM->AttachCurrentThread(&env, nullptr); if(taoyaoJavaVM->GetEnv((void**) &env, JNI_VERSION_1_6) == JNI_OK) {
env->DeleteLocalRef(this->routerCallback); env->DeleteGlobalRef(this->routerCallback);
env->DeleteGlobalRef(this->routerCallback); } else {
this->javaVM->DetachCurrentThread(); taoyaoJavaVM->AttachCurrentThreadAsDaemon(&env, nullptr);
env->DeleteGlobalRef(this->routerCallback);
taoyaoJavaVM->DetachCurrentThread();
}
} }
void Room::enterRoom( void Room::enterRoom(
@@ -149,12 +185,11 @@ namespace acgist {
webrtc::PeerConnectionInterface::RTCConfiguration& rtcConfiguration webrtc::PeerConnectionInterface::RTCConfiguration& rtcConfiguration
) { ) {
this->factory = factory; this->factory = factory;
nlohmann::json json;
// TODO全局 // TODO全局
mediasoupclient::PeerConnection::Options options; mediasoupclient::PeerConnection::Options options;
options.config = rtcConfiguration; options.config = rtcConfiguration;
options.factory = factory; options.factory = factory;
json["routerRtpCapabilities"] = nlohmann::json::parse(rtpCapabilities); nlohmann::json json = nlohmann::json::parse(rtpCapabilities);
this->device->Load(json, &options); this->device->Load(json, &options);
const std::string cRtpCapabilities = this->device->GetRtpCapabilities().dump(); const std::string cRtpCapabilities = this->device->GetRtpCapabilities().dump();
const std::string cSctpCapabilities = this->device->GetSctpCapabilities().dump(); const std::string cSctpCapabilities = this->device->GetSctpCapabilities().dump();
@@ -320,8 +355,12 @@ namespace acgist {
} }
void Room::closeRoom() { void Room::closeRoom() {
this->audioProducer->Close(); if(this->audioProducer != nullptr) {
this->videoProducer->Close(); this->audioProducer->Close();
}
if(this->videoProducer != nullptr) {
this->videoProducer->Close();
}
std::map<std::string, mediasoupclient::Consumer *>::iterator iterator; std::map<std::string, mediasoupclient::Consumer *>::iterator iterator;
for (iterator = this->consumers.begin(); iterator != this->consumers.end(); iterator++) { for (iterator = this->consumers.begin(); iterator != this->consumers.end(); iterator++) {
iterator->second->Close(); iterator->second->Close();
@@ -330,12 +369,20 @@ namespace acgist {
// consumer->Close(); // consumer->Close();
// }); // });
this->consumers.clear(); this->consumers.clear();
this->sendTransport->Close(); if(this->sendTransport != nullptr) {
this->recvTransport->Close(); this->sendTransport->Close();
}
if(this->recvTransport != nullptr) {
this->recvTransport->Close();
}
JNIEnv* env; JNIEnv* env;
this->javaVM->AttachCurrentThread(&env, nullptr); if(taoyaoJavaVM->GetEnv((void**) &env, JNI_VERSION_1_6) == JNI_OK) {
this->closeRoomCallback(env); this->closeRoomCallback(env);
this->javaVM->DetachCurrentThread(); } else {
taoyaoJavaVM->AttachCurrentThreadAsDaemon(&env, nullptr);
this->closeRoomCallback(env);
taoyaoJavaVM->DetachCurrentThread();
}
} }
extern "C" JNIEXPORT jlong JNICALL extern "C" JNIEXPORT jlong JNICALL
@@ -343,11 +390,9 @@ namespace acgist {
JNIEnv* env, jobject me, JNIEnv* env, jobject me,
jstring jRoomId, jobject jRouterCallback jstring jRoomId, jobject jRouterCallback
) { ) {
JavaVM* javaVM;
env->GetJavaVM(&javaVM);
jobject routerCallback = env->NewGlobalRef(jRouterCallback); jobject routerCallback = env->NewGlobalRef(jRouterCallback);
const char* roomId = env->GetStringUTFChars(jRoomId, nullptr); const char* roomId = env->GetStringUTFChars(jRoomId, nullptr);
Room* room = new Room(javaVM, roomId, routerCallback); Room* room = new Room(roomId, routerCallback);
env->DeleteLocalRef(jRoomId); env->DeleteLocalRef(jRoomId);
env->ReleaseStringUTFChars(jRoomId, roomId); env->ReleaseStringUTFChars(jRoomId, roomId);
return (jlong) room; return (jlong) room;

View File

@@ -300,6 +300,7 @@ public final class MediaManager {
PeerConnectionFactory.InitializationOptions.builder(this.context) PeerConnectionFactory.InitializationOptions.builder(this.context)
// .setFieldTrials("WebRTC-IntelVP8/Enabled/") // .setFieldTrials("WebRTC-IntelVP8/Enabled/")
// .setFieldTrials("WebRTC-H264HighProfile/Enabled/") // .setFieldTrials("WebRTC-H264HighProfile/Enabled/")
// TODO测试是否需要c++全局加载JavaVM
// .setNativeLibraryName("jingle_peerconnection_so") // .setNativeLibraryName("jingle_peerconnection_so")
// .setEnableInternalTracer(true) // .setEnableInternalTracer(true)
.createInitializationOptions() .createInitializationOptions()

View File

@@ -289,6 +289,9 @@ class Session {
if(this.closed) { if(this.closed) {
return; return;
} }
if(!candidate || candidate.sdpMid === undefined || candidate.sdpMLineIndex === undefined && candidate.candidate === undefined) {
return;
}
if(this.peerConnection) { if(this.peerConnection) {
await this.peerConnection.addIceCandidate(new RTCIceCandidate(candidate)); await this.peerConnection.addIceCandidate(new RTCIceCandidate(candidate));
} else { } else {
@@ -2199,9 +2202,6 @@ class Taoyao extends RemoteClient {
} else if (type === "answer") { } else if (type === "answer") {
await session.peerConnection.setRemoteDescription(new RTCSessionDescription(message.body)); await session.peerConnection.setRemoteDescription(new RTCSessionDescription(message.body));
} else if (type === "candidate") { } else if (type === "candidate") {
if(!candidate || candidate.sdpMid === undefined || candidate.sdpMLineIndex === undefined && candidate.candidate === undefined) {
return;
}
await session.addIceCandidate(candidate); await session.addIceCandidate(candidate);
} else { } else {
} }

View File

@@ -37,6 +37,7 @@
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
<outputDirectory>config</outputDirectory> <outputDirectory>config</outputDirectory>
<includes> <includes>
<include>*.cer</include>
<include>*.jks</include> <include>*.jks</include>
<include>*.p12</include> <include>*.p12</include>
<include>*.pfx</include> <include>*.pfx</include>

View File

@@ -37,6 +37,7 @@
<directory>src/main/resources</directory> <directory>src/main/resources</directory>
<outputDirectory>config</outputDirectory> <outputDirectory>config</outputDirectory>
<includes> <includes>
<include>*.cer</include>
<include>*.jks</include> <include>*.jks</include>
<include>*.p12</include> <include>*.p12</include>
<include>*.pfx</include> <include>*.pfx</include>

View File

@@ -175,10 +175,12 @@
<configuration> <configuration>
<!-- Jar配置独立config目录 --> <!-- Jar配置独立config目录 -->
<excludes> <excludes>
<include>*.cer</include>
<include>*.jks</include> <include>*.jks</include>
<include>*.p12</include> <include>*.p12</include>
<include>*.pfx</include> <include>*.pfx</include>
<exclude>*.yml</exclude> <exclude>*.yml</exclude>
<include>*.TTF</include>
<exclude>*.properties</exclude> <exclude>*.properties</exclude>
</excludes> </excludes>
<archive> <archive>