PipeWire camera: check for node existence before adding it to the list

This avoids having duplicate camera entries presented to the user when
PipeWire camera is being used.

Bug: webrtc:346350844
Change-Id: I423db7fe0654cc1b1c91ee5264c6ba5dc4e24100
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/354320
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Andreas Pehrson <apehrson@mozilla.com>
Commit-Queue: Jan Grulich <grulja@gmail.com>
Cr-Commit-Position: refs/heads/main@{#42462}
diff --git a/modules/video_capture/linux/pipewire_session.cc b/modules/video_capture/linux/pipewire_session.cc
index 4d1b200..90ac4a3 100644
--- a/modules/video_capture/linux/pipewire_session.cc
+++ b/modules/video_capture/linux/pipewire_session.cc
@@ -354,6 +354,13 @@
                                        const spa_dict* props) {
   PipeWireSession* that = static_cast<PipeWireSession*>(data);
 
+  // Skip already added nodes to avoid duplicate camera entries
+  if (std::find_if(that->nodes_.begin(), that->nodes_.end(),
+                   [id](const PipeWireNode& node) {
+                     return node.id() == id;
+                   }) != that->nodes_.end())
+    return;
+
   if (type != absl::string_view(PW_TYPE_INTERFACE_Node))
     return;
 
@@ -372,12 +379,10 @@
 void PipeWireSession::OnRegistryGlobalRemove(void* data, uint32_t id) {
   PipeWireSession* that = static_cast<PipeWireSession*>(data);
 
-  for (auto it = that->nodes_.begin(); it != that->nodes().end(); ++it) {
-    if ((*it).id() == id) {
-      that->nodes_.erase(it);
-      break;
-    }
-  }
+  auto it = std::remove_if(
+      that->nodes_.begin(), that->nodes_.end(),
+      [id](const PipeWireNode& node) { return node.id() == id; });
+  that->nodes_.erase(it, that->nodes_.end());
 }
 
 void PipeWireSession::Finish(VideoCaptureOptions::Status status) {