From 6e312e51d7982ee94f2a79c468c6cc4c3f78bf03 Mon Sep 17 00:00:00 2001 From: Philipp Hancke Date: Wed, 21 Aug 2024 16:57:35 -0700 Subject: [PATCH] install libsrtp log handler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit which may show useful debug logging. Also document that we need to forward-declare the internal srtp_ctx_ struct instead of srtp_t. BUG=webrtc:361372443 Change-Id: I76b1a4fb385af0fc1532f0ce6d0692b804f003dd Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/360182 Reviewed-by: Henrik Boström Reviewed-by: Harald Alvestrand Commit-Queue: Philipp Hancke Cr-Commit-Position: refs/heads/main@{#43022} --- pc/srtp_session.cc | 44 +++++++++++++++++++++++++++++++++++++++----- pc/srtp_session.h | 2 +- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/pc/srtp_session.cc b/pc/srtp_session.cc index f16679cef1..ee78710709 100644 --- a/pc/srtp_session.cc +++ b/pc/srtp_session.cc @@ -44,6 +44,12 @@ class LibSrtpInitializer { static LibSrtpInitializer* const instance = new LibSrtpInitializer(); return *instance; } + + // There is only one global log handler in libsrtp so we can not resolve this + // to a particular session. + static void LibSrtpLogHandler(srtp_log_level_t level, + const char* msg, + void* data); void ProhibitLibsrtpInitialization(); // These methods are responsible for initializing libsrtp (if the usage count @@ -52,7 +58,7 @@ class LibSrtpInitializer { // // Returns true if successful (will always be successful if already inited). bool IncrementLibsrtpUsageCountAndMaybeInit( - srtp_event_handler_func_t* handler); + srtp_event_handler_func_t* event_handler); void DecrementLibsrtpUsageCountAndMaybeDeinit(); private: @@ -62,25 +68,48 @@ class LibSrtpInitializer { int usage_count_ RTC_GUARDED_BY(mutex_) = 0; }; +void LibSrtpInitializer::LibSrtpLogHandler(srtp_log_level_t level, + const char* msg, + void* data) { + RTC_DCHECK(data == nullptr); + if (level == srtp_log_level_error) { + RTC_LOG(LS_ERROR) << "SRTP log: " << msg; + } else if (level == srtp_log_level_warning) { + RTC_LOG(LS_WARNING) << "SRTP log: " << msg; + } else if (level == srtp_log_level_info) { + RTC_LOG(LS_INFO) << "SRTP log: " << msg; + } else if (level == srtp_log_level_debug) { + RTC_LOG(LS_VERBOSE) << "SRTP log: " << msg; + } +} + void LibSrtpInitializer::ProhibitLibsrtpInitialization() { webrtc::MutexLock lock(&mutex_); ++usage_count_; } bool LibSrtpInitializer::IncrementLibsrtpUsageCountAndMaybeInit( - srtp_event_handler_func_t* handler) { + srtp_event_handler_func_t* event_handler) { webrtc::MutexLock lock(&mutex_); + RTC_DCHECK(event_handler); RTC_DCHECK_GE(usage_count_, 0); if (usage_count_ == 0) { int err; + + err = srtp_install_log_handler(&LibSrtpInitializer::LibSrtpLogHandler, + nullptr); + if (err != srtp_err_status_ok) { + RTC_LOG(LS_ERROR) << "Failed to install libsrtp log handler, err=" << err; + return false; + } err = srtp_init(); if (err != srtp_err_status_ok) { RTC_LOG(LS_ERROR) << "Failed to init SRTP, err=" << err; return false; } - err = srtp_install_event_handler(handler); + err = srtp_install_event_handler(event_handler); if (err != srtp_err_status_ok) { RTC_LOG(LS_ERROR) << "Failed to install SRTP event handler, err=" << err; return false; @@ -101,8 +130,13 @@ void LibSrtpInitializer::DecrementLibsrtpUsageCountAndMaybeDeinit() { RTC_DCHECK_GE(usage_count_, 1); if (--usage_count_ == 0) { - int err = srtp_shutdown(); - if (err) { + int err = srtp_install_log_handler(nullptr, nullptr); + if (err != srtp_err_status_ok) { + RTC_LOG(LS_ERROR) << "Failed to uninstall libsrtp log handler, err=" + << err; + } + err = srtp_shutdown(); + if (err != srtp_err_status_ok) { RTC_LOG(LS_ERROR) << "srtp_shutdown failed. err=" << err; } } diff --git a/pc/srtp_session.h b/pc/srtp_session.h index f8fd3e3123..560f32fcd4 100644 --- a/pc/srtp_session.h +++ b/pc/srtp_session.h @@ -23,7 +23,7 @@ // Forward declaration to avoid pulling in libsrtp headers here struct srtp_event_data_t; -struct srtp_ctx_t_; +struct srtp_ctx_t_; // Trailing _ is required. namespace cricket {