Add thread safety annotation for PeerConnection::observer_ and factory_

Plus all the annotations that are necessary to make things compile
again.

(The latter could only be made const, since it is accessed from both
the signal thread and the worker thread.)

Bug: webrtc:9987
Change-Id: I49f1568d26a327f48cd94c2c70bc5939e9fbd59a
Reviewed-on: https://webrtc-review.googlesource.com/c/122842
Reviewed-by: Steve Anton <steveanton@webrtc.org>
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26715}
diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc
index fccd6df..3d0f664 100644
--- a/pc/peer_connection.cc
+++ b/pc/peer_connection.cc
@@ -897,6 +897,7 @@
 bool PeerConnection::Initialize(
     const PeerConnectionInterface::RTCConfiguration& configuration,
     PeerConnectionDependencies dependencies) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   TRACE_EVENT0("webrtc", "PeerConnection::Initialize");
 
   RTCError config_error = ValidateConfiguration(configuration);
@@ -1155,6 +1156,7 @@
 }
 
 bool PeerConnection::AddStream(MediaStreamInterface* local_stream) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   RTC_CHECK(!IsUnifiedPlan()) << "AddStream is not available with Unified Plan "
                                  "SdpSemantics. Please use AddTrack instead.";
   TRACE_EVENT0("webrtc", "PeerConnection::AddStream");
@@ -1190,6 +1192,7 @@
 }
 
 void PeerConnection::RemoveStream(MediaStreamInterface* local_stream) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   RTC_CHECK(!IsUnifiedPlan()) << "RemoveStream is not available with Unified "
                                  "Plan SdpSemantics. Please use RemoveTrack "
                                  "instead.";
@@ -1220,6 +1223,7 @@
 RTCErrorOr<rtc::scoped_refptr<RtpSenderInterface>> PeerConnection::AddTrack(
     rtc::scoped_refptr<MediaStreamTrackInterface> track,
     const std::vector<std::string>& stream_ids) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   TRACE_EVENT0("webrtc", "PeerConnection::AddTrack");
   if (!track) {
     LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Track is null.");
@@ -1354,6 +1358,7 @@
 
 RTCError PeerConnection::RemoveTrackNew(
     rtc::scoped_refptr<RtpSenderInterface> sender) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   if (!sender) {
     LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_PARAMETER, "Sender is null.");
   }
@@ -1413,6 +1418,7 @@
 PeerConnection::AddTransceiver(
     rtc::scoped_refptr<MediaStreamTrackInterface> track,
     const RtpTransceiverInit& init) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   RTC_CHECK(IsUnifiedPlan())
       << "AddTransceiver is only available with Unified Plan SdpSemantics";
   if (!track) {
@@ -1438,6 +1444,7 @@
 RTCErrorOr<rtc::scoped_refptr<RtpTransceiverInterface>>
 PeerConnection::AddTransceiver(cricket::MediaType media_type,
                                const RtpTransceiverInit& init) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   RTC_CHECK(IsUnifiedPlan())
       << "AddTransceiver is only available with Unified Plan SdpSemantics";
   if (!(media_type == cricket::MEDIA_TYPE_AUDIO ||
@@ -1939,6 +1946,7 @@
 
 void PeerConnection::AddUpToOneReceivingTransceiverOfType(
     cricket::MediaType media_type) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   if (GetReceivingTransceiversOfType(media_type).empty()) {
     RTC_LOG(LS_INFO)
         << "Adding one recvonly " << cricket::MediaTypeToString(media_type)
@@ -3375,6 +3383,7 @@
 
 bool PeerConnection::AddIceCandidate(
     const IceCandidateInterface* ice_candidate) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   TRACE_EVENT0("webrtc", "PeerConnection::AddIceCandidate");
   if (IsClosed()) {
     RTC_LOG(LS_ERROR) << "AddIceCandidate: PeerConnection is closed.";
@@ -3651,6 +3660,7 @@
 }
 
 void PeerConnection::Close() {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   TRACE_EVENT0("webrtc", "PeerConnection::Close");
   // Update stats here so that we have the most recent stats for tracks and
   // streams before the channels are closed.
@@ -3697,6 +3707,7 @@
 }
 
 void PeerConnection::OnMessage(rtc::Message* msg) {
+  RTC_DCHECK_RUN_ON(signaling_thread());
   switch (msg->message_id) {
     case MSG_SET_SESSIONDESCRIPTION_SUCCESS: {
       SetSessionDescriptionMsg* param =
@@ -3900,7 +3911,6 @@
 }
 
 void PeerConnection::SetIceConnectionState(IceConnectionState new_state) {
-  RTC_DCHECK(signaling_thread()->IsCurrent());
   if (ice_connection_state_ == new_state) {
     return;
   }
@@ -3922,7 +3932,6 @@
 
 void PeerConnection::SetStandardizedIceConnectionState(
     PeerConnectionInterface::IceConnectionState new_state) {
-  RTC_DCHECK(signaling_thread()->IsCurrent());
   if (standardized_ice_connection_state_ == new_state)
     return;
   if (IsClosed())
@@ -3933,7 +3942,6 @@
 
 void PeerConnection::SetConnectionState(
     PeerConnectionInterface::PeerConnectionState new_state) {
-  RTC_DCHECK(signaling_thread()->IsCurrent());
   if (connection_state_ == new_state)
     return;
   if (IsClosed())
@@ -3944,7 +3952,6 @@
 
 void PeerConnection::OnIceGatheringChange(
     PeerConnectionInterface::IceGatheringState new_state) {
-  RTC_DCHECK(signaling_thread()->IsCurrent());
   if (IsClosed()) {
     return;
   }
@@ -3954,7 +3961,6 @@
 
 void PeerConnection::OnIceCandidate(
     std::unique_ptr<IceCandidateInterface> candidate) {
-  RTC_DCHECK(signaling_thread()->IsCurrent());
   if (IsClosed()) {
     return;
   }
@@ -3968,7 +3974,6 @@
 
 void PeerConnection::OnIceCandidatesRemoved(
     const std::vector<cricket::Candidate>& candidates) {
-  RTC_DCHECK(signaling_thread()->IsCurrent());
   if (IsClosed()) {
     return;
   }
@@ -3977,7 +3982,6 @@
 
 void PeerConnection::ChangeSignalingState(
     PeerConnectionInterface::SignalingState signaling_state) {
-  RTC_DCHECK(signaling_thread()->IsCurrent());
   if (signaling_state_ == signaling_state) {
     return;
   }
@@ -5876,7 +5880,6 @@
 void PeerConnection::OnTransportControllerCandidatesGathered(
     const std::string& transport_name,
     const cricket::Candidates& candidates) {
-  RTC_DCHECK(signaling_thread()->IsCurrent());
   int sdp_mline_index;
   if (!GetLocalCandidateMediaIndex(transport_name, &sdp_mline_index)) {
     RTC_LOG(LS_ERROR)
@@ -5899,7 +5902,6 @@
 
 void PeerConnection::OnTransportControllerCandidatesRemoved(
     const std::vector<cricket::Candidate>& candidates) {
-  RTC_DCHECK(signaling_thread()->IsCurrent());
   // Sanity check.
   for (const cricket::Candidate& candidate : candidates) {
     if (candidate.transport_name().empty()) {
diff --git a/pc/peer_connection.h b/pc/peer_connection.h
index 660788f..321acde 100644
--- a/pc/peer_connection.h
+++ b/pc/peer_connection.h
@@ -321,10 +321,12 @@
   GetFirstAudioTransceiver() const;
 
   void CreateAudioReceiver(MediaStreamInterface* stream,
-                           const RtpSenderInfo& remote_sender_info);
+                           const RtpSenderInfo& remote_sender_info)
+      RTC_RUN_ON(signaling_thread());
 
   void CreateVideoReceiver(MediaStreamInterface* stream,
-                           const RtpSenderInfo& remote_sender_info);
+                           const RtpSenderInfo& remote_sender_info)
+      RTC_RUN_ON(signaling_thread());
   rtc::scoped_refptr<RtpReceiverInterface> RemoveAndStopReceiver(
       const RtpSenderInfo& remote_sender_info);
 
@@ -361,7 +363,7 @@
       cricket::MediaType media_type,
       rtc::scoped_refptr<MediaStreamTrackInterface> track,
       const RtpTransceiverInit& init,
-      bool fire_callback = true);
+      bool fire_callback = true) RTC_RUN_ON(signaling_thread());
 
   rtc::scoped_refptr<RtpSenderProxyWithInternal<RtpSenderInternal>>
   CreateSender(cricket::MediaType media_type,
@@ -381,32 +383,42 @@
       rtc::scoped_refptr<RtpReceiverProxyWithInternal<RtpReceiverInternal>>
           receiver);
 
-  void SetIceConnectionState(IceConnectionState new_state);
+  void SetIceConnectionState(IceConnectionState new_state)
+      RTC_RUN_ON(signaling_thread());
   void SetStandardizedIceConnectionState(
-      PeerConnectionInterface::IceConnectionState new_state);
+      PeerConnectionInterface::IceConnectionState new_state)
+      RTC_RUN_ON(signaling_thread());
   void SetConnectionState(
-      PeerConnectionInterface::PeerConnectionState new_state);
+      PeerConnectionInterface::PeerConnectionState new_state)
+      RTC_RUN_ON(signaling_thread());
 
   // Called any time the IceGatheringState changes
-  void OnIceGatheringChange(IceGatheringState new_state);
+  void OnIceGatheringChange(IceGatheringState new_state)
+      RTC_RUN_ON(signaling_thread());
   // New ICE candidate has been gathered.
-  void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate);
+  void OnIceCandidate(std::unique_ptr<IceCandidateInterface> candidate)
+      RTC_RUN_ON(signaling_thread());
   // Some local ICE candidates have been removed.
-  void OnIceCandidatesRemoved(
-      const std::vector<cricket::Candidate>& candidates);
+  void OnIceCandidatesRemoved(const std::vector<cricket::Candidate>& candidates)
+      RTC_RUN_ON(signaling_thread());
 
   // Update the state, signaling if necessary.
-  void ChangeSignalingState(SignalingState signaling_state);
+  void ChangeSignalingState(SignalingState signaling_state)
+      RTC_RUN_ON(signaling_thread());
 
   // Signals from MediaStreamObserver.
   void OnAudioTrackAdded(AudioTrackInterface* track,
-                         MediaStreamInterface* stream);
+                         MediaStreamInterface* stream)
+      RTC_RUN_ON(signaling_thread());
   void OnAudioTrackRemoved(AudioTrackInterface* track,
-                           MediaStreamInterface* stream);
+                           MediaStreamInterface* stream)
+      RTC_RUN_ON(signaling_thread());
   void OnVideoTrackAdded(VideoTrackInterface* track,
-                         MediaStreamInterface* stream);
+                         MediaStreamInterface* stream)
+      RTC_RUN_ON(signaling_thread());
   void OnVideoTrackRemoved(VideoTrackInterface* track,
-                           MediaStreamInterface* stream);
+                           MediaStreamInterface* stream)
+      RTC_RUN_ON(signaling_thread());
 
   void PostSetSessionDescriptionSuccess(
       SetSessionDescriptionObserver* observer);
@@ -576,7 +588,8 @@
 
   // Remove all local and remote senders of type |media_type|.
   // Called when a media type is rejected (m-line set to port 0).
-  void RemoveSenders(cricket::MediaType media_type);
+  void RemoveSenders(cricket::MediaType media_type)
+      RTC_RUN_ON(signaling_thread());
 
   // Makes sure a MediaStreamTrack is created for each StreamParam in |streams|,
   // and existing MediaStreamTracks are removed if there is no corresponding
@@ -588,24 +601,26 @@
       const std::vector<cricket::StreamParams>& streams,
       bool default_track_needed,
       cricket::MediaType media_type,
-      StreamCollection* new_streams);
+      StreamCollection* new_streams) RTC_RUN_ON(signaling_thread());
 
   // Triggered when a remote sender has been seen for the first time in a remote
   // session description. It creates a remote MediaStreamTrackInterface
   // implementation and triggers CreateAudioReceiver or CreateVideoReceiver.
   void OnRemoteSenderAdded(const RtpSenderInfo& sender_info,
-                           cricket::MediaType media_type);
+                           cricket::MediaType media_type)
+      RTC_RUN_ON(signaling_thread());
 
   // Triggered when a remote sender has been removed from a remote session
   // description. It removes the remote sender with id |sender_id| from a remote
   // MediaStream and triggers DestroyAudioReceiver or DestroyVideoReceiver.
   void OnRemoteSenderRemoved(const RtpSenderInfo& sender_info,
-                             cricket::MediaType media_type);
+                             cricket::MediaType media_type)
+      RTC_RUN_ON(signaling_thread());
 
   // Finds remote MediaStreams without any tracks and removes them from
   // |remote_streams_| and notifies the observer that the MediaStreams no longer
   // exist.
-  void UpdateEndedRemoteMediaStreams();
+  void UpdateEndedRemoteMediaStreams() RTC_RUN_ON(signaling_thread());
 
   // Loops through the vector of |streams| and finds added and removed
   // StreamParams since last time this method was called.
@@ -831,9 +846,11 @@
                                    int* sdp_mline_index);
   // Uses all remote candidates in |remote_desc| in this session.
   bool UseCandidatesInSessionDescription(
-      const SessionDescriptionInterface* remote_desc);
+      const SessionDescriptionInterface* remote_desc)
+      RTC_RUN_ON(signaling_thread());
   // Uses |candidate| in this session.
-  bool UseCandidate(const IceCandidateInterface* candidate);
+  bool UseCandidate(const IceCandidateInterface* candidate)
+      RTC_RUN_ON(signaling_thread());
   // Deletes the corresponding channel of contents that don't exist in |desc|.
   // |desc| can be null. This means that all channels are deleted.
   void RemoveUnusedChannels(const cricket::SessionDescription* desc);
@@ -908,13 +925,17 @@
   bool SrtpRequired() const;
 
   // JsepTransportController signal handlers.
-  void OnTransportControllerConnectionState(cricket::IceConnectionState state);
-  void OnTransportControllerGatheringState(cricket::IceGatheringState state);
+  void OnTransportControllerConnectionState(cricket::IceConnectionState state)
+      RTC_RUN_ON(signaling_thread());
+  void OnTransportControllerGatheringState(cricket::IceGatheringState state)
+      RTC_RUN_ON(signaling_thread());
   void OnTransportControllerCandidatesGathered(
       const std::string& transport_name,
-      const std::vector<cricket::Candidate>& candidates);
+      const std::vector<cricket::Candidate>& candidates)
+      RTC_RUN_ON(signaling_thread());
   void OnTransportControllerCandidatesRemoved(
-      const std::vector<cricket::Candidate>& candidates);
+      const std::vector<cricket::Candidate>& candidates)
+      RTC_RUN_ON(signaling_thread());
   void OnTransportControllerDtlsHandshakeError(rtc::SSLHandshakeError error);
 
   const char* SessionErrorToString(SessionError error) const;
@@ -938,7 +959,7 @@
                                const std::set<cricket::MediaType>& media_types);
 
   void NoteUsageEvent(UsageEvent event);
-  void ReportUsagePattern() const;
+  void ReportUsagePattern() const RTC_RUN_ON(signaling_thread());
 
   void OnSentPacket_w(const rtc::SentPacket& sent_packet);
 
@@ -969,7 +990,7 @@
                           MediaTransportInterface* media_transport) override;
 
   // Returns the observer. Will crash on CHECK if the observer is removed.
-  PeerConnectionObserver* Observer() const;
+  PeerConnectionObserver* Observer() const RTC_RUN_ON(signaling_thread());
 
   // Returns the CryptoOptions for this PeerConnection. This will always
   // return the RTCConfiguration.crypto_options if set and will only default
@@ -1007,8 +1028,9 @@
   // However, since the reference counting is done in the
   // PeerConnectionFactoryInterface all instances created using the raw pointer
   // will refer to the same reference count.
-  rtc::scoped_refptr<PeerConnectionFactory> factory_;
-  PeerConnectionObserver* observer_ = nullptr;
+  const rtc::scoped_refptr<PeerConnectionFactory> factory_;
+  PeerConnectionObserver* observer_ RTC_GUARDED_BY(signaling_thread()) =
+      nullptr;
 
   // The EventLog needs to outlive |call_| (and any other object that uses it).
   std::unique_ptr<RtcEventLog> event_log_;