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;
}
// 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,45 +1043,37 @@ CallSendStatistics ChannelSend::GetRTCPStatistics() const {
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;
}
void ChannelSend::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
RTC_DCHECK_GT(audio_input->samples_per_channel_, 0);
RTC_DCHECK_LE(audio_input->num_channels_, 8);
// 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.
// 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());
audio_frame->ElapsedProfileTimeMs());
bool is_muted = InputMute();
AudioFrameOperations::Mute(audio_input, previous_frame_muted_, is_muted);
AudioFrameOperations::Mute(audio_frame.get(), previous_frame_muted_,
is_muted);
if (_includeAudioLevelIndication) {
size_t length =
audio_input->samples_per_channel_ * audio_input->num_channels_;
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_input->data(), length));
rtc::ArrayView<const int16_t>(audio_frame->data(), length));
}
}
previous_frame_muted_ = is_muted;
@ -1094,16 +1081,18 @@ void ChannelSend::ProcessAndEncodeAudioOnTaskQueue(AudioFrame* audio_input) {
// 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) {
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;
}
_timeStamp += static_cast<uint32_t>(audio_input->samples_per_channel_);
_timeStamp += static_cast<uint32_t>(audio_frame->samples_per_channel_);
});
}
ANAStats ChannelSend::GetANAStatistics() const {

View File

@ -27,18 +27,9 @@ EmulatedIpPacket::EmulatedIpPacket(const rtc::SocketAddress& from,
: 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));
@ -54,7 +45,8 @@ void LinkEmulation::HandlePacketReceived(EmulatedIpPacket packet) {
Timestamp current_time = clock_->CurrentTime();
process_task_ = RepeatingTaskHandle::DelayedStart(
task_queue_->Get(),
std::max(TimeDelta::Zero(), Timestamp::us(*next_time_us) - current_time),
std::max(TimeDelta::Zero(),
Timestamp::us(*next_time_us) - current_time),
[this]() {
RTC_DCHECK_RUN_ON(task_queue_);
Timestamp current_time = clock_->CurrentTime();
@ -68,6 +60,7 @@ void LinkEmulation::HandlePacketReceived(EmulatedIpPacket packet) {
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 @@ uint64_t EmulatedEndpoint::GetId() const {
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 @@ EmulatedNetworkStats EmulatedEndpoint::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) {
RTC_DCHECK_RUN_ON(task_queue_);
Timestamp current_time = clock_->CurrentTime();

View File

@ -82,7 +82,6 @@ class LinkEmulation : public EmulatedNetworkReceiverInterface {
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 @@ class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
// 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 @@ class EmulatedEndpoint : public EmulatedNetworkReceiverInterface {
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_;

View File

@ -278,16 +278,11 @@ void CallClient::OnPacketReceived(EmulatedIpPacket packet) {
RTC_CHECK(ssrc.has_value());
media_type = ssrc_media_types_[*ssrc];
}
struct Closure {
void operator()() {
task_queue_.PostTask(
[call = call_.get(), media_type, packet = std::move(packet)]() mutable {
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)});
});
}
std::unique_ptr<RtcEventLogOutput> CallClient::GetLogWriter(std::string name) {

View File

@ -622,29 +622,23 @@ int64_t VideoReceiveStream::GetWaitMs() const {
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();
});
});
}