Revert of Remove global list of SRTP sessions. (patchset #4 id:60001 of https://codereview.webrtc.org/1416093010/ )

Reason for revert:
Unfortunately this breaks an internal downstream project since we have an ancient libsrtp. Reverting until we can figure out how to update our libsrtp.

Original issue's description:
> Remove global list of SRTP sessions.
> Instead save a reference to the SrtpSession inside the srtp_ctx_t.
>
> BUG=webrtc:5133
>
> Committed: https://crrev.com/9cafd972779ed7b25886ab276e0ede7b7a8b76a1
> Cr-Commit-Position: refs/heads/master@{#10591}

TBR=juberti@google.com,juberti@webrtc.org,jbauch@webrtc.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=webrtc:5133

Review URL: https://codereview.webrtc.org/1442863003

Cr-Commit-Position: refs/heads/master@{#10635}
diff --git a/talk/session/media/srtpfilter.cc b/talk/session/media/srtpfilter.cc
index 4b2412f..079ddfb 100644
--- a/talk/session/media/srtpfilter.cc
+++ b/talk/session/media/srtpfilter.cc
@@ -481,7 +481,7 @@
 
 bool SrtpSession::inited_ = false;
 
-// This lock protects SrtpSession::inited_.
+// This lock protects SrtpSession::inited_ and SrtpSession::sessions_.
 rtc::GlobalLockPod SrtpSession::lock_;
 
 SrtpSession::SrtpSession()
@@ -490,13 +490,19 @@
       rtcp_auth_tag_len_(0),
       srtp_stat_(new SrtpStat()),
       last_send_seq_num_(-1) {
+  {
+    rtc::GlobalLockScope ls(&lock_);
+    sessions()->push_back(this);
+  }
   SignalSrtpError.repeat(srtp_stat_->SignalSrtpError);
 }
 
 SrtpSession::~SrtpSession() {
-  RTC_DCHECK(thread_checker_.CalledOnValidThread());
+  {
+    rtc::GlobalLockScope ls(&lock_);
+    sessions()->erase(std::find(sessions()->begin(), sessions()->end(), this));
+  }
   if (session_) {
-    session_->user_data = nullptr;
     srtp_dealloc(session_);
   }
 }
@@ -510,7 +516,6 @@
 }
 
 bool SrtpSession::ProtectRtp(void* p, int in_len, int max_len, int* out_len) {
-  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   if (!session_) {
     LOG(LS_WARNING) << "Failed to protect SRTP packet: no SRTP Session";
     return false;
@@ -553,7 +558,6 @@
 }
 
 bool SrtpSession::ProtectRtcp(void* p, int in_len, int max_len, int* out_len) {
-  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   if (!session_) {
     LOG(LS_WARNING) << "Failed to protect SRTCP packet: no SRTP Session";
     return false;
@@ -577,7 +581,6 @@
 }
 
 bool SrtpSession::UnprotectRtp(void* p, int in_len, int* out_len) {
-  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   if (!session_) {
     LOG(LS_WARNING) << "Failed to unprotect SRTP packet: no SRTP Session";
     return false;
@@ -597,7 +600,6 @@
 }
 
 bool SrtpSession::UnprotectRtcp(void* p, int in_len, int* out_len) {
-  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   if (!session_) {
     LOG(LS_WARNING) << "Failed to unprotect SRTCP packet: no SRTP Session";
     return false;
@@ -615,7 +617,6 @@
 
 bool SrtpSession::GetRtpAuthParams(uint8_t** key, int* key_len, int* tag_len) {
 #if defined(ENABLE_EXTERNAL_AUTH)
-  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   ExternalHmacContext* external_hmac = NULL;
   // stream_template will be the reference context for other streams.
   // Let's use it for getting the keys.
@@ -642,7 +643,6 @@
 bool SrtpSession::GetSendStreamPacketIndex(void* p,
                                            int in_len,
                                            int64_t* index) {
-  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   srtp_hdr_t* hdr = reinterpret_cast<srtp_hdr_t*>(p);
   srtp_stream_ctx_t* stream = srtp_get_stream(session_, hdr->ssrc);
   if (stream == NULL)
@@ -662,7 +662,6 @@
                          const std::string& cs,
                          const uint8_t* key,
                          int len) {
-  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   if (session_) {
     LOG(LS_ERROR) << "Failed to create SRTP session: "
                   << "SRTP session already created";
@@ -718,7 +717,7 @@
     return false;
   }
 
-  session_->user_data = this;
+
   rtp_auth_tag_len_ = policy.rtp.auth_tag_len;
   rtcp_auth_tag_len_ = policy.rtcp.auth_tag_len;
   return true;
@@ -767,7 +766,6 @@
 }
 
 void SrtpSession::HandleEvent(const srtp_event_data_t* ev) {
-  RTC_DCHECK(thread_checker_.CalledOnValidThread());
   switch (ev->event) {
     case event_ssrc_collision:
       LOG(LS_INFO) << "SRTP event: SSRC collision";
@@ -788,14 +786,22 @@
 }
 
 void SrtpSession::HandleEventThunk(srtp_event_data_t* ev) {
-  // Callback will be executed from same thread that calls the "srtp_protect"
-  // and "srtp_unprotect" functions.
-  SrtpSession* session = static_cast<SrtpSession*>(ev->session->user_data);
-  if (session) {
-    session->HandleEvent(ev);
+  rtc::GlobalLockScope ls(&lock_);
+
+  for (std::list<SrtpSession*>::iterator it = sessions()->begin();
+       it != sessions()->end(); ++it) {
+    if ((*it)->session_ == ev->session) {
+      (*it)->HandleEvent(ev);
+      break;
+    }
   }
 }
 
+std::list<SrtpSession*>* SrtpSession::sessions() {
+  RTC_DEFINE_STATIC_LOCAL(std::list<SrtpSession*>, sessions, ());
+  return &sessions;
+}
+
 #else   // !HAVE_SRTP
 
 // On some systems, SRTP is not (yet) available.
diff --git a/talk/session/media/srtpfilter.h b/talk/session/media/srtpfilter.h
index fb52222..3c3a8e8 100644
--- a/talk/session/media/srtpfilter.h
+++ b/talk/session/media/srtpfilter.h
@@ -40,7 +40,6 @@
 #include "webrtc/base/scoped_ptr.h"
 #include "webrtc/base/sigslotrepeater.h"
 #include "webrtc/base/sslstreamadapter.h"
-#include "webrtc/base/thread_checker.h"
 
 // Forward declaration to avoid pulling in libsrtp headers here
 struct srtp_event_data_t;
@@ -241,7 +240,8 @@
   void HandleEvent(const srtp_event_data_t* ev);
   static void HandleEventThunk(srtp_event_data_t* ev);
 
-  rtc::ThreadChecker thread_checker_;
+  static std::list<SrtpSession*>* sessions();
+
   srtp_ctx_t* session_;
   int rtp_auth_tag_len_;
   int rtcp_auth_tag_len_;