install libsrtp log handler

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 <hbos@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Philipp Hancke <phancke@meta.com>
Cr-Commit-Position: refs/heads/main@{#43022}
diff --git a/pc/srtp_session.cc b/pc/srtp_session.cc
index f16679c..ee78710 100644
--- a/pc/srtp_session.cc
+++ b/pc/srtp_session.cc
@@ -44,6 +44,12 @@
     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 @@
   //
   // 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 @@
   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 @@
 
   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 f8fd3e3..560f32f 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 {