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}
This commit is contained in:
Sebastian Jansson
2019-09-17 20:34:03 +02:00
committed by Commit Bot
parent 4d461ba298
commit ee5ec9a93a
5 changed files with 108 additions and 147 deletions

View File

@ -213,11 +213,6 @@ class ChannelSend : public ChannelSendInterface,
return media_transport_config_.media_transport; 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 OnReceivedRtt(int64_t rtt_ms);
void OnTargetTransferRate(TargetTransferRate) override; void OnTargetTransferRate(TargetTransferRate) override;
@ -1048,62 +1043,56 @@ CallSendStatistics ChannelSend::GetRTCPStatistics() const {
void ChannelSend::ProcessAndEncodeAudio( void ChannelSend::ProcessAndEncodeAudio(
std::unique_ptr<AudioFrame> audio_frame) { std::unique_ptr<AudioFrame> audio_frame) {
RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_); RTC_DCHECK_RUNS_SERIALIZED(&audio_thread_race_checker_);
struct ProcessAndEncodeAudio { RTC_DCHECK_GT(audio_frame->samples_per_channel_, 0);
void operator()() { RTC_DCHECK_LE(audio_frame->num_channels_, 8);
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;
};
// Profile time between when the audio frame is added to the task queue and // Profile time between when the audio frame is added to the task queue and
// when the task is actually executed. // when the task is actually executed.
audio_frame->UpdateProfileTimeStamp(); 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) { bool is_muted = InputMute();
RTC_DCHECK_GT(audio_input->samples_per_channel_, 0); AudioFrameOperations::Mute(audio_frame.get(), previous_frame_muted_,
RTC_DCHECK_LE(audio_input->num_channels_, 8); is_muted);
// Measure time between when the audio frame is added to the task queue and if (_includeAudioLevelIndication) {
// when the task is actually executed. Goal is to keep track of unwanted size_t length =
// extra latency added by the task queue. audio_frame->samples_per_channel_ * audio_frame->num_channels_;
RTC_HISTOGRAM_COUNTS_10000("WebRTC.Audio.EncodingTaskQueueLatencyMs", RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes);
audio_input->ElapsedProfileTimeMs()); 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(); // Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
if (_includeAudioLevelIndication) { // The ACM resamples internally.
size_t length = audio_frame->timestamp_ = _timeStamp;
audio_input->samples_per_channel_ * audio_input->num_channels_; // This call will trigger AudioPacketizationCallback::SendData if
RTC_CHECK_LE(length, AudioFrame::kMaxDataSizeBytes); // encoding is done and payload is ready for packetization and
if (is_muted && previous_frame_muted_) { // transmission. Otherwise, it will return without invoking the
rms_level_.AnalyzeMuted(length); // callback.
} else { if (audio_coding_->Add10MsData(*audio_frame) < 0) {
rms_level_.Analyze( RTC_DLOG(LS_ERROR) << "ACM::Add10MsData() failed.";
rtc::ArrayView<const int16_t>(audio_input->data(), length)); return;
} }
}
previous_frame_muted_ = is_muted;
// Add 10ms of raw (PCM) audio data to the encoder @ 32kHz. _timeStamp += static_cast<uint32_t>(audio_frame->samples_per_channel_);
});
// 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_);
} }
ANAStats ChannelSend::GetANAStatistics() const { ANAStats ChannelSend::GetANAStatistics() const {

View File

@ -27,47 +27,40 @@ EmulatedIpPacket::EmulatedIpPacket(const rtc::SocketAddress& from,
: from(from), to(to), data(data), arrival_time(arrival_time) {} : from(from), to(to), data(data), arrival_time(arrival_time) {}
void LinkEmulation::OnPacketReceived(EmulatedIpPacket packet) { void LinkEmulation::OnPacketReceived(EmulatedIpPacket packet) {
struct Closure { task_queue_->PostTask([this, packet = std::move(packet)]() mutable {
void operator()() { RTC_DCHECK_RUN_ON(task_queue_);
RTC_DCHECK_RUN_ON(link->task_queue_);
link->HandlePacketReceived(std::move(packet));
}
LinkEmulation* link;
EmulatedIpPacket packet;
};
task_queue_->PostTask(Closure{this, std::move(packet)});
}
void LinkEmulation::HandlePacketReceived(EmulatedIpPacket packet) { uint64_t packet_id = next_packet_id_++;
uint64_t packet_id = next_packet_id_++; bool sent = network_behavior_->EnqueuePacket(
bool sent = network_behavior_->EnqueuePacket( PacketInFlightInfo(packet.size(), packet.arrival_time.us(), packet_id));
PacketInFlightInfo(packet.size(), packet.arrival_time.us(), packet_id)); if (sent) {
if (sent) { packets_.emplace_back(StoredPacket{packet_id, std::move(packet), false});
packets_.emplace_back(StoredPacket{packet_id, std::move(packet), false}); }
} if (process_task_.Running())
if (process_task_.Running()) return;
return; absl::optional<int64_t> next_time_us =
absl::optional<int64_t> next_time_us = network_behavior_->NextDeliveryTimeUs();
network_behavior_->NextDeliveryTimeUs(); if (!next_time_us)
if (!next_time_us) return;
return; Timestamp current_time = clock_->CurrentTime();
Timestamp current_time = clock_->CurrentTime(); process_task_ = RepeatingTaskHandle::DelayedStart(
process_task_ = RepeatingTaskHandle::DelayedStart( task_queue_->Get(),
task_queue_->Get(), std::max(TimeDelta::Zero(),
std::max(TimeDelta::Zero(), Timestamp::us(*next_time_us) - current_time), Timestamp::us(*next_time_us) - current_time),
[this]() { [this]() {
RTC_DCHECK_RUN_ON(task_queue_); RTC_DCHECK_RUN_ON(task_queue_);
Timestamp current_time = clock_->CurrentTime(); Timestamp current_time = clock_->CurrentTime();
Process(current_time); Process(current_time);
absl::optional<int64_t> next_time_us = absl::optional<int64_t> next_time_us =
network_behavior_->NextDeliveryTimeUs(); network_behavior_->NextDeliveryTimeUs();
if (!next_time_us) { if (!next_time_us) {
process_task_.Stop(); process_task_.Stop();
return TimeDelta::Zero(); // This is ignored. return TimeDelta::Zero(); // This is ignored.
} }
RTC_DCHECK_GE(*next_time_us, current_time.us()); RTC_DCHECK_GE(*next_time_us, current_time.us());
return Timestamp::us(*next_time_us) - current_time; return Timestamp::us(*next_time_us) - current_time;
}); });
});
} }
void LinkEmulation::Process(Timestamp at_time) { void LinkEmulation::Process(Timestamp at_time) {
@ -204,19 +197,23 @@ uint64_t EmulatedEndpoint::GetId() const {
void EmulatedEndpoint::SendPacket(const rtc::SocketAddress& from, void EmulatedEndpoint::SendPacket(const rtc::SocketAddress& from,
const rtc::SocketAddress& to, const rtc::SocketAddress& to,
rtc::CopyOnWriteBuffer packet) { rtc::CopyOnWriteBuffer packet_data) {
RTC_CHECK(from.ipaddr() == peer_local_addr_); RTC_CHECK(from.ipaddr() == peer_local_addr_);
struct Closure { EmulatedIpPacket packet(from, to, std::move(packet_data),
void operator()() { clock_->CurrentTime());
endpoint->UpdateSendStats(packet); task_queue_->PostTask([this, packet = std::move(packet)]() mutable {
endpoint->router_.OnPacketReceived(std::move(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());
} }
EmulatedEndpoint* endpoint; stats_.last_packet_sent_time = current_time;
EmulatedIpPacket packet; stats_.packets_sent++;
}; stats_.bytes_sent += DataSize::bytes(packet.size());
task_queue_->PostTask(Closure{
this, router_.OnPacketReceived(std::move(packet));
EmulatedIpPacket(from, to, std::move(packet), clock_->CurrentTime())}); });
} }
absl::optional<uint16_t> EmulatedEndpoint::BindReceiver( absl::optional<uint16_t> EmulatedEndpoint::BindReceiver(
@ -316,18 +313,6 @@ EmulatedNetworkStats EmulatedEndpoint::stats() {
return stats_; 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) { void EmulatedEndpoint::UpdateReceiveStats(const EmulatedIpPacket& packet) {
RTC_DCHECK_RUN_ON(task_queue_); RTC_DCHECK_RUN_ON(task_queue_);
Timestamp current_time = clock_->CurrentTime(); Timestamp current_time = clock_->CurrentTime();

View File

@ -82,7 +82,6 @@ class LinkEmulation : public EmulatedNetworkReceiverInterface {
bool removed; bool removed;
}; };
void Process(Timestamp at_time) RTC_RUN_ON(task_queue_); void Process(Timestamp at_time) RTC_RUN_ON(task_queue_);
void HandlePacketReceived(EmulatedIpPacket packet) RTC_RUN_ON(task_queue_);
Clock* const clock_; Clock* const clock_;
rtc::TaskQueue* const task_queue_; rtc::TaskQueue* const task_queue_;
@ -171,7 +170,7 @@ class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
// on destination endpoint. // on destination endpoint.
void SendPacket(const rtc::SocketAddress& from, void SendPacket(const rtc::SocketAddress& from,
const rtc::SocketAddress& to, const rtc::SocketAddress& to,
rtc::CopyOnWriteBuffer packet); rtc::CopyOnWriteBuffer packet_data);
// Binds receiver to this endpoint to send and receive 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, // |desired_port| is a port that should be used. If it is equal to 0,
@ -203,7 +202,6 @@ class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
private: private:
static constexpr uint16_t kFirstEphemeralPort = 49152; static constexpr uint16_t kFirstEphemeralPort = 49152;
uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_); uint16_t NextPort() RTC_EXCLUSIVE_LOCKS_REQUIRED(receiver_lock_);
void UpdateSendStats(const EmulatedIpPacket& packet);
void UpdateReceiveStats(const EmulatedIpPacket& packet); void UpdateReceiveStats(const EmulatedIpPacket& packet);
rtc::CriticalSection receiver_lock_; rtc::CriticalSection receiver_lock_;

View File

@ -278,16 +278,11 @@ void CallClient::OnPacketReceived(EmulatedIpPacket packet) {
RTC_CHECK(ssrc.has_value()); RTC_CHECK(ssrc.has_value());
media_type = ssrc_media_types_[*ssrc]; media_type = ssrc_media_types_[*ssrc];
} }
struct Closure { task_queue_.PostTask(
void operator()() { [call = call_.get(), media_type, packet = std::move(packet)]() mutable {
call->Receiver()->DeliverPacket(media_type, packet.data, call->Receiver()->DeliverPacket(media_type, packet.data,
packet.arrival_time.us()); packet.arrival_time.us());
} });
Call* call;
MediaType media_type;
EmulatedIpPacket packet;
};
task_queue_.PostTask(Closure{call_.get(), media_type, std::move(packet)});
} }
std::unique_ptr<RtcEventLogOutput> CallClient::GetLogWriter(std::string name) { std::unique_ptr<RtcEventLogOutput> CallClient::GetLogWriter(std::string name) {

View File

@ -622,29 +622,23 @@ int64_t VideoReceiveStream::GetWaitMs() const {
void VideoReceiveStream::StartNextDecode() { void VideoReceiveStream::StartNextDecode() {
TRACE_EVENT0("webrtc", "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( frame_buffer_->NextFrame(
GetWaitMs(), keyframe_required_, &decode_queue_, GetWaitMs(), keyframe_required_, &decode_queue_,
/* encoded frame handler */
[this](std::unique_ptr<EncodedFrame> frame, ReturnReason res) { [this](std::unique_ptr<EncodedFrame> frame, ReturnReason res) {
RTC_DCHECK_EQ(frame == nullptr, res == ReturnReason::kTimeout); RTC_DCHECK_EQ(frame == nullptr, res == ReturnReason::kTimeout);
RTC_DCHECK_EQ(frame != nullptr, res == ReturnReason::kFrameFound); 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();
});
}); });
} }