mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-19 00:27:51 +01:00
Remove a sigslot from webrtc_session_description_factory
callback are know at construction time and only need some synchronization at destruction time. In this case such synchronization can be done with cheaper/simpler WeakPtr concept. Asynchronous call to SetCertificate is no longer needed thanks to previous removal of sigslot in https://webrtc-review.googlesource.com/c/src/+/192362 Bug: webrtc:11943 Change-Id: Icadbcb4f83be9ed4b8f53a72beaef8573f2c9356 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/272402 Reviewed-by: Harald Alvestrand <hta@webrtc.org> Commit-Queue: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/main@{#37868}
This commit is contained in:
parent
5cb3a90870
commit
b22f0c2238
3 changed files with 49 additions and 74 deletions
|
@ -1396,7 +1396,7 @@ rtc_source_set("webrtc_session_description_factory") {
|
||||||
"../rtc_base:rtc_base",
|
"../rtc_base:rtc_base",
|
||||||
"../rtc_base:stringutils",
|
"../rtc_base:stringutils",
|
||||||
"../rtc_base:threading",
|
"../rtc_base:threading",
|
||||||
"../rtc_base/third_party/sigslot:sigslot",
|
"../rtc_base:weak_ptr",
|
||||||
]
|
]
|
||||||
absl_deps = [
|
absl_deps = [
|
||||||
"//third_party/abseil-cpp/absl/algorithm:container",
|
"//third_party/abseil-cpp/absl/algorithm:container",
|
||||||
|
|
|
@ -71,7 +71,6 @@ static bool ValidMediaSessionOptions(
|
||||||
enum {
|
enum {
|
||||||
MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
|
MSG_CREATE_SESSIONDESCRIPTION_SUCCESS,
|
||||||
MSG_CREATE_SESSIONDESCRIPTION_FAILED,
|
MSG_CREATE_SESSIONDESCRIPTION_FAILED,
|
||||||
MSG_USE_CONSTRUCTOR_CERTIFICATE
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct CreateSessionDescriptionMsg : public rtc::MessageData {
|
struct CreateSessionDescriptionMsg : public rtc::MessageData {
|
||||||
|
@ -86,14 +85,28 @@ struct CreateSessionDescriptionMsg : public rtc::MessageData {
|
||||||
};
|
};
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
void WebRtcCertificateGeneratorCallback::OnFailure() {
|
class WebRtcSessionDescriptionFactory::WebRtcCertificateGeneratorCallback
|
||||||
SignalRequestFailed();
|
: public rtc::RTCCertificateGeneratorCallback {
|
||||||
|
public:
|
||||||
|
explicit WebRtcCertificateGeneratorCallback(
|
||||||
|
rtc::WeakPtr<WebRtcSessionDescriptionFactory> ptr)
|
||||||
|
: weak_ptr_(std::move(ptr)) {}
|
||||||
|
// `rtc::RTCCertificateGeneratorCallback` overrides.
|
||||||
|
void OnSuccess(
|
||||||
|
const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override {
|
||||||
|
if (weak_ptr_) {
|
||||||
|
weak_ptr_->SetCertificate(certificate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void OnFailure() override {
|
||||||
|
if (weak_ptr_) {
|
||||||
|
weak_ptr_->OnCertificateRequestFailed();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebRtcCertificateGeneratorCallback::OnSuccess(
|
private:
|
||||||
const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) {
|
rtc::WeakPtr<WebRtcSessionDescriptionFactory> weak_ptr_;
|
||||||
SignalCertificateReady(certificate);
|
};
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
// static
|
||||||
void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
|
void WebRtcSessionDescriptionFactory::CopyCandidatesFromSessionDescription(
|
||||||
|
@ -167,21 +180,14 @@ WebRtcSessionDescriptionFactory::WebRtcSessionDescriptionFactory(
|
||||||
certificate_request_state_ = CERTIFICATE_WAITING;
|
certificate_request_state_ = CERTIFICATE_WAITING;
|
||||||
|
|
||||||
RTC_LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
|
RTC_LOG(LS_VERBOSE) << "DTLS-SRTP enabled; has certificate parameter.";
|
||||||
// We already have a certificate but we wait to do `SetIdentity`; if we do
|
RTC_LOG(LS_INFO) << "Using certificate supplied to the constructor.";
|
||||||
// it in the constructor then the caller has not had a chance to connect to
|
SetCertificate(certificate);
|
||||||
// `SignalCertificateReady`.
|
|
||||||
signaling_thread_->Post(
|
|
||||||
RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
|
|
||||||
new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate.get()));
|
|
||||||
} else {
|
} else {
|
||||||
// Generate certificate.
|
// Generate certificate.
|
||||||
certificate_request_state_ = CERTIFICATE_WAITING;
|
certificate_request_state_ = CERTIFICATE_WAITING;
|
||||||
|
|
||||||
auto callback = rtc::make_ref_counted<WebRtcCertificateGeneratorCallback>();
|
auto callback = rtc::make_ref_counted<WebRtcCertificateGeneratorCallback>(
|
||||||
callback->SignalRequestFailed.connect(
|
weak_factory_.GetWeakPtr());
|
||||||
this, &WebRtcSessionDescriptionFactory::OnCertificateRequestFailed);
|
|
||||||
callback->SignalCertificateReady.connect(
|
|
||||||
this, &WebRtcSessionDescriptionFactory::SetCertificate);
|
|
||||||
|
|
||||||
rtc::KeyParams key_params = rtc::KeyParams();
|
rtc::KeyParams key_params = rtc::KeyParams();
|
||||||
RTC_LOG(LS_VERBOSE)
|
RTC_LOG(LS_VERBOSE)
|
||||||
|
@ -206,17 +212,7 @@ WebRtcSessionDescriptionFactory::~WebRtcSessionDescriptionFactory() {
|
||||||
rtc::MessageList list;
|
rtc::MessageList list;
|
||||||
signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
|
signaling_thread_->Clear(this, rtc::MQID_ANY, &list);
|
||||||
for (auto& msg : list) {
|
for (auto& msg : list) {
|
||||||
if (msg.message_id != MSG_USE_CONSTRUCTOR_CERTIFICATE) {
|
|
||||||
OnMessage(&msg);
|
OnMessage(&msg);
|
||||||
} else {
|
|
||||||
// Skip MSG_USE_CONSTRUCTOR_CERTIFICATE because we don't want to trigger
|
|
||||||
// SetIdentity-related callbacks in the destructor. This can be a problem
|
|
||||||
// when WebRtcSession listens to the callback but it was the WebRtcSession
|
|
||||||
// destructor that caused WebRtcSessionDescriptionFactory's destruction.
|
|
||||||
// The callback is then ignored, leaking memory allocated by OnMessage for
|
|
||||||
// MSG_USE_CONSTRUCTOR_CERTIFICATE.
|
|
||||||
delete msg.pdata;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -317,15 +313,6 @@ void WebRtcSessionDescriptionFactory::OnMessage(rtc::Message* msg) {
|
||||||
delete param;
|
delete param;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case MSG_USE_CONSTRUCTOR_CERTIFICATE: {
|
|
||||||
rtc::ScopedRefMessageData<rtc::RTCCertificate>* param =
|
|
||||||
static_cast<rtc::ScopedRefMessageData<rtc::RTCCertificate>*>(
|
|
||||||
msg->pdata);
|
|
||||||
RTC_LOG(LS_INFO) << "Using certificate supplied to the constructor.";
|
|
||||||
SetCertificate(param->data());
|
|
||||||
delete param;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
RTC_DCHECK_NOTREACHED();
|
RTC_DCHECK_NOTREACHED();
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -28,50 +28,18 @@
|
||||||
#include "rtc_base/message_handler.h"
|
#include "rtc_base/message_handler.h"
|
||||||
#include "rtc_base/rtc_certificate.h"
|
#include "rtc_base/rtc_certificate.h"
|
||||||
#include "rtc_base/rtc_certificate_generator.h"
|
#include "rtc_base/rtc_certificate_generator.h"
|
||||||
#include "rtc_base/third_party/sigslot/sigslot.h"
|
|
||||||
#include "rtc_base/thread.h"
|
#include "rtc_base/thread.h"
|
||||||
#include "rtc_base/thread_message.h"
|
#include "rtc_base/thread_message.h"
|
||||||
#include "rtc_base/unique_id_generator.h"
|
#include "rtc_base/unique_id_generator.h"
|
||||||
|
#include "rtc_base/weak_ptr.h"
|
||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
// DTLS certificate request callback class.
|
|
||||||
class WebRtcCertificateGeneratorCallback
|
|
||||||
: public rtc::RTCCertificateGeneratorCallback {
|
|
||||||
public:
|
|
||||||
// `rtc::RTCCertificateGeneratorCallback` overrides.
|
|
||||||
void OnSuccess(
|
|
||||||
const rtc::scoped_refptr<rtc::RTCCertificate>& certificate) override;
|
|
||||||
void OnFailure() override;
|
|
||||||
|
|
||||||
sigslot::signal0<> SignalRequestFailed;
|
|
||||||
sigslot::signal1<const rtc::scoped_refptr<rtc::RTCCertificate>&>
|
|
||||||
SignalCertificateReady;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CreateSessionDescriptionRequest {
|
|
||||||
enum Type {
|
|
||||||
kOffer,
|
|
||||||
kAnswer,
|
|
||||||
};
|
|
||||||
|
|
||||||
CreateSessionDescriptionRequest(Type type,
|
|
||||||
CreateSessionDescriptionObserver* observer,
|
|
||||||
const cricket::MediaSessionOptions& options)
|
|
||||||
: type(type), observer(observer), options(options) {}
|
|
||||||
|
|
||||||
Type type;
|
|
||||||
rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
|
|
||||||
cricket::MediaSessionOptions options;
|
|
||||||
};
|
|
||||||
|
|
||||||
// This class is used to create offer/answer session description. Certificates
|
// This class is used to create offer/answer session description. Certificates
|
||||||
// for WebRtcSession/DTLS are either supplied at construction or generated
|
// for WebRtcSession/DTLS are either supplied at construction or generated
|
||||||
// asynchronously. It queues the create offer/answer request until the
|
// asynchronously. It queues the create offer/answer request until the
|
||||||
// certificate generation has completed, i.e. when OnCertificateRequestFailed or
|
// certificate generation has completed, i.e. when OnCertificateRequestFailed or
|
||||||
// OnCertificateReady is called.
|
// OnCertificateReady is called.
|
||||||
class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
|
class WebRtcSessionDescriptionFactory : public rtc::MessageHandler {
|
||||||
public sigslot::has_slots<> {
|
|
||||||
public:
|
public:
|
||||||
// Can specify either a `cert_generator` or `certificate` to enable DTLS. If
|
// Can specify either a `cert_generator` or `certificate` to enable DTLS. If
|
||||||
// a certificate generator is given, starts generating the certificate
|
// a certificate generator is given, starts generating the certificate
|
||||||
|
@ -130,6 +98,25 @@ class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
|
||||||
CERTIFICATE_FAILED,
|
CERTIFICATE_FAILED,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// DTLS certificate request callback class.
|
||||||
|
class WebRtcCertificateGeneratorCallback;
|
||||||
|
|
||||||
|
struct CreateSessionDescriptionRequest {
|
||||||
|
enum Type {
|
||||||
|
kOffer,
|
||||||
|
kAnswer,
|
||||||
|
};
|
||||||
|
|
||||||
|
CreateSessionDescriptionRequest(Type type,
|
||||||
|
CreateSessionDescriptionObserver* observer,
|
||||||
|
const cricket::MediaSessionOptions& options)
|
||||||
|
: type(type), observer(observer), options(options) {}
|
||||||
|
|
||||||
|
Type type;
|
||||||
|
rtc::scoped_refptr<CreateSessionDescriptionObserver> observer;
|
||||||
|
cricket::MediaSessionOptions options;
|
||||||
|
};
|
||||||
|
|
||||||
// MessageHandler implementation.
|
// MessageHandler implementation.
|
||||||
virtual void OnMessage(rtc::Message* msg);
|
virtual void OnMessage(rtc::Message* msg);
|
||||||
|
|
||||||
|
@ -161,6 +148,7 @@ class WebRtcSessionDescriptionFactory : public rtc::MessageHandler,
|
||||||
|
|
||||||
std::function<void(const rtc::scoped_refptr<rtc::RTCCertificate>&)>
|
std::function<void(const rtc::scoped_refptr<rtc::RTCCertificate>&)>
|
||||||
on_certificate_ready_;
|
on_certificate_ready_;
|
||||||
|
rtc::WeakPtrFactory<WebRtcSessionDescriptionFactory> weak_factory_{this};
|
||||||
};
|
};
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue