Prevent potential UAF during VideoStreamEncoder teardown.

Bug: chromium:1357413
Change-Id: I9ec4d4fbafe1c25530346faf09f5b437fad718cc
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/273482
Reviewed-by: Markus Handell <handellm@webrtc.org>
Commit-Queue: Henrik Boström <hbos@webrtc.org>
Auto-Submit: Erik Språng <sprang@webrtc.org>
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Commit-Queue: Markus Handell <handellm@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37948}
diff --git a/video/video_stream_encoder.h b/video/video_stream_encoder.h
index 468998f..60fd263 100644
--- a/video/video_stream_encoder.h
+++ b/video/video_stream_encoder.h
@@ -450,12 +450,15 @@
 
   const absl::optional<int> vp9_low_tier_core_threshold_;
 
+  // Used to cancel any potentially pending tasks to the worker thread.
+  // Refrenced by tasks running on `encoder_queue_` so need to be destroyed
+  // after stopping that queue. Must be created and destroyed on
+  // `worker_queue_`.
+  ScopedTaskSafety task_safety_;
+
   // Public methods are proxied to the task queues. The queues must be destroyed
   // first to make sure no tasks run that use other members.
   rtc::TaskQueue encoder_queue_;
-
-  // Used to cancel any potentially pending tasks to the worker thread.
-  ScopedTaskSafety task_safety_;
 };
 
 }  // namespace webrtc