Make AndroidVideoTrackSource tasks safe against destruction.

Use SafeTask with a PendingTaskSafetyFlag when posting tasks to the
signaling thread from AndroidVideoTrackSource. This prevents
use-after-free issues if the AndroidVideoTrackSource is destroyed before
the posted task runs.

Bug: b:403168866
Change-Id: I55525579935a8b9b748d874d86658d3813afd773
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/415281
Reviewed-by: Zoé Lepaul <zlep@webrtc.org>
Reviewed-by: Jonas Oreland <jonaso@webrtc.org>
Commit-Queue: Viktor Grönroos <wilhelmsson@google.com>
Cr-Commit-Position: refs/heads/main@{#45868}
diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn
index 047f551..041264a 100644
--- a/sdk/android/BUILD.gn
+++ b/sdk/android/BUILD.gn
@@ -714,6 +714,7 @@
       "../../api:sequence_checker",
       "../../api/environment",
       "../../api/task_queue",
+      "../../api/task_queue:pending_task_safety_flag",
       "../../api/units:data_rate",
       "../../api/video:encoded_image",
       "../../api/video:render_resolution",
diff --git a/sdk/android/src/jni/android_video_track_source.cc b/sdk/android/src/jni/android_video_track_source.cc
index 670f803..bfa493f 100644
--- a/sdk/android/src/jni/android_video_track_source.cc
+++ b/sdk/android/src/jni/android_video_track_source.cc
@@ -18,6 +18,7 @@
 
 #include "api/media_stream_interface.h"
 #include "api/scoped_refptr.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "api/video/video_frame.h"
 #include "api/video/video_frame_buffer.h"
 #include "api/video/video_rotation.h"
@@ -60,7 +61,8 @@
     : AdaptedVideoTrackSource(kRequiredResolutionAlignment),
       signaling_thread_(signaling_thread),
       is_screencast_(is_screencast),
-      align_timestamps_(align_timestamps) {
+      align_timestamps_(align_timestamps),
+      safety_(PendingTaskSafetyFlag::Create()) {
   RTC_LOG(LS_INFO) << "AndroidVideoTrackSource ctor";
 }
 AndroidVideoTrackSource::~AndroidVideoTrackSource() = default;
@@ -79,7 +81,8 @@
     if (Thread::Current() == signaling_thread_) {
       FireOnChanged();
     } else {
-      signaling_thread_->PostTask([this] { FireOnChanged(); });
+      signaling_thread_->PostTask(
+          SafeTask(safety_, [this] { FireOnChanged(); }));
     }
   }
 }
diff --git a/sdk/android/src/jni/android_video_track_source.h b/sdk/android/src/jni/android_video_track_source.h
index 912596d..3ea1e71 100644
--- a/sdk/android/src/jni/android_video_track_source.h
+++ b/sdk/android/src/jni/android_video_track_source.h
@@ -16,6 +16,8 @@
 #include <atomic>
 #include <optional>
 
+#include "api/scoped_refptr.h"
+#include "api/task_queue/pending_task_safety_flag.h"
 #include "media/base/adapted_video_track_source.h"
 #include "rtc_base/thread.h"
 #include "rtc_base/timestamp_aligner.h"
@@ -90,6 +92,7 @@
   std::atomic<bool> is_screencast_;
   TimestampAligner timestamp_aligner_;
   const bool align_timestamps_;
+  scoped_refptr<PendingTaskSafetyFlag> safety_;
 };
 
 }  // namespace jni