webrtc/audio/channel_receive_frame_transformer_delegate.cc
Tommi e2e046452a Remove a couple of locks from ChannelReceive and add thread checks.
* Removes playing_lock_, sync_info_lock_ and video_sync_lock_.
* Also remove video_capture_thread_race_checker_ which was redundant.

Only video_sync_lock_ was actually needed. The other two aren't needed
anymore because of changes made to RtpStreamsSynchronizer class last
year (see webrtc:11489).

In the one case where we had a lock, we post a task to the thread
where the state is maintained. This task is for capturing histograms
which I'm not sure we should have been capturing on the audio thread
anyway.

Also making ChannelReceiveFrameTransformerDelegate compatible with more
tests by using TaskQueueBase instead of rtc::Thread. A number of tests
that instantiate ChannelReceive (and thereby CRFTD) set the worker
thread as a TQ and not actually an rtc::Thread instance. In those cases
CRFTD would previously have gotten a nullptr for the worker thread.

Bug: webrtc:11993
Change-Id: I59f4b2afbfedb06f241d9a613f8538adc19cd6d8
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/221364
Commit-Queue: Tommi <tommi@webrtc.org>
Reviewed-by: Henrik Lundin <henrik.lundin@webrtc.org>
Reviewed-by: Markus Handell <handellm@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#34257}
2021-06-09 16:07:02 +00:00

95 lines
3.4 KiB
C++

/*
* Copyright (c) 2020 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "audio/channel_receive_frame_transformer_delegate.h"
#include <utility>
#include "rtc_base/buffer.h"
#include "rtc_base/task_utils/to_queued_task.h"
namespace webrtc {
namespace {
class TransformableAudioFrame : public TransformableAudioFrameInterface {
public:
TransformableAudioFrame(rtc::ArrayView<const uint8_t> payload,
const RTPHeader& header,
uint32_t ssrc)
: payload_(payload.data(), payload.size()),
header_(header),
ssrc_(ssrc) {}
~TransformableAudioFrame() override = default;
rtc::ArrayView<const uint8_t> GetData() const override { return payload_; }
void SetData(rtc::ArrayView<const uint8_t> data) override {
payload_.SetData(data.data(), data.size());
}
uint32_t GetTimestamp() const override { return header_.timestamp; }
uint32_t GetSsrc() const override { return ssrc_; }
const RTPHeader& GetHeader() const override { return header_; }
private:
rtc::Buffer payload_;
RTPHeader header_;
uint32_t ssrc_;
};
} // namespace
ChannelReceiveFrameTransformerDelegate::ChannelReceiveFrameTransformerDelegate(
ReceiveFrameCallback receive_frame_callback,
rtc::scoped_refptr<FrameTransformerInterface> frame_transformer,
TaskQueueBase* channel_receive_thread)
: receive_frame_callback_(receive_frame_callback),
frame_transformer_(std::move(frame_transformer)),
channel_receive_thread_(channel_receive_thread) {}
void ChannelReceiveFrameTransformerDelegate::Init() {
RTC_DCHECK_RUN_ON(&sequence_checker_);
frame_transformer_->RegisterTransformedFrameCallback(
rtc::scoped_refptr<TransformedFrameCallback>(this));
}
void ChannelReceiveFrameTransformerDelegate::Reset() {
RTC_DCHECK_RUN_ON(&sequence_checker_);
frame_transformer_->UnregisterTransformedFrameCallback();
frame_transformer_ = nullptr;
receive_frame_callback_ = ReceiveFrameCallback();
}
void ChannelReceiveFrameTransformerDelegate::Transform(
rtc::ArrayView<const uint8_t> packet,
const RTPHeader& header,
uint32_t ssrc) {
RTC_DCHECK_RUN_ON(&sequence_checker_);
frame_transformer_->Transform(
std::make_unique<TransformableAudioFrame>(packet, header, ssrc));
}
void ChannelReceiveFrameTransformerDelegate::OnTransformedFrame(
std::unique_ptr<TransformableFrameInterface> frame) {
rtc::scoped_refptr<ChannelReceiveFrameTransformerDelegate> delegate = this;
channel_receive_thread_->PostTask(ToQueuedTask(
[delegate = std::move(delegate), frame = std::move(frame)]() mutable {
delegate->ReceiveFrame(std::move(frame));
}));
}
void ChannelReceiveFrameTransformerDelegate::ReceiveFrame(
std::unique_ptr<TransformableFrameInterface> frame) const {
RTC_DCHECK_RUN_ON(&sequence_checker_);
if (!receive_frame_callback_)
return;
auto* transformed_frame = static_cast<TransformableAudioFrame*>(frame.get());
receive_frame_callback_(transformed_frame->GetData(),
transformed_frame->GetHeader());
}
} // namespace webrtc