Add packet_sequence_checker_ to RtpVideoStreamReceiver2.

Specifying guards for functions and member variables. Also updating
a few places for VideoReceiveStream2 accordingly.

Bug: webrtc:11993
Change-Id: I2d13b009ec9853c6b2d90b08af555ecdd2b1ced6
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/220765
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Markus Handell <handellm@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34174}
diff --git a/video/rtp_video_stream_receiver2.cc b/video/rtp_video_stream_receiver2.cc
index 8a71181..e9cdde0 100644
--- a/video/rtp_video_stream_receiver2.cc
+++ b/video/rtp_video_stream_receiver2.cc
@@ -134,17 +134,18 @@
   RTC_DCHECK(key_frame_request_sender_);
   RTC_DCHECK(nack_sender_);
   RTC_DCHECK(loss_notification_sender_);
+  packet_sequence_checker_.Detach();
 }
 
 void RtpVideoStreamReceiver2::RtcpFeedbackBuffer::RequestKeyFrame() {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   request_key_frame_ = true;
 }
 
 void RtpVideoStreamReceiver2::RtcpFeedbackBuffer::SendNack(
     const std::vector<uint16_t>& sequence_numbers,
     bool buffering_allowed) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   RTC_DCHECK(!sequence_numbers.empty());
   nack_sequence_numbers_.insert(nack_sequence_numbers_.end(),
                                 sequence_numbers.cbegin(),
@@ -161,7 +162,7 @@
     uint16_t last_received_seq_num,
     bool decodability_flag,
     bool buffering_allowed) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   RTC_DCHECK(buffering_allowed);
   RTC_DCHECK(!lntf_state_)
       << "SendLossNotification() called twice in a row with no call to "
@@ -171,7 +172,7 @@
 }
 
 void RtpVideoStreamReceiver2::RtcpFeedbackBuffer::SendBufferedRtcpFeedback() {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
 
   bool request_key_frame = false;
   std::vector<uint16_t> nack_sequence_numbers;
@@ -255,6 +256,7 @@
       has_received_frame_(false),
       frames_decryptable_(false),
       absolute_capture_time_interpolator_(clock) {
+  packet_sequence_checker_.Detach();
   constexpr bool remb_candidate = true;
   if (packet_router_)
     packet_router_->AddReceiveRtpModule(rtp_rtcp_.get(), remb_candidate);
@@ -329,7 +331,7 @@
     const VideoCodec& video_codec,
     const std::map<std::string, std::string>& codec_params,
     bool raw_payload) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   if (codec_params.count(cricket::kH264FmtpSpsPpsIdrInKeyframe) ||
       field_trial::IsEnabled("WebRTC-SpsPpsIdrIsH264Keyframe")) {
     packet_buffer_.ForceSpsPpsIdrIsH264Keyframe();
@@ -342,7 +344,7 @@
 }
 
 absl::optional<Syncable::Info> RtpVideoStreamReceiver2::GetSyncInfo() const {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   Syncable::Info info;
   if (rtp_rtcp_->RemoteNTP(&info.capture_time_ntp_secs,
                            &info.capture_time_ntp_frac,
@@ -362,6 +364,7 @@
   return info;
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 RtpVideoStreamReceiver2::ParseGenericDependenciesResult
 RtpVideoStreamReceiver2::ParseGenericDependenciesExtension(
     const RtpPacketReceived& rtp_packet,
@@ -471,7 +474,7 @@
     rtc::CopyOnWriteBuffer codec_payload,
     const RtpPacketReceived& rtp_packet,
     const RTPVideoHeader& video) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
 
   auto packet =
       std::make_unique<video_coding::PacketBuffer::Packet>(rtp_packet, video);
@@ -629,6 +632,8 @@
 
 void RtpVideoStreamReceiver2::OnRecoveredPacket(const uint8_t* rtp_packet,
                                                 size_t rtp_packet_length) {
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
+
   RtpPacketReceived packet;
   if (!packet.Parse(rtp_packet, rtp_packet_length))
     return;
@@ -651,11 +656,10 @@
 // This method handles both regular RTP packets and packets recovered
 // via FlexFEC.
 void RtpVideoStreamReceiver2::OnRtpPacket(const RtpPacketReceived& packet) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
 
-  if (!receiving_) {
+  if (!receiving_)
     return;
-  }
 
   ReceivePacket(packet);
 
@@ -712,9 +716,9 @@
   return frames_decryptable_;
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 void RtpVideoStreamReceiver2::OnInsertedPacket(
     video_coding::PacketBuffer::InsertResult result) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
   video_coding::PacketBuffer::Packet* first_packet = nullptr;
   int max_nack_count;
   int64_t min_recv_time;
@@ -789,9 +793,9 @@
   }
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 void RtpVideoStreamReceiver2::OnAssembledFrame(
     std::unique_ptr<RtpFrameObject> frame) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
   RTC_DCHECK(frame);
 
   const absl::optional<RTPVideoHeader::GenericDescriptorInfo>& descriptor =
@@ -856,10 +860,10 @@
   }
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 void RtpVideoStreamReceiver2::OnCompleteFrames(
     RtpFrameReferenceFinder::ReturnVector frames) {
   for (auto& frame : frames) {
-    RTC_DCHECK_RUN_ON(&worker_task_checker_);
     RtpFrameObject* rtp_frame = static_cast<RtpFrameObject*>(frame.get());
     last_seq_num_for_pic_id_[rtp_frame->Id()] = rtp_frame->last_seq_num();
 
@@ -871,7 +875,7 @@
 
 void RtpVideoStreamReceiver2::OnDecryptedFrame(
     std::unique_ptr<RtpFrameObject> frame) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   OnCompleteFrames(reference_finder_->ManageFrame(std::move(frame)));
 }
 
@@ -886,7 +890,9 @@
 
 void RtpVideoStreamReceiver2::SetFrameDecryptor(
     rtc::scoped_refptr<FrameDecryptorInterface> frame_decryptor) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  // TODO(bugs.webrtc.org/11993): Update callers or post the operation over to
+  // the network thread.
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   if (buffered_frame_decryptor_ == nullptr) {
     buffered_frame_decryptor_ =
         std::make_unique<BufferedFrameDecryptor>(this, this);
@@ -911,7 +917,7 @@
 }
 
 absl::optional<int64_t> RtpVideoStreamReceiver2::LastReceivedPacketMs() const {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   if (last_received_rtp_system_time_) {
     return absl::optional<int64_t>(last_received_rtp_system_time_->ms());
   }
@@ -920,7 +926,7 @@
 
 absl::optional<int64_t> RtpVideoStreamReceiver2::LastReceivedKeyframePacketMs()
     const {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   if (last_received_keyframe_rtp_system_time_) {
     return absl::optional<int64_t>(
         last_received_keyframe_rtp_system_time_->ms());
@@ -930,12 +936,12 @@
 
 void RtpVideoStreamReceiver2::ManageFrame(
     std::unique_ptr<RtpFrameObject> frame) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   OnCompleteFrames(reference_finder_->ManageFrame(std::move(frame)));
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 void RtpVideoStreamReceiver2::ReceivePacket(const RtpPacketReceived& packet) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
   if (packet.payload_size() == 0) {
     // Padding or keep-alive packet.
     // TODO(nisse): Could drop empty packets earlier, but need to figure out how
@@ -963,9 +969,9 @@
                         parsed_payload->video_header);
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 void RtpVideoStreamReceiver2::ParseAndHandleEncapsulatingHeader(
     const RtpPacketReceived& packet) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
   if (packet.PayloadType() == config_.rtp.red_payload_type &&
       packet.payload_size() > 0) {
     if (packet.payload()[0] == config_.rtp.ulpfec_payload_type) {
@@ -984,9 +990,8 @@
 // In the case of a video stream without picture ids and no rtx the
 // RtpFrameReferenceFinder will need to know about padding to
 // correctly calculate frame references.
+// RTC_RUN_ON(packet_sequence_checker_)
 void RtpVideoStreamReceiver2::NotifyReceiverOfEmptyPacket(uint16_t seq_num) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
-
   OnCompleteFrames(reference_finder_->PaddingReceived(seq_num));
 
   OnInsertedPacket(packet_buffer_.InsertPadding(seq_num));
@@ -1003,7 +1008,7 @@
 
 bool RtpVideoStreamReceiver2::DeliverRtcp(const uint8_t* rtcp_packet,
                                           size_t rtcp_packet_length) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
 
   if (!receiving_) {
     return false;
@@ -1045,7 +1050,7 @@
 }
 
 void RtpVideoStreamReceiver2::FrameContinuous(int64_t picture_id) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   if (!nack_module_)
     return;
 
@@ -1058,8 +1063,7 @@
 }
 
 void RtpVideoStreamReceiver2::FrameDecoded(int64_t picture_id) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
-  // Running on the decoder thread.
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   int seq_num = -1;
   auto seq_num_it = last_seq_num_for_pic_id_.find(picture_id);
   if (seq_num_it != last_seq_num_for_pic_id_.end()) {
@@ -1084,12 +1088,12 @@
 }
 
 void RtpVideoStreamReceiver2::StartReceive() {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   receiving_ = true;
 }
 
 void RtpVideoStreamReceiver2::StopReceive() {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   receiving_ = false;
 }
 
@@ -1120,9 +1124,8 @@
   }
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 void RtpVideoStreamReceiver2::InsertSpsPpsIntoTracker(uint8_t payload_type) {
-  RTC_DCHECK_RUN_ON(&worker_task_checker_);
-
   auto codec_params_it = pt_codec_params_.find(payload_type);
   if (codec_params_it == pt_codec_params_.end())
     return;
diff --git a/video/rtp_video_stream_receiver2.h b/video/rtp_video_stream_receiver2.h
index ab14060..78b8177 100644
--- a/video/rtp_video_stream_receiver2.h
+++ b/video/rtp_video_stream_receiver2.h
@@ -121,7 +121,7 @@
 
   // Returns number of different frames seen.
   int GetUniqueFramesSeen() const {
-    RTC_DCHECK_RUN_ON(&worker_task_checker_);
+    RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
     return frame_counter_.GetUniqueSeen();
   }
 
@@ -156,8 +156,6 @@
   // Don't use, still experimental.
   void RequestPacketRetransmit(const std::vector<uint16_t>& sequence_numbers);
 
-  void OnCompleteFrames(RtpFrameReferenceFinder::ReturnVector frame);
-
   // Implements OnDecryptedFrameCallback.
   void OnDecryptedFrame(std::unique_ptr<RtpFrameObject> frame) override;
 
@@ -185,6 +183,9 @@
   // Implements RtpVideoFrameReceiver.
   void ManageFrame(std::unique_ptr<RtpFrameObject> frame) override;
 
+  void OnCompleteFrames(RtpFrameReferenceFinder::ReturnVector frame)
+      RTC_RUN_ON(packet_sequence_checker_);
+
   // Used for buffering RTCP feedback messages and sending them all together.
   // Note:
   // 1. Key frame requests and NACKs are mutually exclusive, with the
@@ -232,20 +233,20 @@
       bool decodability_flag;
     };
 
-    RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_task_checker_;
+    RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_;
     KeyFrameRequestSender* const key_frame_request_sender_;
     NackSender* const nack_sender_;
     LossNotificationSender* const loss_notification_sender_;
 
     // Key-frame-request-related state.
-    bool request_key_frame_ RTC_GUARDED_BY(worker_task_checker_);
+    bool request_key_frame_ RTC_GUARDED_BY(packet_sequence_checker_);
 
     // NACK-related state.
     std::vector<uint16_t> nack_sequence_numbers_
-        RTC_GUARDED_BY(worker_task_checker_);
+        RTC_GUARDED_BY(packet_sequence_checker_);
 
     absl::optional<LossNotificationState> lntf_state_
-        RTC_GUARDED_BY(worker_task_checker_);
+        RTC_GUARDED_BY(packet_sequence_checker_);
   };
   enum ParseGenericDependenciesResult {
     kDropPacket,
@@ -255,22 +256,29 @@
 
   // Entry point doing non-stats work for a received packet. Called
   // for the same packet both before and after RED decapsulation.
-  void ReceivePacket(const RtpPacketReceived& packet);
+  void ReceivePacket(const RtpPacketReceived& packet)
+      RTC_RUN_ON(packet_sequence_checker_);
+
   // Parses and handles RED headers.
   // This function assumes that it's being called from only one thread.
-  void ParseAndHandleEncapsulatingHeader(const RtpPacketReceived& packet);
-  void NotifyReceiverOfEmptyPacket(uint16_t seq_num);
+  void ParseAndHandleEncapsulatingHeader(const RtpPacketReceived& packet)
+      RTC_RUN_ON(packet_sequence_checker_);
+  void NotifyReceiverOfEmptyPacket(uint16_t seq_num)
+      RTC_RUN_ON(packet_sequence_checker_);
   void UpdateHistograms();
   bool IsRedEnabled() const;
-  void InsertSpsPpsIntoTracker(uint8_t payload_type);
-  void OnInsertedPacket(video_coding::PacketBuffer::InsertResult result);
+  void InsertSpsPpsIntoTracker(uint8_t payload_type)
+      RTC_RUN_ON(packet_sequence_checker_);
+  void OnInsertedPacket(video_coding::PacketBuffer::InsertResult result)
+      RTC_RUN_ON(packet_sequence_checker_);
   ParseGenericDependenciesResult ParseGenericDependenciesExtension(
       const RtpPacketReceived& rtp_packet,
-      RTPVideoHeader* video_header) RTC_RUN_ON(worker_task_checker_);
-  void OnAssembledFrame(std::unique_ptr<RtpFrameObject> frame);
+      RTPVideoHeader* video_header) RTC_RUN_ON(packet_sequence_checker_);
+  void OnAssembledFrame(std::unique_ptr<RtpFrameObject> frame)
+      RTC_RUN_ON(packet_sequence_checker_);
   void UpdatePacketReceiveTimestamps(const RtpPacketReceived& packet,
                                      bool is_keyframe)
-      RTC_RUN_ON(worker_task_checker_);
+      RTC_RUN_ON(packet_sequence_checker_);
 
   Clock* const clock_;
   // Ownership of this object lies with VideoReceiveStream, which owns |this|.
@@ -289,8 +297,16 @@
   std::unique_ptr<UlpfecReceiver> ulpfec_receiver_;
 
   RTC_NO_UNIQUE_ADDRESS SequenceChecker worker_task_checker_;
-  bool receiving_ RTC_GUARDED_BY(worker_task_checker_);
-  int64_t last_packet_log_ms_ RTC_GUARDED_BY(worker_task_checker_);
+  // TODO(bugs.webrtc.org/11993): This checker conceptually represents
+  // operations that belong to the network thread. The Call class is currently
+  // moving towards handling network packets on the network thread and while
+  // that work is ongoing, this checker may in practice represent the worker
+  // thread, but still serves as a mechanism of grouping together concepts
+  // that belong to the network thread. Once the packets are fully delivered
+  // on the network thread, this comment will be deleted.
+  RTC_NO_UNIQUE_ADDRESS SequenceChecker packet_sequence_checker_;
+  bool receiving_ RTC_GUARDED_BY(packet_sequence_checker_);
+  int64_t last_packet_log_ms_ RTC_GUARDED_BY(packet_sequence_checker_);
 
   const std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp_;
 
@@ -302,66 +318,68 @@
   std::unique_ptr<LossNotificationController> loss_notification_controller_;
 
   video_coding::PacketBuffer packet_buffer_
-      RTC_GUARDED_BY(worker_task_checker_);
-  UniqueTimestampCounter frame_counter_ RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
+  UniqueTimestampCounter frame_counter_
+      RTC_GUARDED_BY(packet_sequence_checker_);
   SeqNumUnwrapper<uint16_t> frame_id_unwrapper_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
 
   // Video structure provided in the dependency descriptor in a first packet
   // of a key frame. It is required to parse dependency descriptor in the
   // following delta packets.
   std::unique_ptr<FrameDependencyStructure> video_structure_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
   // Frame id of the last frame with the attached video structure.
   // absl::nullopt when `video_structure_ == nullptr`;
   absl::optional<int64_t> video_structure_frame_id_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
 
   std::unique_ptr<RtpFrameReferenceFinder> reference_finder_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
   absl::optional<VideoCodecType> current_codec_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
   uint32_t last_assembled_frame_rtp_timestamp_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
 
   std::map<int64_t, uint16_t> last_seq_num_for_pic_id_
-      RTC_GUARDED_BY(worker_task_checker_);
-  video_coding::H264SpsPpsTracker tracker_ RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
+  video_coding::H264SpsPpsTracker tracker_
+      RTC_GUARDED_BY(packet_sequence_checker_);
 
   // Maps payload id to the depacketizer.
   std::map<uint8_t, std::unique_ptr<VideoRtpDepacketizer>> payload_type_map_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
 
   // TODO(johan): Remove pt_codec_params_ once
   // https://bugs.chromium.org/p/webrtc/issues/detail?id=6883 is resolved.
   // Maps a payload type to a map of out-of-band supplied codec parameters.
   std::map<uint8_t, std::map<std::string, std::string>> pt_codec_params_
-      RTC_GUARDED_BY(worker_task_checker_);
-  int16_t last_payload_type_ RTC_GUARDED_BY(worker_task_checker_) = -1;
+      RTC_GUARDED_BY(packet_sequence_checker_);
+  int16_t last_payload_type_ RTC_GUARDED_BY(packet_sequence_checker_) = -1;
 
-  bool has_received_frame_ RTC_GUARDED_BY(worker_task_checker_);
+  bool has_received_frame_ RTC_GUARDED_BY(packet_sequence_checker_);
 
   absl::optional<uint32_t> last_received_rtp_timestamp_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
   absl::optional<uint32_t> last_received_keyframe_rtp_timestamp_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
   absl::optional<Timestamp> last_received_rtp_system_time_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
   absl::optional<Timestamp> last_received_keyframe_rtp_system_time_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
 
   // Handles incoming encrypted frames and forwards them to the
   // rtp_reference_finder if they are decryptable.
   std::unique_ptr<BufferedFrameDecryptor> buffered_frame_decryptor_
-      RTC_PT_GUARDED_BY(worker_task_checker_);
+      RTC_PT_GUARDED_BY(packet_sequence_checker_);
   bool frames_decryptable_ RTC_GUARDED_BY(worker_task_checker_);
   absl::optional<ColorSpace> last_color_space_;
 
   AbsoluteCaptureTimeInterpolator absolute_capture_time_interpolator_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
 
   CaptureClockOffsetUpdater capture_clock_offset_updater_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
 
   int64_t last_completed_picture_id_ = 0;
 
@@ -369,9 +387,9 @@
       frame_transformer_delegate_;
 
   SeqNumUnwrapper<uint16_t> rtp_seq_num_unwrapper_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
   std::map<int64_t, RtpPacketInfo> packet_infos_
-      RTC_GUARDED_BY(worker_task_checker_);
+      RTC_GUARDED_BY(packet_sequence_checker_);
 };
 
 }  // namespace webrtc
diff --git a/video/video_receive_stream2.cc b/video/video_receive_stream2.cc
index 78e436a..627a291 100644
--- a/video/video_receive_stream2.cc
+++ b/video/video_receive_stream2.cc
@@ -384,9 +384,13 @@
 
     const bool raw_payload =
         config_.rtp.raw_payload_types.count(decoder.payload_type) > 0;
-    rtp_video_stream_receiver_.AddReceiveCodec(decoder.payload_type, codec,
-                                               decoder.video_format.parameters,
-                                               raw_payload);
+    {
+      // TODO(bugs.webrtc.org/11993): Make this call on the network thread.
+      RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
+      rtp_video_stream_receiver_.AddReceiveCodec(
+          decoder.payload_type, codec, decoder.video_format.parameters,
+          raw_payload);
+    }
     RTC_CHECK_EQ(VCM_OK, video_receiver_.RegisterReceiveCodec(
                              decoder.payload_type, &codec, num_cpu_cores_));
   }
@@ -408,12 +412,23 @@
     StartNextDecode();
   });
   decoder_running_ = true;
-  rtp_video_stream_receiver_.StartReceive();
+
+  {
+    // TODO(bugs.webrtc.org/11993): Make this call on the network thread.
+    RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
+    rtp_video_stream_receiver_.StartReceive();
+  }
 }
 
 void VideoReceiveStream2::Stop() {
   RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
-  rtp_video_stream_receiver_.StopReceive();
+  {
+    // TODO(bugs.webrtc.org/11993): Make this call on the network thread.
+    // Also call `GetUniqueFramesSeen()` at the same time (since it's a counter
+    // that's updated on the network thread).
+    RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
+    rtp_video_stream_receiver_.StopReceive();
+  }
 
   stats_proxy_.OnUniqueFramesCounted(
       rtp_video_stream_receiver_.GetUniqueFramesSeen());
@@ -636,8 +651,13 @@
   }
 
   int64_t last_continuous_pid = frame_buffer_->InsertFrame(std::move(frame));
-  if (last_continuous_pid != -1)
-    rtp_video_stream_receiver_.FrameContinuous(last_continuous_pid);
+  if (last_continuous_pid != -1) {
+    {
+      // TODO(bugs.webrtc.org/11993): Call on the network thread.
+      RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
+      rtp_video_stream_receiver_.FrameContinuous(last_continuous_pid);
+    }
+  }
 }
 
 void VideoReceiveStream2::OnRttUpdate(int64_t avg_rtt_ms, int64_t max_rtt_ms) {
@@ -653,7 +673,7 @@
 }
 
 absl::optional<Syncable::Info> VideoReceiveStream2::GetInfo() const {
-  RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   absl::optional<Syncable::Info> info =
       rtp_video_stream_receiver_.GetSyncInfo();
 
@@ -704,9 +724,10 @@
           HandleEncodedFrame(std::move(frame));
         } else {
           int64_t now_ms = clock_->TimeInMilliseconds();
+          // TODO(bugs.webrtc.org/11993): PostTask to the network thread.
           call_->worker_thread()->PostTask(ToQueuedTask(
               task_safety_, [this, now_ms, wait_ms = GetMaxWaitMs()]() {
-                RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
+                RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
                 HandleFrameBufferTimeout(now_ms, wait_ms);
               }));
         }
@@ -765,19 +786,22 @@
     force_request_key_frame = true;
   }
 
-  call_->worker_thread()->PostTask(ToQueuedTask(
-      task_safety_,
-      [this, now_ms, received_frame_is_keyframe, force_request_key_frame,
-       decoded_frame_picture_id, keyframe_request_is_due]() {
-        RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
+  {
+    // TODO(bugs.webrtc.org/11993): Make this PostTask to the network thread.
+    call_->worker_thread()->PostTask(ToQueuedTask(
+        task_safety_,
+        [this, now_ms, received_frame_is_keyframe, force_request_key_frame,
+         decoded_frame_picture_id, keyframe_request_is_due]() {
+          RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
 
-        if (decoded_frame_picture_id != -1)
-          rtp_video_stream_receiver_.FrameDecoded(decoded_frame_picture_id);
+          if (decoded_frame_picture_id != -1)
+            rtp_video_stream_receiver_.FrameDecoded(decoded_frame_picture_id);
 
-        HandleKeyFrameGeneration(received_frame_is_keyframe, now_ms,
-                                 force_request_key_frame,
-                                 keyframe_request_is_due);
-      }));
+          HandleKeyFrameGeneration(received_frame_is_keyframe, now_ms,
+                                   force_request_key_frame,
+                                   keyframe_request_is_due);
+        }));
+  }
 }
 
 int VideoReceiveStream2::DecodeAndMaybeDispatchEncodedFrame(
@@ -840,13 +864,12 @@
   return decode_result;
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 void VideoReceiveStream2::HandleKeyFrameGeneration(
     bool received_frame_is_keyframe,
     int64_t now_ms,
     bool always_request_key_frame,
     bool keyframe_request_is_due) {
-  // Running on worker_sequence_checker_.
-
   bool request_key_frame = always_request_key_frame;
 
   // Repeat sending keyframe requests if we've requested a keyframe.
@@ -870,9 +893,9 @@
   }
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 void VideoReceiveStream2::HandleFrameBufferTimeout(int64_t now_ms,
                                                    int64_t wait_ms) {
-  // Running on |worker_sequence_checker_|.
   absl::optional<int64_t> last_packet_ms =
       rtp_video_stream_receiver_.LastReceivedPacketMs();
 
@@ -892,8 +915,8 @@
   }
 }
 
+// RTC_RUN_ON(packet_sequence_checker_)
 bool VideoReceiveStream2::IsReceivingKeyFrame(int64_t timestamp_ms) const {
-  // Running on worker_sequence_checker_.
   absl::optional<int64_t> last_keyframe_packet_ms =
       rtp_video_stream_receiver_.LastReceivedKeyframePacketMs();
 
@@ -965,13 +988,13 @@
         event.Set();
       });
 
-  old_state.keyframe_needed = keyframe_generation_requested_;
-
   if (generate_key_frame) {
     rtp_video_stream_receiver_.RequestKeyFrame();
-    keyframe_generation_requested_ = true;
-  } else {
-    keyframe_generation_requested_ = state.keyframe_needed;
+    {
+      // TODO(bugs.webrtc.org/11993): Post this to the network thread.
+      RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
+      keyframe_generation_requested_ = true;
+    }
   }
 
   event.Wait(rtc::Event::kForever);
@@ -979,7 +1002,7 @@
 }
 
 void VideoReceiveStream2::GenerateKeyFrame() {
-  RTC_DCHECK_RUN_ON(&worker_sequence_checker_);
+  RTC_DCHECK_RUN_ON(&packet_sequence_checker_);
   RequestKeyFrame(clock_->TimeInMilliseconds());
   keyframe_generation_requested_ = true;
 }
diff --git a/video/video_receive_stream2.h b/video/video_receive_stream2.h
index 8bf673d..dbd46cc 100644
--- a/video/video_receive_stream2.h
+++ b/video/video_receive_stream2.h
@@ -179,18 +179,18 @@
   void HandleEncodedFrame(std::unique_ptr<EncodedFrame> frame)
       RTC_RUN_ON(decode_queue_);
   void HandleFrameBufferTimeout(int64_t now_ms, int64_t wait_ms)
-      RTC_RUN_ON(worker_sequence_checker_);
+      RTC_RUN_ON(packet_sequence_checker_);
   void UpdatePlayoutDelays() const
       RTC_EXCLUSIVE_LOCKS_REQUIRED(worker_sequence_checker_);
   void RequestKeyFrame(int64_t timestamp_ms)
-      RTC_RUN_ON(worker_sequence_checker_);
+      RTC_RUN_ON(packet_sequence_checker_);
   void HandleKeyFrameGeneration(bool received_frame_is_keyframe,
                                 int64_t now_ms,
                                 bool always_request_key_frame,
                                 bool keyframe_request_is_due)
-      RTC_RUN_ON(worker_sequence_checker_);
+      RTC_RUN_ON(packet_sequence_checker_);
   bool IsReceivingKeyFrame(int64_t timestamp_ms) const
-      RTC_RUN_ON(worker_sequence_checker_);
+      RTC_RUN_ON(packet_sequence_checker_);
   int DecodeAndMaybeDispatchEncodedFrame(std::unique_ptr<EncodedFrame> frame)
       RTC_RUN_ON(decode_queue_);
 
@@ -284,7 +284,7 @@
   std::function<void(const RecordableEncodedFrame&)>
       encoded_frame_buffer_function_ RTC_GUARDED_BY(decode_queue_);
   // Set to true while we're requesting keyframes but not yet received one.
-  bool keyframe_generation_requested_ RTC_GUARDED_BY(worker_sequence_checker_) =
+  bool keyframe_generation_requested_ RTC_GUARDED_BY(packet_sequence_checker_) =
       false;
   // Lock to avoid unnecessary per-frame idle wakeups in the code.
   webrtc::Mutex pending_resolution_mutex_;