PipeWire capturer: make restore tokens re-usable more than one time

Do not automatically remove all tokens once we attempt to use them. This
mitigates an issue with Google Meet where an additional instance of a
DesktopCapturer is created and destroyed right away, taking away the
token we would use otherwise. Also save the token under same SourceId
once we get a new (but could be same) token from the restored session.

Bug: webrtc:15544
Change-Id: I565b22f5bf6a4d8a3b7d6d757f9c1046c7a0557d
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/322621
Commit-Queue: Jan Grulich <grulja@gmail.com>
Reviewed-by: Alexander Cooper <alcooper@chromium.org>
Cr-Commit-Position: refs/heads/main@{#40892}
diff --git a/modules/desktop_capture/linux/wayland/base_capturer_pipewire.cc b/modules/desktop_capture/linux/wayland/base_capturer_pipewire.cc
index 4ef00e6..40764de 100644
--- a/modules/desktop_capture/linux/wayland/base_capturer_pipewire.cc
+++ b/modules/desktop_capture/linux/wayland/base_capturer_pipewire.cc
@@ -82,8 +82,10 @@
                       << static_cast<uint>(result);
   } else if (ScreenCastPortal* screencast_portal = GetScreenCastPortal()) {
     if (!screencast_portal->RestoreToken().empty()) {
+      const SourceId token_id =
+          selected_source_id_ ? selected_source_id_ : source_id_;
       RestoreTokenManager::GetInstance().AddToken(
-          source_id_, screencast_portal->RestoreToken());
+          token_id, screencast_portal->RestoreToken());
     }
   }
 
@@ -137,7 +139,7 @@
         ScreenCastPortal::PersistMode::kTransient);
     if (selected_source_id_) {
       screencast_portal->SetRestoreToken(
-          RestoreTokenManager::GetInstance().TakeToken(selected_source_id_));
+          RestoreTokenManager::GetInstance().GetToken(selected_source_id_));
     }
   }
 
diff --git a/modules/desktop_capture/linux/wayland/restore_token_manager.cc b/modules/desktop_capture/linux/wayland/restore_token_manager.cc
index 5ca9b95..a17d9a4 100644
--- a/modules/desktop_capture/linux/wayland/restore_token_manager.cc
+++ b/modules/desktop_capture/linux/wayland/restore_token_manager.cc
@@ -23,10 +23,8 @@
   restore_tokens_.insert({id, token});
 }
 
-std::string RestoreTokenManager::TakeToken(DesktopCapturer::SourceId id) {
-  std::string token = restore_tokens_[id];
-  // Remove the token as it cannot be used anymore
-  restore_tokens_.erase(id);
+std::string RestoreTokenManager::GetToken(DesktopCapturer::SourceId id) {
+  const std::string token = restore_tokens_[id];
   return token;
 }
 
diff --git a/modules/desktop_capture/linux/wayland/restore_token_manager.h b/modules/desktop_capture/linux/wayland/restore_token_manager.h
index 174bef1..ad4f747 100644
--- a/modules/desktop_capture/linux/wayland/restore_token_manager.h
+++ b/modules/desktop_capture/linux/wayland/restore_token_manager.h
@@ -27,7 +27,7 @@
   static RestoreTokenManager& GetInstance();
 
   void AddToken(DesktopCapturer::SourceId id, const std::string& token);
-  std::string TakeToken(DesktopCapturer::SourceId id);
+  std::string GetToken(DesktopCapturer::SourceId id);
 
   // Returns a source ID which does not have any token associated with it yet.
   DesktopCapturer::SourceId GetUnusedId();