Replacing local closure classes with C++14 moving capture lambdas.

Bug: webrtc:10945
Change-Id: I569b9495cae98f204065911e13c37c31f35da372
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/153241
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Oskar Sundbom <ossu@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29214}
diff --git a/audio/channel_send.cc b/audio/channel_send.cc
index f38df24..fbc4583 100644
--- a/audio/channel_send.cc
+++ b/audio/channel_send.cc
@@ -213,11 +213,6 @@
     return media_transport_config_.media_transport;
   }
 
-  // Called on the encoder task queue when a new input audio frame is ready
-  // for encoding.
-  void ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input)
-      RTC_RUN_ON(encoder_queue_);
-
   void OnReceivedRtt(int64_t rtt_ms);
 
   void OnTargetTransferRate(TargetTransferRate) override;
@@ -1048,62 +1043,56 @@
 void ChannelSend::ProcessAndEncodeAudio(
     std::unique_ptr<AudioFrame> audio_frame) {
   RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
-  struct ProcessAndEncodeAudio {
-    void operator()() {
-      RTC_DCHECK_RUN_ON(&channel->encoder_queue_);
-      if (!channel->encoder_queue_is_active_) {
-        return;
-      }
-      channel->ProcessAndEncodeAudioOnTaskQueue(audio_frame.get());
-    }
-    std::unique_ptr<AudioFrame> audio_frame;
-    ChannelSend* const channel;
-  };
+  RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0);
+  RTC_DCHECK_LE(audio_frame->num_channels_, 8);
+
   // Profile time between when the audio frame is added to the task queue and
   // when the task is actually executed.
   audio_frame->UpdateProfileTimeStamp();
-  encoder_queue_.PostTask(ProcessAndEncodeAudio{std::move(audio_frame), this});
-}
+  encoder_queue_.PostTask(
+      [this, audio_frame = std::move(audio_frame)]() mutable {
+        RTC_DCHECK_RUN_ON(&encoder_queue_);
+        if (!encoder_queue_is_active_) {
+          return;
+        }
+        // Measure time between when the audio frame is added to the task queue
+        // and when the task is actually executed. Goal is to keep track of
+        // unwanted extra latency added by the task queue.
+        RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs",
+                                   audio_frame->ElapsedProfileTimeMs());
 
-void ChannelSend::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
-  RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
-  RTC_DCHECK_LE(audio_input->num_channels_, 8);
+        bool is_muted = InputMute();
+        AudioFrameOperations::Mute(audio_frame.get(), previous_frame_muted_,
+                                   is_muted);
 
-  // Measure time between when the audio frame is added to the task queue and
-  // when the task is actually executed. Goal is to keep track of unwanted
-  // extra latency added by the task queue.
-  RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs",
-                             audio_input->ElapsedProfileTimeMs());
+        if (_includeAudioLevelIndication) {
+          size_t length =
+              audio_frame->samples_per_channel_ * audio_frame->num_channels_;
+          RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
+          if (is_muted && previous_frame_muted_) {
+            rms_level_.AnalyzeMuted(length);
+          } else {
+            rms_level_.Analyze(
+                rtc::ArrayView<const int16_t>(audio_frame->data(), length));
+          }
+        }
+        previous_frame_muted_ = is_muted;
 
-  bool is_muted = InputMute();
-  AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
+        // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
 
-  if (_includeAudioLevelIndication) {
-    size_t length =
-        audio_input->samples_per_channel_ * audio_input->num_channels_;
-    RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
-    if (is_muted && previous_frame_muted_) {
-      rms_level_.AnalyzeMuted(length);
-    } else {
-      rms_level_.Analyze(
-          rtc::ArrayView<const int16_t>(audio_input->data(), length));
-    }
-  }
-  previous_frame_muted_ = is_muted;
+        // The ACM resamples internally.
+        audio_frame->timestamp_ = _timeStamp;
+        // This call will trigger AudioPacketizationCallback::SendData if
+        // encoding is done and payload is ready for packetization and
+        // transmission. Otherwise, it will return without invoking the
+        // callback.
+        if (audio_coding_->Add10MsData(*audio_frame) < 0) {
+          RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed.";
+          return;
+        }
 
-  // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
-
-  // The ACM resamples internally.
-  audio_input->timestamp_ = _timeStamp;
-  // This call will trigger AudioPacketizationCallback::SendData if encoding
-  // is done and payload is ready for packetization and transmission.
-  // Otherwise, it will return without invoking the callback.
-  if (audio_coding_->Add10MsData(*audio_input) < 0) {
-    RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed.";
-    return;
-  }
-
-  _timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
+        _timeStamp += static_cast<uint32_t>(audio_frame->samples_per_channel_);
+      });
 }
 
 ANAStats ChannelSend::GetANAStatistics() const {
diff --git a/test/network/network_emulation.cc b/test/network/network_emulation.cc
index 2d4a057..f41f266 100644
--- a/test/network/network_emulation.cc
+++ b/test/network/network_emulation.cc
@@ -27,47 +27,40 @@
     : from(from), to(to), data(data), arrival_time(arrival_time) {}
 
 void LinkEmulation::OnPacketReceived(EmulatedIpPacket packet) {
-  struct Closure {
-    void operator()() {
-      RTC_DCHECK_RUN_ON(link->task_queue_);
-      link->HandlePacketReceived(std::move(packet));
-    }
-    LinkEmulation* link;
-    EmulatedIpPacket packet;
-  };
-  task_queue_->PostTask(Closure{this, std::move(packet)});
-}
+  task_queue_->PostTask([this, packet = std::move(packet)]() mutable {
+    RTC_DCHECK_RUN_ON(task_queue_);
 
-void LinkEmulation::HandlePacketReceived(EmulatedIpPacket packet) {
-  uint64_t packet_id = next_packet_id_++;
-  bool sent = network_behavior_->EnqueuePacket(
-      PacketInFlightInfo(packet.size(), packet.arrival_time.us(), packet_id));
-  if (sent) {
-    packets_.emplace_back(StoredPacket{packet_id, std::move(packet), false});
-  }
-  if (process_task_.Running())
-    return;
-  absl::optional<int64_t> next_time_us =
-      network_behavior_->NextDeliveryTimeUs();
-  if (!next_time_us)
-    return;
-  Timestamp current_time = clock_->CurrentTime();
-  process_task_ = RepeatingTaskHandle::DelayedStart(
-      task_queue_->Get(),
-      std::max(TimeDelta::Zero(), Timestamp::us(*next_time_us) - current_time),
-      [this]() {
-        RTC_DCHECK_RUN_ON(task_queue_);
-        Timestamp current_time = clock_->CurrentTime();
-        Process(current_time);
-        absl::optional<int64_t> next_time_us =
-            network_behavior_->NextDeliveryTimeUs();
-        if (!next_time_us) {
-          process_task_.Stop();
-          return TimeDelta::Zero();  // This is ignored.
-        }
-        RTC_DCHECK_GE(*next_time_us, current_time.us());
-        return Timestamp::us(*next_time_us) - current_time;
-      });
+    uint64_t packet_id = next_packet_id_++;
+    bool sent = network_behavior_->EnqueuePacket(
+        PacketInFlightInfo(packet.size(), packet.arrival_time.us(), packet_id));
+    if (sent) {
+      packets_.emplace_back(StoredPacket{packet_id, std::move(packet), false});
+    }
+    if (process_task_.Running())
+      return;
+    absl::optional<int64_t> next_time_us =
+        network_behavior_->NextDeliveryTimeUs();
+    if (!next_time_us)
+      return;
+    Timestamp current_time = clock_->CurrentTime();
+    process_task_ = RepeatingTaskHandle::DelayedStart(
+        task_queue_->Get(),
+        std::max(TimeDelta::Zero(),
+                 Timestamp::us(*next_time_us) - current_time),
+        [this]() {
+          RTC_DCHECK_RUN_ON(task_queue_);
+          Timestamp current_time = clock_->CurrentTime();
+          Process(current_time);
+          absl::optional<int64_t> next_time_us =
+              network_behavior_->NextDeliveryTimeUs();
+          if (!next_time_us) {
+            process_task_.Stop();
+            return TimeDelta::Zero();  // This is ignored.
+          }
+          RTC_DCHECK_GE(*next_time_us, current_time.us());
+          return Timestamp::us(*next_time_us) - current_time;
+        });
+  });
 }
 
 void LinkEmulation::Process(Timestamp at_time) {
@@ -204,19 +197,23 @@
 
 void EmulatedEndpoint::SendPacket(const rtc::SocketAddress& from,
                                   const rtc::SocketAddress& to,
-                                  rtc::CopyOnWriteBuffer packet) {
+                                  rtc::CopyOnWriteBuffer packet_data) {
   RTC_CHECK(from.ipaddr() == peer_local_addr_);
-  struct Closure {
-    void operator()() {
-      endpoint->UpdateSendStats(packet);
-      endpoint->router_.OnPacketReceived(std::move(packet));
+  EmulatedIpPacket packet(from, to, std::move(packet_data),
+                          clock_->CurrentTime());
+  task_queue_->PostTask([this, packet = std::move(packet)]() mutable {
+    RTC_DCHECK_RUN_ON(task_queue_);
+    Timestamp current_time = clock_->CurrentTime();
+    if (stats_.first_packet_sent_time.IsInfinite()) {
+      stats_.first_packet_sent_time = current_time;
+      stats_.first_sent_packet_size = DataSize::bytes(packet.size());
     }
-    EmulatedEndpoint* endpoint;
-    EmulatedIpPacket packet;
-  };
-  task_queue_->PostTask(Closure{
-      this,
-      EmulatedIpPacket(from, to, std::move(packet), clock_->CurrentTime())});
+    stats_.last_packet_sent_time = current_time;
+    stats_.packets_sent++;
+    stats_.bytes_sent += DataSize::bytes(packet.size());
+
+    router_.OnPacketReceived(std::move(packet));
+  });
 }
 
 absl::optional<uint16_t> EmulatedEndpoint::BindReceiver(
@@ -316,18 +313,6 @@
   return stats_;
 }
 
-void EmulatedEndpoint::UpdateSendStats(const EmulatedIpPacket& packet) {
-  RTC_DCHECK_RUN_ON(task_queue_);
-  Timestamp current_time = clock_->CurrentTime();
-  if (stats_.first_packet_sent_time.IsInfinite()) {
-    stats_.first_packet_sent_time = current_time;
-    stats_.first_sent_packet_size = DataSize::bytes(packet.size());
-  }
-  stats_.last_packet_sent_time = current_time;
-  stats_.packets_sent++;
-  stats_.bytes_sent += DataSize::bytes(packet.size());
-}
-
 void EmulatedEndpoint::UpdateReceiveStats(const EmulatedIpPacket& packet) {
   RTC_DCHECK_RUN_ON(task_queue_);
   Timestamp current_time = clock_->CurrentTime();
diff --git a/test/network/network_emulation.h b/test/network/network_emulation.h
index c5ed539..e825c65 100644
--- a/test/network/network_emulation.h
+++ b/test/network/network_emulation.h
@@ -82,7 +82,6 @@
     bool removed;
   };
   void Process(Timestamp at_time) RTC_RUN_ON(task_queue_);
-  void HandlePacketReceived(EmulatedIpPacket packet) RTC_RUN_ON(task_queue_);
 
   Clock* const clock_;
   rtc::TaskQueue* const task_queue_;
@@ -171,7 +170,7 @@
   // on destination endpoint.
   void SendPacket(const rtc::SocketAddress& from,
                   const rtc::SocketAddress& to,
-                  rtc::CopyOnWriteBuffer packet);
+                  rtc::CopyOnWriteBuffer packet_data);
 
   // Binds receiver to this endpoint to send and receive data.
   // |desired_port| is a port that should be used. If it is equal to 0,
@@ -203,7 +202,6 @@
  private:
   static constexpr uint16_t kFirstEphemeralPort = 49152;
   uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_);
-  void UpdateSendStats(const EmulatedIpPacket& packet);
   void UpdateReceiveStats(const EmulatedIpPacket& packet);
 
   rtc::CriticalSection receiver_lock_;
diff --git a/test/scenario/call_client.cc b/test/scenario/call_client.cc
index 3d907f3..7118e2d 100644
--- a/test/scenario/call_client.cc
+++ b/test/scenario/call_client.cc
@@ -278,16 +278,11 @@
     RTC_CHECK(ssrc.has_value());
     media_type = ssrc_media_types_[*ssrc];
   }
-  struct Closure {
-    void operator()() {
-      call->Receiver()->DeliverPacket(media_type, packet.data,
-                                      packet.arrival_time.us());
-    }
-    Call* call;
-    MediaType media_type;
-    EmulatedIpPacket packet;
-  };
-  task_queue_.PostTask(Closure{call_.get(), media_type, std::move(packet)});
+  task_queue_.PostTask(
+      [call = call_.get(), media_type, packet = std::move(packet)]() mutable {
+        call->Receiver()->DeliverPacket(media_type, packet.data,
+                                        packet.arrival_time.us());
+      });
 }
 
 std::unique_ptr<RtcEventLogOutput> CallClient::GetLogWriter(std::string name) {
diff --git a/video/video_receive_stream.cc b/video/video_receive_stream.cc
index f32d087..09a2796 100644
--- a/video/video_receive_stream.cc
+++ b/video/video_receive_stream.cc
@@ -622,29 +622,23 @@
 
 void VideoReceiveStream::StartNextDecode() {
   TRACE_EVENT0("webrtc", "VideoReceiveStream::StartNextDecode");
-
-  struct DecodeTask {
-    void operator()() {
-      RTC_DCHECK_RUN_ON(&stream->decode_queue_);
-      if (stream->decoder_stopped_)
-        return;
-      if (frame) {
-        stream->HandleEncodedFrame(std::move(frame));
-      } else {
-        stream->HandleFrameBufferTimeout();
-      }
-      stream->StartNextDecode();
-    }
-    VideoReceiveStream* stream;
-    std::unique_ptr<EncodedFrame> frame;
-  };
-
   frame_buffer_->NextFrame(
       GetWaitMs(), keyframe_required_, &decode_queue_,
+      /* encoded frame handler */
       [this](std::unique_ptr<EncodedFrame> frame, ReturnReason res) {
         RTC_DCHECK_EQ(frame == nullptr, res == ReturnReason::kTimeout);
         RTC_DCHECK_EQ(frame != nullptr, res == ReturnReason::kFrameFound);
-        decode_queue_.PostTask(DecodeTask{this, std::move(frame)});
+        decode_queue_.PostTask([this, frame = std::move(frame)]() mutable {
+          RTC_DCHECK_RUN_ON(&decode_queue_);
+          if (decoder_stopped_)
+            return;
+          if (frame) {
+            HandleEncodedFrame(std::move(frame));
+          } else {
+            HandleFrameBufferTimeout();
+          }
+          StartNextDecode();
+        });
       });
 }